From 3a39afe890493c3f75d95271317c5a3b9f54482e Mon Sep 17 00:00:00 2001
From: josebb <jose.bautista.barato@gmail.com>
Date: Mon, 28 Feb 2022 13:01:23 +0200
Subject: [PATCH] Add helm hook for Rally jobs

Change-Id: I983c5c25527761c028f550e5de2a9d8b08d892ce
---
 rally/Chart.yaml                         |  2 +-
 rally/templates/job-db-init.yaml         | 10 ++++++++++
 rally/templates/job-image-repo-sync.yaml |  9 +++++++++
 rally/templates/job-ks-endpoints.yaml    |  4 ++++
 rally/templates/job-ks-service.yaml      |  4 ++++
 rally/templates/job-ks-user.yaml         | 10 ++++++++++
 rally/templates/job-manage-db.yaml       |  4 ++++
 rally/templates/job-run-task.yaml        |  4 ++++
 rally/values.yaml                        |  4 ++++
 releasenotes/notes/rally.yaml            |  1 +
 10 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/rally/Chart.yaml b/rally/Chart.yaml
index 00ba2dbf0a..514b0a5956 100644
--- a/rally/Chart.yaml
+++ b/rally/Chart.yaml
@@ -14,7 +14,7 @@ apiVersion: v1
 appVersion: v1.0.0
 description: OpenStack-Helm rally
 name: rally
-version: 0.2.4
+version: 0.2.5
 home: https://docs.openstack.org/developer/rally
 icon: https://www.openstack.org/themes/openstack/images/project-mascots/rally/OpenStack_Project_rally_vertical.png
 sources:
diff --git a/rally/templates/job-db-init.yaml b/rally/templates/job-db-init.yaml
index cba4b06ab4..f1fcbd7ea8 100644
--- a/rally/templates/job-db-init.yaml
+++ b/rally/templates/job-db-init.yaml
@@ -12,7 +12,17 @@ See the License for the specific language governing permissions and
 limitations under the License.
 */}}
 
+{{- define "metadata.annotations.job.db_init" }}
+helm.sh/hook: post-install,post-upgrade
+helm.sh/hook-weight: "-3"
+{{- end }}
+
 {{- if .Values.manifests.job_db_init }}
 {{- $dbInitJob := dict "envAll" . "serviceName" "rally" -}}
+
+{{- if .Values.helm3_hook }}
+{{- $_ := set $dbInitJob "jobAnnotations" (include "metadata.annotations.job.db_init" . | fromYaml) }}
+{{- end }}
+
 {{ $dbInitJob | include "helm-toolkit.manifests.job_db_init_mysql" }}
 {{- end }}
diff --git a/rally/templates/job-image-repo-sync.yaml b/rally/templates/job-image-repo-sync.yaml
index 64ace2ddac..a2b4e32f24 100644
--- a/rally/templates/job-image-repo-sync.yaml
+++ b/rally/templates/job-image-repo-sync.yaml
@@ -12,7 +12,16 @@ See the License for the specific language governing permissions and
 limitations under the License.
 */}}
 
+{{- define "metadata.annotations.job.repo_sync" }}
+helm.sh/hook: post-install,post-upgrade
+{{- end }}
+
 {{- if and .Values.manifests.job_image_repo_sync .Values.images.local_registry.active }}
 {{- $imageRepoSyncJob := dict "envAll" . "serviceName" "rally" -}}
+
+{{- if .Values.helm3_hook }}
+{{- $_ := $imageRepoSyncJob "jobAnnotations" (include "metadata.annotations.job.repo_sync" . | fromYaml) }}
+{{- end }}
+
 {{ $imageRepoSyncJob | include "helm-toolkit.manifests.job_image_repo_sync" }}
 {{- end }}
diff --git a/rally/templates/job-ks-endpoints.yaml b/rally/templates/job-ks-endpoints.yaml
index 08244c778a..32749feca3 100644
--- a/rally/templates/job-ks-endpoints.yaml
+++ b/rally/templates/job-ks-endpoints.yaml
@@ -24,6 +24,10 @@ metadata:
   name: rally-ks-endpoints
   annotations:
     {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
+{{- if .Values.helm3_hook }}
+    helm.sh/hook: post-install,post-upgrade
+    helm.sh/hook-weight: "-1"
+{{- end }}
 spec:
   template:
     metadata:
diff --git a/rally/templates/job-ks-service.yaml b/rally/templates/job-ks-service.yaml
index 4ad07f5589..392472f8f8 100644
--- a/rally/templates/job-ks-service.yaml
+++ b/rally/templates/job-ks-service.yaml
@@ -24,6 +24,10 @@ metadata:
   name: rally-ks-service
   annotations:
     {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
+{{- if .Values.helm3_hook }}
+    helm.sh/hook: post-install,post-upgrade
+    helm.sh/hook-weight: "-2"
+{{- end }}
 spec:
   template:
     metadata:
diff --git a/rally/templates/job-ks-user.yaml b/rally/templates/job-ks-user.yaml
index 6e95b72bc8..146787691f 100644
--- a/rally/templates/job-ks-user.yaml
+++ b/rally/templates/job-ks-user.yaml
@@ -12,7 +12,17 @@ See the License for the specific language governing permissions and
 limitations under the License.
 */}}
 
+{{- define "metadata.annotations.job.ks_user" }}
+helm.sh/hook: post-install,post-upgrade
+helm.sh/hook-weight: "1"
+{{- end }}
+
 {{- if .Values.manifests.job_ks_user }}
 {{- $ksUserJob := dict "envAll" . "serviceName" "rally" -}}
+
+{{- if .Values.helm3_hook }}
+{{- $_ := set $ksUserJob "jobAnnotations" (include "metadata.annotations.job.ks_user" . | fromYaml) }}
+{{- end }}
+
 {{ $ksUserJob | include "helm-toolkit.manifests.job_ks_user" }}
 {{- end }}
diff --git a/rally/templates/job-manage-db.yaml b/rally/templates/job-manage-db.yaml
index 9d54d94aa5..0e6040182a 100644
--- a/rally/templates/job-manage-db.yaml
+++ b/rally/templates/job-manage-db.yaml
@@ -24,6 +24,10 @@ metadata:
   name: rally-manage-db
   annotations:
     {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
+{{- if .Values.helm3_hook }}
+    helm.sh/hook: post-install,post-upgrade
+    helm.sh/hook-weight: "2"
+{{- end }}
 spec:
   template:
     metadata:
diff --git a/rally/templates/job-run-task.yaml b/rally/templates/job-run-task.yaml
index a757b611b9..efcdc58d1f 100644
--- a/rally/templates/job-run-task.yaml
+++ b/rally/templates/job-run-task.yaml
@@ -24,6 +24,10 @@ metadata:
   name: rally-run-task
   annotations:
     {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
+{{- if .Values.helm3_hook }}
+    helm.sh/hook: post-install,post-upgrade
+    helm.sh/hook-weight: "3"
+{{- end }}
 spec:
   backoffLimit: {{ .Values.jobs.run_tasks.backoffLimit }}
   template:
diff --git a/rally/values.yaml b/rally/values.yaml
index d6e3a9df59..1346ba0d89 100644
--- a/rally/values.yaml
+++ b/rally/values.yaml
@@ -4001,6 +4001,10 @@ conf:
             times: 6
             type: constant
 
+# NOTE(helm_hook): helm_hook might break for helm2 binary.
+# set helm3_hook: false when using the helm2 binary.
+helm3_hook: true
+
 manifests:
   configmap_bin: true
   configmap_etc: true
diff --git a/releasenotes/notes/rally.yaml b/releasenotes/notes/rally.yaml
index 754b2fc633..276d4b7d63 100644
--- a/releasenotes/notes/rally.yaml
+++ b/releasenotes/notes/rally.yaml
@@ -7,4 +7,5 @@ rally:
   - 0.2.2 Add values for backoffLimit and restartPolicy
   - 0.2.3 Update default image values to Wallaby
   - 0.2.4 Migrated PodDisruptionBudget resource to policy/v1 API version
+  - 0.2.5 Add helm hook for jobs
 ...