@@ -12,7 +12,7 @@ import (
1212 yamlformat "github.com/arttor/helmify/pkg/yaml"
1313)
1414
15- const defaultMetaTemplate = `apiVersion: %[1]s
15+ const metaTemplate = `apiVersion: %[1]s
1616kind: %[2]s
1717metadata:
1818 name: %[3]s
@@ -21,16 +21,8 @@ metadata:
2121 {{- include "%[4]s.labels" . | nindent 4 }}
2222%[6]s`
2323
24- const annotationsMetaTemplate = `apiVersion: %[1]s
25- kind: %[2]s
26- metadata:
27- name: %[3]s
28- labels:
29- %[5]s
30- {{- include "%[4]s.labels" . | nindent 4 }}
31- annotations:
32- %[6]s
33- {{- toYaml .Values.%[7]s.%[8]s.annotations | nindent 4 }}`
24+ const annotationsTemplate = ` annotations:
25+ {{- toYaml .Values.%[1]s.%[2]s.annotations | nindent 4 }}`
3426
3527type MetaOpt interface {
3628 apply (* options )
@@ -58,10 +50,7 @@ func WithAnnotations(values helmify.Values) MetaOpt {
5850
5951// ProcessObjMeta - returns object apiVersion, kind and metadata as helm template.
6052func ProcessObjMeta (appMeta helmify.AppMetadata , obj * unstructured.Unstructured , opts ... MetaOpt ) (string , error ) {
61- options := & options {
62- values : nil ,
63- annotations : false ,
64- }
53+ options := & options {}
6554 for _ , opt := range opts {
6655 opt .apply (options )
6756 }
@@ -91,23 +80,27 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured,
9180 return "" , err
9281 }
9382 }
83+
9484 templatedName := appMeta .TemplatedName (obj .GetName ())
9585 apiVersion , kind := obj .GetObjectKind ().GroupVersionKind ().ToAPIVersionAndKind ()
9686
9787 var metaStr string
9888 if options .values != nil && options .annotations {
99- if len (obj .GetAnnotations ()) != 0 {
100- annotations , err = yamlformat .Marshal (obj .GetAnnotations (), 4 )
101- if err != nil {
102- return "" , err
103- }
104- }
10589 name := strcase .ToLowerCamel (appMeta .TrimName (obj .GetName ()))
106- err = unstructured .SetNestedField (options .values , map [string ]interface {}{}, name , strings .ToLower (kind ), "annotations" )
107- metaStr = fmt .Sprintf (annotationsMetaTemplate , apiVersion , kind , templatedName , appMeta .ChartName (), labels , annotations , name , strings .ToLower (kind ))
108- } else {
109- metaStr = fmt .Sprintf (defaultMetaTemplate , apiVersion , kind , templatedName , appMeta .ChartName (), labels , annotations )
90+ kind := strcase .ToLowerCamel (kind )
91+ valuesAnnotations := make (map [string ]interface {})
92+ for k , v := range obj .GetAnnotations () {
93+ valuesAnnotations [k ] = v
94+ }
95+ err = unstructured .SetNestedField (options .values , valuesAnnotations , name , kind , "annotations" )
96+ if err != nil {
97+ return "" , err
98+ }
99+
100+ annotations = fmt .Sprintf (annotationsTemplate , name , kind )
110101 }
102+
103+ metaStr = fmt .Sprintf (metaTemplate , apiVersion , kind , templatedName , appMeta .ChartName (), labels , annotations )
111104 metaStr = strings .Trim (metaStr , " \n " )
112105 metaStr = strings .ReplaceAll (metaStr , "\n \n " , "\n " )
113106 return metaStr , nil
0 commit comments