From 85b2e0f85822d64b4be20939e8c08c619b767293 Mon Sep 17 00:00:00 2001 From: slimm609 Date: Wed, 23 Aug 2023 16:54:04 -0400 Subject: [PATCH] feat: add support for multiple ingress class names - add support for multiple ingress class names on a single controller Signed-off-by: slimm609 --- charts/ingress-nginx/README.md | 6 +-- charts/ingress-nginx/templates/_helpers.tpl | 42 +++++++++++++++---- charts/ingress-nginx/templates/_params.tpl | 4 +- charts/ingress-nginx/templates/_tplvalues.tpl | 22 ++++++++++ .../templates/controller-ingressclass.yaml | 15 ++++--- charts/ingress-nginx/values.yaml | 5 ++- 6 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 charts/ingress-nginx/templates/_tplvalues.tpl diff --git a/charts/ingress-nginx/README.md b/charts/ingress-nginx/README.md index 4a0cb94a2..375ca30c8 100644 --- a/charts/ingress-nginx/README.md +++ b/charts/ingress-nginx/README.md @@ -326,12 +326,12 @@ As of version `1.26.0` of this chart, by simply not providing any clusterIP valu | controller.image.runAsUser | int | `101` | | | controller.image.seccompProfile.type | string | `"RuntimeDefault"` | | | controller.image.tag | string | `"v1.9.4"` | | -| controller.ingressClass | string | `"nginx"` | For backwards compatibility with ingress.class annotation, use ingressClass. Algorithm is as follows, first ingressClassName is considered, if not present, controller looks for ingress.class annotation | +| controller.ingressClass | string | `"nginx"` | For backwards compatibility with ingress.class annotation, use ingressClass. (comma-seperated list NOT supported) Algorithm is as follows, first ingressClassName is considered, if not present, controller looks for ingress.class annotation | | controller.ingressClassByName | bool | `false` | Process IngressClass per name (additionally as per spec.controller). | | controller.ingressClassResource.controllerValue | string | `"k8s.io/ingress-nginx"` | Controller-value of the controller that is processing this ingressClass | -| controller.ingressClassResource.default | bool | `false` | Is this the default ingressClass for the cluster | +| controller.ingressClassResource.default | bool | `false` | If multiple ingressClassResources are set, the default will always be the first in the list | | controller.ingressClassResource.enabled | bool | `true` | Is this ingressClass enabled or not | -| controller.ingressClassResource.name | string | `"nginx"` | Name of the ingressClass | +| controller.ingressClassResource.name | string | `"nginx"` | Name of the ingressClass (comma-seperated list supported) | | controller.ingressClassResource.parameters | object | `{}` | Parameters is a link to a custom resource containing additional configuration for the controller. This is optional if the controller does not require extra parameters. | | controller.keda.apiVersion | string | `"keda.sh/v1alpha1"` | | | controller.keda.behavior | object | `{}` | | diff --git a/charts/ingress-nginx/templates/_helpers.tpl b/charts/ingress-nginx/templates/_helpers.tpl index c936dab79..bdd98cc28 100644 --- a/charts/ingress-nginx/templates/_helpers.tpl +++ b/charts/ingress-nginx/templates/_helpers.tpl @@ -124,27 +124,55 @@ Users can provide an override for an explicit service they want bound via `.Valu {{- print $servicePath | trimSuffix "-" -}} {{- end -}} +{{/* +Create a default fully qualified default backend name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +*/}} +{{- define "ingress-nginx.defaultBackend.fullname" -}} +{{- printf "%s-%s" (include "ingress-nginx.fullname" .) .Values.defaultBackend.name | trunc 63 | trimSuffix "-" -}} +{{- end -}} + + +{{/* +Set the version of the Chart to include in the labels mapping +*/}} +{{- define "ingress-nginx.version" -}} +{{- default .Chart.Version .Chart.AppVersion | quote -}} +{{- end -}} + {{/* Common labels */}} {{- define "ingress-nginx.labels" -}} -helm.sh/chart: {{ include "ingress-nginx.chart" . }} +{{- if and (hasKey . "customLabels") (hasKey . "context") -}} +{{ merge + (include "tplvalues.render" (dict "value" .customLabels "context" .context) | fromYaml) + (dict + "app.kubernetes.io/version" (include "ingress-nginx.version" .context) + "app.kubernetes.io/name" (include "ingress-nginx.name" .context) + "helm.sh/chart" (include "ingress-nginx.chart" .context) + "app.kubernetes.io/part-of" .context.Release.Name + "app.kubernetes.io/managed-by" .context.Release.Service + ) + | toYaml +}} +{{- else -}} +app.kubernetes.io/version: {{ include "ingress-nginx.version" . }} {{ include "ingress-nginx.selectorLabels" . }} -{{- if .Chart.AppVersion }} -app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} -{{- end }} -app.kubernetes.io/part-of: {{ template "ingress-nginx.name" . }} +app.kubernetes.io/name: {{ include "ingress-nginx.name" . }} +helm.sh/chart: {{ include "ingress-nginx.chart" . }} +app.kubernetes.io/part-of: {{ .Release.Name }} app.kubernetes.io/managed-by: {{ .Release.Service }} -{{- if .Values.commonLabels}} +{{- if .Values.commonLabels }} {{ toYaml .Values.commonLabels }} {{- end }} {{- end -}} +{{- end -}} {{/* Selector labels */}} {{- define "ingress-nginx.selectorLabels" -}} -app.kubernetes.io/name: {{ include "ingress-nginx.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} {{- end -}} diff --git a/charts/ingress-nginx/templates/_params.tpl b/charts/ingress-nginx/templates/_params.tpl index 47d024e85..fb2399a17 100644 --- a/charts/ingress-nginx/templates/_params.tpl +++ b/charts/ingress-nginx/templates/_params.tpl @@ -16,7 +16,9 @@ - --election-id={{ include "ingress-nginx.controller.electionID" . }} - --controller-class={{ .Values.controller.ingressClassResource.controllerValue }} {{- if .Values.controller.ingressClass }} -- --ingress-class={{ .Values.controller.ingressClass }} +{{- range $index, $name := ( split "," .Values.controller.ingressClassResource.name ) }} +- --ingress-class={{ $name | trim }} +{{- end }} {{- end }} - --configmap={{ default "$(POD_NAMESPACE)" .Values.controller.configMapNamespace }}/{{ include "ingress-nginx.controller.fullname" . }} {{- if .Values.tcp }} diff --git a/charts/ingress-nginx/templates/_tplvalues.tpl b/charts/ingress-nginx/templates/_tplvalues.tpl new file mode 100644 index 000000000..77b4809b2 --- /dev/null +++ b/charts/ingress-nginx/templates/_tplvalues.tpl @@ -0,0 +1,22 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Renders a value that contains template perhaps with scope if the scope is present. +Usage: +{{ include "tplvalues.render" ( dict "value" .Values.path.to.the.Value "context" $ ) }} +{{ include "tplvalues.render" ( dict "value" .Values.path.to.the.Value "context" $ "scope" $app ) }} +*/}} +{{- define "tplvalues.render" -}} +{{- if .scope }} + {{- if typeIs "string" .value }} + {{- tpl (cat "{{- with $.RelativeScope -}}" .value "{{- end }}") (merge (dict "RelativeScope" .scope) .context) }} + {{- else }} + {{- tpl (cat "{{- with $.RelativeScope -}}" (.value | toYaml) "{{- end }}") (merge (dict "RelativeScope" .scope) .context) }} + {{- end }} +{{- else }} + {{- if typeIs "string" .value }} + {{- tpl .value .context }} + {{- else }} + {{- tpl (.value | toYaml) .context }} + {{- end }} +{{- end -}} +{{- end -}} \ No newline at end of file diff --git a/charts/ingress-nginx/templates/controller-ingressclass.yaml b/charts/ingress-nginx/templates/controller-ingressclass.yaml index 9492784a2..83590fec5 100644 --- a/charts/ingress-nginx/templates/controller-ingressclass.yaml +++ b/charts/ingress-nginx/templates/controller-ingressclass.yaml @@ -1,21 +1,24 @@ {{- if .Values.controller.ingressClassResource.enabled -}} +{{- range $index, $name := ( split "," .Values.controller.ingressClassResource.name ) }} +--- # We don't support namespaced ingressClass yet # So a ClusterRole and a ClusterRoleBinding is required apiVersion: networking.k8s.io/v1 kind: IngressClass metadata: labels: - {{- include "ingress-nginx.labels" . | nindent 4 }} + {{- include "ingress-nginx.labels" ( dict "customLabels" $.Values.commonLabels "context" $ ) | nindent 4 }} app.kubernetes.io/component: controller - {{- with .Values.controller.labels }} + {{- with $.Values.controller.labels }} {{- toYaml . | nindent 4 }} {{- end }} - name: {{ .Values.controller.ingressClassResource.name }} -{{- if .Values.controller.ingressClassResource.default }} + name: {{ $name | trim }} +{{- if and ($.Values.controller.ingressClassResource.default) (eq $index "_0") }} annotations: ingressclass.kubernetes.io/is-default-class: "true" {{- end }} spec: - controller: {{ .Values.controller.ingressClassResource.controllerValue }} - {{ template "ingressClass.parameters" . }} + controller: {{ $.Values.controller.ingressClassResource.controllerValue }} + {{ template "ingressClass.parameters" $ }} {{- end }} +{{- end }} \ No newline at end of file diff --git a/charts/ingress-nginx/values.yaml b/charts/ingress-nginx/values.yaml index cb50b9d07..ae23091c9 100644 --- a/charts/ingress-nginx/values.yaml +++ b/charts/ingress-nginx/values.yaml @@ -112,11 +112,12 @@ controller: ## This section refers to the creation of the IngressClass resource ## IngressClass resources are supported since k8s >= 1.18 and required since k8s >= 1.19 ingressClassResource: - # -- Name of the ingressClass + # -- Name of the ingressClass (comma-seperated list supported) name: nginx # -- Is this ingressClass enabled or not enabled: true # -- Is this the default ingressClass for the cluster + # -- If multiple ingressClassResources are set, the default will always be the first in the list default: false # -- Controller-value of the controller that is processing this ingressClass controllerValue: "k8s.io/ingress-nginx" @@ -124,7 +125,7 @@ controller: # configuration for the controller. This is optional if the controller # does not require extra parameters. parameters: {} - # -- For backwards compatibility with ingress.class annotation, use ingressClass. + # -- For backwards compatibility with ingress.class annotation, use ingressClass. (comma-seperated list NOT supported) # Algorithm is as follows, first ingressClassName is considered, if not present, controller looks for ingress.class annotation ingressClass: nginx # -- Labels to add to the pod container metadata