From a72a24d49299321a6b6dc90ddf070b9f781df86a Mon Sep 17 00:00:00 2001 From: smerle33 Date: Thu, 28 Sep 2023 09:43:26 +0200 Subject: [PATCH 1/4] feat(account-app): adding a PodDisruptionBudget --- charts/accountapp/templates/_helpers.tpl | 8 ++++++++ charts/accountapp/templates/pdb.yaml | 18 ++++++++++++++++++ .../accountapp/tests/custom_values_test.yaml | 16 ++++++++++++++++ charts/accountapp/tests/defaults_test.yaml | 6 ++++++ charts/accountapp/values.yaml | 2 ++ 5 files changed, 50 insertions(+) create mode 100644 charts/accountapp/templates/pdb.yaml diff --git a/charts/accountapp/templates/_helpers.tpl b/charts/accountapp/templates/_helpers.tpl index 6093258d6..b0cad3a31 100644 --- a/charts/accountapp/templates/_helpers.tpl +++ b/charts/accountapp/templates/_helpers.tpl @@ -44,3 +44,11 @@ app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} app.kubernetes.io/managed-by: {{ .Release.Service }} jenkins.io/maintainer: {{ (index .Chart.Maintainers 0).Name }} {{- end -}} + +{{/* +Selector labels +*/}} +{{- define "accountapp.selectorLabels" -}} +app.kubernetes.io/name: {{ include "accountapp.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} diff --git a/charts/accountapp/templates/pdb.yaml b/charts/accountapp/templates/pdb.yaml new file mode 100644 index 000000000..7110d0208 --- /dev/null +++ b/charts/accountapp/templates/pdb.yaml @@ -0,0 +1,18 @@ +{{- if (gt (int .Values.replicaCount) 1) }} +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{ include "accountapp.fullname" . }} + labels: + {{- include "accountapp.labels" . | nindent 4 }} +spec: + {{- with .Values.poddisruptionbudget.minAvailable }} + minAvailable: {{ . }} + {{- end }} + {{- with .Values.poddisruptionbudget.maxUnavailable }} + maxUnavailable: {{ . }} + {{- end }} + selector: + matchLabels: + {{- include "accountapp.selectorLabels" . | nindent 6 }} +{{- end }} diff --git a/charts/accountapp/tests/custom_values_test.yaml b/charts/accountapp/tests/custom_values_test.yaml index 5deeb477a..efd80ec10 100644 --- a/charts/accountapp/tests/custom_values_test.yaml +++ b/charts/accountapp/tests/custom_values_test.yaml @@ -3,6 +3,7 @@ templates: - deployment.yaml - ingress.yaml - secret.yaml # Direct dependency of deployment.yaml + - pdb.yaml tests: - it: should create a Secret with the correct value set: @@ -51,3 +52,18 @@ tests: - equal: path: spec.template.spec.containers[0].resources.requests.cpu value: 500m + - it: should ensure the pdb has correct spec + template: pdb.yaml + set: + replicaCount: 2 + poddisruptionbudget.minAvailable: 2 + poddisruptionbudget.maxUnavailable: 1 + asserts: + - isKind: + of: PodDisruptionBudget + - equal: + path: spec.minAvailable + value: 2 + - equal: + path: spec.selector.matchLabels['app.kubernetes.io/name'] + value: "accountapp" diff --git a/charts/accountapp/tests/defaults_test.yaml b/charts/accountapp/tests/defaults_test.yaml index d663044bd..93507623d 100644 --- a/charts/accountapp/tests/defaults_test.yaml +++ b/charts/accountapp/tests/defaults_test.yaml @@ -3,6 +3,7 @@ templates: - deployment.yaml - ingress.yaml - secret.yaml # Direct dependency of deployment.yaml + - pdb.yaml tests: - it: should create an ingress by default template: ingress.yaml @@ -21,3 +22,8 @@ tests: value: IfNotPresent - notExists: path: spec.template.spec.containers[0].resources + - it: should not generate any pdb with default values + template: pdb.yaml + asserts: + - hasDocuments: + count: 0 diff --git a/charts/accountapp/values.yaml b/charts/accountapp/values.yaml index 6ecf4a23b..75cad7f48 100644 --- a/charts/accountapp/values.yaml +++ b/charts/accountapp/values.yaml @@ -66,3 +66,5 @@ election: close: 1970-01-02 logDir: /var/log/accountapp/elections candidates: bob,alice +poddisruptionbudget: + minAvailable: 1 From 55bfc89507fd1d442ffb6456ced9a89ab38ffcac Mon Sep 17 00:00:00 2001 From: smerle33 Date: Thu, 28 Sep 2023 14:09:18 +0200 Subject: [PATCH 2/4] fixup --- charts/accountapp/Chart.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/accountapp/Chart.yaml b/charts/accountapp/Chart.yaml index 2317d5414..4c48cbf34 100644 --- a/charts/accountapp/Chart.yaml +++ b/charts/accountapp/Chart.yaml @@ -3,4 +3,4 @@ description: A Helm chart for accounts.jenkins.io maintainers: - name: timja name: accountapp -version: 0.7.3 +version: 0.8.0 From 759391593abb9d72d91234f6a9658470e64dc054 Mon Sep 17 00:00:00 2001 From: smerle33 Date: Thu, 28 Sep 2023 14:22:39 +0200 Subject: [PATCH 3/4] unit test for labels --- charts/accountapp/tests/defaults_test.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/charts/accountapp/tests/defaults_test.yaml b/charts/accountapp/tests/defaults_test.yaml index 93507623d..9db42b55b 100644 --- a/charts/accountapp/tests/defaults_test.yaml +++ b/charts/accountapp/tests/defaults_test.yaml @@ -22,6 +22,12 @@ tests: value: IfNotPresent - notExists: path: spec.template.spec.containers[0].resources + - equal: + path: metadata.labels["app.kubernetes.io/name"] + value: accountapp + - equal: + path: metadata.labels["app.kubernetes.io/managed-by"] + value: Helm - it: should not generate any pdb with default values template: pdb.yaml asserts: From a2f3b3b77db776d0c9b0a9d604ccc971ed99b55c Mon Sep 17 00:00:00 2001 From: smerle33 Date: Thu, 28 Sep 2023 11:48:47 +0200 Subject: [PATCH 4/4] encapsulation --- charts/accountapp/templates/_helpers.tpl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/charts/accountapp/templates/_helpers.tpl b/charts/accountapp/templates/_helpers.tpl index b0cad3a31..4ac303e71 100644 --- a/charts/accountapp/templates/_helpers.tpl +++ b/charts/accountapp/templates/_helpers.tpl @@ -31,24 +31,23 @@ Create chart name and version as used by the chart label. {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} {{- end -}} +{{/* +Selector labels +*/}} +{{- define "accountapp.selectorLabels" -}} +app.kubernetes.io/name: {{ include "accountapp.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + {{/* Common labels */}} {{- define "accountapp.labels" -}} -app.kubernetes.io/name: {{ include "accountapp.name" . }} +{{ include "accountapp.selectorLabels" . }} helm.sh/chart: {{ include "accountapp.chart" . }} -app.kubernetes.io/instance: {{ .Release.Name }} {{- if .Chart.AppVersion }} app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} {{- end }} app.kubernetes.io/managed-by: {{ .Release.Service }} jenkins.io/maintainer: {{ (index .Chart.Maintainers 0).Name }} {{- end -}} - -{{/* -Selector labels -*/}} -{{- define "accountapp.selectorLabels" -}} -app.kubernetes.io/name: {{ include "accountapp.name" . }} -app.kubernetes.io/instance: {{ .Release.Name }} -{{- end }}