From 929ebf5200112d011b90415f1d82e4e1677909c5 Mon Sep 17 00:00:00 2001 From: Doug Goldstein Date: Tue, 2 Apr 2024 11:53:54 -0500 Subject: [PATCH] add custom job annotations snippet and use it Add the ability for charts that use helm-toolkit to allow the users to set custom annotations on jobs. Use the snippet in a generic way in the job templates provided by helm-toolkit. Change-Id: I5d60fe849e172c19d865b614c3c44ea618f92f20 Depends-On: I3991d6984563813d5a3a776eabd52e2e89933bd8 Signed-off-by: Doug Goldstein --- helm-toolkit/Chart.yaml | 2 +- .../templates/manifests/_job-bootstrap.tpl | 1 + .../manifests/_job-db-drop-mysql.tpl | 1 + .../manifests/_job-db-init-mysql.tpl | 1 + .../templates/manifests/_job-db-sync.tpl | 1 + .../templates/manifests/_job-ks-endpoints.tpl | 1 + .../templates/manifests/_job-ks-service.tpl | 1 + .../templates/manifests/_job-ks-user.yaml.tpl | 1 + .../manifests/_job-rabbit-init.yaml.tpl | 1 + .../manifests/_job-s3-bucket.yaml.tpl | 1 + .../templates/manifests/_job-s3-user.yaml.tpl | 1 + .../snippets/_custom_job_annotations.tpl | 76 +++++++++++++++++++ releasenotes/notes/helm-toolkit.yaml | 1 + 13 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 helm-toolkit/templates/snippets/_custom_job_annotations.tpl diff --git a/helm-toolkit/Chart.yaml b/helm-toolkit/Chart.yaml index 5c4ebff5b..a94b72386 100644 --- a/helm-toolkit/Chart.yaml +++ b/helm-toolkit/Chart.yaml @@ -15,7 +15,7 @@ apiVersion: v1 appVersion: v1.0.0 description: OpenStack-Helm Helm-Toolkit name: helm-toolkit -version: 0.2.62 +version: 0.2.63 home: https://docs.openstack.org/openstack-helm icon: https://www.openstack.org/themes/openstack/images/project-mascots/OpenStack-Helm/OpenStack_Project_OpenStackHelm_vertical.png sources: diff --git a/helm-toolkit/templates/manifests/_job-bootstrap.tpl b/helm-toolkit/templates/manifests/_job-bootstrap.tpl index 5d98c8b7f..6b77004f0 100644 --- a/helm-toolkit/templates/manifests/_job-bootstrap.tpl +++ b/helm-toolkit/templates/manifests/_job-bootstrap.tpl @@ -51,6 +51,7 @@ metadata: {{ toYaml $jobLabels | indent 4 }} {{- end }} annotations: +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-db-drop-mysql.tpl b/helm-toolkit/templates/manifests/_job-db-drop-mysql.tpl index 62ed11916..2b7ff2cdc 100644 --- a/helm-toolkit/templates/manifests/_job-db-drop-mysql.tpl +++ b/helm-toolkit/templates/manifests/_job-db-drop-mysql.tpl @@ -54,6 +54,7 @@ metadata: annotations: "helm.sh/hook": pre-delete "helm.sh/hook-delete-policy": hook-succeeded +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-db-init-mysql.tpl b/helm-toolkit/templates/manifests/_job-db-init-mysql.tpl index 745e8dab8..b8a1dce3b 100644 --- a/helm-toolkit/templates/manifests/_job-db-init-mysql.tpl +++ b/helm-toolkit/templates/manifests/_job-db-init-mysql.tpl @@ -52,6 +52,7 @@ metadata: {{ toYaml $jobLabels | indent 4 }} {{- end }} annotations: +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-db-sync.tpl b/helm-toolkit/templates/manifests/_job-db-sync.tpl index 24d2496d1..4696c88fd 100644 --- a/helm-toolkit/templates/manifests/_job-db-sync.tpl +++ b/helm-toolkit/templates/manifests/_job-db-sync.tpl @@ -49,6 +49,7 @@ metadata: {{ toYaml $jobLabels | indent 4 }} {{- end }} annotations: +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-ks-endpoints.tpl b/helm-toolkit/templates/manifests/_job-ks-endpoints.tpl index 3a7df7ff9..d69c9e6ec 100644 --- a/helm-toolkit/templates/manifests/_job-ks-endpoints.tpl +++ b/helm-toolkit/templates/manifests/_job-ks-endpoints.tpl @@ -52,6 +52,7 @@ metadata: {{ toYaml $jobLabels | indent 4 }} {{- end }} annotations: +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-ks-service.tpl b/helm-toolkit/templates/manifests/_job-ks-service.tpl index a109e3cc0..9604c6372 100644 --- a/helm-toolkit/templates/manifests/_job-ks-service.tpl +++ b/helm-toolkit/templates/manifests/_job-ks-service.tpl @@ -52,6 +52,7 @@ metadata: {{ toYaml $jobLabels | indent 4 }} {{- end }} annotations: +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-ks-user.yaml.tpl b/helm-toolkit/templates/manifests/_job-ks-user.yaml.tpl index 905eb71a6..58dcdc5c6 100644 --- a/helm-toolkit/templates/manifests/_job-ks-user.yaml.tpl +++ b/helm-toolkit/templates/manifests/_job-ks-user.yaml.tpl @@ -74,6 +74,7 @@ metadata: {{ toYaml $jobLabels | indent 4 }} {{- end }} annotations: +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-rabbit-init.yaml.tpl b/helm-toolkit/templates/manifests/_job-rabbit-init.yaml.tpl index 698206426..2cfadafe3 100644 --- a/helm-toolkit/templates/manifests/_job-rabbit-init.yaml.tpl +++ b/helm-toolkit/templates/manifests/_job-rabbit-init.yaml.tpl @@ -42,6 +42,7 @@ metadata: {{ toYaml $jobLabels | indent 4 }} {{- end }} annotations: +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-s3-bucket.yaml.tpl b/helm-toolkit/templates/manifests/_job-s3-bucket.yaml.tpl index 29cb99378..b5fdc09c3 100644 --- a/helm-toolkit/templates/manifests/_job-s3-bucket.yaml.tpl +++ b/helm-toolkit/templates/manifests/_job-s3-bucket.yaml.tpl @@ -49,6 +49,7 @@ metadata: {{- end }} annotations: {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }} +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/manifests/_job-s3-user.yaml.tpl b/helm-toolkit/templates/manifests/_job-s3-user.yaml.tpl index 50d9af599..77d1a71e9 100644 --- a/helm-toolkit/templates/manifests/_job-s3-user.yaml.tpl +++ b/helm-toolkit/templates/manifests/_job-s3-user.yaml.tpl @@ -47,6 +47,7 @@ metadata: annotations: "helm.sh/hook-delete-policy": before-hook-creation {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }} +{{ tuple $serviceAccountName $envAll | include "helm-toolkit.snippets.custom_job_annotations" | indent 4 -}} {{- if $jobAnnotations }} {{ toYaml $jobAnnotations | indent 4 }} {{- end }} diff --git a/helm-toolkit/templates/snippets/_custom_job_annotations.tpl b/helm-toolkit/templates/snippets/_custom_job_annotations.tpl new file mode 100644 index 000000000..fc426142f --- /dev/null +++ b/helm-toolkit/templates/snippets/_custom_job_annotations.tpl @@ -0,0 +1,76 @@ +{{/* +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/}} + +{{/* +abstract: | + Adds custom annotations to the job spec of a component. +examples: + - values: | + annotations: + job: + default: + custom.tld/key: "value" + custom.tld/key2: "value2" + keystone_domain_manage: + another.tld/foo: "bar" + usage: | + {{ tuple "keystone_domain_manage" . | include "helm-toolkit.snippets.custom_job_annotations" }} + return: | + another.tld/foo: bar + - values: | + annotations: + job: + default: + custom.tld/key: "value" + custom.tld/key2: "value2" + keystone_domain_manage: + another.tld/foo: "bar" + usage: | + {{ tuple "keystone_bootstrap" . | include "helm-toolkit.snippets.custom_job_annotations" }} + return: | + custom.tld/key: "value" + custom.tld/key2: "value2" + - values: | + annotations: + job: + default: + custom.tld/key: "value" + custom.tld/key2: "value2" + keystone_domain_manage: + another.tld/foo: "bar" + keystone_bootstrap: + usage: | + {{ tuple "keystone_bootstrap" . | include "helm-toolkit.snippets.custom_job_annotations" }} + return: | + custom.tld/key: "value" + custom.tld/key2: "value2" +*/}} + +{{- define "helm-toolkit.snippets.custom_job_annotations" -}} +{{- $envAll := index . 1 -}} +{{- $component := index . 0 | replace "-" "_" -}} +{{- if (hasKey $envAll.Values "annotations") -}} +{{- if (hasKey $envAll.Values.annotations "job") -}} +{{- $annotationsMap := $envAll.Values.annotations.job -}} +{{- $defaultAnnotations := dict -}} +{{- if (hasKey $annotationsMap "default" ) -}} +{{- $defaultAnnotations = $annotationsMap.default -}} +{{- end -}} +{{- $annotations := index $annotationsMap $component | default $defaultAnnotations -}} +{{- if (not (empty $annotations)) -}} +{{- toYaml $annotations -}} +{{- end -}} +{{- end -}} +{{- end -}} +{{- end -}} diff --git a/releasenotes/notes/helm-toolkit.yaml b/releasenotes/notes/helm-toolkit.yaml index 8473ac926..d5cb85182 100644 --- a/releasenotes/notes/helm-toolkit.yaml +++ b/releasenotes/notes/helm-toolkit.yaml @@ -69,4 +69,5 @@ helm-toolkit: - 0.2.60 Change default ingress pathType to Prefix - 0.2.61 Add custom pod annotations snippet - 0.2.62 Add custom secret annotations snippet + - 0.2.63 Add custom job annotations snippet and wire it into job templates ...