Skip to content

Commit b00f847

Browse files
committed
feat: introduced liveness and readiness probes
Signed-off-by: RealAnna <anna.reale@dynatrace.com>
1 parent 4075e9a commit b00f847

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

pkg/processor/deployment/deployment.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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.%[4]s }}"
50+
const envValue = "{{ .Values.%[1]s.%[2]s.%[3]s }}"
5151

5252
// New creates processor for k8s Deployment resource.
5353
func New() helmify.Processor {
@@ -162,21 +162,19 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
162162
imagePullSecrets.ProcessSpecMap(specMap, &values)
163163
}
164164

165+
spec, err := yamlformat.Marshal(specMap, 6)
166+
if err != nil {
167+
return true, nil, err
168+
}
169+
165170
if appMeta.Config().Probes {
166-
err = probes.ProcessSpecMap(nameCamel, specMap, &values)
171+
spec, err = probes.ProcessSpecMap(nameCamel, specMap, &values)
167172
if err != nil {
168173
return true, nil, err
169174
}
170175
}
171176

172-
spec, err := yamlformat.Marshal(specMap, 6)
173-
if err != nil {
174-
return true, nil, err
175-
}
176-
177177
spec = strings.ReplaceAll(spec, "'", "")
178-
spec = strings.ReplaceAll(spec, "|\n ", "")
179-
spec = strings.ReplaceAll(spec, "|-\n ", "")
180178
return true, &result{
181179
values: values,
182180
data: struct {

pkg/processor/probes/probes.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package probes
22

33
import (
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

1313
const livenessProbe = "livenessProbe"
1414
const 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

4956
func 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

7178
func 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

8895
func 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

Comments
 (0)