From 43f24adf576240b82a9d053caeb09dc3900aa69e Mon Sep 17 00:00:00 2001
From: Phil Sphicas <phil.sphicas@att.com>
Date: Sun, 6 Jun 2021 02:47:14 +0000
Subject: [PATCH] Barbican: Add conditional wrapper to helm hook

The pre-install hooks for several of the barbican templates [0] cause
upgrade failures when using helm2. Similar to what was done for keystone
[1], this change wraps them in a conditional that can be toggled off for
anyone still using helm2.

0: https://review.opendev.org/c/openstack/openstack-helm/+/782710
1: https://review.opendev.org/c/openstack/openstack-helm/+/785517

Change-Id: I6a160916ec65d73eef1aaffb510c85ee7fb0d501
---
 barbican/Chart.yaml                         | 2 +-
 barbican/templates/job-bootstrap.yaml       | 2 ++
 barbican/templates/job-db-init.yaml         | 2 ++
 barbican/templates/job-db-sync.yaml         | 2 ++
 barbican/templates/job-image-repo-sync.yaml | 2 ++
 barbican/templates/job-ks-endpoints.yaml    | 2 ++
 barbican/templates/job-ks-service.yaml      | 2 ++
 barbican/templates/job-ks-user.yaml         | 2 ++
 barbican/templates/job-rabbit-init.yaml     | 2 ++
 barbican/values.yaml                        | 4 ++++
 releasenotes/notes/barbican.yaml            | 1 +
 11 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/barbican/Chart.yaml b/barbican/Chart.yaml
index 5ae99f94ef..bab804b198 100644
--- a/barbican/Chart.yaml
+++ b/barbican/Chart.yaml
@@ -14,7 +14,7 @@ apiVersion: v1
 appVersion: v1.0.0
 description: OpenStack-Helm Barbican
 name: barbican
-version: 0.2.1
+version: 0.2.2
 home: https://docs.openstack.org/barbican/latest/
 icon: https://www.openstack.org/themes/openstack/images/project-mascots/Barbican/OpenStack_Project_Barbican_vertical.png
 sources:
diff --git a/barbican/templates/job-bootstrap.yaml b/barbican/templates/job-bootstrap.yaml
index 1bee0d852e..a98148ed87 100644
--- a/barbican/templates/job-bootstrap.yaml
+++ b/barbican/templates/job-bootstrap.yaml
@@ -13,9 +13,11 @@ limitations under the License.
 */}}
 
 {{- define "metadata.annotations.job.bootstrap" }}
+{{- if .Values.helm3_hook }}
 helm.sh/hook: post-install,post-upgrade
 helm.sh/hook-weight: "5"
 {{- end }}
+{{- end }}
 
 {{- if and .Values.manifests.job_bootstrap .Values.bootstrap.enabled }}
 {{- $bootstrapJob := dict "envAll" . "serviceName" "barbican" "keystoneUser" .Values.bootstrap.ks_user "logConfigFile" .Values.conf.barbican.DEFAULT.log_config_append "jobAnnotations" (include "metadata.annotations.job.bootstrap" . | fromYaml) -}}
diff --git a/barbican/templates/job-db-init.yaml b/barbican/templates/job-db-init.yaml
index e58cb9d8aa..46f7c6af97 100644
--- a/barbican/templates/job-db-init.yaml
+++ b/barbican/templates/job-db-init.yaml
@@ -13,9 +13,11 @@ limitations under the License.
 */}}
 
 {{- define "metadata.annotations.job.db_init" }}
+{{- if .Values.helm3_hook }}
 helm.sh/hook: post-install,post-upgrade
 helm.sh/hook-weight: "-5"
 {{- end }}
+{{- end }}
 
 {{- if .Values.manifests.job_db_init }}
 {{- $serviceName := "barbican" -}}
diff --git a/barbican/templates/job-db-sync.yaml b/barbican/templates/job-db-sync.yaml
index 7328a52203..86fe7f35fc 100644
--- a/barbican/templates/job-db-sync.yaml
+++ b/barbican/templates/job-db-sync.yaml
@@ -13,9 +13,11 @@ limitations under the License.
 */}}
 
 {{- define "metadata.annotations.job.db_sync" }}
+{{- if .Values.helm3_hook }}
 helm.sh/hook: post-install,post-upgrade
 helm.sh/hook-weight: "-4"
 {{- end }}
+{{- end }}
 
 {{- if .Values.manifests.job_db_sync }}
 {{- $dbSyncJob := dict "envAll" . "serviceName" "barbican" "podVolMounts" .Values.pod.mounts.barbican_db_sync.barbican_db_sync.volumeMounts "podVols" .Values.pod.mounts.barbican_db_sync.barbican_db_sync.volumes "jobAnnotations" (include "metadata.annotations.job.db_sync" . | fromYaml) -}}
diff --git a/barbican/templates/job-image-repo-sync.yaml b/barbican/templates/job-image-repo-sync.yaml
index e6648245c7..0181de3a44 100644
--- a/barbican/templates/job-image-repo-sync.yaml
+++ b/barbican/templates/job-image-repo-sync.yaml
@@ -13,8 +13,10 @@ limitations under the License.
 */}}
 
 {{- define "metadata.annotations.job.repo_sync" }}
+{{- if .Values.helm3_hook }}
 helm.sh/hook: post-install,post-upgrade
 {{- end }}
+{{- end }}
 
 {{- if and .Values.manifests.job_image_repo_sync .Values.images.local_registry.active }}
 {{- $imageRepoSyncJob := dict "envAll" . "serviceName" "barbican" "jobAnnotations" (include "metadata.annotations.job.repo_sync" . | fromYaml) -}}
diff --git a/barbican/templates/job-ks-endpoints.yaml b/barbican/templates/job-ks-endpoints.yaml
index b46da30777..e8ec4c5a55 100644
--- a/barbican/templates/job-ks-endpoints.yaml
+++ b/barbican/templates/job-ks-endpoints.yaml
@@ -13,9 +13,11 @@ limitations under the License.
 */}}
 
 {{- define "metadata.annotations.job.ks_endpoints" }}
+{{- if .Values.helm3_hook }}
 helm.sh/hook: post-install,post-upgrade
 helm.sh/hook-weight: "-2"
 {{- end }}
+{{- end }}
 
 {{- if .Values.manifests.job_ks_endpoints }}
 {{- $ksServiceJob := dict "envAll" . "serviceName" "barbican" "serviceTypes" ( tuple "key-manager" ) "jobAnnotations" (include "metadata.annotations.job.ks_endpoints" . | fromYaml) -}}
diff --git a/barbican/templates/job-ks-service.yaml b/barbican/templates/job-ks-service.yaml
index ac4f5a5f24..a86c1bcbe3 100644
--- a/barbican/templates/job-ks-service.yaml
+++ b/barbican/templates/job-ks-service.yaml
@@ -13,9 +13,11 @@ limitations under the License.
 */}}
 
 {{- define "metadata.annotations.job.ks_service" }}
+{{- if .Values.helm3_hook }}
 helm.sh/hook: post-install,post-upgrade
 helm.sh/hook-weight: "-3"
 {{- end }}
+{{- end }}
 
 {{- if .Values.manifests.job_ks_service }}
 {{- $ksServiceJob := dict "envAll" . "serviceName" "barbican" "serviceTypes" ( tuple "key-manager" ) "jobAnnotations" (include "metadata.annotations.job.ks_service" . | fromYaml) -}}
diff --git a/barbican/templates/job-ks-user.yaml b/barbican/templates/job-ks-user.yaml
index bbc486da0f..ab5ac9a8db 100644
--- a/barbican/templates/job-ks-user.yaml
+++ b/barbican/templates/job-ks-user.yaml
@@ -13,9 +13,11 @@ limitations under the License.
 */}}
 
 {{- define "metadata.annotations.job.ks_user" }}
+{{- if .Values.helm3_hook }}
 helm.sh/hook: post-install,post-upgrade
 helm.sh/hook-weight: "-1"
 {{- end }}
+{{- end }}
 
 {{- if .Values.manifests.job_ks_user }}
 {{- $ksUserJob := dict "envAll" . "serviceName" "barbican" "jobAnnotations" (include "metadata.annotations.job.ks_user" . | fromYaml) -}}
diff --git a/barbican/templates/job-rabbit-init.yaml b/barbican/templates/job-rabbit-init.yaml
index 2ec40f5d01..702bcaeab5 100644
--- a/barbican/templates/job-rabbit-init.yaml
+++ b/barbican/templates/job-rabbit-init.yaml
@@ -13,9 +13,11 @@ limitations under the License.
 */}}
 
 {{- define "metadata.annotations.job.rabbit_init" }}
+{{- if .Values.helm3_hook }}
 helm.sh/hook: post-install,post-upgrade
 helm.sh/hook-weight: "-4"
 {{- end }}
+{{- end }}
 
 {{- if .Values.manifests.job_rabbit_init }}
 {{- $rmqUserJob := dict "envAll" . "serviceName" "barbican" "jobAnnotations" (include "metadata.annotations.job.rabbit_init" . | fromYaml) -}}
diff --git a/barbican/values.yaml b/barbican/values.yaml
index 51b9024457..c2a031e37b 100644
--- a/barbican/values.yaml
+++ b/barbican/values.yaml
@@ -29,6 +29,10 @@ labels:
 
 release_group: null
 
+# NOTE(philsphicas): the pre-install hook breaks upgrade for helm2
+# Set to false to upgrade using helm2
+helm3_hook: true
+
 images:
   tags:
     bootstrap: docker.io/openstackhelm/heat:stein-ubuntu_bionic
diff --git a/releasenotes/notes/barbican.yaml b/releasenotes/notes/barbican.yaml
index 5ed6049537..d898918cf4 100644
--- a/releasenotes/notes/barbican.yaml
+++ b/releasenotes/notes/barbican.yaml
@@ -5,3 +5,4 @@ barbican:
   - 0.1.2 Added post-install and post-upgrade helm hook for Jobs
   - 0.2.0 Remove support for releases before T
   - 0.2.1 Use policies in yaml format
+  - 0.2.2 Add helm hook conditional