-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce readiness and liveness probe feature
Signed-off-by: RealAnna <[email protected]> feat: introduce readiness and liveness probe feature Signed-off-by: RealAnna <[email protected]> feat: added tests Signed-off-by: RealAnna <[email protected]> feat: introduce readiness and liveness probe feature Signed-off-by: RealAnna <[email protected]> feat: added fix Signed-off-by: RealAnna <[email protected]> feat: added fix Signed-off-by: RealAnna <[email protected]> feat: added fix Signed-off-by: RealAnna <[email protected]> feat: added fix Signed-off-by: RealAnna <[email protected]> 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 <[email protected]> * Update pkg/processor/probes/probes.go Co-authored-by: Florian Bacher <[email protected]> Signed-off-by: realanna <[email protected]> --------- Signed-off-by: realanna <[email protected]> Co-authored-by: Florian Bacher <[email protected]>
- Loading branch information
Showing
11 changed files
with
281 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package probes | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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" + | ||
"livenessProbe: {{- include \"tplvalues.render\" (dict \"value\" .Values.%[1]s.%[2]s.livenessProbe \"context\" $) | nindent 10 }}\n" + | ||
" {{- else }}\n" + | ||
"livenessProbe:\n%[3]s" + | ||
"\n{{- end }}" | ||
|
||
const readinessProbeTemplate = "\n{{- if .Values.%[1]s.%[2]s.readinessProbe }}\n" + | ||
"readinessProbe: {{- include \"tplvalues.render\" (dict \"value\" .Values.%[1]s.%[2]s.readinessProbe \"context\" $) | nindent 10 }}\n" + | ||
" {{- else }}\n" + | ||
"readinessProbe:\n%[3]s" + | ||
"\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 { | ||
|
||
cs, _, err := unstructured.NestedSlice(specMap, "containers") | ||
if err != nil { | ||
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 unstructured.SetNestedSlice(specMap, strContainers, "containers") | ||
|
||
} | ||
|
||
func setProbesTemplates(name string, castedContainer map[string]interface{}, values *helmify.Values) (string, error) { | ||
|
||
var ready, live string | ||
var err error | ||
if _, defined := castedContainer[livenessProbe]; defined { | ||
live, err = setProbe(name, castedContainer, values, livenessProbe) | ||
if err != nil { | ||
return "", err | ||
} | ||
delete(castedContainer, livenessProbe) | ||
} | ||
if _, defined := castedContainer[readinessProbe]; defined { | ||
ready, err = setProbe(name, castedContainer, values, readinessProbe) | ||
if err != nil { | ||
return "", err | ||
} | ||
delete(castedContainer, readinessProbe) | ||
} | ||
return setMap(name, castedContainer, live, ready) | ||
|
||
} | ||
|
||
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) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
strContainer := string(content) | ||
if live != "" { | ||
strContainer = strContainer + fmt.Sprintf(livenessProbeTemplate, name, containerName, live) | ||
} | ||
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) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return live, unstructured.SetNestedField(*values, castedContainer[probe], name, containerName, probe) | ||
|
||
} |
Oops, something went wrong.