From d79ed281d3213db48d9df6ea804035d6e90e5ce8 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Fri, 16 Dec 2022 18:29:48 +0200 Subject: [PATCH] make some functions reusable --- .../1.0.0/templates/lib/container/_images.tpl | 10 ++++++---- .../templates/lib/container/_lifecycle.tpl | 17 ++++++++++------- .../templates/lib/container/_termination.tpl | 10 ++++++++-- .../1.0.0/templates/lib/pod/_container.tpl | 8 ++++---- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/library/common/1.0.0/templates/lib/container/_images.tpl b/library/common/1.0.0/templates/lib/container/_images.tpl index 22ef1c34e9..d180d5fb00 100644 --- a/library/common/1.0.0/templates/lib/container/_images.tpl +++ b/library/common/1.0.0/templates/lib/container/_images.tpl @@ -6,10 +6,12 @@ {{- end -}} {{- define "ix.v1.common.images.selector" -}} - {{- $image := get .Values "image" -}} - {{- $selected := .Values.imageSelector -}} - {{- if hasKey .Values $selected -}} - {{- $image = get .Values $selected -}} + {{- $root := .root -}} + {{- $selectedImage := .selectedImage -}} + + {{- $image := get $root.Values "image" -}} + {{- if hasKey $root.Values $selectedImage -}} + {{- $image = get $root.Values $selectedImage -}} {{- end -}} {{- include "ix.v1.common.images.image" (dict "imageRoot" $image) -}} {{- end -}} 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 1b195257b6..4857f8f57b 100644 --- a/library/common/1.0.0/templates/lib/container/_lifecycle.tpl +++ b/library/common/1.0.0/templates/lib/container/_lifecycle.tpl @@ -1,28 +1,31 @@ {{/* Returns the lifecycle for the container */}} {{- define "ix.v1.common.container.lifecycle" -}} - {{- with .Values.lifecycle -}} + {{- $root := .root -}} + {{- $lifecycle := .lifecycle -}} + + {{- with $lifecycle -}} {{- range $k, $v := . -}} {{- if not (mustHas $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 }} + {{- if (hasKey $lifecycle "preStop") -}} + {{- with $lifecycle.preStop.command }} {{- print "preStop:" | nindent 0 }} {{- print "exec:" | nindent 2 }} {{- print "command:" | nindent 4 }} - {{- include "ix.v1.common.container.command" (dict "commands" . "root" $) | trim | nindent 6 }} + {{- include "ix.v1.common.container.command" (dict "commands" . "root" $root) | trim | nindent 6 }} {{- else -}} {{- fail "No commands were given for preStop lifecycle hook" -}} {{- end -}} {{- end -}} - {{- if (hasKey .Values.lifecycle "postStart") -}} - {{- with .Values.lifecycle.postStart.command }} + {{- if (hasKey $lifecycle "postStart") -}} + {{- with $lifecycle.postStart.command }} {{- print "postStart:" | nindent 0 }} {{- print "exec:" | nindent 2 }} {{- print "command:" | nindent 4 }} - {{- include "ix.v1.common.container.command" (dict "commands" . "root" $) | trim | nindent 6 }} + {{- include "ix.v1.common.container.command" (dict "commands" . "root" $root) | trim | nindent 6 }} {{- else -}} {{- fail "No commands were given for postStart lifecycle hook" -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/container/_termination.tpl b/library/common/1.0.0/templates/lib/container/_termination.tpl index fa8fa63f75..eaaa947f01 100644 --- a/library/common/1.0.0/templates/lib/container/_termination.tpl +++ b/library/common/1.0.0/templates/lib/container/_termination.tpl @@ -1,11 +1,17 @@ {{/* Returns the terminationMessagePath for the container */}} {{- define "ix.v1.common.container.termination.messagePath" -}} - {{- tpl .Values.termination.messagePath $ }} + {{- $msgPath := .msgPath -}} + {{- $root := .root -}} + + {{- tpl $msgPath $root -}} {{- end -}} {{/* Returns the terminationMessagePolicy for the container */}} {{- define "ix.v1.common.container.termination.messagePolicy" -}} - {{- $policy := (tpl .Values.termination.messagePolicy $) -}} + {{- $msgPolicy := .msgPolicy -}} + {{- $root := .root -}} + + {{- $policy := (tpl $msgPolicy $root) -}} {{- with $policy -}} {{- if not (mustHas . (list "File" "FallbackToLogsOnError")) }} {{- fail "Not valid option for messagePolicy" -}} diff --git a/library/common/1.0.0/templates/lib/pod/_container.tpl b/library/common/1.0.0/templates/lib/pod/_container.tpl index a888a1aec9..987814771a 100644 --- a/library/common/1.0.0/templates/lib/pod/_container.tpl +++ b/library/common/1.0.0/templates/lib/pod/_container.tpl @@ -9,7 +9,7 @@ So it can work on multiple places, like additional containers and not only the m */}} {{- define "ix.v1.common.controller.mainContainer" -}} - name: {{ include "ix.v1.common.names.fullname" . }} - image: {{ include "ix.v1.common.images.selector" . }} + image: {{ include "ix.v1.common.images.selector" (dict "root" . "selectedImage" .Values.imageSelector ) }} imagePullPolicy: {{ include "ix.v1.common.images.pullPolicy" (dict "policy" .Values.image.pullPolicy) }} tty: {{ .Values.tty }} stdin: {{ .Values.stdin }} @@ -25,14 +25,14 @@ So it can work on multiple places, like additional containers and not only the m securityContext: {{- . | nindent 4 }} {{- end -}} - {{- with (include "ix.v1.common.container.lifecycle" .) | trim }} + {{- with (include "ix.v1.common.container.lifecycle" (dict "lifecycle" .Values.lifecycle "root" $)) | trim }} lifecycle: {{- . | nindent 4 }} {{- end }} - {{- with (include "ix.v1.common.container.termination.messagePath" .) | trim }} + {{- with (include "ix.v1.common.container.termination.messagePath" (dict "msgPath" .Values.termination.messagePath "root" $)) | trim }} terminationMessagePath: {{ . }} {{- end }} - {{- with (include "ix.v1.common.container.termination.messagePolicy" .) | trim }} + {{- with (include "ix.v1.common.container.termination.messagePolicy" (dict "msgPolicy" .Values.termination.messagePolicy "root" $)) | trim }} terminationMessagePolicy: {{ . }} {{- end -}} {{- with (include "ix.v1.common.container.envVars" (dict "envs" .Values.env "envList" .Values.envList "root" $) | trim) }}