diff --git a/library/common-test/tests/deployment/names_test.yaml b/library/common-test/tests/controller/names_test.yaml similarity index 74% rename from library/common-test/tests/deployment/names_test.yaml rename to library/common-test/tests/controller/names_test.yaml index 7a1bb5a3ba..b6cdfd299d 100644 --- a/library/common-test/tests/deployment/names_test.yaml +++ b/library/common-test/tests/controller/names_test.yaml @@ -1,8 +1,8 @@ -suite: deployment name test +suite: controller name test templates: - common.yaml tests: - - it: should generate correct name + - it: should generate correct controller name set: controllers: controller-name: @@ -12,7 +12,7 @@ tests: other-controller-name: enabled: true primary: false - type: Deployment + type: DaemonSet asserts: - documentIndex: &deploymentDoc 0 isKind: @@ -24,13 +24,13 @@ tests: equal: path: metadata.name value: release-name-common-test - - documentIndex: &deploymentDoc 1 + - documentIndex: &daemonSetDoc 1 isKind: - of: Deployment - - documentIndex: *deploymentDoc + of: DaemonSet + - documentIndex: *daemonSetDoc isAPIVersion: of: apps/v1 - - documentIndex: *deploymentDoc + - documentIndex: *daemonSetDoc equal: path: metadata.name value: release-name-common-test-other-controller-name diff --git a/library/common-test/tests/daemonset/metadata_test.yaml b/library/common-test/tests/daemonset/metadata_test.yaml new file mode 100644 index 0000000000..f322b70b6b --- /dev/null +++ b/library/common-test/tests/daemonset/metadata_test.yaml @@ -0,0 +1,56 @@ +suite: daemonset metadata test +templates: + - common.yaml +chart: + appVersion: &appVer v9.9.9 +tests: + - it: should pass with daemonset created with labels and annotations + set: + label1: label1 + label2: global_label2 + annotation1: annotation1 + annotation2: global_annotation2 + global: + labels: + g_label1: global_label1 + g_label2: "{{ .Values.label2 }}" + annotations: + g_annotation1: global_annotation1 + g_annotation2: "{{ .Values.annotation2 }}" + controllers: + controller-name: + enabled: true + primary: true + type: DaemonSet + labels: + label1: "{{ .Values.label1 }}" + label2: label2 + annotations: + annotation1: "{{ .Values.annotation1 }}" + annotation2: annotation2 + asserts: + - documentIndex: &daemonSetDoc 0 + isKind: + of: DaemonSet + - documentIndex: *daemonSetDoc + equal: + path: metadata.annotations + value: + annotation1: annotation1 + annotation2: annotation2 + g_annotation1: global_annotation1 + g_annotation2: global_annotation2 + - documentIndex: *daemonSetDoc + equal: + path: metadata.labels + value: + app: common-test-1.0.0 + release: release-name + helm-revision: 0 + helm.sh/chart: common-test-1.0.0 + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/version: v9.9.9 + g_label1: global_label1 + g_label2: global_label2 + label1: label1 + label2: label2 diff --git a/library/common-test/tests/daemonset/spec_test.yaml b/library/common-test/tests/daemonset/spec_test.yaml new file mode 100644 index 0000000000..6673d54c07 --- /dev/null +++ b/library/common-test/tests/daemonset/spec_test.yaml @@ -0,0 +1,49 @@ +suite: daemonset spec test +templates: + - common.yaml +tests: + - it: should pass with controller enabled + set: + controllers: + controller-name: + enabled: true + primary: true + type: DaemonSet + asserts: + - documentIndex: &daemonSetDoc 0 + isKind: + of: DaemonSet + - documentIndex: *daemonSetDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *daemonSetDoc + isSubset: + path: spec + content: + revisionHistoryLimit: 3 + updateStrategy: + type: RollingUpdate + + - it: should apply spec correctly + set: + controllers: + controller-name: + enabled: true + primary: true + type: DaemonSet + revisionHistoryLimit: 4 + strategy: RollingUpdate + rollingUpdate: + maxSurge: 5 + maxUnavailable: 5 + asserts: + - documentIndex: *daemonSetDoc + isSubset: + path: spec + content: + revisionHistoryLimit: 4 + updateStrategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 5 + maxUnavailable: 5 diff --git a/library/common-test/tests/daemonset/validation_test.yaml b/library/common-test/tests/daemonset/validation_test.yaml new file mode 100644 index 0000000000..d8e9d0549e --- /dev/null +++ b/library/common-test/tests/daemonset/validation_test.yaml @@ -0,0 +1,28 @@ +suite: daemonset validation test +templates: + - common.yaml +tests: + - it: should fail with invalid strategy + set: + controllers: + controller-name: + enabled: true + primary: true + type: DaemonSet + strategy: not-a-strategy + asserts: + - failedTemplate: + errorMessage: DaemonSet - Expected to be one of [OnDelete, RollingUpdate], but got [not-a-strategy] + + - it: should fail with rollingUpdate not a dict + set: + controllers: + controller-name: + enabled: true + primary: true + type: DaemonSet + strategy: RollingUpdate + rollingUpdate: "not a dict" + asserts: + - failedTemplate: + errorMessage: DaemonSet - Expected to be a dictionary, but got [string] diff --git a/library/common-test/tests/deployment/metadata_test.yaml b/library/common-test/tests/deployment/metadata_test.yaml index d0b30b267a..281bc15195 100644 --- a/library/common-test/tests/deployment/metadata_test.yaml +++ b/library/common-test/tests/deployment/metadata_test.yaml @@ -5,7 +5,6 @@ chart: appVersion: &appVer v9.9.9 tests: - it: should pass with deployment created with labels and annotations - set: label1: label1 label2: global_label2 diff --git a/library/common/1.0.0/templates/classes/_daemonset.tpl b/library/common/1.0.0/templates/classes/_daemonset.tpl index e69de29bb2..458a7a5167 100644 --- a/library/common/1.0.0/templates/classes/_daemonset.tpl +++ b/library/common/1.0.0/templates/classes/_daemonset.tpl @@ -0,0 +1,38 @@ +{{/* DaemonSet Class */}} +{{/* Call this template: +{{ include "ix.v1.common.class.deployment" (dict "objectData" $objectData "rootCtx" $) }} + +rootCtx: The root context of the template. It is used to access the global context. +objectData: + name: The name of the configmap. + labels: The labels of the configmap. + annotations: The annotations of the configmap. + replicas: The number of replicas. + revisionHistoryLimit: The number of old ReplicaSets to retain to allow rollback. + strategy: The deployment strategy. +*/}} + +{{- define "ix.v1.common.class.daemonset" -}} + + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + {{- include "ix.v1.common.lib.controller.daemonsetValidation" (dict "objectData" $objectData) }} +--- +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: {{ $objectData.name }} + {{- $labels := (mustMerge ($objectData.labels | default dict) (include "ix.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} + labels: + {{- . | nindent 4 }} + {{- end -}} + {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "ix.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} + annotations: + {{- . | nindent 4 }} + {{- end }} +spec: + {{- include "ix.v1.common.lib.controller.daemonsetSpec" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 2 }} + +{{- end -}} diff --git a/library/common/1.0.0/templates/classes/_deployment.tpl b/library/common/1.0.0/templates/classes/_deployment.tpl index 9df0bc083f..ba4fefe904 100644 --- a/library/common/1.0.0/templates/classes/_deployment.tpl +++ b/library/common/1.0.0/templates/classes/_deployment.tpl @@ -16,7 +16,7 @@ objectData: {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData -}} - {{- include "ix.v1.common.lib.controller.deploymentValidation" (dict "objectData" $objectData "rootCtx" $rootCtx) }} + {{- include "ix.v1.common.lib.controller.deploymentValidation" (dict "objectData" $objectData) }} --- apiVersion: apps/v1 kind: Deployment diff --git a/library/common/1.0.0/templates/lib/controller/_daemonsetSpec.tpl b/library/common/1.0.0/templates/lib/controller/_daemonsetSpec.tpl new file mode 100644 index 0000000000..efa8407def --- /dev/null +++ b/library/common/1.0.0/templates/lib/controller/_daemonsetSpec.tpl @@ -0,0 +1,25 @@ +{{/* DaemonSet Spec */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.controller.daemonsetSpec" (dict "objectData" $objectData "rootCtx" $rootCtx) -}} +rootCtx: The root context of the template. It is used to access the global context. +objectData: + replicas: The number of replicas. + revisionHistoryLimit: The number of old ReplicaSets to retain to allow rollback. + strategy: The daemonset strategy to use to replace existing pods with new ones. +*/}} +{{- define "ix.v1.common.lib.controller.daemonsetSpec" -}} + {{- $objectData := .objectData -}} + {{- $rootCtx := .rootCtx -}} +revisionHistoryLimit: {{ $objectData.revisionHistoryLimit | default 3 }} +updateStrategy: + type: {{ $objectData.strategy | default "RollingUpdate" }} + {{- if and (eq $objectData.strategy "RollingUpdate") $objectData.rollingUpdate }} + rollingUpdate: + {{- with $objectData.rollingUpdate.maxUnavailable }} + maxUnavailable: {{ .}} + {{- end -}} + {{- with $objectData.rollingUpdate.maxSurge }} + maxSurge: {{ . }} + {{- end -}} + {{- end -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/controller/_validation.tpl b/library/common/1.0.0/templates/lib/controller/_validation.tpl index d425cc8d64..07e4b0a28d 100644 --- a/library/common/1.0.0/templates/lib/controller/_validation.tpl +++ b/library/common/1.0.0/templates/lib/controller/_validation.tpl @@ -64,15 +64,14 @@ objectData: {{/* Deployment Validation */}} {{/* Call this template: -{{ include "ix.v1.common.lib.controller.basicValidation" (dict "objectData" $objectData) -}} +{{ include "ix.v1.common.lib.controller.deploymentValidation" (dict "objectData" $objectData) -}} rootCtx: The root context of the template. It is used to access the global context. objectData: - labels: The labels of the object. - annotations: The annotations of the object. + strategy: The strategy of the object. + rollingUpdate: The rollingUpdate of the object. */}} {{- define "ix.v1.common.lib.controller.deploymentValidation" -}} {{- $objectData := .objectData -}} - {{- $rootCtx := .rootCtx -}} {{- if $objectData.strategy -}} {{- $strategy := $objectData.strategy -}} @@ -92,3 +91,33 @@ objectData: {{- end -}} {{- end -}} + +{{/* DaemonSet Validation */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.controller.daemonsetValidation" (dict "objectData" $objectData) -}} +rootCtx: The root context of the template. It is used to access the global context. +objectData: + strategy: The strategy of the object. + rollingUpdate: The rollingUpdate of the object. +*/}} +{{- define "ix.v1.common.lib.controller.daemonsetValidation" -}} + {{- $objectData := .objectData -}} + + {{- if $objectData.strategy -}} + {{- $strategy := $objectData.strategy -}} + + {{- if not (mustHas $strategy (list "OnDelete" "RollingUpdate")) -}} + {{- fail (printf "DaemonSet - Expected to be one of [OnDelete, RollingUpdate], but got [%v]" $strategy) -}} + {{- end -}} + + {{- end -}} + + {{- if $objectData.rollingUpdate -}} + {{- $rollUp := $objectData.rollingUpdate -}} + + {{- if and $rollUp (not (kindIs "map" $rollUp)) -}} + {{- fail (printf "DaemonSet - Expected to be a dictionary, but got [%v]" (kindOf $rollUp)) -}} + {{- end -}} + + {{- end -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/spawner/_controller.tpl b/library/common/1.0.0/templates/spawner/_controller.tpl index 174b087612..6fef3c6261 100644 --- a/library/common/1.0.0/templates/spawner/_controller.tpl +++ b/library/common/1.0.0/templates/spawner/_controller.tpl @@ -33,6 +33,7 @@ {{- include "ix.v1.common.class.deployment" (dict "objectData" $objectData "rootCtx" $) -}} {{- else if eq $objectData.type "StatefulSet" -}} {{- else if eq $objectData.type "DaemonSet" -}} + {{- include "ix.v1.common.class.daemonset" (dict "objectData" $objectData "rootCtx" $) -}} {{- else if eq $objectData.type "Job" -}} {{- else if eq $objectData.type "CronJob" -}} {{- end -}}