diff --git a/charts/nextjs/templates/ingress-external.yaml b/charts/nextjs/templates/ingress-external.yaml index 5942970..3074cdb 100644 --- a/charts/nextjs/templates/ingress-external.yaml +++ b/charts/nextjs/templates/ingress-external.yaml @@ -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 "-@kubernetescrd", where is the release namespace and 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 }} diff --git a/charts/nextjs/templates/middlewares.yaml b/charts/nextjs/templates/middlewares.yaml new file mode 100644 index 0000000..a1c410b --- /dev/null +++ b/charts/nextjs/templates/middlewares.yaml @@ -0,0 +1,9 @@ +{{- range $name, $spec := .Values.traefikMiddlewares }} +--- +apiVersion: traefik.io/v1alpha1 +kind: Middleware +metadata: + name: {{ $name }} +spec: + {{- $spec | toYaml | nindent 2 }} +{{- end }} diff --git a/charts/nextjs/values.yaml b/charts/nextjs/values.yaml index fe5b087..16fbafb 100644 --- a/charts/nextjs/values.yaml +++ b/charts/nextjs/values.yaml @@ -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