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