diff --git a/library/common-test/tests/container_in_deployment/lifecycle_test.yaml b/library/common-test/tests/container_in_deployment/lifecycle_test.yaml index e8ff8bf8..34823bac 100644 --- a/library/common-test/tests/container_in_deployment/lifecycle_test.yaml +++ b/library/common-test/tests/container_in_deployment/lifecycle_test.yaml @@ -148,6 +148,15 @@ tests: - /bin/bash - some_value + - it: should fail with invalid key + set: + lifecycle: + preStart: + command: something + asserts: + - failedTemplate: + errorMessage: Invalid key (preStart) in lifecycle. Valid keys are preStop and postStart + - it: should fail with no command in preStop set: lifecycle: diff --git a/library/common/1.0.0/templates/lib/container/_lifecycle.tpl b/library/common/1.0.0/templates/lib/container/_lifecycle.tpl index a30ff449..a9d6adea 100644 --- a/library/common/1.0.0/templates/lib/container/_lifecycle.tpl +++ b/library/common/1.0.0/templates/lib/container/_lifecycle.tpl @@ -1,5 +1,12 @@ {{/* Returns the lifecycle for the container */}} {{- define "ix.v1.common.container.lifecycle" -}} + {{- with .Values.lifecycle -}} + {{- range $k, $v := . -}} + {{- if not (has $k (list "preStop" "postStart")) -}} + {{- fail (printf "Invalid key (%s) in lifecycle. Valid keys are preStop and postStart" $k ) -}} + {{- end -}} + {{- end -}} + {{- end -}} {{- if (hasKey .Values.lifecycle "preStop") -}} {{- with .Values.lifecycle.preStop.command }} {{- print "preStop:" | nindent 0 }} diff --git a/library/common/_docs/values/lifecycle.md b/library/common/_docs/values/lifecycle.md new file mode 100644 index 00000000..65018795 --- /dev/null +++ b/library/common/_docs/values/lifecycle.md @@ -0,0 +1,75 @@ +# Lifecycle + +## Key: lifecycle + +- Type: `dict` +- Default: `{}` +- Helm Template: + - lifecycle.preStop.command - String: ✅ + - lifecycle.preStop.command - List entry: ✅ + - lifecycle.postStart.command - String: ✅ + - lifecycle.postStart.command - List entry: ✅ + +`lifecycle` key defines hooks that can run on the pod. Like `preStop` or `postStart` + +Examples `preStop`: + +```yaml +# String / Single command +lifecycle: + preStop: + command: ./custom-script.sh +# String / Single command (tpl) +lifecycle: + preStop: + command: "{{ .Values.customCommand }}" + +# List +lifecycle: + preStop: + command: + - /bin/sh + - -c + - | + echo "Doing things..." +# List (tpl) +lifecycle: + preStop: + command: + - /path/to/executable + - --port + - "{{ .Values.service.main.ports.main.port }}" +``` + +Examples `postStart`: + +```yaml +# String / Single command +lifecycle: + postStart: + command: ./custom-script.sh +# String / Single command (tpl) +lifecycle: + postStart: + command: "{{ .Values.customCommand }}" + +# List +lifecycle: + postStart: + command: + - /bin/sh + - -c + - | + echo "Doing things..." +# List (tpl) +lifecycle: + postStart: + command: + - /path/to/executable + - --port + - "{{ .Values.service.main.ports.main.port }}" +``` + +Kubernetes Documentation: + +- [lifecycle hooks](https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks)