@@ -2,18 +2,18 @@ package probes
22
33import (
44 "fmt"
5+ "strings"
56
67 "github.com/arttor/helmify/pkg/helmify"
78 yamlformat "github.com/arttor/helmify/pkg/yaml"
89 "github.com/iancoleman/strcase"
910 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10- "sigs.k8s.io/yaml"
1111)
1212
1313const livenessProbe = "livenessProbe"
1414const readinessProbe = "readinessProbe"
1515
16- const livenessProbeTemplate = "{{- if .Values.%[1]s.%[2]s.livenessProbe }}\n " +
16+ const livenessProbeTemplate = "\n {{- if .Values.%[1]s.%[2]s.livenessProbe }}\n " +
1717 "livenessProbe: {{- include \" tplvalues.render\" (dict \" value\" .Values.%[1]s.%[2]s.livenessProbe \" context\" $) | nindent 10 }}\n " +
1818 " {{- else }}\n " +
1919 "livenessProbe:\n %[3]s" +
@@ -26,24 +26,31 @@ const readinessProbeTemplate = "\n{{- if .Values.%[1]s.%[2]s.readinessProbe }}\n
2626 "\n {{- end }}"
2727
2828// ProcessSpecMap adds 'probes' to the Containers in specMap, if they are defined
29- func ProcessSpecMap (name string , specMap map [string ]interface {}, values * helmify.Values ) error {
29+ func ProcessSpecMap (name string , specMap map [string ]interface {}, values * helmify.Values ) ( string , error ) {
3030
3131 cs , _ , err := unstructured .NestedSlice (specMap , "containers" )
32+
3233 if err != nil {
33- return err
34+ return "" , err
3435 }
3536
3637 strContainers := make ([]interface {}, len (cs ))
3738 for i , c := range cs {
3839 castedContainer := c .(map [string ]interface {})
3940 strContainers [i ], err = setProbesTemplates (name , castedContainer , values )
4041 if err != nil {
41- return err
42+ return "" , err
4243 }
4344 }
45+ specMap ["containers" ] = strContainers
46+ specs , err := yamlformat .Marshal (specMap , 6 )
47+ if err != nil {
48+ return "" , err
49+ }
50+ res := strings .ReplaceAll (string (specs ), "|\n " , "" )
51+ res = strings .ReplaceAll (res , "|-\n " , "" )
4452
45- return unstructured .SetNestedSlice (specMap , strContainers , "containers" )
46-
53+ return res , nil
4754}
4855
4956func setProbesTemplates (name string , castedContainer map [string ]interface {}, values * helmify.Values ) (string , error ) {
@@ -70,8 +77,7 @@ func setProbesTemplates(name string, castedContainer map[string]interface{}, val
7077
7178func setMap (name string , castedContainer map [string ]interface {}, live string , ready string ) (string , error ) {
7279 containerName := strcase .ToLowerCamel (castedContainer ["name" ].(string ))
73- content , err := yaml .Marshal (castedContainer )
74-
80+ content , err := yamlformat .Marshal (castedContainer , 0 )
7581 if err != nil {
7682 return "" , err
7783 }
@@ -82,16 +88,17 @@ func setMap(name string, castedContainer map[string]interface{}, live string, re
8288 if ready != "" {
8389 strContainer = strContainer + fmt .Sprintf (readinessProbeTemplate , name , containerName , ready )
8490 }
91+
8592 return strContainer , nil
8693}
8794
8895func setProbe (name string , castedContainer map [string ]interface {}, values * helmify.Values , probe string ) (string , error ) {
8996 containerName := strcase .ToLowerCamel (castedContainer ["name" ].(string ))
90- live , err := yamlformat .Marshal (castedContainer [probe ], 1 )
97+ templatedProbe , err := yamlformat .Marshal (castedContainer [probe ], 1 )
9198 if err != nil {
9299 return "" , err
93100 }
94101
95- return live , unstructured .SetNestedField (* values , castedContainer [probe ], name , containerName , probe )
102+ return templatedProbe , unstructured .SetNestedField (* values , castedContainer [probe ], name , containerName , probe )
96103
97104}
0 commit comments