Skip to content

Commit

Permalink
feat: introduced liveness and readiness probes
Browse files Browse the repository at this point in the history
Signed-off-by: RealAnna <[email protected]>
  • Loading branch information
RealAnna committed Feb 20, 2023
1 parent 4075e9a commit b00f847
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
16 changes: 7 additions & 9 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const selectorTempl = `%[1]s
{{- include "%[2]s.selectorLabels" . | nindent 6 }}
%[3]s`

const envValue = "{{ .Values.%[1]s.%[2]s.%[3]s.%[4]s }}"
const envValue = "{{ .Values.%[1]s.%[2]s.%[3]s }}"

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

spec, err := yamlformat.Marshal(specMap, 6)
if err != nil {
return true, nil, err
}

if appMeta.Config().Probes {
err = probes.ProcessSpecMap(nameCamel, specMap, &values)
spec, err = probes.ProcessSpecMap(nameCamel, specMap, &values)
if err != nil {
return true, nil, err
}
}

spec, err := yamlformat.Marshal(specMap, 6)
if err != nil {
return true, nil, err
}

spec = strings.ReplaceAll(spec, "'", "")
spec = strings.ReplaceAll(spec, "|\n ", "")
spec = strings.ReplaceAll(spec, "|-\n ", "")
return true, &result{
values: values,
data: struct {
Expand Down
29 changes: 18 additions & 11 deletions pkg/processor/probes/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package probes

import (
"fmt"
"strings"

"github.com/arttor/helmify/pkg/helmify"
yamlformat "github.com/arttor/helmify/pkg/yaml"
"github.com/iancoleman/strcase"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)

const livenessProbe = "livenessProbe"
const readinessProbe = "readinessProbe"

const livenessProbeTemplate = "{{- if .Values.%[1]s.%[2]s.livenessProbe }}\n" +
const livenessProbeTemplate = "\n{{- if .Values.%[1]s.%[2]s.livenessProbe }}\n" +
"livenessProbe: {{- include \"tplvalues.render\" (dict \"value\" .Values.%[1]s.%[2]s.livenessProbe \"context\" $) | nindent 10 }}\n" +
" {{- else }}\n" +
"livenessProbe:\n%[3]s" +
Expand All @@ -26,24 +26,31 @@ const readinessProbeTemplate = "\n{{- if .Values.%[1]s.%[2]s.readinessProbe }}\n
"\n{{- end }}"

// ProcessSpecMap adds 'probes' to the Containers in specMap, if they are defined
func ProcessSpecMap(name string, specMap map[string]interface{}, values *helmify.Values) error {
func ProcessSpecMap(name string, specMap map[string]interface{}, values *helmify.Values) (string, error) {

cs, _, err := unstructured.NestedSlice(specMap, "containers")

if err != nil {
return err
return "", err
}

strContainers := make([]interface{}, len(cs))
for i, c := range cs {
castedContainer := c.(map[string]interface{})
strContainers[i], err = setProbesTemplates(name, castedContainer, values)
if err != nil {
return err
return "", err
}
}
specMap["containers"] = strContainers
specs, err := yamlformat.Marshal(specMap, 6)
if err != nil {
return "", err
}
res := strings.ReplaceAll(string(specs), "|\n ", "")
res = strings.ReplaceAll(res, "|-\n ", "")

return unstructured.SetNestedSlice(specMap, strContainers, "containers")

return res, nil
}

func setProbesTemplates(name string, castedContainer map[string]interface{}, values *helmify.Values) (string, error) {
Expand All @@ -70,8 +77,7 @@ func setProbesTemplates(name string, castedContainer map[string]interface{}, val

func setMap(name string, castedContainer map[string]interface{}, live string, ready string) (string, error) {
containerName := strcase.ToLowerCamel(castedContainer["name"].(string))
content, err := yaml.Marshal(castedContainer)

content, err := yamlformat.Marshal(castedContainer, 0)
if err != nil {
return "", err
}
Expand All @@ -82,16 +88,17 @@ func setMap(name string, castedContainer map[string]interface{}, live string, re
if ready != "" {
strContainer = strContainer + fmt.Sprintf(readinessProbeTemplate, name, containerName, ready)
}

return strContainer, nil
}

func setProbe(name string, castedContainer map[string]interface{}, values *helmify.Values, probe string) (string, error) {
containerName := strcase.ToLowerCamel(castedContainer["name"].(string))
live, err := yamlformat.Marshal(castedContainer[probe], 1)
templatedProbe, err := yamlformat.Marshal(castedContainer[probe], 1)
if err != nil {
return "", err
}

return live, unstructured.SetNestedField(*values, castedContainer[probe], name, containerName, probe)
return templatedProbe, unstructured.SetNestedField(*values, castedContainer[probe], name, containerName, probe)

}

0 comments on commit b00f847

Please sign in to comment.