@@ -21,13 +21,12 @@ const crdTeml = `apiVersion: apiextensions.k8s.io/v1
2121kind: CustomResourceDefinition
2222metadata:
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 }}
2928spec:
30- %[3 ]s
29+ %[5 ]s
3130status:
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" )
0 commit comments