Skip to content

Commit

Permalink
feat: make Init Job annotations customizable
Browse files Browse the repository at this point in the history
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 #246
to address #149
  • Loading branch information
diogokiss committed Jun 10, 2022
1 parent 9f0f849 commit 6164007
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
3 changes: 2 additions & 1 deletion build/templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | `[]` |
Expand Down
3 changes: 3 additions & 0 deletions build/templates/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}

Expand Down
3 changes: 2 additions & 1 deletion cockroachdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | `[]` |
Expand Down
3 changes: 3 additions & 0 deletions cockroachdb/templates/job.init.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions cockroachdb/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}

Expand Down
55 changes: 55 additions & 0 deletions tests/template/cockroachdb_helm_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 6164007

Please sign in to comment.