From aaa042373bb076d4d8cfb9a32bee798e72014cfa Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Sun, 12 Jan 2025 15:25:37 +0100 Subject: [PATCH] Define template helpers to emit match labels including the modern labels --- jupyterhub/templates/_helpers.tpl | 83 +++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 11 deletions(-) diff --git a/jupyterhub/templates/_helpers.tpl b/jupyterhub/templates/_helpers.tpl index 693bd65caa..73ca89acd2 100644 --- a/jupyterhub/templates/_helpers.tpl +++ b/jupyterhub/templates/_helpers.tpl @@ -124,52 +124,113 @@ app.kubernetes.io/managed-by ("heritage") */}} {{- define "jupyterhub.commonLabels" -}} +{{- if .legacyLabels -}} app: {{ .appLabel | default (include "jupyterhub.appLabel" .) | quote }} release: {{ .Release.Name | quote }} {{- if not .matchLabels }} chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} heritage: {{ .Release.Service }} +{{- end }} +{{- end }} +{{- if and .legacyLabels .modernLabels -}} +{{ printf "\n" }} +{{- end }} +{{- if .modernLabels -}} app.kubernetes.io/name: {{ .appLabel | default (include "jupyterhub.appLabel" .) | quote }} app.kubernetes.io/instance: {{ .Release.Name | quote }} +{{- if not .matchLabels }} helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} app.kubernetes.io/managed-by: {{ .Release.Service }} {{- end }} {{- end }} +{{- end }} {{- /* jupyterhub.labels: - Provides old labels: + Provides labels conditionally on .legacyLabels, .modernLabels, and .matchLabels, + that are supposed to in the scoped passed this helper function. + + The legacy labels are: component app release chart (omitted for matchLabels) heritage (omitted for matchLabels) - Provides modern labels (omitted for matchLabels): - app.kubernetes.io/component ("component") - app.kubernetes.io/name ("app") - app.kubernetes.io/instance release ("release") - helm.sh/chart ("chart") - app.kubernetes.io/managed-by ("heritage") + + The equivalent modern labels are: + app.kubernetes.io/component + app.kubernetes.io/name + app.kubernetes.io/instance release + helm.sh/chart (omitted for matchLabels) + app.kubernetes.io/managed-by (omitted for matchLabels) */}} {{- define "jupyterhub.labels" -}} +{{- /* + .legacyLabels defaults to true + .modernLabels defaults to false +*/ -}} +{{- $_ := . -}} +{{- if typeIs "" .legacyLabels -}} +{{- $_ = merge (dict "legacyLabels" true) $_ -}} +{{- end -}} +{{- if typeIs "" .modernLabels -}} +{{- $_ = merge (dict "modernLabels" true) $_ -}} +{{- end -}} + +{{- if $_.legacyLabels -}} component: {{ include "jupyterhub.componentLabel" . }} -{{- if not .matchLabels }} +{{- end }} + +{{- if and $_.legacyLabels $_.modernLabels -}} +{{ printf "\n" }} +{{- end }} + +{{- if $_.modernLabels -}} app.kubernetes.io/component: {{ include "jupyterhub.componentLabel" . }} {{- end }} -{{ include "jupyterhub.commonLabels" . }} +{{ include "jupyterhub.commonLabels" $_ }} {{- end }} {{- /* jupyterhub.matchLabels: - Provides old labels: + Provides legacy labels: component app release */}} {{- define "jupyterhub.matchLabels" -}} -{{- $_ := merge (dict "matchLabels" true) . -}} +{{- $_ := merge (dict "matchLabels" true "legacyLabels" true "modernLabels" false) . -}} +{{ include "jupyterhub.labels" $_ }} +{{- end }} + + +{{- /* + jupyterhub.matchLabelsModern: + Provides modern labels: + app.kubernetes.io/component + app.kubernetes.io/name + app.kubernetes.io/instance +*/}} +{{- define "jupyterhub.matchLabelsModern" -}} +{{- $_ := merge (dict "matchLabels" true "legacyLabels" false "modernLabels" true) . -}} +{{ include "jupyterhub.labels" $_ }} +{{- end }} + + +{{- /* + jupyterhub.matchLabelsLegacyAndModern: + Provides legacy and modern labels: + component + app + release + app.kubernetes.io/component + app.kubernetes.io/name + app.kubernetes.io/instance +*/}} +{{- define "jupyterhub.matchLabelsLegacyAndModern" -}} +{{- $_ := merge (dict "matchLabels" true "legacyLabels" true "modernLabels" true) . -}} {{ include "jupyterhub.labels" $_ }} {{- end }}