Skip to content

Commit

Permalink
Add label to CRDs created with kubebuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittrock committed Mar 20, 2018
1 parent 61a6891 commit b973cff
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions cmd/kubebuilder/create/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ import (
// CodeGenerator generates code for Kubernetes resources and controllers
type CodeGenerator struct{}

var kblabels = map[string]string{
"platform": "kubernetes_sigs_kubebuilder",
}

func addLabels(m map[string]string) map[string]string {
for k, v := range kblabels {
m[k] = v
}
m["api"] = name
return m
}

// Execute parses packages and executes the code generators against the resource and controller packages
func (g CodeGenerator) Execute() error {
arguments := args.Default()
Expand Down Expand Up @@ -84,10 +96,8 @@ func getClusterRole(p *parse.APIs) string {
APIVersion: "rbac.authorization.k8s.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name + "-role",
Labels: map[string]string{
"api": name,
},
Name: name + "-role",
Labels: addLabels(map[string]string{}),
},
Rules: rules,
}
Expand All @@ -107,9 +117,7 @@ func getClusterRoleBinding(p *parse.APIs) string {
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-rolebinding", name),
Namespace: fmt.Sprintf("%s-system", name),
Labels: map[string]string{
"api": name,
},
Labels: addLabels(map[string]string{}),
},
Subjects: []rbacv1.Subject{
{
Expand All @@ -133,9 +141,7 @@ func getClusterRoleBinding(p *parse.APIs) string {
}

func getDeployment(p *parse.APIs) string {
labels := map[string]string{
"api": name,
}
labels := addLabels(map[string]string{})
dep := appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Expand Down Expand Up @@ -191,9 +197,7 @@ func getCrds(p *parse.APIs) []string {
for _, v := range g.Versions {
for _, r := range v.Resources {
crd := r.CRD
crd.Labels = map[string]string{
"api": name,
}
crd.Labels = addLabels(map[string]string{})
s, err := yaml.Marshal(crd)
if err != nil {
glog.Fatalf("Error: %v", err)
Expand All @@ -208,10 +212,8 @@ func getCrds(p *parse.APIs) []string {
func getNamespace(p *parse.APIs) string {
ns := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v-system", name),
Labels: map[string]string{
"api": name,
},
Name: fmt.Sprintf("%v-system", name),
Labels: addLabels(map[string]string{}),
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Expand Down

0 comments on commit b973cff

Please sign in to comment.