Skip to content

Commit 5060b0f

Browse files
authored
Merge pull request #265 from cognifloyd/jobs_extra_hooks
Enable extra post-install and post-upgrade helm-hook jobs for environment-specific config
2 parents 270f358 + e4b72dc commit 5060b0f

12 files changed

+271
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Initialize basic unittest infrastructure using `helm-unittest`. Added tests for labels, custom annotations, SecurityContext, pullSecrets, pullPolicy, Resources, nodeSelector, tolerations, affinity, dnsPolicy, dnsConfig, ServiceAccount attach, postStartScript, both sensor-modes, env, envFrom, st2.packs.images, and st2.packs.volumes. (#284, #288, #292)
88
* Allow partitioning sensors using the hash_range strategy instead of one sensor per pod. (#218) (by @cognifloyd)
99
* New feature to include possibility for external services in st2api, st2stream and st2auth, setting default value for this services as `ClusterIP` and `hostname: ""`. Also, added new entry for custom_annotations_test.yaml and created new unit test services_test.yaml. (by @sandesvitor)
10+
* Advanced Feature: Add extra Helm hook Jobs. This minimizes the boilerplate required to run stackstorm workflows at various helm hook stages: post-install, pre-upgrade, post-upgrade. (#265) (by @cognifloyd)
1011

1112
## v0.80.0
1213
* Switch st2 to `v3.6` as a new default stable version (#274)

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,36 @@ Grab all logs only for stackstorm backend services, excluding st2web and DB/MQ/r
325325
kubectl logs -l release=<release-name>,tier=backend
326326
```
327327

328+
## Running jobs before/after install, upgrade, or rollback
329+
WARNING: The feature described in this section is an Advanced feature that new users should not need.
330+
331+
It may be convenient to run one or more `Job`(s) in your `stackstorm-ha` cluster to manage your release's life cycle.
332+
As the [Helm docs]() explain:
333+
334+
> Helm provides a _hook_ mechanism to allow chart developers to intervene at certain points in a release's life cycle.
335+
336+
The `jobs.extra_hooks` feature in this chart simplifies creating `Jobs` that Helm will run in its hooks.
337+
These jobs will use the same settings as any other job defined by this chart (eg image, annotations, pod placement).
338+
The `st2.conf` files and packs volumes will be mounted in the Job and the `st2` cli will be configured.
339+
This feature is primarily useful when you need to run a StackStorm workflow (with `st2 run ...`) after install,
340+
before/after upgrades, or before/after rollbacks.
341+
342+
NOTE: The `jobs.extra_hooks` feature is very opinionated. If you need to to apply helm hooks to anything other than
343+
`Jobs`, or if these jobs do not meet your needs, then you will need to do so from a parent chart. For example, parent charts
344+
are much better suited to jobs that don't need access to the packs, configs, configmaps, and secrets that this chart provides.
345+
See "Extending this chart" below.
346+
347+
These extra hooks jobs can be used for st2 installation-specific jobs like:
348+
349+
- running a pre-upgrade st2 workflow that notifies on various channels that the upgrade is happening,
350+
- running post-upgrade smoke tests to ensure st2 can connect to vital services (vault, kubernetes, aws, etc),
351+
- running a pre-upgrade st2 workflow that pauses long-running workflows,
352+
- running a post-upgrade st2 workflow that resumes long-running workflows,
353+
- running one-time post-install configuration (such as generating dynamic secrets in the st2kv datastore),
354+
355+
To configure the `jobs.extra_hooks`, set `jobs.extra_hooks` in your values file.
356+
Please refer to stackstorm-ha's default values.yaml file for examples.
357+
328358
## Extending this chart
329359
If you have any suggestions or ideas about how to extend this chart functionality,
330360
we welcome you to collaborate in [Issues](https://github.com/stackstorm/stackstorm-ha/issues)

templates/jobs.yaml

+136
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,139 @@ spec:
504504
{{- end }}
505505

506506
{{- end }}
507+
{{- range .Values.jobs.extra_hooks -}}
508+
{{- $name := print "extra-helm-hook" (include "stackstorm-ha.hyphenPrefix" (required "You must name each entry in jobs.extra_hooks." .name)) }}
509+
{{- if not ($.Values.jobs.skip | has $name) }}
510+
---
511+
apiVersion: batch/v1
512+
kind: Job
513+
metadata:
514+
name: {{ $.Release.Name }}-job-{{ $name }}
515+
labels:
516+
app: {{ $name }}
517+
tier: backend
518+
vendor: stackstorm
519+
chart: {{ $.Chart.Name }}-{{ $.Chart.Version }}
520+
release: {{ $.Release.Name }}
521+
heritage: {{ $.Release.Service }}
522+
annotations:
523+
helm.sh/hook: {{ required "Each entry in jobs.extra_hooks must include 'hook' (the helm.sh/hook value)" .hook }}
524+
helm.sh/hook-delete-policy: before-hook-creation
525+
helm.sh/hook-weight: {{ .hook_weight | default 10 | toString | quote }}
526+
{{- if $.Values.jobs.annotations }}
527+
{{- toYaml $.Values.jobs.annotations | nindent 4 }}
528+
{{- end }}
529+
spec:
530+
template:
531+
metadata:
532+
name: job-{{ $name }}
533+
labels:
534+
app: {{ $name }}
535+
tier: backend
536+
vendor: stackstorm
537+
chart: {{ $.Chart.Name }}-{{ $.Chart.Version }}
538+
release: {{ $.Release.Name }}
539+
heritage: {{ $.Release.Service }}
540+
annotations:
541+
checksum/config: {{ include (print $.Template.BasePath "/configmaps_st2-conf.yaml") $ | sha256sum }}
542+
checksum/packs: {{ include (print $.Template.BasePath "/configmaps_packs.yaml") $ | sha256sum }}
543+
{{- if $.Values.jobs.annotations }}
544+
{{- toYaml $.Values.jobs.annotations | nindent 8 }}
545+
{{- end }}
546+
spec:
547+
imagePullSecrets:
548+
{{- if $.Values.image.pullSecret }}
549+
- name: {{ $.Values.image.pullSecret }}
550+
{{- end }}
551+
{{- if $.Values.st2.packs.images -}}
552+
{{- include "stackstorm-ha.packs-pullSecrets" $ | nindent 6 }}
553+
{{- end }}
554+
initContainers:
555+
{{- include "stackstorm-ha.init-containers-wait-for-db" $ | nindent 6 }}
556+
{{- include "stackstorm-ha.packs-initContainers" $ | nindent 6 }}
557+
- name: generate-st2client-config
558+
image: '{{ template "stackstorm-ha.imageRepository" $ }}/st2actionrunner:{{ tpl ($.Values.jobs.image.tag | default ($.Values.st2actionrunner.image.tag | default $.Values.image.tag)) $ }}'
559+
imagePullPolicy: {{ $.Values.image.pullPolicy }}
560+
{{- with $.Values.securityContext }}
561+
securityContext: {{- toYaml . | nindent 10 }}
562+
{{- end }}
563+
envFrom:
564+
- configMapRef:
565+
name: {{ $.Release.Name }}-st2-urls
566+
- secretRef:
567+
name: {{ $.Release.Name }}-st2-auth
568+
{{- range $.Values.jobs.envFromSecrets }}
569+
- secretRef:
570+
name: {{ . }}
571+
{{- end }}
572+
volumeMounts:
573+
- name: st2client-config-vol
574+
mountPath: /root/.st2/
575+
# `st2 login` doesn't exit on failure correctly, use old methods instead. See bug: https://github.com/StackStorm/st2/issues/4338
576+
command:
577+
- 'sh'
578+
- '-ec'
579+
- |
580+
cat <<EOT > /root/.st2/config
581+
[credentials]
582+
{{- tpl $.Values.jobs.st2clientConfig $ | nindent 12 }}
583+
EOT
584+
containers:
585+
- name: {{ $name }}
586+
image: '{{ template "stackstorm-ha.imageRepository" $ }}/st2actionrunner:{{ tpl ($.Values.jobs.image.tag | default ($.Values.st2actionrunner.image.tag | default $.Values.image.tag)) $ }}'
587+
imagePullPolicy: {{ $.Values.image.pullPolicy }}
588+
{{- with $.Values.securityContext }}
589+
securityContext: {{- toYaml . | nindent 10 }}
590+
{{- end }}
591+
{{- if $.Values.jobs.env }}
592+
env: {{- include "stackstorm-ha.customEnv" $.Values.jobs | nindent 8 }}
593+
{{- end }}
594+
envFrom:
595+
- configMapRef:
596+
name: {{ $.Release.Name }}-st2-urls
597+
{{- range $.Values.jobs.envFromSecrets }}
598+
- secretRef:
599+
name: {{ . }}
600+
{{- end }}
601+
command:
602+
{{- range (required "Each entry in jobs.extra_hooks must include the 'command' to run." .command) }}
603+
{{- tpl . $ | list | toYaml | nindent 10 }}
604+
{{- end }}
605+
volumeMounts:
606+
- name: st2client-config-vol
607+
mountPath: /root/.st2/
608+
{{- include "stackstorm-ha.st2-config-volume-mounts" $ | nindent 8 }}
609+
{{- include "stackstorm-ha.packs-volume-mounts-for-register-job" $ | nindent 8 }}
610+
{{- include "stackstorm-ha.pack-configs-volume-mount" $ | nindent 8 }}
611+
{{- if .resources }}
612+
resources: {{- toYaml .resources | nindent 10 }}
613+
{{- end }}
614+
volumes:
615+
- name: st2client-config-vol
616+
emptyDir:
617+
medium: Memory
618+
{{- include "stackstorm-ha.st2-config-volume" $ | nindent 8 }}
619+
{{- include "stackstorm-ha.packs-volumes" $ | nindent 8 }}
620+
{{- include "stackstorm-ha.pack-configs-volume" $ | nindent 8 }}
621+
restartPolicy: OnFailure
622+
{{- if $.Values.dnsPolicy }}
623+
dnsPolicy: {{ $.Values.dnsPolicy }}
624+
{{- end }}
625+
{{- with $.Values.dnsConfig }}
626+
dnsConfig: {{- toYaml . | nindent 8 }}
627+
{{- end }}
628+
{{- with $.Values.podSecurityContext }}
629+
securityContext: {{- toYaml . | nindent 8 }}
630+
{{- end }}
631+
{{- with $.Values.jobs.nodeSelector }}
632+
nodeSelector: {{- toYaml . | nindent 8 }}
633+
{{- end }}
634+
{{- with $.Values.jobs.affinity }}
635+
affinity: {{- toYaml . | nindent 8 }}
636+
{{- end }}
637+
{{- with $.Values.jobs.tolerations }}
638+
tolerations: {{- toYaml . | nindent 8 }}
639+
{{- end }}
640+
641+
{{- end }}
642+
{{- end }}

tests/unit/custom_annotations_test.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,19 @@ tests:
153153
annotations:
154154
foo: bar
155155
answer: "42"
156+
extra_hooks:
157+
- name: upgrade-warning
158+
hook: pre-upgrade, pre-rollback
159+
hook_weight: -5
160+
command: ["st2", "run", "--tail", "custom_pack.warn_about_upgrade"]
156161
asserts:
157162
- hasDocuments:
158-
count: 4
163+
count: 5
159164
# job-st2-apply-rbac-defintions
160165
# job-st2-apikey-load
161166
# job-st2-key-load
162167
# job-st2-register-content
168+
# extra_hooks job
163169

164170
# job annotations
165171
- isNotNull:

tests/unit/dns_test.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ tests:
2626
st2:
2727
packs: { sensors: [] } # ensure only 1 sensor
2828
rbac: { enabled: true } # enable rbac job
29+
jobs:
30+
extra_hooks: &jobs_extra_hooks
31+
- name: upgrade-warning
32+
hook: pre-upgrade, pre-rollback
33+
hook_weight: -5
34+
command: ["st2", "run", "--tail", "custom_pack.warn_about_upgrade"]
2935
asserts:
3036
- isNull:
3137
path: spec.template.spec.dnsPolicy
@@ -51,6 +57,8 @@ tests:
5157
st2:
5258
packs: { sensors: [] } # ensure only 1 sensor
5359
rbac: { enabled: true } # enable rbac job
60+
jobs:
61+
extra_hooks: *jobs_extra_hooks
5462
asserts:
5563
- equal:
5664
path: spec.template.spec.dnsPolicy

tests/unit/env_test.yaml

+13-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,15 @@ tests:
6666
set:
6767
st2:
6868
rbac: { enabled: true } # enable rbac job
69+
jobs:
70+
extra_hooks: &extra_hooks_jobs
71+
- name: upgrade-warning
72+
hook: pre-upgrade, pre-rollback
73+
hook_weight: -5
74+
command: ["st2", "run", "--tail", "custom_pack.warn_about_upgrade"]
6975
asserts:
7076
- hasDocuments:
71-
count: 4
77+
count: 5
7278
- isNull: *is_null_env
7379

7480
- it: Deployments accept custom env
@@ -121,9 +127,10 @@ tests:
121127
rbac: { enabled: true } # enable rbac job
122128
jobs:
123129
env: *env
130+
extra_hooks: *extra_hooks_jobs
124131
asserts:
125132
- hasDocuments:
126-
count: 4
133+
count: 5
127134

128135
- contains: *contains_env
129136

@@ -156,11 +163,13 @@ tests:
156163
set:
157164
st2:
158165
rbac: { enabled: true } # enable rbac job
166+
jobs:
167+
extra_hooks: *extra_hooks_jobs
159168
release:
160169
name: st2ha
161170
asserts:
162171
- hasDocuments:
163-
count: 4
172+
count: 5
164173

165174
- contains: *contains_st2_urls
166175
documentIndex: 1
@@ -215,6 +224,7 @@ tests:
215224
rbac: { enabled: true } # enable rbac job
216225
jobs:
217226
envFromSecrets: *envFromSecrets
227+
extra_hooks: *extra_hooks_jobs
218228
asserts:
219229
- contains: *contains_external_secret1
220230
- contains: *contains_external_secret2

tests/unit/image_pull_test.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ tests:
4444
# job-st2-apikey-load
4545
# job-st2-key-load
4646
# job-st2-register-content
47+
# extra_hooks job
4748
set:
4849
# image.pullPolicy defaults to IfNotPresent
4950
# image.pullSecret defaults to None
@@ -56,6 +57,12 @@ tests:
5657
packs: { sensors: [] } # ensure only 1 sensor
5758
st2chatops:
5859
enabled: true
60+
jobs:
61+
extra_hooks: &jobs_extra_hooks
62+
- name: upgrade-warning
63+
hook: pre-upgrade, pre-rollback
64+
hook_weight: -5
65+
command: ["st2", "run", "--tail", "custom_pack.warn_about_upgrade"]
5966
asserts:
6067
- isNull:
6168
path: spec.template.spec.imagePullSecrets
@@ -84,6 +91,8 @@ tests:
8491
packs: { sensors: [] } # ensure only 1 sensor
8592
st2chatops:
8693
enabled: true
94+
jobs:
95+
extra_hooks: *jobs_extra_hooks
8796
asserts:
8897
- equal:
8998
path: spec.template.spec.imagePullSecrets[0].name

tests/unit/labels_test.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,20 @@ tests:
101101
st2:
102102
rbac:
103103
enabled: true # enable rbac job
104+
jobs:
105+
extra_hooks:
106+
- name: upgrade-warning
107+
hook: pre-upgrade, pre-rollback
108+
hook_weight: -5
109+
command: ["st2", "run", "--tail", "custom_pack.warn_about_upgrade"]
104110
asserts:
105111
- hasDocuments:
106-
count: 4
112+
count: 5
107113
# job-st2-apply-rbac-defintions
108114
# job-st2-apikey-load
109115
# job-st2-key-load
110116
# job-st2-register-content
117+
# extra_hooks job
111118

112119
# unlike deployments, jobs should not have selector.matchLabels
113120

tests/unit/packs_volumes_test.yaml

+16-4
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,17 @@ tests:
140140
volumes:
141141
enabled: false
142142
configs: {} # has one core.yaml config file by default (dicts get merged)
143+
jobs:
144+
extra_hooks: &extra_hooks_jobs
145+
- name: upgrade-warning
146+
hook: pre-upgrade, pre-rollback
147+
hook_weight: -5
148+
command: ["st2", "run", "--tail", "custom_pack.warn_about_upgrade"]
143149
release:
144150
name: st2ha
145151
asserts:
146152
- hasDocuments:
147-
count: 4
153+
count: 5
148154

149155
- notContains: *packs_vol
150156
- notContains: *venvs_vol
@@ -393,11 +399,13 @@ tests:
393399
volumes:
394400
enabled: false
395401
configs: {} # has one core.yaml config file by default (dicts get merged)
402+
jobs:
403+
extra_hooks: *extra_hooks_jobs
396404
release:
397405
name: st2ha
398406
asserts:
399407
- hasDocuments:
400-
count: 4
408+
count: 5
401409

402410
- contains: *packs_vol
403411
documentIndex: 3 # register_content
@@ -687,11 +695,13 @@ tests:
687695
virtualenvs: *nfs_venvs_volume
688696
configs: *nfs_pack_configs_volume
689697
configs: {} # has one core.yaml config file by default (dicts get merged)
698+
jobs:
699+
extra_hooks: *extra_hooks_jobs
690700
release:
691701
name: st2ha
692702
asserts:
693703
- hasDocuments:
694-
count: 4
704+
count: 5
695705

696706
- contains: *nfs_packs_vol
697707
documentIndex: 3 # register_content
@@ -960,11 +970,13 @@ tests:
960970
virtualenvs: *nfs_venvs_volume
961971
configs: *nfs_pack_configs_volume
962972
configs: {} # has one core.yaml config file by default (dicts get merged)
973+
jobs:
974+
extra_hooks: *extra_hooks_jobs
963975
release:
964976
name: st2ha
965977
asserts:
966978
- hasDocuments:
967-
count: 4
979+
count: 5
968980

969981
- contains: *nfs_packs_vol
970982
documentIndex: 3 # register_content

0 commit comments

Comments
 (0)