11package configmap
22
33import (
4- "fmt "
4+ "github.com/arttor/helmify/pkg/format "
55 "io"
66 "strings"
77 "text/template"
@@ -14,7 +14,6 @@ import (
1414 "github.com/sirupsen/logrus"
1515 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1616 "k8s.io/apimachinery/pkg/runtime/schema"
17- "sigs.k8s.io/yaml"
1817)
1918
2019var configMapTempl , _ = template .New ("configMap" ).Parse (
@@ -68,7 +67,7 @@ func (d configMap) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstru
6867
6968 name := appMeta .TrimName (obj .GetName ())
7069 var values helmify.Values
71- if field , exists , _ := unstructured .NestedMap (obj .Object , "data" ); exists {
70+ if field , exists , _ := unstructured .NestedStringMap (obj .Object , "data" ); exists {
7271 field , values = parseMapData (field , name )
7372 data , err = yamlformat .Marshal (map [string ]interface {}{"data" : field }, 0 )
7473 if err != nil {
@@ -89,28 +88,31 @@ func (d configMap) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstru
8988 }, nil
9089}
9190
92- func parseMapData (data map [string ]interface {} , configName string ) (map [string ]interface {} , helmify.Values ) {
91+ func parseMapData (data map [string ]string , configName string ) (map [string ]string , helmify.Values ) {
9392 values := helmify.Values {}
9493 for key , value := range data {
9594 valuesNamePath := []string {configName , key }
96- if strings .HasSuffix (key , ".yaml" ) || strings .HasSuffix (key , ".yml" ) {
97- templated , err := parseYaml (value , valuesNamePath , values )
95+ if strings .HasSuffix (key , ".properties" ) {
96+ // handle properties
97+ templated , err := parseProperties (value , valuesNamePath , values )
9898 if err != nil {
9999 logrus .WithError (err ).Errorf ("unable to process configmap data: %v" , valuesNamePath )
100100 continue
101101 }
102102 data [key ] = templated
103103 continue
104104 }
105- if strings .HasSuffix (key , ".properties" ) {
106- templated , err := parseProperties (value , valuesNamePath , values )
105+ if strings .Contains (value , "\n " ) {
106+ value = format .RemoveTrailingWhitespaces (value )
107+ templatedVal , err := values .AddYaml (value , 1 , valuesNamePath ... )
107108 if err != nil {
108- logrus .WithError (err ).Errorf ("unable to process configmap data: %v" , valuesNamePath )
109+ logrus .WithError (err ).Errorf ("unable to process multiline configmap data: %v" , valuesNamePath )
109110 continue
110111 }
111- data [key ] = templated
112+ data [key ] = templatedVal
112113 continue
113114 }
115+ // handle plain string
114116 templatedVal , err := values .Add (value , valuesNamePath ... )
115117 if err != nil {
116118 logrus .WithError (err ).Errorf ("unable to process configmap data: %v" , valuesNamePath )
@@ -121,20 +123,6 @@ func parseMapData(data map[string]interface{}, configName string) (map[string]in
121123 return data , values
122124}
123125
124- func parseYaml (value interface {}, path []string , values helmify.Values ) (string , error ) {
125- config := map [string ]interface {}{}
126- err := yaml .Unmarshal ([]byte (value .(string )), & config )
127- if err != nil {
128- return "" , errors .Wrapf (err , "unable to unmarshal configmap %v" , path )
129- }
130- parseConfig (config , values , path )
131- confBytes , err := yaml .Marshal (config )
132- if err != nil {
133- return "" , errors .Wrapf (err , "unable to marshal configmap %v" , path )
134- }
135- return string (confBytes ), nil
136- }
137-
138126// func parseProperties(properties string, path []string, values helmify.Values) (string, error) {
139127func parseProperties (properties interface {}, path []string , values helmify.Values ) (string , error ) {
140128 var res strings.Builder
@@ -157,51 +145,6 @@ func parseProperties(properties interface{}, path []string, values helmify.Value
157145 return res .String (), nil
158146}
159147
160- func parseConfig (config map [string ]interface {}, values helmify.Values , path []string ) {
161- for k , v := range config {
162- switch t := v .(type ) {
163- case string , bool , float64 , int64 :
164- if k == "kind" || k == "apiVersion" {
165- continue
166- }
167- templated , err := values .Add (v , append (path , k )... )
168- if err != nil {
169- logrus .WithError (err ).Error ()
170- continue
171- }
172- config [k ] = templated
173- case []interface {}:
174- templated , err := values .Add (v , append (path , k )... )
175- if err != nil {
176- logrus .WithError (err ).Error ()
177- continue
178- }
179- config [k ] = templated
180- case map [string ]interface {}:
181- if len (t ) == 0 {
182- templated , err := values .Add (v , append (path , k )... )
183- if err != nil {
184- logrus .WithError (err ).Error ()
185- continue
186- }
187- config [k ] = templated
188- continue
189- }
190- parseConfig (t , values , append (path , k ))
191- case map [interface {}]interface {}:
192- c , ok := v .(map [string ]interface {})
193- if ! ok {
194- logrus .Warn ("configmap: unable to cast to map[string]interface{}" )
195- continue
196- }
197- parseConfig (c , values , append (path , k ))
198- default :
199- logrus .Warn ("configmap: unknown type " , t )
200- fmt .Printf ("\n %T\n " , t )
201- }
202- }
203- }
204-
205148type result struct {
206149 name string
207150 data struct {
0 commit comments