From cb471fff916be04f837c184fbdf4ac563d91fb0c Mon Sep 17 00:00:00 2001 From: smerle33 Date: Fri, 6 Oct 2023 14:59:39 +0200 Subject: [PATCH 1/2] feat(plugin-site-frontend): add PDB and unittest --- charts/plugin-site/Chart.yaml | 2 +- charts/plugin-site/templates/_helpers.tpl | 19 ++++- .../templates/deployment-frontend.yaml | 4 ++ .../plugin-site/templates/pdb-frontend.yaml | 18 +++++ .../plugin-site/tests/custom_values_test.yaml | 72 +++++++++++++++++++ .../tests/defaults_values_test.yaml | 42 +++++++++++ charts/plugin-site/values.yaml | 2 + 7 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 charts/plugin-site/templates/pdb-frontend.yaml create mode 100644 charts/plugin-site/tests/custom_values_test.yaml create mode 100644 charts/plugin-site/tests/defaults_values_test.yaml diff --git a/charts/plugin-site/Chart.yaml b/charts/plugin-site/Chart.yaml index 0185b7b0d..22dce8c2a 100644 --- a/charts/plugin-site/Chart.yaml +++ b/charts/plugin-site/Chart.yaml @@ -5,4 +5,4 @@ maintainers: - name: timja - name: halkeye name: plugin-site -version: 0.2.0 +version: 0.3.0 diff --git a/charts/plugin-site/templates/_helpers.tpl b/charts/plugin-site/templates/_helpers.tpl index dc96d0b1b..1876748c4 100644 --- a/charts/plugin-site/templates/_helpers.tpl +++ b/charts/plugin-site/templates/_helpers.tpl @@ -31,13 +31,28 @@ 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 frontend +*/}} +{{- define "plugin-site-frontend.selectorLabels" -}} +app.kubernetes.io/name: {{ include "plugin-site.name" . }}-frontend +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "plugin-site.selectorLabels" -}} +app.kubernetes.io/name: {{ include "plugin-site.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + {{/* Common labels */}} {{- define "plugin-site.labels" -}} -app.kubernetes.io/name: {{ include "plugin-site.name" . }} +{{ include "plugin-site.selectorLabels" . }} helm.sh/chart: {{ include "plugin-site.chart" . }} -app.kubernetes.io/instance: {{ .Release.Name }} {{- if .Chart.AppVersion }} app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} {{- end }} diff --git a/charts/plugin-site/templates/deployment-frontend.yaml b/charts/plugin-site/templates/deployment-frontend.yaml index 30bf57482..5912b654b 100644 --- a/charts/plugin-site/templates/deployment-frontend.yaml +++ b/charts/plugin-site/templates/deployment-frontend.yaml @@ -67,7 +67,11 @@ spec: {{- end }} volumes: - name: html + {{- if .Values.htmlVolume }} {{ toYaml .Values.htmlVolume | indent 10 }} + {{- else }} + emptyDir: {} + {{- end }} - name: config configMap: name: {{ include "plugin-site.fullname" . }}-nginx diff --git a/charts/plugin-site/templates/pdb-frontend.yaml b/charts/plugin-site/templates/pdb-frontend.yaml new file mode 100644 index 000000000..07ffe520b --- /dev/null +++ b/charts/plugin-site/templates/pdb-frontend.yaml @@ -0,0 +1,18 @@ +{{- if (gt (int .Values.frontend.replicaCount) 1) }} +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{ include "plugin-site.fullname" . }}-frontend + labels: + {{- include "plugin-site.labels" . | nindent 4 }} +spec: + {{- with .Values.frontend.poddisruptionbudget.minAvailable }} + minAvailable: {{ . }} + {{- end }} + {{- with .Values.frontend.poddisruptionbudget.maxUnavailable }} + maxUnavailable: {{ . }} + {{- end }} + selector: + matchLabels: + {{- include "plugin-site-frontend.selectorLabels" . | nindent 6 }} +{{- end }} diff --git a/charts/plugin-site/tests/custom_values_test.yaml b/charts/plugin-site/tests/custom_values_test.yaml new file mode 100644 index 000000000..56eda7947 --- /dev/null +++ b/charts/plugin-site/tests/custom_values_test.yaml @@ -0,0 +1,72 @@ +suite: Test with custom values +templates: + - deployment-frontend.yaml + - ingress.yaml + - pdb-frontend.yaml + - nginx-configmap.yaml # Direct dependency of deployment.yaml +tests: + - it: should create an ingress when ingress.enabled is true + set: + ingress: + enabled: true + template: ingress.yaml + asserts: + - hasDocuments: + count: 1 + - it: should mount the html volume when htmlVolume is set + template: deployment-frontend.yaml + set: + htmlVolume: + hostPath: /host + frontend: + resources: + limits: + cpu: 300ms + asserts: + - hasDocuments: + count: 1 + - isKind: + of: Deployment + - equal: + path: spec.template.spec.volumes[0].name + value: html + - equal: + path: spec.template.spec.volumes[0].hostPath + value: /host + - equal: + path: spec.template.spec.containers[0].resources.limits.cpu + value: 300ms + - it: should create a PDB with defaults when multiple replicas are set + template: pdb-frontend.yaml + set: + frontend: + replicaCount: 4 + asserts: + - isKind: + of: PodDisruptionBudget + - equal: + path: spec.minAvailable + value: 1 + - equal: + path: spec.selector.matchLabels['app.kubernetes.io/name'] + value: "plugin-site-frontend" + - it: should ensure the pdb has correct spec + template: pdb-frontend.yaml + set: + frontend: + replicaCount: 3 + poddisruptionbudget: + minAvailable: 2 + 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: "plugin-site-frontend" diff --git a/charts/plugin-site/tests/defaults_values_test.yaml b/charts/plugin-site/tests/defaults_values_test.yaml new file mode 100644 index 000000000..6d7c34422 --- /dev/null +++ b/charts/plugin-site/tests/defaults_values_test.yaml @@ -0,0 +1,42 @@ +suite: Test with default values +templates: + - deployment-frontend.yaml + - ingress.yaml + - pdb-frontend.yaml + - nginx-configmap.yaml +tests: + - it: should not create an ingress by default + template: ingress.yaml + asserts: + - hasDocuments: + count: 0 + - it: should define the default deployment with default imagePullPolicy + template: deployment-frontend.yaml + asserts: + - hasDocuments: + count: 1 + - isKind: + of: Deployment + - equal: + path: spec.template.spec.containers[0].imagePullPolicy + value: IfNotPresent + - equal: + path: metadata.labels["app.kubernetes.io/name"] + value: plugin-site + - 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: {} + - equal: + path: spec.template.spec.containers[0].resources.limits.cpu + value: 100m + - it: should generate a pdb with default values + template: pdb-frontend.yaml + asserts: + - hasDocuments: + count: 1 diff --git a/charts/plugin-site/values.yaml b/charts/plugin-site/values.yaml index e4bb4ad62..da41eb288 100644 --- a/charts/plugin-site/values.yaml +++ b/charts/plugin-site/values.yaml @@ -30,6 +30,8 @@ frontend: requests: cpu: 100m memory: 32Mi + poddisruptionbudget: + minAvailable: 1 imagePullSecrets: [] nameOverride: "" fullnameOverride: "" From 12c9b945ca6c91269ff330c15df39c7483d75e36 Mon Sep 17 00:00:00 2001 From: Damien Duportal Date: Fri, 6 Oct 2023 16:10:11 +0200 Subject: [PATCH 2/2] code review Signed-off-by: Damien Duportal --- charts/plugin-site/tests/custom_values_test.yaml | 16 +--------------- .../plugin-site/tests/defaults_values_test.yaml | 10 +++++++++- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/charts/plugin-site/tests/custom_values_test.yaml b/charts/plugin-site/tests/custom_values_test.yaml index 56eda7947..b670fdfc8 100644 --- a/charts/plugin-site/tests/custom_values_test.yaml +++ b/charts/plugin-site/tests/custom_values_test.yaml @@ -36,21 +36,7 @@ tests: - equal: path: spec.template.spec.containers[0].resources.limits.cpu value: 300ms - - it: should create a PDB with defaults when multiple replicas are set - template: pdb-frontend.yaml - set: - frontend: - replicaCount: 4 - asserts: - - isKind: - of: PodDisruptionBudget - - equal: - path: spec.minAvailable - value: 1 - - equal: - path: spec.selector.matchLabels['app.kubernetes.io/name'] - value: "plugin-site-frontend" - - it: should ensure the pdb has correct spec + - it: should create a customized PDB with the provided spec template: pdb-frontend.yaml set: frontend: diff --git a/charts/plugin-site/tests/defaults_values_test.yaml b/charts/plugin-site/tests/defaults_values_test.yaml index 6d7c34422..1ffa41652 100644 --- a/charts/plugin-site/tests/defaults_values_test.yaml +++ b/charts/plugin-site/tests/defaults_values_test.yaml @@ -35,8 +35,16 @@ tests: - equal: path: spec.template.spec.containers[0].resources.limits.cpu value: 100m - - it: should generate a pdb with default values + - it: should create a pdb with default values as the frontend service is replicated to 2 by default template: pdb-frontend.yaml asserts: - hasDocuments: count: 1 + - isKind: + of: PodDisruptionBudget + - equal: + path: spec.minAvailable + value: 1 + - equal: + path: spec.selector.matchLabels['app.kubernetes.io/name'] + value: "plugin-site-frontend"