Skip to content

Commit

Permalink
Create Traefik middlewares in nextjs chart
Browse files Browse the repository at this point in the history
  • Loading branch information
bbliem committed Jan 2, 2025
1 parent d9bf0f2 commit 978a202
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
19 changes: 18 additions & 1 deletion charts/nextjs/templates/ingress-external.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,30 @@
{{- $hasAnyHostWithTls = true }}
{{- end }}
{{- end -}}
{{- /*
If .Values.traefikMiddlewares is nonempty, we add a new item to .Values.ingressExternal.annotations:
The key of this extra item is "traefik.ingress.kubernetes.io/router.middlewares".
The value is a comma-separated list of items of the form "<namespace>-<x>@kubernetescrd", where <namespace> is the release namespace and <x> is an item in .Values.traefikMiddlewares.
To the best of my knowledge, we need to construct this string in this template instead of letting users specify a Traefik middleware annotation themselves in ingressExternal.annotations because the names must contain the namespace, which is not available in the values.yaml file (or wherever the user overrides the values).
*/}}
{{- $additionalAnnotations := dict }}
{{- if .Values.traefikMiddlewares }}
{{- $namespace := .Release.Namespace }}
{{- $middlewares := list }}
{{- range $name, $spec := .Values.traefikMiddlewares }}
{{- $middleware := printf "%s-%s@kubernetescrd" $namespace $name }}
{{- $middlewares = append $middlewares $middleware }}
{{- end }}
{{- $additionalAnnotations = dict "traefik.ingress.kubernetes.io/router.middlewares" (join "," $middlewares) }}
{{- end }}
{{- $mergedAnnotations := merge .Values.ingressExternal.annotations $additionalAnnotations }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ $fullName }}-external
labels:
{{- include "nextjs-helm.labels" . | nindent 4 }}
{{- with .Values.ingressExternal.annotations }}
{{- with $mergedAnnotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
Expand Down
9 changes: 9 additions & 0 deletions charts/nextjs/templates/middlewares.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{- range $name, $spec := .Values.traefikMiddlewares }}
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: {{ $name }}
spec:
{{- $spec | toYaml | nindent 2 }}
{{- end }}
7 changes: 7 additions & 0 deletions charts/nextjs/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ externalHosts: []
# tls: true
# path: /

# If `traefikMiddlewares` is nonempty, middleware resources are created and a `traefik.ingress.kubernetes.io/router.middlewares` annotation is added to the ones already in `ingressExternal.annotations`
traefikMiddlewares: {}
# example-redirect:
# redirectRegex:
# regex: ^https://example\.com/$
# replacement: https://example\.org/

resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
Expand Down

0 comments on commit 978a202

Please sign in to comment.