Skip to content

Commit b973cff

Browse files
committed
Add label to CRDs created with kubebuilder
1 parent 61a6891 commit b973cff

File tree

1 file changed

+19
-17
lines changed
  • cmd/kubebuilder/create/config

1 file changed

+19
-17
lines changed

cmd/kubebuilder/create/config/gen.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ import (
3535
// CodeGenerator generates code for Kubernetes resources and controllers
3636
type CodeGenerator struct{}
3737

38+
var kblabels = map[string]string{
39+
"platform": "kubernetes_sigs_kubebuilder",
40+
}
41+
42+
func addLabels(m map[string]string) map[string]string {
43+
for k, v := range kblabels {
44+
m[k] = v
45+
}
46+
m["api"] = name
47+
return m
48+
}
49+
3850
// Execute parses packages and executes the code generators against the resource and controller packages
3951
func (g CodeGenerator) Execute() error {
4052
arguments := args.Default()
@@ -84,10 +96,8 @@ func getClusterRole(p *parse.APIs) string {
8496
APIVersion: "rbac.authorization.k8s.io/v1",
8597
},
8698
ObjectMeta: metav1.ObjectMeta{
87-
Name: name + "-role",
88-
Labels: map[string]string{
89-
"api": name,
90-
},
99+
Name: name + "-role",
100+
Labels: addLabels(map[string]string{}),
91101
},
92102
Rules: rules,
93103
}
@@ -107,9 +117,7 @@ func getClusterRoleBinding(p *parse.APIs) string {
107117
ObjectMeta: metav1.ObjectMeta{
108118
Name: fmt.Sprintf("%s-rolebinding", name),
109119
Namespace: fmt.Sprintf("%s-system", name),
110-
Labels: map[string]string{
111-
"api": name,
112-
},
120+
Labels: addLabels(map[string]string{}),
113121
},
114122
Subjects: []rbacv1.Subject{
115123
{
@@ -133,9 +141,7 @@ func getClusterRoleBinding(p *parse.APIs) string {
133141
}
134142

135143
func getDeployment(p *parse.APIs) string {
136-
labels := map[string]string{
137-
"api": name,
138-
}
144+
labels := addLabels(map[string]string{})
139145
dep := appsv1.Deployment{
140146
TypeMeta: metav1.TypeMeta{
141147
APIVersion: "apps/v1",
@@ -191,9 +197,7 @@ func getCrds(p *parse.APIs) []string {
191197
for _, v := range g.Versions {
192198
for _, r := range v.Resources {
193199
crd := r.CRD
194-
crd.Labels = map[string]string{
195-
"api": name,
196-
}
200+
crd.Labels = addLabels(map[string]string{})
197201
s, err := yaml.Marshal(crd)
198202
if err != nil {
199203
glog.Fatalf("Error: %v", err)
@@ -208,10 +212,8 @@ func getCrds(p *parse.APIs) []string {
208212
func getNamespace(p *parse.APIs) string {
209213
ns := corev1.Namespace{
210214
ObjectMeta: metav1.ObjectMeta{
211-
Name: fmt.Sprintf("%v-system", name),
212-
Labels: map[string]string{
213-
"api": name,
214-
},
215+
Name: fmt.Sprintf("%v-system", name),
216+
Labels: addLabels(map[string]string{}),
215217
},
216218
TypeMeta: metav1.TypeMeta{
217219
APIVersion: "v1",

0 commit comments

Comments
 (0)