@@ -7,10 +7,10 @@ import (
77 "text/template"
88
99 "github.com/arttor/helmify/pkg/cluster"
10+ "github.com/arttor/helmify/pkg/helmify"
1011 "github.com/arttor/helmify/pkg/processor"
1112 "github.com/arttor/helmify/pkg/processor/imagePullSecrets"
12-
13- "github.com/arttor/helmify/pkg/helmify"
13+ "github.com/arttor/helmify/pkg/processor/probes"
1414 yamlformat "github.com/arttor/helmify/pkg/yaml"
1515 "github.com/iancoleman/strcase"
1616 "github.com/pkg/errors"
@@ -47,7 +47,7 @@ const selectorTempl = `%[1]s
4747{{- include "%[2]s.selectorLabels" . | nindent 6 }}
4848%[3]s`
4949
50- const envValue = "{{ .Values.%[1]s.%[2]s.%[3]s }}"
50+ const envValue = "{{ .Values.%[1]s.%[2]s.%[3]s.%[4]s }}"
5151
5252// New creates processor for k8s Deployment resource.
5353func New () helmify.Processor {
@@ -130,28 +130,32 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
130130 depl .Spec .Template .Spec .Volumes [i ].PersistentVolumeClaim .ClaimName = tempPVCName
131131 }
132132
133+ // remove from spec things that will be processed separately
134+ cleanSpec := cleanSpec (* depl .Spec .Template .Spec .DeepCopy ())
135+
133136 // replace container resources with template to values.
134- specMap , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& depl . Spec . Template . Spec )
137+ specMap , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& cleanSpec )
135138 if err != nil {
136139 return true , nil , err
137140 }
138141 containers , _ , err := unstructured .NestedSlice (specMap , "containers" )
139142 if err != nil {
140143 return true , nil , err
141144 }
145+
142146 for i := range containers {
143147 containerName := strcase .ToLowerCamel ((containers [i ].(map [string ]interface {})["name" ]).(string ))
144148 res , exists , err := unstructured .NestedMap (values , nameCamel , containerName , "resources" )
145149 if err != nil {
146150 return true , nil , err
147151 }
148- if ! exists || len (res ) == 0 {
149- continue
150- }
151- err = unstructured .SetNestedField (containers [i ].(map [string ]interface {}), fmt .Sprintf (`{{- toYaml .Values.%s.%s.resources | nindent 10 }}` , nameCamel , containerName ), "resources" )
152- if err != nil {
153- return true , nil , err
152+ if exists && len (res ) > 0 {
153+ err = unstructured .SetNestedField (containers [i ].(map [string ]interface {}), fmt .Sprintf (`{{- toYaml .Values.%s.%s.resources | nindent 10 }}` , nameCamel , containerName ), "resources" )
154+ if err != nil {
155+ return true , nil , err
156+ }
154157 }
158+
155159 }
156160 err = unstructured .SetNestedSlice (specMap , containers , "containers" )
157161 if err != nil {
@@ -162,12 +166,21 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
162166 imagePullSecrets .ProcessSpecMap (specMap , & values )
163167 }
164168
169+ if appMeta .Config ().Probes {
170+ err = probes .ProcessSpecMap (nameCamel , specMap , & values , depl .Spec .Template .Spec )
171+ if err != nil {
172+ return true , nil , err
173+ }
174+ }
175+
165176 spec , err := yamlformat .Marshal (specMap , 6 )
166177 if err != nil {
167178 return true , nil , err
168179 }
169- spec = strings .ReplaceAll (spec , "'" , "" )
170180
181+ spec = strings .ReplaceAll (spec , "'" , "" )
182+ spec = strings .ReplaceAll (spec , "|\n " , "" )
183+ spec = strings .ReplaceAll (spec , "|-\n " , "" )
171184 return true , & result {
172185 values : values ,
173186 data : struct {
@@ -188,6 +201,15 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
188201 }, nil
189202}
190203
204+ func cleanSpec (spec corev1.PodSpec ) corev1.PodSpec {
205+
206+ for i := 0 ; i < len (spec .Containers ); i ++ {
207+ spec .Containers [i ].LivenessProbe = nil
208+ spec .Containers [i ].ReadinessProbe = nil
209+ }
210+ return spec
211+ }
212+
191213func processReplicas (name string , deployment * appsv1.Deployment , values * helmify.Values ) (string , error ) {
192214 if deployment .Spec .Replicas == nil {
193215 return "" , nil
@@ -208,6 +230,7 @@ func processPodSpec(name string, appMeta helmify.AppMetadata, pod *corev1.PodSpe
208230 values := helmify.Values {}
209231 for i , c := range pod .Containers {
210232 processed , err := processPodContainer (name , appMeta , c , & values )
233+
211234 if err != nil {
212235 return nil , err
213236 }
@@ -260,13 +283,16 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont
260283 if e .ConfigMapRef != nil {
261284 e .ConfigMapRef .Name = appMeta .TemplatedName (e .ConfigMapRef .Name )
262285 }
286+
263287 }
288+
264289 c .Env = append (c .Env , corev1.EnvVar {
265290 Name : cluster .DomainEnv ,
266291 Value : fmt .Sprintf ("{{ .Values.%s }}" , cluster .DomainKey ),
267292 })
268293 for k , v := range c .Resources .Requests {
269294 err = unstructured .SetNestedField (* values , v .ToUnstructured (), name , containerName , "resources" , "requests" , k .String ())
295+
270296 if err != nil {
271297 return c , errors .Wrap (err , "unable to set container resources value" )
272298 }
@@ -277,7 +303,8 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont
277303 return c , errors .Wrap (err , "unable to set container resources value" )
278304 }
279305 }
280- return c , nil
306+
307+ return c , err
281308}
282309
283310func processEnv (name string , appMeta helmify.AppMetadata , c corev1.Container , values * helmify.Values ) (corev1.Container , error ) {
@@ -293,7 +320,7 @@ func processEnv(name string, appMeta helmify.AppMetadata, c corev1.Container, va
293320 if err != nil {
294321 return c , errors .Wrap (err , "unable to set deployment value field" )
295322 }
296- c .Env [i ].Value = fmt .Sprintf (envValue , name , containerName , strcase .ToLowerCamel (strings .ToLower (c .Env [i ].Name )))
323+ c .Env [i ].Value = fmt .Sprintf (envValue , name , containerName , "env" , strcase .ToLowerCamel (strings .ToLower (c .Env [i ].Name )))
297324 }
298325 }
299326 return c , nil
0 commit comments