Skip to content

Commit

Permalink
Add Job, CronJob (#12)
Browse files Browse the repository at this point in the history
* added Job

* added CronJob
  • Loading branch information
rantanevich authored Mar 22, 2023
1 parent e448b7d commit 2f5d92d
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 1 deletion.
2 changes: 1 addition & 1 deletion charts/universal/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v2
name: universal
version: 0.5.0
version: 0.6.0
description: A universal Helm chart for deploying services to Kubernetes
maintainers:
- name: rantanevich
Expand Down
14 changes: 14 additions & 0 deletions charts/universal/ci/cronjob-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
image:
repository: busybox
tag: 1.35.0

cronJobs:
cleaner:
schedule: 10 * * * *
backoffLimit: 1
restartPolicy: Never
containers:
cleaner:
command: ["echo", "bundle", "exec", "rails", "runner", "'InboxItems::Cleaner.call'"]
env:
LOGGING_LEVEL_ROOT: debug
11 changes: 11 additions & 0 deletions charts/universal/ci/job-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
image:
repository: busybox
tag: 1.35.0

jobs:
migration:
backoffLimit: 1
restartPolicy: Never
containers:
migration:
command: ["echo", "bundle", "exec", "rake", "db:migrate"]
24 changes: 24 additions & 0 deletions charts/universal/templates/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{- range $name, $_ := $.Values.cronJobs }}
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: {{ include "helpers.app.name" $ }}-{{ $name }}
namespace: {{ $.Release.Namespace }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
{{- with .annotations }}
annotations:
{{- include "helpers.render" (dict "value" . "context" $) | nindent 4 }}
{{- end }}
spec:
{{- range $fname, $fvalue := . }}
{{- if has $fname (list "concurrencyPolicy" "failedJobsHistoryLimit" "startingDeadlineSeconds" "successfulJobsHistoryLimit" "timeZone") }}
{{ $fname }}: {{ $fvalue }}
{{- end }}
{{- end }}
schedule: {{ .schedule }}
jobTemplate:
spec:
{{- include "helpers.job" (dict "component" $name "value" . "context" $) | nindent 6 }}
{{- end }}
13 changes: 13 additions & 0 deletions charts/universal/templates/helpers/_job.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{- define "helpers.job" }}
{{- $ := .context }}
{{- $val := .value }}
{{- $component := .component }}
{{- range $fname, $fvalue := $val }}
{{- if has $fname (list "activeDeadlineSeconds" "backoffLimit" "completionMode" "completions" "parallelism" "ttlSecondsAfterFinished") }}
{{ $fname }}: {{ $fvalue }}
{{- end }}
{{- end }}
template:
spec:
{{- include "helpers.pod" (dict "component" $component "value" $val "context" $) | nindent 4 }}
{{- end }}
16 changes: 16 additions & 0 deletions charts/universal/templates/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- range $name, $_ := $.Values.jobs }}
---
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "helpers.app.name" $ }}-{{ $name }}
namespace: {{ $.Release.Namespace }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
{{- with .annotations }}
annotations:
{{- include "helpers.render" (dict "value" . "context" $) | nindent 4 }}
{{- end }}
spec:
{{- include "helpers.job" (dict "component" $name "value" . "context" $) | nindent 2 }}
{{- end }}
56 changes: 56 additions & 0 deletions charts/universal/tests/cronjob_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
suite: test cronjob
templates:
- cronjob.yaml
release:
name: example
namespace: &namespace sandbox
tests:
- it: common
values: &values
- values/cronjob.yaml
asserts:
- hasDocuments:
count: 1
- it: cleaner
values: *values
asserts:
- containsDocument:
apiVersion: batch/v1
kind: CronJob
name: example-cleaner
namespace: *namespace
- equal:
path: spec.schedule
value: 10 * * * *
- equal:
path: spec.jobTemplate.spec.backoffLimit
value: 3
- equal:
path: spec.jobTemplate.spec.template.spec.restartPolicy
value: Never
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].name
value: cleaner
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].command
value: ["bundle", "exec", "rails", "runner", "InboxItemsCleaner.perform"]
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].env
value:
- name: LOGGING_LEVEL_ROOT
value: debug
- name: DB_NAME
valueFrom:
secretKeyRef:
name: example-mysql
key: database
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: example-mysql
key: password
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: example-mysql
key: username
41 changes: 41 additions & 0 deletions charts/universal/tests/job_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
suite: test job
templates:
- job.yaml
release:
name: example
namespace: &namespace sandbox
tests:
- it: common
values: &values
- values/job.yaml
asserts:
- hasDocuments:
count: 1
- it: migration
values: *values
asserts:
- containsDocument:
apiVersion: batch/v1
kind: Job
name: example-migration
namespace: *namespace
- equal:
path: metadata.annotations
value:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
- equal:
path: spec.backoffLimit
value: 1
- equal:
path: spec.template.spec.restartPolicy
value: Never
- equal:
path: spec.template.spec.containers[0].name
value: migration
- equal:
path: spec.template.spec.containers[0].image
value: busybox:1.35.0
- equal:
path: spec.template.spec.containers[0].command
value: ["bundle", "exec", "rake", "db:migrate"]
19 changes: 19 additions & 0 deletions charts/universal/tests/values/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
image:
repository: busybox
tag: 1.35.0

cronJobs:
cleaner:
schedule: "10 * * * *"
backoffLimit: 3
restartPolicy: Never
containers:
cleaner:
command: ["bundle", "exec", "rails", "runner", "InboxItemsCleaner.perform"]
env:
LOGGING_LEVEL_ROOT: debug
envFromSecret:
example-mysql:
DB_NAME: database
DB_USERNAME: username
DB_PASSWORD: password
14 changes: 14 additions & 0 deletions charts/universal/tests/values/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
image:
repository: busybox
tag: 1.35.0

jobs:
migration:
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
backoffLimit: 1
restartPolicy: Never
containers:
migration:
command: ["bundle", "exec", "rake", "db:migrate"]
3 changes: 3 additions & 0 deletions charts/universal/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ ingresses: {}

pdbs: {}

jobs: {}
cronJobs: {}

rbac:
serviceAccounts: {}
roles: {}
Expand Down

0 comments on commit 2f5d92d

Please sign in to comment.