From adcf0f6a65e2ecffc7162a7e9ad46847ee31e8df Mon Sep 17 00:00:00 2001 From: David Courtey Date: Wed, 6 Nov 2024 20:06:09 +0100 Subject: [PATCH] feat: middleware inside proper namespace --- helm-charts/generic-app/Chart.yaml | 2 +- .../generic-app/templates/_helpers.tpl | 2 +- .../generic-app/templates/cronjob.yaml | 2 +- .../generic-app/templates/middleware.yaml | 1 - .../tests/__snapshot__/ingress_test.yaml.snap | 3 +- .../__snapshot__/middleware_test.yaml.snap | 63 +++++++++++++++++++ .../generic-app/tests/cronjob_test.yaml | 16 ++++- .../generic-app/tests/middleware_test.yaml | 26 ++++++++ 8 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 helm-charts/generic-app/tests/__snapshot__/middleware_test.yaml.snap create mode 100644 helm-charts/generic-app/tests/middleware_test.yaml diff --git a/helm-charts/generic-app/Chart.yaml b/helm-charts/generic-app/Chart.yaml index 356b7f0..5658cd4 100644 --- a/helm-charts/generic-app/Chart.yaml +++ b/helm-charts/generic-app/Chart.yaml @@ -2,4 +2,4 @@ apiVersion: v2 name: generic-app description: A Helm Chart for Generic App type: application -version: 0.3.10 +version: 0.3.11 diff --git a/helm-charts/generic-app/templates/_helpers.tpl b/helm-charts/generic-app/templates/_helpers.tpl index cfe9737..22a8362 100644 --- a/helm-charts/generic-app/templates/_helpers.tpl +++ b/helm-charts/generic-app/templates/_helpers.tpl @@ -138,7 +138,7 @@ Create the name of the service account to use {{- $fullName := include "generic-app.fullname" . -}} {{- $middlewares := "" -}} {{- range .Values.ingress.redirections }} -{{- $middlewareName := printf "traefik-%s-redirect-to-%s@kubernetescrd" $fullName (include "toSlug" .to) -}} +{{- $middlewareName := printf "%s-%s-redirect-to-%s@kubernetescrd" $.Release.Namespace $fullName (include "toSlug" .to) -}} {{- if $middlewares }} {{- $middlewares = printf "%s,%s" $middlewares $middlewareName -}} {{- else }} diff --git a/helm-charts/generic-app/templates/cronjob.yaml b/helm-charts/generic-app/templates/cronjob.yaml index 9893d43..9116a7d 100644 --- a/helm-charts/generic-app/templates/cronjob.yaml +++ b/helm-charts/generic-app/templates/cronjob.yaml @@ -22,7 +22,7 @@ spec: template: spec: containers: - - image: {{ include "generic-app.image" $.Values | quote }} + - image: {{ if $cronjob.image }}{{ include "generic-app.image" $cronjob | quote }}{{ else }}{{ include "generic-app.image" $.Values | quote }}{{ end }} name: {{ $fullName }}-{{ $name }} imagePullPolicy: Always command: diff --git a/helm-charts/generic-app/templates/middleware.yaml b/helm-charts/generic-app/templates/middleware.yaml index 1297b55..0e33cd1 100644 --- a/helm-charts/generic-app/templates/middleware.yaml +++ b/helm-charts/generic-app/templates/middleware.yaml @@ -6,7 +6,6 @@ apiVersion: traefik.io/v1alpha1 kind: Middleware metadata: name: {{ $fullName }}-redirect-to-{{ .to | include "toSlug" }} - namespace: traefik labels: {{- include "generic-app.labels" $ | nindent 4 }} spec: diff --git a/helm-charts/generic-app/tests/__snapshot__/ingress_test.yaml.snap b/helm-charts/generic-app/tests/__snapshot__/ingress_test.yaml.snap index 38eeaf3..55f88ac 100644 --- a/helm-charts/generic-app/tests/__snapshot__/ingress_test.yaml.snap +++ b/helm-charts/generic-app/tests/__snapshot__/ingress_test.yaml.snap @@ -5,7 +5,7 @@ should create middleware redirections: metadata: annotations: cert-manager.io/cluster-issuer: letsencrypt-prod - traefik.ingress.kubernetes.io/router.middlewares: traefik-test-generic-app-redirect-to-crty-dev@kubernetescrd,traefik-redirect-to-https@kubernetescrd + traefik.ingress.kubernetes.io/router.middlewares: NAMESPACE-test-generic-app-redirect-to-crty-dev@kubernetescrd,traefik-redirect-to-https@kubernetescrd name: test-generic-app spec: ingressClassName: traefik @@ -67,7 +67,6 @@ should create middleware redirections: app.kubernetes.io/name: generic-app helm.sh/chart: generic-app-0.1.0 name: test-generic-app-redirect-to-crty-dev - namespace: traefik spec: redirectRegex: permanent: true diff --git a/helm-charts/generic-app/tests/__snapshot__/middleware_test.yaml.snap b/helm-charts/generic-app/tests/__snapshot__/middleware_test.yaml.snap new file mode 100644 index 0000000..bd489da --- /dev/null +++ b/helm-charts/generic-app/tests/__snapshot__/middleware_test.yaml.snap @@ -0,0 +1,63 @@ +should have a redirect middleware: + 1: | + apiVersion: networking.k8s.io/v1 + kind: Ingress + metadata: + annotations: + cert-manager.io/cluster-issuer: letsencrypt-prod + traefik.ingress.kubernetes.io/router.middlewares: NAMESPACE-test-generic-app-redirect-to-crty-dev@kubernetescrd,traefik-redirect-to-https@kubernetescrd + name: test-generic-app + spec: + ingressClassName: traefik + rules: + - host: crty.dev + http: + paths: + - backend: + service: + name: test-generic-app + port: + number: 80 + path: / + pathType: Prefix + - host: www.crty.dev + http: + paths: + - backend: + service: + name: test-generic-app + port: + number: 80 + path: / + pathType: Prefix + - host: infra.crty.dev + http: + paths: + - backend: + service: + name: test-generic-app + port: + number: 80 + path: / + pathType: Prefix + tls: + - hosts: + - crty.dev + - www.crty.dev + - infra.crty.dev + secretName: test-generic-app-cert + 2: | + apiVersion: traefik.io/v1alpha1 + kind: Middleware + metadata: + labels: + app.kubernetes.io/instance: test + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: generic-app + helm.sh/chart: generic-app-0.1.0 + name: test-generic-app-redirect-to-crty-dev + spec: + redirectRegex: + permanent: true + regex: ^https?://(www\.crty\.dev|infra\.crty\.dev)(/.*) + replacement: https://crty.dev$2 diff --git a/helm-charts/generic-app/tests/cronjob_test.yaml b/helm-charts/generic-app/tests/cronjob_test.yaml index e19fba3..8c5475f 100644 --- a/helm-charts/generic-app/tests/cronjob_test.yaml +++ b/helm-charts/generic-app/tests/cronjob_test.yaml @@ -17,4 +17,18 @@ tests: - "hello world" schedule: "* * * * *" asserts: - - matchSnapshot: {} \ No newline at end of file + - matchSnapshot: {} + - it: should have a cronjob with custom image + set: + cronJobs: + cron-1: + image: "busybox" + tag: "19" + command: + - echo + - "hello world" + schedule: "* * * * *" + asserts: + - equal: + path: spec.jobTemplate.spec.template.spec.containers[0].image + value: busybox:19 \ No newline at end of file diff --git a/helm-charts/generic-app/tests/middleware_test.yaml b/helm-charts/generic-app/tests/middleware_test.yaml new file mode 100644 index 0000000..f6d8221 --- /dev/null +++ b/helm-charts/generic-app/tests/middleware_test.yaml @@ -0,0 +1,26 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json + +suite: test cronjob +release: + name: test +chart: + version: 0.1.0 +templates: + - middleware.yaml + - ingress.yaml +tests: + - it: should have a redirect middleware + set: + ingress: + enabled: true + hosts: + - crty.dev + - www.crty.dev + - infra.crty.dev + redirections: + - from: + - www.crty.dev + - infra.crty.dev + to: crty.dev + asserts: + - matchSnapshot: {} \ No newline at end of file