Skip to content

Commit

Permalink
Add post-upgrade hook to helm installer job to support upgrades (#312)
Browse files Browse the repository at this point in the history
* Update _pega-installer-job.tpl

added post upgrade hook

* Update pega-installer-config.yaml

config changes in case of upgrade

* Update _helpers.tpl

added pega-upgrade-config

* Update _helpers.tpl

* Update pega-installer-config.yaml

* Update _helpers.tpl

pega-installer-config -> pega-install-config

* Update _pega-installer-job.tpl

config map changes for pegaUpgradeConfig in case of upgrade

* Update _pega-credentials-secret.tpl

added delete hook policy so that it recreates next time on helm upgrade

* Update _pega-registry-secret.tpl

added delete hook policy so that it recreates again on helm upgrade

* Update _pega-installer-job.tpl

* Update _pega-installer-job.tpl

config map changes

* Update _helpers.tpl

* Update _pega-installer-job.tpl

* Update _helpers.tpl

* Update _helpers.tpl

* Update _pega-installer-job.tpl

* Update _pega-installer-job.tpl

* Update pega-installer-job_test.go

* Update _helpers.tpl

* Update _pega-installer-job.tpl

* Update _pega-installer-job.tpl

* Update _pega-credentials-secret.tpl

* Update _pega-registry-secret.tpl

* Update _pega-installer-job.tpl

* Update _helpers.tpl

* Update _pega-installer-job.tpl

* Update _pega-installer-job.tpl

* Update _pega-installer-job.tpl

* Update _pega-installer-job.tpl

* Update _pega-installer-job.tpl

* Update _pega-installer-job.tpl

* Update _helpers.tpl

pegaDBCustomUpgrade

* Update pega-installer-job_test.go

* Update pega-installer-job_test.go

* Create pega-custom-upgrade_test.go

test case for upgradeType -> custom

* Update pega-custom-upgrade_test.go

Co-authored-by: MadhuriArugula <madhuriarugula77@gmail.com>
  • Loading branch information
amansharma00a and MadhuriArugula authored Jul 15, 2021
1 parent 05ad482 commit a39c99f
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 12 deletions.
14 changes: 11 additions & 3 deletions charts/pega/charts/installer/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{{- define "pegaVolumeInstall" }}pega-volume-installer{{- end }}
{{- define "pegaInstallConfig" }}pega-installer-config{{- end }}
{{- define "pegaInstallConfig" }}pega-install-config{{- end }}
{{- define "pegaUpgradeConfig" }}pega-upgrade-config{{- end }}
{{- define "pegaDBInstall" -}}pega-db-install{{- end -}}
{{- define "pegaDBCustomUpgrade" -}}pega-db-custom-upgrade{{- end -}}
{{- define "pegaDBCustomUpgrade" -}}
{{- if (contains "," .Values.upgrade.upgradeSteps) -}}
pega-db-custom-upgrade
{{- else -}}
{{- $jobName := printf "%s-%s" "pega-db-upgrade" .Values.upgrade.upgradeSteps -}}
{{- $jobName | replace "_" "-" -}}
{{- end -}}
{{- end -}}
{{- define "pegaDBOOPRulesUpgrade" -}}pega-db-ooprules-upgrade{{- end -}}
{{- define "pegaDBOOPDataUpgrade" -}}pega-db-oopdata-upgrade{{- end -}}
{{- define "pegaDBZDTUpgrade" -}}pega-zdt-upgrade{{- end -}}
Expand Down Expand Up @@ -98,4 +106,4 @@
- name: KUBERNETES_SERVICE_PORT
value: {{ $apiserver.httpsServicePort | quote }}
{{- end }}
{{- end }}
{{- end }}
16 changes: 13 additions & 3 deletions charts/pega/charts/installer/templates/_pega-installer-job.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ metadata:
name: {{ .name }}
namespace: {{ .root.Release.Namespace }}
annotations:
{{- if and (eq .root.Values.waitForJobCompletion "true") (or (eq .root.Values.global.actions.execute "install") (eq .root.Values.global.actions.execute "upgrade")) }}
# Forces Helm to wait for the install or upgrade to complete.
"helm.sh/hook": post-install
{{- if (eq .root.Values.waitForJobCompletion "true") }}
"helm.sh/hook-weight": "0"
"helm.sh/hook-delete-policy": {{ if .root.Values.cleanAfterInstall -}} before-hook-creation,hook-succeeded {{- else -}} before-hook-creation {{- end }}
{{- if (eq .root.Values.global.actions.execute "install") }}
# Forces Helm to wait for the install to complete.
"helm.sh/hook": post-install
{{- end }}
{{- if (eq .root.Values.global.actions.execute "upgrade") }}
# Forces Helm to wait for the upgrade to complete.
"helm.sh/hook": post-install, post-upgrade
{{- end }}
{{- end }}
{{- if .root.Values.global.pegaJob }}{{- if .root.Values.global.pegaJob.annotations }}
{{ toYaml .root.Values.global.pegaJob.annotations | indent 4 }}
Expand All @@ -34,7 +40,11 @@ spec:
- name: {{ template "pegaVolumeInstall" }}
configMap:
# This name will be referred in the volume mounts kind.
{{- if or (eq $arg "install") (eq $arg "install-deploy") }}
name: {{ template "pegaInstallConfig"}}
{{- else }}
name: {{ template "pegaUpgradeConfig"}}
{{- end }}
# Used to specify permissions on files within the volume.
defaultMode: 420
initContainers:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{{ if or (eq (include "performInstall" .) "true") (eq (include "performUpgrade" .) "true") }}
{{ if (eq (include "performInstall" .) "true") }}
{{ template "pega.installer.config" dict "root" $ "dbType" .Values.global.jdbc.dbType "name" (include "pegaInstallConfig" .) "mode" (include "installerConfig" .) }}
{{ end }}
{{ end }}

{{ if (eq (include "performUpgrade" .) "true") }}
{{ template "pega.installer.config" dict "root" $ "dbType" .Values.global.jdbc.dbType "name" (include "pegaUpgradeConfig" .) "mode" (include "installerConfig" .) }}
{{ end }}
6 changes: 5 additions & 1 deletion charts/pega/templates/_pega-credentials-secret.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ apiVersion: v1
metadata:
name: {{ template "pegaCredentialsSecret" $ }}
namespace: {{ .Release.Namespace }}
annotations:
"helm.sh/hook": pre-install, pre-upgrade
"helm.sh/hook-weight": "0"
"helm.sh/hook-delete-policy": before-hook-creation
data:
# Base64 encoded username for connecting to the Pega DB
DB_USERNAME: {{ .Values.global.jdbc.username | b64enc }}
Expand Down Expand Up @@ -39,4 +43,4 @@ data:
{{ end }}
{{ end }}
type: Opaque
{{- end }}
{{- end }}
6 changes: 5 additions & 1 deletion charts/pega/templates/_pega-registry-secret.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ apiVersion: v1
metadata:
name: {{ template "pegaRegistrySecret" $ }}
namespace: {{ .Release.Namespace }}
annotations:
"helm.sh/hook": pre-install, pre-upgrade
"helm.sh/hook-weight": "0"
"helm.sh/hook-delete-policy": before-hook-creation
data:
.dockerconfigjson: {{ template "imagePullSecret" . }}
type: kubernetes.io/dockerconfigjson
{{- end }}
{{- end }}
101 changes: 101 additions & 0 deletions terratest/src/test/pega/pega-custom-upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package pega

import (
"github.com/gruntwork-io/terratest/modules/helm"
"github.com/stretchr/testify/require"
k8sbatch "k8s.io/api/batch/v1"
k8score "k8s.io/api/core/v1"
"path/filepath"
"strings"
"testing"
)

func TestPegaUpgradeJob(t *testing.T) {

var supportedVendors = []string{"k8s", "openshift", "eks", "gke", "aks", "pks"}
var supportedOperations = []string{"upgrade"}
var deploymentNames = []string{"pega", "myapp-dev"}
var upgradeType = []string{"custom"}
var upgradeSteps = []string{"rules-migration", "rules-upgrade", "data-upgrade"}

helmChartPath, err := filepath.Abs(PegaHelmChartPath)
require.NoError(t, err)

for _, vendor := range supportedVendors {
for _, operation := range supportedOperations {
for _, depName := range deploymentNames {
for _, upType := range upgradeType {
for _, upSteps := range upgradeSteps {
var options = &helm.Options{
SetValues: map[string]string{
"global.deployment.name": depName,
"global.provider": vendor,
"global.actions.execute": operation,
"installer.upgrade.upgradeType": upType,
"installer.upgrade.upgradeSteps": upSteps,
},
}

yamlContent := RenderTemplate(t, options, helmChartPath, []string{"charts/installer/templates/pega-installer-job.yaml"})
yamlSplit := strings.Split(yamlContent, "---")

assertUpgradeJob(t, yamlSplit[1], pegaDbJob{"pega-db-custom-upgrade", []string{}, "pega-upgrade-environment-config"}, options)


}
}
}
}
}
}

func assertUpgradeJob(t *testing.T, jobYaml string, expectedJob pegaDbJob, options *helm.Options) {
var jobObj k8sbatch.Job
UnmarshalK8SYaml(t, jobYaml, &jobObj)

jobSpec := jobObj.Spec.Template.Spec
jobContainers := jobObj.Spec.Template.Spec.Containers

var containerPort int32 = 8080

require.Equal(t, jobSpec.Volumes[0].Name, "pega-volume-credentials")
require.Equal(t, jobSpec.Volumes[0].VolumeSource.Secret.SecretName, getObjName(options, "-credentials-secret"))
require.Equal(t, jobSpec.Volumes[0].VolumeSource.Secret.DefaultMode, volDefaultModePointer)
require.Equal(t, jobSpec.Volumes[1].Name, "pega-volume-installer")

require.Equal(t, jobSpec.Volumes[1].VolumeSource.ConfigMap.LocalObjectReference.Name, "pega-upgrade-config")

require.Equal(t, jobSpec.Volumes[1].VolumeSource.ConfigMap.DefaultMode, volDefaultModePointer)

if(jobContainers[0].Name=="pega-db-upgrade-rules-migration") {
require.Equal(t, jobContainers[0].Name, "pega-db-upgrade-rules-migration")
}
if(jobContainers[0].Name=="pega-db-upgrade-rules-upgrade") {
require.Equal(t, jobContainers[0].Name, "pega-db-upgrade-rules-upgrade")
}
if(jobContainers[0].Name=="pega-db-upgrade-data-upgrade") {
require.Equal(t, jobContainers[0].Name, "pega-db-upgrade-data-upgrade")
}

require.Equal(t, "YOUR_INSTALLER_IMAGE:TAG", jobContainers[0].Image)
require.Equal(t, jobContainers[0].Ports[0].ContainerPort, containerPort)
require.Equal(t, jobContainers[0].VolumeMounts[0].Name, "pega-volume-installer")
require.Equal(t, jobContainers[0].VolumeMounts[0].MountPath, "/opt/pega/config")
require.Equal(t, jobContainers[0].VolumeMounts[1].Name, "pega-volume-credentials")
require.Equal(t, jobContainers[0].VolumeMounts[1].MountPath, "/opt/pega/secrets")
require.Equal(t, jobContainers[0].EnvFrom[0].ConfigMapRef.LocalObjectReference.Name, expectedJob.configMapName)

require.Equal(t, jobSpec.ImagePullSecrets[0].Name, getObjName(options, "-registry-secret"))

require.Equal(t, jobSpec.RestartPolicy, k8score.RestartPolicy("Never"))

actualInitContainers := jobSpec.InitContainers
count := len(actualInitContainers)
actualInitContainerNames := make([]string, count)
for i := 0; i < count; i++ {
actualInitContainerNames[i] = actualInitContainers[i].Name
}

require.Equal(t, expectedJob.initContainers, actualInitContainerNames)
VerifyInitContinerData(t, actualInitContainers, options)
}
10 changes: 8 additions & 2 deletions terratest/src/test/pega/pega-installer-job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestPegaInstallerJob(t *testing.T) {
}
}


func assertJob(t *testing.T, jobYaml string, expectedJob pegaDbJob, options *helm.Options) {
var jobObj k8sbatch.Job
UnmarshalK8SYaml(t, jobYaml, &jobObj)
Expand All @@ -86,9 +87,14 @@ func assertJob(t *testing.T, jobYaml string, expectedJob pegaDbJob, options *hel
require.Equal(t, jobSpec.Volumes[0].VolumeSource.Secret.SecretName, getObjName(options, "-credentials-secret"))
require.Equal(t, jobSpec.Volumes[0].VolumeSource.Secret.DefaultMode, volDefaultModePointer)
require.Equal(t, jobSpec.Volumes[1].Name, "pega-volume-installer")
require.Equal(t, jobSpec.Volumes[1].VolumeSource.ConfigMap.LocalObjectReference.Name, "pega-installer-config")
if(jobSpec.Volumes[1].VolumeSource.ConfigMap.LocalObjectReference.Name=="pega-install-config") {
require.Equal(t, jobSpec.Volumes[1].VolumeSource.ConfigMap.LocalObjectReference.Name, "pega-install-config")
}
if(jobSpec.Volumes[1].VolumeSource.ConfigMap.LocalObjectReference.Name=="pega-upgrade-config") {
require.Equal(t, jobSpec.Volumes[1].VolumeSource.ConfigMap.LocalObjectReference.Name, "pega-upgrade-config")
}
require.Equal(t, jobSpec.Volumes[1].VolumeSource.ConfigMap.DefaultMode, volDefaultModePointer)

require.Equal(t, jobContainers[0].Name, expectedJob.name)
require.Equal(t, "YOUR_INSTALLER_IMAGE:TAG", jobContainers[0].Image)
require.Equal(t, jobContainers[0].Ports[0].ContainerPort, containerPort)
Expand Down

0 comments on commit a39c99f

Please sign in to comment.