diff --git a/library/common-test/default-values.yaml b/library/common-test/default-values.yaml index b43ffc4cac..578ed8dab7 100644 --- a/library/common-test/default-values.yaml +++ b/library/common-test/default-values.yaml @@ -4,7 +4,8 @@ workload: podSpec: containers: main: - enabled: false + enabled: true + service: main: diff --git a/library/common-test/tests/container/arg_test.yaml b/library/common-test/tests/container/arg_test.yaml new file mode 100644 index 0000000000..751583d85c --- /dev/null +++ b/library/common-test/tests/container/arg_test.yaml @@ -0,0 +1,114 @@ +suite: container arg test +templates: + - common.yaml +tests: + - it: should create the correct multiple args + set: + some_port: 80 + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + args: + - --port + - "{{ .Values.some_port }}" + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + args: + - --port + - "80" + + - it: should create the correct arg command + set: + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + args: --accept-eula + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + args: + - --accept-eula + + - it: should create the correct multiple args and extraArgs + set: + some_path: /some/path + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + args: + - --port + - "80" + extraArgs: + - --accept-eula + - --path + - "{{ .Values.some_path }}" + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + args: + - --port + - "80" + - --accept-eula + - --path + - /some/path diff --git a/library/common-test/tests/container/command_test.yaml b/library/common-test/tests/container/command_test.yaml new file mode 100644 index 0000000000..fefe609dcf --- /dev/null +++ b/library/common-test/tests/container/command_test.yaml @@ -0,0 +1,109 @@ +suite: container command test +templates: + - common.yaml +tests: + - it: should create the correct command in scalar + set: + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + command: + - /bin/sh + - -c + - | + echo "Using image {{ .Values.image.repository }}" + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + command: + - /bin/sh + - -c + - | + echo "Using image nginx" + + - it: should create the correct single command + set: + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + command: ./start.sh + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + command: + - ./start.sh + + - it: should create the correct multiple command + set: + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + command: + - ./print.sh + - "{{ .Values.image.repository }}" + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + command: + - ./print.sh + - nginx diff --git a/library/common/1.0.0/docs/container/README.md b/library/common/1.0.0/docs/container/README.md index add5796264..eb61eaea85 100644 --- a/library/common/1.0.0/docs/container/README.md +++ b/library/common/1.0.0/docs/container/README.md @@ -2,13 +2,16 @@ Assume every key bellow has a prefix of `workload.[workload-name].podSpec`. -| Key | Type | Required | Helm Template | Default | Description | -| :---------------------------------- | :-------: | :------: | :-----------: | :-----: | :-------------------------------- | -| containers.[container-name] | `dict` | ✅ | ❌ | `{}` | Define the container as dict | -| containers.[container-name].enabled | `boolean` | ✅ | ❌ | `false` | Enables or Disables the container | -| containers.[container-name].primary | `boolean` | ✅ | ❌ | `false` | Sets the container as primary | -| containers.[container-name].stdin | `boolean` | ❌ | ❌ | `false` | whether to enable stdin or not | -| containers.[container-name].tty | `boolean` | ❌ | ❌ | `false` | whether to enable tty or not | +| Key | Type | Required | Helm Template | Default | Description | +| :------------------------------------ | :-----------: | :------: | :-----------: | :-----: | :--------------------------------------------------------------------------------------------- | +| containers.[container-name] | `dict` | ✅ | ❌ | `{}` | Define the container as dict | +| containers.[container-name].enabled | `boolean` | ✅ | ❌ | `false` | Enables or Disables the container | +| containers.[container-name].primary | `boolean` | ✅ | ❌ | `false` | Sets the container as primary | +| containers.[container-name].stdin | `boolean` | ❌ | ❌ | `false` | whether to enable stdin or not | +| containers.[container-name].tty | `boolean` | ❌ | ❌ | `false` | whether to enable tty or not | +| containers.[container-name].command | `list/string` | ❌ | ✅ | `[]` | Define command(s). If it's single, can be defined as string | +| containers.[container-name].args | `list/string` | ❌ | ✅ | `[]` | Define arg(s). If it's single, can be defined as string | +| containers.[container-name].extraArgs | `list/string` | ❌ | ✅ | `[]` | Define extraArg(s). Those are appended after the `args`. Useful for user defined args from GUI | --- @@ -43,4 +46,10 @@ workload: primary: true stdin: true tty: true + command: + - command + args: arg + extraArgs: + - extraArg + ``` diff --git a/library/common/1.0.0/templates/lib/container/_args.tpl b/library/common/1.0.0/templates/lib/container/_args.tpl new file mode 100644 index 0000000000..c46c2202fe --- /dev/null +++ b/library/common/1.0.0/templates/lib/container/_args.tpl @@ -0,0 +1,22 @@ +{{/* Returns args list */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.container.args" (dict "rootCtx" $ "objectData" $objectData) }} +rootCtx: The root context of the template. It is used to access the global context. +objectData: The object data to be used to render the container. +*/}} +{{- define "ix.v1.common.lib.container.args" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- range $key := (list "args" "extraArgs") -}} + {{- with (get $objectData $key) -}} + {{- if kindIs "string" . }} +- {{ tpl . $rootCtx | quote }} + {{- else if kindIs "slice" . -}} + {{- range $arg := . }} +- {{ tpl $arg $rootCtx | quote }} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/container/_command.tpl b/library/common/1.0.0/templates/lib/container/_command.tpl new file mode 100644 index 0000000000..acdd6a5276 --- /dev/null +++ b/library/common/1.0.0/templates/lib/container/_command.tpl @@ -0,0 +1,18 @@ +{{/* Returns command list */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.container.command" (dict "rootCtx" $ "objectData" $objectData) }} +rootCtx: The root context of the template. It is used to access the global context. +objectData: The object data to be used to render the container. +*/}} +{{- define "ix.v1.common.lib.container.command" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- if kindIs "string" $objectData.command }} +- {{ tpl $objectData.command $rootCtx | quote }} + {{- else if kindIs "slice" $objectData.command -}} + {{- range $objectData.command }} +- {{ tpl . $rootCtx | quote }} + {{- end -}} + {{- end -}} +{{- end -}} 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 921b83f46c..8128164003 100644 --- a/library/common/1.0.0/templates/lib/pod/_container.tpl +++ b/library/common/1.0.0/templates/lib/pod/_container.tpl @@ -14,4 +14,12 @@ objectData: The object data to be used to render the Pod. imagePullPolicy: {{ $imageObj.pullPolicy }} tty: {{ $objectData.tty | default false }} stdin: {{ $objectData.stdin | default false }} + {{- with (include "ix.v1.common.lib.container.command" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} + command: + {{- . | nindent 4 }} + {{- end -}} + {{- with (include "ix.v1.common.lib.container.args" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} + args: + {{- . | nindent 4 }} + {{- end -}} {{- end -}}