diff --git a/templates/server-statefulset.yaml b/templates/server-statefulset.yaml index f39f9d970..65dc05e87 100644 --- a/templates/server-statefulset.yaml +++ b/templates/server-statefulset.yaml @@ -63,6 +63,14 @@ spec: {{- if .Values.server.tolerations }} tolerations: {{ tpl .Values.server.tolerations . | nindent 8 | trim }} + {{- end }} + {{- if .Values.server.topologySpreadConstraints }} + {{- if and (ge .Capabilities.KubeVersion.Major "1") (ge .Capabilities.KubeVersion.Minor "18") }} + topologySpreadConstraints: + {{ tpl .Values.server.topologySpreadConstraints . | nindent 8 | trim }} + {{- else }} + {{- fail "`topologySpreadConstraints` requires Kubernetes 1.18 and above." }} + {{- end }} {{- end }} terminationGracePeriodSeconds: 30 serviceAccountName: {{ template "consul.fullname" . }}-server diff --git a/test/unit/server-statefulset.bats b/test/unit/server-statefulset.bats index fd6590466..5605108fd 100755 --- a/test/unit/server-statefulset.bats +++ b/test/unit/server-statefulset.bats @@ -669,6 +669,37 @@ load _helpers [ "${actual}" = "true" ] } +#-------------------------------------------------------------------- +# tolerations + +@test "server/StatefulSet: topologySpreadConstraints not set by default" { + cd `chart_dir` + local actual=$(helm template \ + -s templates/server-statefulset.yaml \ + . | tee /dev/stderr | + yq '.spec.template.spec | .topologySpreadConstraints? == null' | tee /dev/stderr) + [ "${actual}" = "true" ] +} + +@test "server/StatefulSet: topologySpreadConstraints can be set" { + cd `chart_dir` + local actual=$(helm template \ + -s templates/server-statefulset.yaml \ + --set 'server.topologySpreadConstraints=foobar' \ + . | tee /dev/stderr | + yq '.spec.template.spec.topologySpreadConstraints == "foobar"' | tee /dev/stderr) + [ "${actual}" = "true" ] + + # todo: test for Kube versions < 1.18 when helm supports --kube-version flag (https://github.com/helm/helm/pull/9040) + # not supported before 1.18 + # run helm template \ + # -s templates/server-statefulset.yaml \ + # --kube-version "1.17" \ + # . + # [ "$status" -eq 1 ] + # [[ "$output" =~ "`topologySpreadConstraints` requires Kubernetes 1.18 and above." ]] +} + #-------------------------------------------------------------------- # global.openshift.enabled & server.securityContext diff --git a/values.yaml b/values.yaml index 9eff82f19..f02a7f1c5 100644 --- a/values.yaml +++ b/values.yaml @@ -519,6 +519,27 @@ server: # (https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/) array in a Pod spec. tolerations: "" + # Pod topology spread constraints for server pods. + # This should be a multi-line YAML string matching the `topologySpreadConstraints` array + # (https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/) in a Pod Spec. + # + # This requires K8S >= 1.18 (beta) or 1.19 (stable). + # + # Example: + # + # ```yaml + # topologySpreadConstraints: | + # - maxSkew: 1 + # topologyKey: topology.kubernetes.io/zone + # whenUnsatisfiable: DoNotSchedule + # labelSelector: + # matchLabels: + # app: {{ template "consul.name" . }} + # release: "{{ .Release.Name }}" + # component: server + # ``` + topologySpreadConstraints: "" + # This value defines `nodeSelector` (https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector) # labels for server pod assignment, formatted as a multi-line string. #