From 15eff5f65181484b7b02b25590faa5cd4bb514b6 Mon Sep 17 00:00:00 2001 From: smerle33 Date: Tue, 3 Oct 2023 11:00:39 +0200 Subject: [PATCH 1/3] feat(reports): add PodDisruptionBudget and unittests --- charts/reports/Chart.yaml | 2 +- charts/reports/templates/_helpers.tpl | 11 +++++-- charts/reports/templates/deployment.yaml | 8 +++-- charts/reports/templates/pdb.yaml | 18 +++++++++++ charts/reports/tests/custom_values_test.yaml | 32 +++++++++++++++++++ .../reports/tests/defaults_values_test.yaml | 20 ++++++++++++ charts/reports/values.yaml | 2 ++ 7 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 charts/reports/templates/pdb.yaml diff --git a/charts/reports/Chart.yaml b/charts/reports/Chart.yaml index 7465c6f71..e164fd1d0 100644 --- a/charts/reports/Chart.yaml +++ b/charts/reports/Chart.yaml @@ -4,4 +4,4 @@ description: A Helm chart for reports.jenkins.io maintainers: - name: timja name: reports -version: 0.3.1 +version: 0.4.0 diff --git a/charts/reports/templates/_helpers.tpl b/charts/reports/templates/_helpers.tpl index 3f5415d72..6c4c35f5d 100644 --- a/charts/reports/templates/_helpers.tpl +++ b/charts/reports/templates/_helpers.tpl @@ -31,13 +31,20 @@ 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 "reports.selectorLabels" -}} +app.kubernetes.io/name: {{ include "reports.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + {{/* Common labels */}} {{- define "reports.labels" -}} -app.kubernetes.io/name: {{ include "reports.name" . }} +{{ include "reports.selectorLabels" . }} helm.sh/chart: {{ include "reports.chart" . }} -app.kubernetes.io/instance: {{ .Release.Name }} {{- if .Chart.AppVersion }} app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} {{- end }} diff --git a/charts/reports/templates/deployment.yaml b/charts/reports/templates/deployment.yaml index 47a62ac8d..bbc80a9a7 100644 --- a/charts/reports/templates/deployment.yaml +++ b/charts/reports/templates/deployment.yaml @@ -51,8 +51,10 @@ spec: mountPath: /usr/share/nginx/html - name: config mountPath: /etc/nginx/conf.d + {{- with .Values.resources }} resources: {{- toYaml .Values.resources | nindent 12 }} + {{- end}} {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} @@ -66,9 +68,11 @@ spec: {{- toYaml . | nindent 8 }} {{- end }} volumes: - {{- with .Values.htmlVolume }} - name: html - {{- toYaml . | nindent 10 }} + {{- if .Values.htmlVolume }} +{{ toYaml .Values.htmlVolume | indent 10 }} + {{- else }} + emptyDir: {} {{- end }} - name: config configMap: diff --git a/charts/reports/templates/pdb.yaml b/charts/reports/templates/pdb.yaml new file mode 100644 index 000000000..75c1a6cc1 --- /dev/null +++ b/charts/reports/templates/pdb.yaml @@ -0,0 +1,18 @@ +{{- if (gt (int .Values.replicaCount) 1) }} +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{ include "reports.fullname" . }} + labels: + {{- include "reports.labels" . | nindent 4 }} +spec: + {{- with .Values.poddisruptionbudget.minAvailable }} + minAvailable: {{ . }} + {{- end }} + {{- with .Values.poddisruptionbudget.maxUnavailable }} + maxUnavailable: {{ . }} + {{- end }} + selector: + matchLabels: + {{- include "reports.selectorLabels" . | nindent 6 }} +{{- end }} diff --git a/charts/reports/tests/custom_values_test.yaml b/charts/reports/tests/custom_values_test.yaml index 85f672b4b..a53734058 100644 --- a/charts/reports/tests/custom_values_test.yaml +++ b/charts/reports/tests/custom_values_test.yaml @@ -3,6 +3,7 @@ templates: - deployment.yaml - ingress.yaml - nginx-configmap.yaml # Direct dependency of deployment.yaml + - pdb.yaml tests: - it: should create an ingress when ingress.enabled is true set: @@ -28,3 +29,34 @@ tests: - equal: path: spec.template.spec.volumes[0].hostPath value: /host + - it: should create a PDB with defaults when multiple replicas are set + template: pdb.yaml + set: + replicaCount: 2 + asserts: + - isKind: + of: PodDisruptionBudget + - equal: + path: spec.minAvailable + value: 1 + - equal: + path: spec.selector.matchLabels['app.kubernetes.io/name'] + value: "reports" + - it: should ensure the pdb has correct spec + template: pdb.yaml + set: + replicaCount: 2 + poddisruptionbudget.minAvailable: 2 + poddisruptionbudget.maxUnavailable: 3 + asserts: + - isKind: + of: PodDisruptionBudget + - equal: + path: spec.minAvailable + value: 2 + - equal: + path: spec.maxUnavailable + value: 3 + - equal: + path: spec.selector.matchLabels['app.kubernetes.io/name'] + value: "reports" diff --git a/charts/reports/tests/defaults_values_test.yaml b/charts/reports/tests/defaults_values_test.yaml index 12d7365c5..37ce4aaf6 100644 --- a/charts/reports/tests/defaults_values_test.yaml +++ b/charts/reports/tests/defaults_values_test.yaml @@ -3,6 +3,7 @@ templates: - deployment.yaml - ingress.yaml - nginx-configmap.yaml # Direct dependency of deployment.yaml + - pdb.yaml tests: - it: should not create an ingress by default template: ingress.yaml @@ -19,3 +20,22 @@ tests: - equal: path: spec.template.spec.containers[0].imagePullPolicy value: IfNotPresent + - equal: + path: metadata.labels["app.kubernetes.io/name"] + value: reports + - equal: + path: metadata.labels["app.kubernetes.io/managed-by"] + value: Helm + - equal: + path: spec.template.spec.volumes[0].name + value: html + - equal: + path: spec.template.spec.volumes[0].emptyDir + value: {} + - 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/reports/values.yaml b/charts/reports/values.yaml index 0ef02c044..b895be226 100644 --- a/charts/reports/values.yaml +++ b/charts/reports/values.yaml @@ -53,3 +53,5 @@ htmlVolume: azureStorageAccountName: # key for accessing the azure storage account azureStorageAccountKey: +poddisruptionbudget: + minAvailable: 1 From 9433951a341da06addba1b1da7539d539e61c034 Mon Sep 17 00:00:00 2001 From: smerle33 Date: Wed, 4 Oct 2023 11:16:29 +0200 Subject: [PATCH 2/3] correction --- charts/reports/templates/deployment.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/reports/templates/deployment.yaml b/charts/reports/templates/deployment.yaml index bbc80a9a7..a05b5776a 100644 --- a/charts/reports/templates/deployment.yaml +++ b/charts/reports/templates/deployment.yaml @@ -53,7 +53,7 @@ spec: mountPath: /etc/nginx/conf.d {{- with .Values.resources }} resources: - {{- toYaml .Values.resources | nindent 12 }} + {{- toYaml . | nindent 12 }} {{- end}} {{- with .Values.nodeSelector }} nodeSelector: From 7f1382d3fe72bebbc577a6d5993b79326231b54e Mon Sep 17 00:00:00 2001 From: smerle33 Date: Wed, 4 Oct 2023 11:24:00 +0200 Subject: [PATCH 3/3] unittest for resources --- charts/reports/tests/custom_values_test.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/charts/reports/tests/custom_values_test.yaml b/charts/reports/tests/custom_values_test.yaml index a53734058..fdef7a19a 100644 --- a/charts/reports/tests/custom_values_test.yaml +++ b/charts/reports/tests/custom_values_test.yaml @@ -18,6 +18,8 @@ tests: set: htmlVolume: hostPath: /host + resources: + cpu: 100ms asserts: - hasDocuments: count: 1 @@ -29,6 +31,9 @@ tests: - equal: path: spec.template.spec.volumes[0].hostPath value: /host + - equal: + path: spec.template.spec.containers[0].resources.cpu + value: 100ms - it: should create a PDB with defaults when multiple replicas are set template: pdb.yaml set: