diff --git a/library/common-test/tests/statefulset/metadata_test.yaml b/library/common-test/tests/statefulset/metadata_test.yaml new file mode 100644 index 0000000000..b6181e47f4 --- /dev/null +++ b/library/common-test/tests/statefulset/metadata_test.yaml @@ -0,0 +1,56 @@ +suite: statefulset metadata test +templates: + - common.yaml +chart: + appVersion: &appVer v9.9.9 +tests: + - it: should pass with statefulset 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: StatefulSet + labels: + label1: "{{ .Values.label1 }}" + label2: label2 + annotations: + annotation1: "{{ .Values.annotation1 }}" + annotation2: annotation2 + asserts: + - documentIndex: &statefulSetDoc 0 + isKind: + of: StatefulSet + - documentIndex: *statefulSetDoc + equal: + path: metadata.annotations + value: + annotation1: annotation1 + annotation2: annotation2 + g_annotation1: global_annotation1 + g_annotation2: global_annotation2 + - documentIndex: *statefulSetDoc + 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/statefulset/spec_test.yaml b/library/common-test/tests/statefulset/spec_test.yaml new file mode 100644 index 0000000000..8081c13b49 --- /dev/null +++ b/library/common-test/tests/statefulset/spec_test.yaml @@ -0,0 +1,49 @@ +suite: statefulset spec test +templates: + - common.yaml +tests: + - it: should pass with controller enabled + set: + controllers: + controller-name: + enabled: true + primary: true + type: StatefulSet + asserts: + - documentIndex: &statefulSetDoc 0 + isKind: + of: StatefulSet + - documentIndex: *statefulSetDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *statefulSetDoc + isSubset: + path: spec + content: + revisionHistoryLimit: 3 + updateStrategy: + type: RollingUpdate + + - it: should apply spec correctly + set: + controllers: + controller-name: + enabled: true + primary: true + type: StatefulSet + revisionHistoryLimit: 4 + strategy: RollingUpdate + rollingUpdate: + partition: 5 + maxUnavailable: 5 + asserts: + - documentIndex: *statefulSetDoc + isSubset: + path: spec + content: + revisionHistoryLimit: 4 + updateStrategy: + type: RollingUpdate + rollingUpdate: + partition: 5 + maxUnavailable: 5 diff --git a/library/common-test/tests/statefulset/validation_test.yaml b/library/common-test/tests/statefulset/validation_test.yaml new file mode 100644 index 0000000000..530be44972 --- /dev/null +++ b/library/common-test/tests/statefulset/validation_test.yaml @@ -0,0 +1,28 @@ +suite: statefulset validation test +templates: + - common.yaml +tests: + - it: should fail with invalid strategy + set: + controllers: + controller-name: + enabled: true + primary: true + type: StatefulSet + strategy: not-a-strategy + asserts: + - failedTemplate: + errorMessage: StatefulSet - 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: StatefulSet + strategy: RollingUpdate + rollingUpdate: "not a dict" + asserts: + - failedTemplate: + errorMessage: StatefulSet - Expected to be a dictionary, but got [string] diff --git a/library/common/1.0.0/templates/classes/_statefulset.tpl b/library/common/1.0.0/templates/classes/_statefulset.tpl index e69de29bb2..925b4f3f8f 100644 --- a/library/common/1.0.0/templates/classes/_statefulset.tpl +++ b/library/common/1.0.0/templates/classes/_statefulset.tpl @@ -0,0 +1,38 @@ +{{/* StatefulSet 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.statefulset" -}} + + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + {{- include "ix.v1.common.lib.controller.statefulsetValidation" (dict "objectData" $objectData) }} +--- +apiVersion: apps/v1 +kind: StatefulSet +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.statefulsetSpec" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 2 }} + +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/controller/_daemonsetSpec.tpl b/library/common/1.0.0/templates/lib/controller/_daemonsetSpec.tpl index efa8407def..547ea517a4 100644 --- a/library/common/1.0.0/templates/lib/controller/_daemonsetSpec.tpl +++ b/library/common/1.0.0/templates/lib/controller/_daemonsetSpec.tpl @@ -13,7 +13,10 @@ objectData: revisionHistoryLimit: {{ $objectData.revisionHistoryLimit | default 3 }} updateStrategy: type: {{ $objectData.strategy | default "RollingUpdate" }} - {{- if and (eq $objectData.strategy "RollingUpdate") $objectData.rollingUpdate }} + {{- if and + (eq $objectData.strategy "RollingUpdate") + $objectData.rollingUpdate + (or $objectData.rollingUpdate.maxUnavailable $objectData.rollingUpdate.maxSurge) }} rollingUpdate: {{- with $objectData.rollingUpdate.maxUnavailable }} maxUnavailable: {{ .}} diff --git a/library/common/1.0.0/templates/lib/controller/_deployementSpec.tpl b/library/common/1.0.0/templates/lib/controller/_deployementSpec.tpl index 6ed7e31f1e..50192efbb6 100644 --- a/library/common/1.0.0/templates/lib/controller/_deployementSpec.tpl +++ b/library/common/1.0.0/templates/lib/controller/_deployementSpec.tpl @@ -14,7 +14,10 @@ replicas: {{ $objectData.replicas | default 1 }} revisionHistoryLimit: {{ $objectData.revisionHistoryLimit | default 3 }} strategy: type: {{ $objectData.strategy | default "Recreate" }} - {{- if and (eq $objectData.strategy "RollingUpdate") $objectData.rollingUpdate }} + {{- if and + (eq $objectData.strategy "RollingUpdate") + $objectData.rollingUpdate + (or $objectData.rollingUpdate.maxUnavailable $objectData.rollingUpdate.maxSurge) }} rollingUpdate: {{- with $objectData.rollingUpdate.maxUnavailable }} maxUnavailable: {{ .}} diff --git a/library/common/1.0.0/templates/lib/controller/_statefulsettSpec.tpl b/library/common/1.0.0/templates/lib/controller/_statefulsettSpec.tpl new file mode 100644 index 0000000000..b0a5d9d49b --- /dev/null +++ b/library/common/1.0.0/templates/lib/controller/_statefulsettSpec.tpl @@ -0,0 +1,30 @@ +{{/* StatefulSet Spec */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.controller.statefulsetSpec" (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 statefulset strategy to use to replace existing pods with new ones. +*/}} +{{- define "ix.v1.common.lib.controller.statefulsetSpec" -}} + {{- $objectData := .objectData -}} + {{- $rootCtx := .rootCtx -}} +replicas: {{ $objectData.replicas | default 1 }} +revisionHistoryLimit: {{ $objectData.revisionHistoryLimit | default 3 }} +serviceName: {{ $objectData.name }} +updateStrategy: + type: {{ $objectData.strategy | default "RollingUpdate" }} + {{- if and + (eq $objectData.strategy "RollingUpdate") + $objectData.rollingUpdate + (or $objectData.rollingUpdate.maxUnavailable $objectData.rollingUpdate.partition) }} + rollingUpdate: + {{- with $objectData.rollingUpdate.maxUnavailable }} + maxUnavailable: {{ .}} + {{- end -}} + {{- with $objectData.rollingUpdate.partition }} + partition: {{ . }} + {{- 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 07e4b0a28d..beb824181e 100644 --- a/library/common/1.0.0/templates/lib/controller/_validation.tpl +++ b/library/common/1.0.0/templates/lib/controller/_validation.tpl @@ -121,3 +121,33 @@ objectData: {{- end -}} {{- end -}} + +{{/* StatefulSet Validation */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.controller.statefulsetValidation" (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.statefulsetValidation" -}} + {{- $objectData := .objectData -}} + + {{- if $objectData.strategy -}} + {{- $strategy := $objectData.strategy -}} + + {{- if not (mustHas $strategy (list "OnDelete" "RollingUpdate")) -}} + {{- fail (printf "StatefulSet - 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 "StatefulSet - 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 6fef3c6261..61c44be2c2 100644 --- a/library/common/1.0.0/templates/spawner/_controller.tpl +++ b/library/common/1.0.0/templates/spawner/_controller.tpl @@ -32,6 +32,7 @@ {{- if eq $objectData.type "Deployment" -}} {{- include "ix.v1.common.class.deployment" (dict "objectData" $objectData "rootCtx" $) -}} {{- else if eq $objectData.type "StatefulSet" -}} + {{- include "ix.v1.common.class.statefulset" (dict "objectData" $objectData "rootCtx" $) -}} {{- else if eq $objectData.type "DaemonSet" -}} {{- include "ix.v1.common.class.daemonset" (dict "objectData" $objectData "rootCtx" $) -}} {{- else if eq $objectData.type "Job" -}}