Skip to content

Commit 2457e53

Browse files
Specify timeout for migrate + allow extending batch jobs to be run (#59)
1 parent 0a6bcdc commit 2457e53

File tree

6 files changed

+74
-69
lines changed

6 files changed

+74
-69
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.idea
22
.vscode
33
.python-version
4+
.direnv
5+
.envrc

CHANGES.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ caktus.django-k8s
44
Changes
55
-------
66

7+
v1.5.1 on May 24th, 2022
8+
~~~~~~~~~~~~~~~~~~~~~
9+
* Rename ``k8s_migration_command`` to ``k8s_migrations_command`` (the old name will continue
10+
working for now, but update your projects!)
11+
* Add ``k8s_migrations_timeout`` variable
12+
* Support further customizing batch jobs run before and after deploys via the new
13+
``k8s_predeploy_batchjobs`` and ``k8s_postdeploy_batchjobs`` variables
14+
715

816
v1.5.0 on April 20th, 2022
917
~~~~~~~~~~~~~~~~~~~~~
@@ -20,7 +28,6 @@ v1.5.0 on April 20th, 2022
2028
* Add support for mounting data volumes via Secrets within containers
2129

2230

23-
2431
v1.4.0 on Oct 14, 2021
2532
~~~~~~~~~~~~~~~~~~~~~~
2633

defaults/main.yml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,20 @@ k8s_worker_containers:
172172
k8s_worker_beat_enabled: false
173173

174174
k8s_migrations_enabled: true
175-
k8s_migration_command:
175+
k8s_migrations_timeout: 120 # seconds
176+
k8s_migration_command: # DEPRECATED: keep name without trailing 's' for backwards compatibility
176177
- python
177178
- manage.py
178179
- migrate
179180
- --noinput
180181
- -v
181182
- "2"
183+
k8s_migrations_command: "{{ k8s_migration_command }}"
184+
k8s_migrations_batchjob:
185+
job_name: migrate
186+
batch_command: "{{ k8s_migrations_command }}"
187+
enabled: "{{ k8s_migrations_enabled }}"
188+
timeout: "{{ k8s_migrations_timeout }}"
182189

183190
k8s_collectstatic_enabled: true
184191
k8s_collectstatic_timeout: 120 # seconds
@@ -189,8 +196,21 @@ k8s_collectstatic_command:
189196
- --noinput
190197
- -v
191198
- "2"
192-
193-
k8s_batchjob_state: "{{ (k8s_migrations_enabled or k8s_collectstatic_enabled) | ternary('present', 'absent') }}"
199+
k8s_collectstatic_batchjob:
200+
job_name: collectstatic
201+
batch_command: "{{ k8s_collectstatic_command }}"
202+
enabled: "{{ k8s_collectstatic_enabled }}"
203+
timeout: "{{ k8s_collectstatic_timeout }}"
204+
205+
# k8s_predeploy_batchjobs are one-off jobs that are run *before* every deploy. You can keep
206+
# the default or override to customize the jobs needed for your project.
207+
k8s_predeploy_batchjobs:
208+
- "{{ k8s_migrations_batchjob }}"
209+
- "{{ k8s_collectstatic_batchjob }}"
210+
# k8s_postdeploy_batchjobs is a hook for running batch jobs *after* every deploy.
211+
k8s_postdeploy_batchjobs: []
212+
# k8s_batchjob_state is a hook used to delete job secrets if no batch jobs are enabled
213+
k8s_batchjob_state: "{{ (k8s_predeploy_batchjobs + k8s_postdeploy_batchjobs) | map(attribute='enabled', default=true) | select | ternary('present', 'absent') }}"
194214

195215
k8s_s3_cluster_name: "" # name of EKS cluster in AWS
196216
k8s_s3_region: "us-east-1"

tasks/batchjob_tasks.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- name: remove any old "{{ batchjob.job_name }}" jobs
2+
k8s:
3+
api_key: "{{ k8s_auth_api_key }}"
4+
host: "{{ k8s_auth_host }}"
5+
ca_cert: "{{ k8s_auth_ssl_ca_cert }}"
6+
definition: "{{ lookup('template', 'batchjob.yaml.j2') }}"
7+
state: absent
8+
wait: yes
9+
validate:
10+
fail_on_error: yes
11+
strict: yes
12+
when: batchjob.enabled | default(true)
13+
- name: run job "{{ batchjob.job_name }}"
14+
k8s:
15+
api_key: "{{ k8s_auth_api_key }}"
16+
host: "{{ k8s_auth_host }}"
17+
ca_cert: "{{ k8s_auth_ssl_ca_cert }}"
18+
definition: "{{ lookup('template', 'batchjob.yaml.j2') }}"
19+
state: present
20+
wait: yes
21+
wait_condition:
22+
type: Complete
23+
status: "True"
24+
wait_timeout: "{{ batchjob.timeout | default(120) }}"
25+
validate:
26+
fail_on_error: yes
27+
strict: yes
28+
when: batchjob.enabled | default(true)

tasks/main.yml

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -158,69 +158,11 @@
158158
type: Opaque
159159
stringData: "{{ k8s_environment_variables | from_yaml }}"
160160

161-
# Run migrations if wanted
162-
- when: k8s_migrations_enabled
163-
vars:
164-
job_name: "migrate"
165-
batch_command: "{{ k8s_migration_command }}"
166-
block:
167-
- name: remove any old migration jobs
168-
k8s:
169-
api_key: "{{ k8s_auth_api_key }}"
170-
host: "{{ k8s_auth_host }}"
171-
ca_cert: "{{ k8s_auth_ssl_ca_cert }}"
172-
definition: "{{ lookup('template', 'batchjob.yaml.j2') }}"
173-
state: absent
174-
wait: yes
175-
validate:
176-
fail_on_error: yes
177-
strict: yes
178-
- name: run migrations
179-
k8s:
180-
api_key: "{{ k8s_auth_api_key }}"
181-
host: "{{ k8s_auth_host }}"
182-
ca_cert: "{{ k8s_auth_ssl_ca_cert }}"
183-
definition: "{{ lookup('template', 'batchjob.yaml.j2') }}"
184-
state: present
185-
wait: yes
186-
wait_condition:
187-
type: Complete
188-
status: "True"
189-
validate:
190-
fail_on_error: yes
191-
strict: yes
192-
193-
- when: k8s_collectstatic_enabled
194-
vars:
195-
job_name: "collectstatic"
196-
batch_command: "{{ k8s_collectstatic_command }}"
197-
block:
198-
- name: remove any old collectstatic jobs
199-
k8s:
200-
api_key: "{{ k8s_auth_api_key }}"
201-
host: "{{ k8s_auth_host }}"
202-
ca_cert: "{{ k8s_auth_ssl_ca_cert }}"
203-
definition: "{{ lookup('template', 'batchjob.yaml.j2') }}"
204-
state: absent
205-
wait: yes
206-
validate:
207-
fail_on_error: yes
208-
strict: yes
209-
- name: run collectstatic
210-
k8s:
211-
api_key: "{{ k8s_auth_api_key }}"
212-
host: "{{ k8s_auth_host }}"
213-
ca_cert: "{{ k8s_auth_ssl_ca_cert }}"
214-
definition: "{{ lookup('template', 'batchjob.yaml.j2') }}"
215-
state: present
216-
wait: yes
217-
wait_condition:
218-
type: Complete
219-
status: "True"
220-
wait_timeout: "{{ k8s_collectstatic_timeout }}"
221-
validate:
222-
fail_on_error: yes
223-
strict: yes
161+
# Run each pre-deploy batch job in turn, if wanted
162+
- loop: "{{ k8s_predeploy_batchjobs }}"
163+
loop_control:
164+
loop_var: batchjob
165+
include_tasks: batchjob_tasks.yml
224166

225167
- name: Create/update templates in Kubernetes
226168
k8s:
@@ -272,3 +214,9 @@
272214
command: kubectl --namespace "{{ k8s_namespace }}" rollout restart statefulset/celery-beat
273215
no_log: True
274216
when: k8s_worker_beat_enabled
217+
218+
# Run each post-deploy batch job in turn, if wanted
219+
- loop: "{{ k8s_postdeploy_batchjobs }}"
220+
loop_control:
221+
loop_var: batchjob
222+
include_tasks: batchjob_tasks.yml

templates/batchjob.yaml.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ apiVersion: batch/v1
22
kind: Job
33
metadata:
44
namespace: "{{ k8s_namespace }}"
5-
name: "{{ job_name }}"
5+
name: "{{ batchjob.job_name }}"
66
spec:
77
template:
88
spec:
99
containers:
1010
- name: "{{ k8s_container_name }}"
1111
image: "{{ k8s_container_image }}:{{ k8s_container_image_tag }}"
1212
imagePullPolicy: "{{ k8s_container_image_pull_policy }}"
13-
command: {{ batch_command }}
13+
command: {{ batchjob.batch_command }}
1414
env:
1515
- name: GET_HOSTS_FROM
1616
value: dns

0 commit comments

Comments
 (0)