From 3d7c93ed290076e53e57d6c5916aeb9c00358c81 Mon Sep 17 00:00:00 2001 From: Richard Hillmann <2286479+project0@users.noreply.github.com> Date: Mon, 3 Apr 2023 09:30:06 +0200 Subject: [PATCH] feat: allow setting custom pod affinity --- traefikee/Chart.yaml | 2 +- traefikee/templates/proxy/deployment.yaml | 24 +----- traefikee/templates/stateful-sets.yaml | 48 ++---------- traefikee/tests/controller_test.yaml | 89 +++++++++++++++++++++-- traefikee/tests/proxy_test.yaml | 25 +++++++ traefikee/values.yaml | 67 ++++++++++++++++- 6 files changed, 187 insertions(+), 68 deletions(-) diff --git a/traefikee/Chart.yaml b/traefikee/Chart.yaml index b063a8f..b240978 100644 --- a/traefikee/Chart.yaml +++ b/traefikee/Chart.yaml @@ -1,6 +1,6 @@ apiVersion: v2 name: traefikee -version: 1.7.0 +version: 1.8.0 appVersion: v2.9.1 # Because of https://github.com/helm/helm/issues/3810 the pre-release version suffix has to be define. # This allows the installation on Kubernetes cluster with a pre-release version (e.g. v1.19.9-gke.1900) diff --git a/traefikee/templates/proxy/deployment.yaml b/traefikee/templates/proxy/deployment.yaml index 42b0472..1c84d74 100644 --- a/traefikee/templates/proxy/deployment.yaml +++ b/traefikee/templates/proxy/deployment.yaml @@ -22,7 +22,7 @@ spec: component: proxies template: metadata: - labels: + labels: component: proxies {{ include "common.labels" . | nindent 8 }} {{- with .Values.proxy.podLabels }} @@ -37,26 +37,10 @@ spec: imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} + {{- if .Values.proxy.affinity }} affinity: - nodeAffinity: - requiredDuringSchedulingIgnoredDuringExecution: - nodeSelectorTerms: - - matchExpressions: - - key: kubernetes.io/os - operator: In - values: - - linux - podAntiAffinity: - preferredDuringSchedulingIgnoredDuringExecution: - - weight: 100 - podAffinityTerm: - labelSelector: - matchExpressions: - - key: component - operator: In - values: - - proxies - topologyKey: "kubernetes.io/hostname" + {{- tpl (toYaml .Values.proxy.affinity) . | nindent 8 }} + {{- end }} terminationGracePeriodSeconds: {{ .Values.proxy.terminationGracePeriodSeconds | default 30 }} automountServiceAccountToken: false initContainers: diff --git a/traefikee/templates/stateful-sets.yaml b/traefikee/templates/stateful-sets.yaml index bed068d..2728934 100644 --- a/traefikee/templates/stateful-sets.yaml +++ b/traefikee/templates/stateful-sets.yaml @@ -8,7 +8,7 @@ metadata: annotations: "helm.sh/resource-policy": keep type: Opaque -data: +data: token: {{ $tokenStr }} {{- end }} @@ -44,7 +44,7 @@ spec: {{- with (.Values.registry).podAnnotations }} {{- toYaml . | nindent 8 }} {{- end }} - labels: + labels: component: registry {{ include "common.labels" . | nindent 8 }} {{- with (.Values.registry).podLabels }} @@ -55,26 +55,10 @@ spec: imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} + {{- if .Values.registry.affinity }} affinity: - nodeAffinity: - requiredDuringSchedulingIgnoredDuringExecution: - nodeSelectorTerms: - - matchExpressions: - - key: kubernetes.io/os - operator: In - values: - - linux - podAntiAffinity: - preferredDuringSchedulingIgnoredDuringExecution: - - weight: 100 - podAffinityTerm: - labelSelector: - matchExpressions: - - key: component - operator: In - values: - - registry - topologyKey: "kubernetes.io/hostname" + {{- tpl (toYaml .Values.registry.affinity) . | nindent 8 }} + {{- end }} terminationGracePeriodSeconds: 30 automountServiceAccountToken: false initContainers: @@ -204,26 +188,10 @@ spec: {{- toYaml . | nindent 8 }} {{- end }} serviceAccountName: {{ .Values.cluster }}-svc-acc + {{- if .Values.controller.affinity }} affinity: - nodeAffinity: - requiredDuringSchedulingIgnoredDuringExecution: - nodeSelectorTerms: - - matchExpressions: - - key: kubernetes.io/os - operator: In - values: - - linux - podAntiAffinity: - preferredDuringSchedulingIgnoredDuringExecution: - - weight: 100 - podAffinityTerm: - labelSelector: - matchExpressions: - - key: component - operator: In - values: - - controllers - topologyKey: "kubernetes.io/hostname" + {{- tpl (toYaml .Values.controller.affinity) . | nindent 8 }} + {{- end }} terminationGracePeriodSeconds: 30 initContainers: - name: wait-dns diff --git a/traefikee/tests/controller_test.yaml b/traefikee/tests/controller_test.yaml index 0bdb7de..c91af56 100644 --- a/traefikee/tests/controller_test.yaml +++ b/traefikee/tests/controller_test.yaml @@ -57,7 +57,53 @@ tests: documentIndex: 2 - hasDocuments: count: 3 - - it: should override defaults + - documentIndex: 1 + equal: + path: spec.template.spec.affinity + value: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: kubernetes.io/os + operator: In + values: + - linux + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchExpressions: + - key: component + operator: In + values: + - registry + topologyKey: kubernetes.io/hostname + - documentIndex: 2 + equal: + path: spec.template.spec.affinity + value: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: kubernetes.io/os + operator: In + values: + - linux + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchExpressions: + - key: component + operator: In + values: + - controllers + topologyKey: kubernetes.io/hostname + - it: should override controller defaults set: cluster: "mysupertraefikee" image.name: "mycustompublisher/myspecialimage" @@ -65,6 +111,7 @@ tests: image.pullPolicy: Never controller: replicas: 2 + affinity: null documentIndex: 2 asserts: - isKind: @@ -86,18 +133,48 @@ tests: - equal: path: spec.template.spec.containers[0].imagePullPolicy value: Never + - isNull: + path: spec.template.spec.affinity + - it: should override registry defaults + set: + cluster: "mysupertraefikee" + image.name: "mycustompublisher/myspecialimage" + image.tag: "myspecialversion" + image.pullPolicy: Never + registry: + affinity: null + documentIndex: 1 + asserts: + - isKind: + of: StatefulSet + - isAPIVersion: + of: apps/v1 + - equal: + path: metadata.name + value: mysupertraefikee-plugin-registry + - equal: + path: spec.template.spec.containers[0].image + value: "mycustompublisher/myspecialimage:myspecialversion" + - equal: + path: spec.serviceName + value: mysupertraefikee-plugin-registry-svc + - equal: + path: spec.template.spec.containers[0].imagePullPolicy + value: Never + - isNull: + path: spec.template.spec.affinity - it: should add labels and annotations set: cluster: "mysupertraefikee" controller: statefulSetLabels: - st-lb-foo: bar + st-lb-foo: bar statefulSetAnnotations: - st-an-foo: bar + st-an-foo: bar podLabels: - pod-lb-foo: bar + pod-lb-foo: bar podAnnotations: - pod-an-foo: bar + pod-an-foo: bar documentIndex: 2 asserts: - isKind: @@ -120,7 +197,7 @@ tests: set: controller: additionalArguments: - - --foo=bar + - --foo=bar documentIndex: 2 asserts: - isKind: diff --git a/traefikee/tests/proxy_test.yaml b/traefikee/tests/proxy_test.yaml index d124045..1047934 100644 --- a/traefikee/tests/proxy_test.yaml +++ b/traefikee/tests/proxy_test.yaml @@ -14,6 +14,28 @@ tests: - equal: path: spec.template.spec.containers[0].image value: "traefik/traefikee:v2.9.1" + - equal: + path: spec.template.spec.affinity + value: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: kubernetes.io/os + operator: In + values: + - linux + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchExpressions: + - key: component + operator: In + values: + - proxies + topologyKey: "kubernetes.io/hostname" - it: should override defaults set: cluster: "mysupertraefikee" @@ -22,6 +44,7 @@ tests: image.pullPolicy: Never proxy: replicas: 2 + affinity: null asserts: - isKind: of: Deployment @@ -41,6 +64,8 @@ tests: value: Never - hasDocuments: count: 1 + - isNull: + path: spec.template.spec.affinity - it: default ports are set asserts: - isKind: diff --git a/traefikee/values.yaml b/traefikee/values.yaml index 9ea43c5..de3f006 100644 --- a/traefikee/values.yaml +++ b/traefikee/values.yaml @@ -18,7 +18,29 @@ image: # format: # file: -# registry: +registry: + # To disable affinity at all set this value to null + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: kubernetes.io/os + operator: In + values: + - linux + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchExpressions: + - key: component + operator: In + values: + - registry + topologyKey: "kubernetes.io/hostname" + # serviceLabels: # foo: bar # serviceAnnotations: @@ -35,6 +57,27 @@ image: controller: replicas: 1 + # To disable affinity at all set this value to null + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: kubernetes.io/os + operator: In + values: + - linux + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchExpressions: + - key: component + operator: In + values: + - controllers + topologyKey: "kubernetes.io/hostname" # staticConfig: # configMap: # name: traefik-config @@ -70,6 +113,28 @@ proxy: port: 80 - name: https port: 443 + + # To disable affinity at all set this value to null + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: kubernetes.io/os + operator: In + values: + - linux + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchExpressions: + - key: component + operator: In + values: + - proxies + topologyKey: "kubernetes.io/hostname" # readinessProbe: # tcpSocket: # port: http