Skip to content

Commit c4a0299

Browse files
RealAnnabacherfl
andcommitted
feat: introduce readiness and liveness probe feature
Signed-off-by: RealAnna <anna.reale@dynatrace.com> feat: introduce readiness and liveness probe feature Signed-off-by: RealAnna <anna.reale@dynatrace.com> feat: added tests Signed-off-by: RealAnna <anna.reale@dynatrace.com> feat: introduce readiness and liveness probe feature Signed-off-by: RealAnna <anna.reale@dynatrace.com> feat: added fix Signed-off-by: RealAnna <anna.reale@dynatrace.com> feat: added fix Signed-off-by: RealAnna <anna.reale@dynatrace.com> feat: added fix Signed-off-by: RealAnna <anna.reale@dynatrace.com> feat: added fix Signed-off-by: RealAnna <anna.reale@dynatrace.com> feat: introduce readiness and liveness probe feature (#3) * feat: introduce readiness and liveness probe feature feat: introduce readiness and liveness probe feature Signed-off-by: realanna <anna.reale@dynatrace.com> * Update pkg/processor/probes/probes.go Co-authored-by: Florian Bacher <florian.bacher@dynatrace.com> Signed-off-by: realanna <anna.reale@dynatrace.com> --------- Signed-off-by: realanna <anna.reale@dynatrace.com> Co-authored-by: Florian Bacher <florian.bacher@dynatrace.com>
1 parent 5b663ea commit c4a0299

11 files changed

Lines changed: 281 additions & 30 deletions

File tree

cmd/helmify/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func ReadFlags() config.Config {
3737
flag.BoolVar(&result.VeryVerbose, "vv", false, "Enable very verbose output. Same as verbose but with DEBUG. Example: helmify -vv")
3838
flag.BoolVar(&crd, "crd-dir", false, "Enable crd install into 'crds' directory.\nWarning: CRDs placed in 'crds' directory will not be templated by Helm.\nSee https://helm.sh/docs/chart_best_practices/custom_resource_definitions/#some-caveats-and-explanations\nExample: helmify -crd-dir")
3939
flag.BoolVar(&result.ImagePullSecrets, "image-pull-secrets", false, "Allows the user to use existing secrets as imagePullSecrets in values.yaml")
40+
flag.BoolVar(&result.Probes, "probes", true, "Allows the user to customize liveness and readiness probes")
4041

4142
flag.Parse()
4243
if h || help {

examples/operator/templates/_helpers.tpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,17 @@ Create the name of the service account to use
6060
{{- default "default" .Values.serviceAccount.name }}
6161
{{- end }}
6262
{{- end }}
63+
64+
{{/*
65+
Renders a value that contains template.
66+
Usage:
67+
{{ include "tplvalues.render" ( dict "value" .Values.path.to.the.Value "context" $) }}
68+
*/}}
69+
{{- define "tplvalues.render" -}}
70+
{{- if typeIs "string" .value }}
71+
{{- tpl .value .context }}
72+
{{- else }}
73+
{{- tpl (.value | toYaml) .context }}
74+
{{- end }}
75+
{{- end -}}
76+

examples/operator/templates/deployment.yaml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,15 @@ spec:
5353
key: VAR1
5454
name: {{ include "operator.fullname" . }}-secret-vars
5555
- name: VAR2
56-
value: {{ .Values.controllerManager.manager.var2 }}
56+
value: {{ .Values.controllerManager.manager.env.var2 }}
5757
- name: VAR3_MY_ENV
58-
value: {{ .Values.controllerManager.manager.var3MyEnv }}
58+
value: {{ .Values.controllerManager.manager.env.var3MyEnv }}
5959
- name: KUBERNETES_CLUSTER_DOMAIN
6060
value: {{ .Values.kubernetesClusterDomain }}
6161
image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag
6262
| default .Chart.AppVersion }}
63-
livenessProbe:
64-
httpGet:
65-
path: /healthz
66-
port: 8081
67-
initialDelaySeconds: 15
68-
periodSeconds: 20
6963
name: manager
70-
readinessProbe:
71-
httpGet:
72-
path: /readyz
73-
port: 8081
74-
initialDelaySeconds: 5
75-
periodSeconds: 10
76-
resources: {{- toYaml .Values.controllerManager.manager.resources | nindent 10
77-
}}
64+
resources: {{- toYaml .Values.controllerManager.manager.resources | nindent 10 }}
7865
securityContext:
7966
allowPrivilegeEscalation: false
8067
volumeMounts:
@@ -83,6 +70,26 @@ spec:
8370
subPath: controller_manager_config.yaml
8471
- mountPath: /my.ca
8572
name: secret-volume
73+
{{- if .Values.controllerManager.manager.livenessProbe }}
74+
livenessProbe: {{- include "tplvalues.render" (dict "value" .Values.controllerManager.manager.livenessProbe "context" $) | nindent 10 }}
75+
{{- else }}
76+
livenessProbe:
77+
httpGet:
78+
path: /healthz
79+
port: 8081
80+
initialDelaySeconds: 15
81+
periodSeconds: 20
82+
{{- end }}
83+
{{- if .Values.controllerManager.manager.readinessProbe }}
84+
readinessProbe: {{- include "tplvalues.render" (dict "value" .Values.controllerManager.manager.readinessProbe "context" $) | nindent 10 }}
85+
{{- else }}
86+
readinessProbe:
87+
httpGet:
88+
path: /readyz
89+
port: 8081
90+
initialDelaySeconds: 5
91+
periodSeconds: 10
92+
{{- end }}
8693
imagePullSecrets:
8794
- name: {{ include "operator.fullname" . }}-secret-registry-credentials
8895
securityContext:

examples/operator/values.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ controllerManager:
1010
image:
1111
repository: controller
1212
tag: latest
13+
livenessProbe:
14+
httpGet:
15+
path: /healthz
16+
port: 8081
17+
initialDelaySeconds: 15
18+
periodSeconds: 20
19+
readinessProbe:
20+
httpGet:
21+
path: /readyz
22+
port: 8081
23+
initialDelaySeconds: 5
24+
periodSeconds: 10
1325
resources:
1426
limits:
1527
cpu: 100m

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Config struct {
2323
Crd bool
2424
// ImagePullSecrets flag
2525
ImagePullSecrets bool
26+
// Probes flag if true the probes will be parametrised
27+
Probes bool
2628
}
2729

2830
func (c *Config) Validate() error {

pkg/helm/init.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ Create the name of the service account to use
9999
{{- default "default" .Values.serviceAccount.name }}
100100
{{- end }}
101101
{{- end }}
102+
103+
{{/*
104+
Renders a value that contains template.
105+
Usage:
106+
{{ include "tplvalues.render" ( dict "value" .Values.path.to.the.Value "context" $) }}
107+
*/}}
108+
{{- define "tplvalues.render" -}}
109+
{{- if typeIs "string" .value }}
110+
{{- tpl .value .context }}
111+
{{- else }}
112+
{{- tpl (.value | toYaml) .context }}
113+
{{- end }}
114+
{{- end -}}
115+
102116
`
103117

104118
const defaultChartfile = `apiVersion: v2

pkg/helmify/model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package helmify
22

33
import (
4-
"github.com/arttor/helmify/pkg/config"
54
"io"
65

6+
"github.com/arttor/helmify/pkg/config"
7+
78
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
89
)
910

pkg/metadata/metadata.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package metadata
22

33
import (
44
"fmt"
5-
"github.com/arttor/helmify/pkg/config"
65
"strings"
76

7+
"github.com/arttor/helmify/pkg/config"
8+
89
"github.com/arttor/helmify/pkg/helmify"
910
"github.com/sirupsen/logrus"
1011
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

pkg/processor/deployment/deployment.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
5353
func New() helmify.Processor {
@@ -139,19 +139,20 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
139139
if err != nil {
140140
return true, nil, err
141141
}
142+
142143
for i := range containers {
143144
containerName := strcase.ToLowerCamel((containers[i].(map[string]interface{})["name"]).(string))
144145
res, exists, err := unstructured.NestedMap(values, nameCamel, containerName, "resources")
145146
if err != nil {
146147
return true, nil, err
147148
}
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
149+
if exists && len(res) > 0 {
150+
err = unstructured.SetNestedField(containers[i].(map[string]interface{}), fmt.Sprintf(`{{- toYaml .Values.%s.%s.resources | nindent 10 }}`, nameCamel, containerName), "resources")
151+
if err != nil {
152+
return true, nil, err
153+
}
154154
}
155+
155156
}
156157
err = unstructured.SetNestedSlice(specMap, containers, "containers")
157158
if err != nil {
@@ -162,12 +163,21 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
162163
imagePullSecrets.ProcessSpecMap(specMap, &values)
163164
}
164165

166+
if appMeta.Config().Probes {
167+
err = probes.ProcessSpecMap(nameCamel, specMap, &values)
168+
if err != nil {
169+
return true, nil, err
170+
}
171+
}
172+
165173
spec, err := yamlformat.Marshal(specMap, 6)
166174
if err != nil {
167175
return true, nil, err
168176
}
169-
spec = strings.ReplaceAll(spec, "'", "")
170177

178+
spec = strings.ReplaceAll(spec, "'", "")
179+
spec = strings.ReplaceAll(spec, "|\n ", "")
180+
spec = strings.ReplaceAll(spec, "|-\n ", "")
171181
return true, &result{
172182
values: values,
173183
data: struct {
@@ -208,6 +218,7 @@ func processPodSpec(name string, appMeta helmify.AppMetadata, pod *corev1.PodSpe
208218
values := helmify.Values{}
209219
for i, c := range pod.Containers {
210220
processed, err := processPodContainer(name, appMeta, c, &values)
221+
211222
if err != nil {
212223
return nil, err
213224
}
@@ -260,13 +271,16 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont
260271
if e.ConfigMapRef != nil {
261272
e.ConfigMapRef.Name = appMeta.TemplatedName(e.ConfigMapRef.Name)
262273
}
274+
263275
}
276+
264277
c.Env = append(c.Env, corev1.EnvVar{
265278
Name: cluster.DomainEnv,
266279
Value: fmt.Sprintf("{{ .Values.%s }}", cluster.DomainKey),
267280
})
268281
for k, v := range c.Resources.Requests {
269282
err = unstructured.SetNestedField(*values, v.ToUnstructured(), name, containerName, "resources", "requests", k.String())
283+
270284
if err != nil {
271285
return c, errors.Wrap(err, "unable to set container resources value")
272286
}
@@ -277,7 +291,8 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont
277291
return c, errors.Wrap(err, "unable to set container resources value")
278292
}
279293
}
280-
return c, nil
294+
295+
return c, err
281296
}
282297

283298
func processEnv(name string, appMeta helmify.AppMetadata, c corev1.Container, values *helmify.Values) (corev1.Container, error) {
@@ -293,7 +308,7 @@ func processEnv(name string, appMeta helmify.AppMetadata, c corev1.Container, va
293308
if err != nil {
294309
return c, errors.Wrap(err, "unable to set deployment value field")
295310
}
296-
c.Env[i].Value = fmt.Sprintf(envValue, name, containerName, strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name)))
311+
c.Env[i].Value = fmt.Sprintf(envValue, name, containerName, "env", strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name)))
297312
}
298313
}
299314
return c, nil

pkg/processor/probes/probes.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package probes
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/arttor/helmify/pkg/helmify"
7+
yamlformat "github.com/arttor/helmify/pkg/yaml"
8+
"github.com/iancoleman/strcase"
9+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10+
"sigs.k8s.io/yaml"
11+
)
12+
13+
const livenessProbe = "livenessProbe"
14+
const readinessProbe = "readinessProbe"
15+
16+
const livenessProbeTemplate = "{{- if .Values.%[1]s.%[2]s.livenessProbe }}\n" +
17+
"livenessProbe: {{- include \"tplvalues.render\" (dict \"value\" .Values.%[1]s.%[2]s.livenessProbe \"context\" $) | nindent 10 }}\n" +
18+
" {{- else }}\n" +
19+
"livenessProbe:\n%[3]s" +
20+
"\n{{- end }}"
21+
22+
const readinessProbeTemplate = "\n{{- if .Values.%[1]s.%[2]s.readinessProbe }}\n" +
23+
"readinessProbe: {{- include \"tplvalues.render\" (dict \"value\" .Values.%[1]s.%[2]s.readinessProbe \"context\" $) | nindent 10 }}\n" +
24+
" {{- else }}\n" +
25+
"readinessProbe:\n%[3]s" +
26+
"\n{{- end }}"
27+
28+
// 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 {
30+
31+
cs, _, err := unstructured.NestedSlice(specMap, "containers")
32+
if err != nil {
33+
return err
34+
}
35+
36+
strContainers := make([]interface{}, len(cs))
37+
for i, c := range cs {
38+
castedContainer := c.(map[string]interface{})
39+
strContainers[i], err = setProbesTemplates(name, castedContainer, values)
40+
if err != nil {
41+
return err
42+
}
43+
}
44+
45+
return unstructured.SetNestedSlice(specMap, strContainers, "containers")
46+
47+
}
48+
49+
func setProbesTemplates(name string, castedContainer map[string]interface{}, values *helmify.Values) (string, error) {
50+
51+
var ready, live string
52+
var err error
53+
if _, defined := castedContainer[livenessProbe]; defined {
54+
live, err = setProbe(name, castedContainer, values, livenessProbe)
55+
if err != nil {
56+
return "", err
57+
}
58+
delete(castedContainer, livenessProbe)
59+
}
60+
if _, defined := castedContainer[readinessProbe]; defined {
61+
ready, err = setProbe(name, castedContainer, values, readinessProbe)
62+
if err != nil {
63+
return "", err
64+
}
65+
delete(castedContainer, readinessProbe)
66+
}
67+
return setMap(name, castedContainer, live, ready)
68+
69+
}
70+
71+
func setMap(name string, castedContainer map[string]interface{}, live string, ready string) (string, error) {
72+
containerName := strcase.ToLowerCamel(castedContainer["name"].(string))
73+
content, err := yaml.Marshal(castedContainer)
74+
75+
if err != nil {
76+
return "", err
77+
}
78+
strContainer := string(content)
79+
if live != "" {
80+
strContainer = strContainer + fmt.Sprintf(livenessProbeTemplate, name, containerName, live)
81+
}
82+
if ready != "" {
83+
strContainer = strContainer + fmt.Sprintf(readinessProbeTemplate, name, containerName, ready)
84+
}
85+
return strContainer, nil
86+
}
87+
88+
func setProbe(name string, castedContainer map[string]interface{}, values *helmify.Values, probe string) (string, error) {
89+
containerName := strcase.ToLowerCamel(castedContainer["name"].(string))
90+
live, err := yamlformat.Marshal(castedContainer[probe], 1)
91+
if err != nil {
92+
return "", err
93+
}
94+
95+
return live, unstructured.SetNestedField(*values, castedContainer[probe], name, containerName, probe)
96+
97+
}

0 commit comments

Comments
 (0)