From 0e65c440c47b4a294caa3e946cfc77398ddb086e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20B=C3=A4hler?= Date: Wed, 20 Sep 2023 15:05:45 +0200 Subject: [PATCH] feat: add tpl for labels and annotations --- charts/common/Chart.yaml | 4 ++-- charts/common/README.md | 2 +- charts/common/templates/_cronjob.yaml | 8 ++------ charts/common/templates/_deployment.yaml | 8 ++------ charts/common/templates/_statefulset.yaml | 8 ++------ charts/common/templates/helpers/_labels.tpl | 17 ++++++++--------- 6 files changed, 17 insertions(+), 30 deletions(-) diff --git a/charts/common/Chart.yaml b/charts/common/Chart.yaml index 6a825f70..b5a58983 100644 --- a/charts/common/Chart.yaml +++ b/charts/common/Chart.yaml @@ -1,7 +1,7 @@ apiVersion: v2 name: common description: "Bedag's common Helm chart to use for creating other Helm charts" -version: 10.6.2 +version: 10.7.0 # A chart can be either an 'application' or a 'library' chart. # # Application charts are a collection of templates that can be packaged into versioned archives @@ -27,4 +27,4 @@ annotations: artifacthub.io/prerelease: "false" artifacthub.io/license: Apache-2.0 artifacthub.io/changes: | - - "[Added]: support for envFrom" + - "[Changed]: extra annotations and labesl support sprig statements" diff --git a/charts/common/README.md b/charts/common/README.md index 6d10d6e3..b5cb980b 100644 --- a/charts/common/README.md +++ b/charts/common/README.md @@ -1,6 +1,6 @@ # common -![Version: 10.6.2](https://img.shields.io/badge/Version-10.6.2-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) +![Version: 10.7.0](https://img.shields.io/badge/Version-10.7.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) Bedag's common Helm chart to use for creating other Helm charts diff --git a/charts/common/templates/_cronjob.yaml b/charts/common/templates/_cronjob.yaml index 0009f78e..2e2e30e7 100644 --- a/charts/common/templates/_cronjob.yaml +++ b/charts/common/templates/_cronjob.yaml @@ -32,16 +32,12 @@ spec: labels: {{ include "library.labels.stable" $root | indent 8 }} app.kubernetes.io/component: {{ $name }} - {{- range $key, $value := $cronjob.extraLabels }} - {{ $key }}: {{ $value | quote }} - {{- end }} + {{- include "library.mapify" (dict "map" $cronjob.extraLabels "ctx" $root) | nindent 8 }} {{- if $cronjob.gatherMetrics }} gatherMetrics: "enabled" {{- end }} annotations: - {{- range $key, $value := $cronjob.extraAnnotations }} - {{ $key }}: {{ $value | quote }} - {{- end }} + {{- include "library.mapify" (dict "map" $cronjob.extraAnnotations "ctx" $root) | nindent 8 }} spec: {{- include "common.jobspec" (dict "root" $root "job" $cronjob "name" $name) | indent 6 }} {{- end }} diff --git a/charts/common/templates/_deployment.yaml b/charts/common/templates/_deployment.yaml index d3213624..0bb7674c 100644 --- a/charts/common/templates/_deployment.yaml +++ b/charts/common/templates/_deployment.yaml @@ -39,9 +39,7 @@ spec: labels: {{ include "library.labels.stable" $root | indent 8 }} app.kubernetes.io/component: {{ $name }} - {{- range $key, $value := $deployment.extraLabels }} - {{ $key }}: {{ $value | quote }} - {{- end }} + {{- include "library.mapify" (dict "map" $deployment.extraLabels "ctx" $root) | nindent 8 }} {{- if $deployment.gatherMetrics }} gatherMetrics: "enabled" {{- end }} @@ -51,9 +49,7 @@ spec: # and thus re-pull the image even if the tag has not changed (if imagePullPolicy = Always) timestamp: {{ now.Unix | quote }} {{- end }} - {{- range $key, $value := $deployment.extraAnnotations }} - {{ $key }}: {{ $value | quote }} - {{- end }} + {{- include "library.mapify" (dict "map" $deployment.extraAnnotations "ctx" $root) | nindent 8 }} {{- range $deployment.extraChecksumAnnotations }} {{ .key }}: {{ include (print $.Template.BasePath .checksumFrom) $root | sha256sum }} {{- end }} diff --git a/charts/common/templates/_statefulset.yaml b/charts/common/templates/_statefulset.yaml index dbcdb325..bbf9fda1 100644 --- a/charts/common/templates/_statefulset.yaml +++ b/charts/common/templates/_statefulset.yaml @@ -29,9 +29,7 @@ spec: labels: {{ include "library.labels.stable" $root | indent 8 }} app.kubernetes.io/component: {{ $name }} - {{- range $key, $value := $statefulset.extraLabels }} - {{ $key }}: {{ $value | quote }} - {{- end }} + {{- include "library.mapify" (dict "map" $statefulset.extraLabels "ctx" $root) | nindent 8 }} {{- if $statefulset.gatherMetrics }} gatherMetrics: "enabled" {{- end }} @@ -41,9 +39,7 @@ spec: # and thus re-pull the image even if the tag has not changed (if imagePullPolicy = Always) timestamp: {{ now.Unix | quote}} {{- end }} - {{- range $key, $value := $statefulset.extraAnnotations }} - {{ $key }}: {{ $value | quote }} - {{- end }} + {{- include "library.mapify" (dict "map" $statefulset.extraAnnotations "ctx" $root) | nindent 8 }} {{- range $statefulset.extraChecksumAnnotations }} {{ .key }}: {{ include (print $.Template.BasePath .checksumFrom) $root | sha256sum }} {{- end }} diff --git a/charts/common/templates/helpers/_labels.tpl b/charts/common/templates/helpers/_labels.tpl index afe06fd7..ebd937ff 100644 --- a/charts/common/templates/helpers/_labels.tpl +++ b/charts/common/templates/helpers/_labels.tpl @@ -1,14 +1,13 @@ {{- /* -library.labelize takes a dict or map and generates labels. -Values will be quoted. Keys will not. -Example output: - first: "Matt" - last: "Butcher" +library.mapify is used to generate annotations and labels +where the value can be templated with the given context */ -}} -{{- define "library.labelize" -}} -{{- range $k, $v := . }} -{{ $k }}: {{ $v | quote }} -{{- end -}} +{{- define "library.mapify" -}} + {{- if $.map -}} + {{- range $k, $v := $.map -}} + {{- $k | nindent 0 }}: {{ (tpl $v $.ctx) | quote }} + {{- end -}} + {{- end -}} {{- end -}} {{- /*