From 6164007f108552f6b57d23f46aa6b803c9670865 Mon Sep 17 00:00:00 2001 From: Diogo Kiss Date: Wed, 8 Jun 2022 15:28:20 +0200 Subject: [PATCH] feat: make Init Job annotations customizable In some cases, there is a need for adding custom annotations to the Init job. For example, when using ArgoCD, we need to instruct ArgoCD to start the job still during the Sync phase and not as a PostSync hook. Otherwise, the job never starts. And this is done by adding the following annotation to the job. ``` argocd.argoproj.io/hook: Sync ``` This is to continue the work started in https://github.com/cockroachdb/helm-charts/pull/246 to address https://github.com/cockroachdb/helm-charts/issues/149 --- build/templates/README.md | 3 +- build/templates/values.yaml | 3 + cockroachdb/README.md | 3 +- cockroachdb/templates/job.init.yaml | 3 + cockroachdb/values.yaml | 3 + .../cockroachdb_helm_template_test.go | 55 +++++++++++++++++++ 6 files changed, 68 insertions(+), 2 deletions(-) diff --git a/build/templates/README.md b/build/templates/README.md index 272ff21c..6e5df294 100644 --- a/build/templates/README.md +++ b/build/templates/README.md @@ -374,7 +374,8 @@ For details see the [`values.yaml`](values.yaml) file. | `storage.persistentVolume.labels` | Additional labels of PersistentVolumeClaim | `{}` | | `storage.persistentVolume.annotations` | Additional annotations of PersistentVolumeClaim | `{}` | | `init.labels` | Additional labels of init Job and its Pod | `{"app.kubernetes.io/component": "init"}` | -| `init.annotations` | Additional labels of the Pod of init Job | `{}` | +| `init.jobAnnotations` | Additional annotations of the init Job itself | `{}` | +| `init.annotations` | Additional annotations of the Pod of init Job | `{}` | | `init.affinity` | [Affinity rules][2] of init Job Pod | `{}` | | `init.nodeSelector` | Node labels for init Job Pod assignment | `{}` | | `init.tolerations` | Node taints to tolerate by init Job Pod | `[]` | diff --git a/build/templates/values.yaml b/build/templates/values.yaml index 60d077f4..fae2ea50 100644 --- a/build/templates/values.yaml +++ b/build/templates/values.yaml @@ -361,6 +361,9 @@ init: labels: app.kubernetes.io/component: init + # Additional annotations to apply to this Job. + jobAnnotations: {} + # Additional annotations to apply to the Pod of this Job. annotations: {} diff --git a/cockroachdb/README.md b/cockroachdb/README.md index 2fef0d44..9b41d257 100644 --- a/cockroachdb/README.md +++ b/cockroachdb/README.md @@ -375,7 +375,8 @@ For details see the [`values.yaml`](values.yaml) file. | `storage.persistentVolume.labels` | Additional labels of PersistentVolumeClaim | `{}` | | `storage.persistentVolume.annotations` | Additional annotations of PersistentVolumeClaim | `{}` | | `init.labels` | Additional labels of init Job and its Pod | `{"app.kubernetes.io/component": "init"}` | -| `init.annotations` | Additional labels of the Pod of init Job | `{}` | +| `init.jobAnnotations` | Additional annotations of the init Job itself | `{}` | +| `init.annotations` | Additional annotations of the Pod of init Job | `{}` | | `init.affinity` | [Affinity rules][2] of init Job Pod | `{}` | | `init.nodeSelector` | Node labels for init Job Pod assignment | `{}` | | `init.tolerations` | Node taints to tolerate by init Job Pod | `[]` | diff --git a/cockroachdb/templates/job.init.yaml b/cockroachdb/templates/job.init.yaml index 2f29cd47..72ddaf1b 100644 --- a/cockroachdb/templates/job.init.yaml +++ b/cockroachdb/templates/job.init.yaml @@ -21,6 +21,9 @@ metadata: annotations: helm.sh/hook: post-install,post-upgrade helm.sh/hook-delete-policy: before-hook-creation + {{- with .Values.init.jobAnnotations }} + {{- toYaml . | nindent 4 }} + {{- end }} spec: template: metadata: diff --git a/cockroachdb/values.yaml b/cockroachdb/values.yaml index b6a60d04..86130ac4 100644 --- a/cockroachdb/values.yaml +++ b/cockroachdb/values.yaml @@ -362,6 +362,9 @@ init: labels: app.kubernetes.io/component: init + # Additional annotations to apply to this Job. + jobAnnotations: {} + # Additional annotations to apply to the Pod of this Job. annotations: {} diff --git a/tests/template/cockroachdb_helm_template_test.go b/tests/template/cockroachdb_helm_template_test.go index fd8f26d9..fcbf0da8 100644 --- a/tests/template/cockroachdb_helm_template_test.go +++ b/tests/template/cockroachdb_helm_template_test.go @@ -1228,3 +1228,58 @@ func TestHelmIngress(t *testing.T) { }) } } + +// TestHelmInitJobAnnotations contains the tests for the annotations of the Init Job +func TestHelmInitJobAnnotations(t *testing.T) { + t.Parallel() + testCases := []struct { + name string + values map[string]string + annotations map[string]string + }{ + { + "No extra job annotations were supplied", + map[string]string{}, + map[string]string{ + "helm.sh/hook": "post-install,post-upgrade", + "helm.sh/hook-delete-policy": "before-hook-creation", + }, + }, + { + "Extra job annotations were supplied", + map[string]string{ + "init.jobAnnotations.test-key-1": "test-value-1", + "init.jobAnnotations.test-key-2": "test-value-2", + }, + map[string]string{ + "helm.sh/hook": "post-install,post-upgrade", + "helm.sh/hook-delete-policy": "before-hook-creation", + "test-key-1": "test-value-1", + "test-key-2": "test-value-2", + }, + }, + } + + for _, testCase := range testCases { + // Here, we capture the range variable and force it into the scope of this block. If we don't do this, when the + // subtest switches contexts (because of t.Parallel), the testCase value will have been updated by the for loop + // and will be the next testCase! + testCase := testCase + t.Run(testCase.name, func(subT *testing.T) { + subT.Parallel() + + options := &helm.Options{ + KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName), + SetValues: testCase.values, + } + output, err := helm.RenderTemplateE(t, options, helmChartPath, releaseName, []string{"templates/job.init.yaml"}) + + require.Equal(subT, err, nil) + + var job batchv1.Job + helm.UnmarshalK8SYaml(t, output, &job) + + require.Equal(t, testCase.annotations, job.Annotations) + }) + } +}