Skip to content

Commit eecc5e6

Browse files
committed
template CRD labels and annotations
1 parent 7d27de4 commit eecc5e6

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

examples/operator/templates/cephvolume-crd.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ kind: CustomResourceDefinition
33
metadata:
44
name: cephvolumes.test.example.com
55
annotations:
6-
cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/{{ include "operator.fullname" . }}-serving-cert
6+
cert-manager.io/inject-ca-from: '{{ .Release.Namespace }}/{{ include "operator.fullname"
7+
. }}-serving-cert'
8+
example-annotation: xyz
79
labels:
810
example-label: my-app
911
{{- include "operator.labels" . | nindent 4 }}

examples/operator/templates/manifestcephvolume-crd.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ kind: CustomResourceDefinition
33
metadata:
44
name: manifestcephvolumes.test.example.com
55
annotations:
6-
cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/{{ include "operator.fullname" . }}-serving-cert
6+
cert-manager.io/inject-ca-from: '{{ .Release.Namespace }}/{{ include "operator.fullname"
7+
. }}-serving-cert'
78
labels:
89

910
{{- include "operator.labels" . | nindent 4 }}

pkg/processor/crd/crd.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ const crdTeml = `apiVersion: apiextensions.k8s.io/v1
2121
kind: CustomResourceDefinition
2222
metadata:
2323
name: %[1]s
24-
annotations:
25-
cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/{{ include "%[2]s.fullname" . }}-%[4]s
24+
%[3]s
2625
labels:
27-
%[5]s
26+
%[4]s
2827
{{- include "%[2]s.labels" . | nindent 4 }}
2928
spec:
30-
%[3]s
29+
%[5]s
3130
status:
3231
acceptedNames:
3332
kind: ""
@@ -53,13 +52,37 @@ func (c crd) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
5352
if obj.GroupVersionKind() != crdGVC {
5453
return false, nil, nil
5554
}
56-
57-
certName, _, err := unstructured.NestedString(obj.Object, "metadata", "annotations", "cert-manager.io/inject-ca-from")
58-
if err != nil {
59-
return true, nil, errors.Wrap(err, "unable get crd certName")
55+
var err error
56+
var labels, annotations string
57+
if len(obj.GetAnnotations()) != 0 {
58+
a := obj.GetAnnotations()
59+
certName := a["cert-manager.io/inject-ca-from"]
60+
if certName != "" {
61+
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
62+
certName = appMeta.TrimName(certName)
63+
a["cert-manager.io/inject-ca-from"] = fmt.Sprintf(`{{ .Release.Namespace }}/{{ include "%[1]s.fullname" . }}-%[2]s`, appMeta.ChartName(), certName)
64+
}
65+
annotations, err = yamlformat.Marshal(map[string]interface{}{"annotations": a}, 2)
66+
if err != nil {
67+
return true, nil, err
68+
}
69+
}
70+
if len(obj.GetLabels()) != 0 {
71+
l := obj.GetLabels()
72+
// provided by Helm
73+
delete(l, "app.kubernetes.io/name")
74+
delete(l, "app.kubernetes.io/instance")
75+
delete(l, "app.kubernetes.io/version")
76+
delete(l, "app.kubernetes.io/managed-by")
77+
delete(l, "helm.sh/chart")
78+
if len(l) != 0 {
79+
labels, err = yamlformat.Marshal(l, 4)
80+
if err != nil {
81+
return true, nil, err
82+
}
83+
labels = strings.Trim(labels, "\n")
84+
}
6085
}
61-
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
62-
certName = appMeta.TrimName(certName)
6386

6487
specUnstr, ok, err := unstructured.NestedMap(obj.Object, "spec")
6588
if err != nil || !ok {
@@ -87,16 +110,7 @@ func (c crd) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
87110
specYaml = yamlformat.Indent(specYaml, 2)
88111
specYaml = bytes.TrimRight(specYaml, "\n ")
89112

90-
labels := obj.GetLabels()
91-
var labelsYaml []byte
92-
93-
if len(labels) > 0 {
94-
labelsYaml, _ = yaml.Marshal(labels)
95-
labelsYaml = yamlformat.Indent(labelsYaml, 4)
96-
labelsYaml = bytes.TrimRight(labelsYaml, "\n ")
97-
}
98-
99-
res := fmt.Sprintf(crdTeml, obj.GetName(), appMeta.ChartName(), string(specYaml), certName, string(labelsYaml))
113+
res := fmt.Sprintf(crdTeml, obj.GetName(), appMeta.ChartName(), annotations, labels, string(specYaml))
100114
name, _, err := unstructured.NestedString(obj.Object, "spec", "names", "singular")
101115
if err != nil || !ok {
102116
return true, nil, errors.Wrap(err, "unable to create crd template")

pkg/processor/meta.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured)
2424
var err error
2525
var labels, annotations string
2626
if len(obj.GetLabels()) != 0 {
27-
labels, err = yamlformat.Marshal(obj.GetLabels(), 4)
27+
l := obj.GetLabels()
28+
// provided by Helm
29+
delete(l, "app.kubernetes.io/name")
30+
delete(l, "app.kubernetes.io/instance")
31+
delete(l, "app.kubernetes.io/version")
32+
delete(l, "app.kubernetes.io/managed-by")
33+
delete(l, "helm.sh/chart")
34+
labels, err = yamlformat.Marshal(l, 4)
2835
if err != nil {
2936
return "", err
3037
}

test_data/k8s-operator-kustomize.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ kind: CustomResourceDefinition
1010
metadata:
1111
annotations:
1212
cert-manager.io/inject-ca-from: my-operator-system/my-operator-serving-cert
13+
example-annotation: xyz
1314
creationTimestamp: null
1415
name: cephvolumes.test.example.com
1516
labels:

0 commit comments

Comments
 (0)