Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for k8s jobs to stack helm chart #190

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

include ./common.mk
8 changes: 8 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

.PHONY: test
test:
@for i in $$(find . -type d); do \
if [ -e "$$i/Chart.yaml" ]; then \
helm unittest $$i; \
fi; \
done
2 changes: 2 additions & 0 deletions stack/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

include ../common.mk
1 change: 1 addition & 0 deletions stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,4 @@ A Helm chart for deploying an Argus stack.
| `global.oidcProxy.resources.requests.memory` | Memory request | `4Gi` |
| `services` | Services to deploy, all values in the above global section are inherited by the services and each service can override them | `{}` |
| `cronJobs` | Cron jobs to deploy, all values in the above global section are inherited by the cron jobs and each cron job can override them | `{}` |
| `jobs` | Jobs to deploy, all values in the above global section are inherited by the jobs and each job can override them | `{}` |
179 changes: 179 additions & 0 deletions stack/templates/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{{ $global := . }}
{{ range $jobName, $jobValues := .Values.jobs }}
{{- $globalValuesDict := $global.Values.global | toYaml -}}
{{- $values := fromYaml $globalValuesDict -}}
{{- $values = set $values "name" $jobName -}}
{{- $values := mergeOverwrite $values $jobValues -}}
{{- $job := dict "Chart" $global.Chart "Release" $global.Release "Capabilities" $global.Capabilities "Values" $values -}}
{{- with $job -}}
---
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "service.fullname" . }}
labels:
{{- include "service.labels" . | nindent 4 }}
annotations:
{{- toYaml (mergeOverwrite
(dict)
(fromYaml (include "stack.annotations" $job ))
) | nindent 4 }}
spec:
{{- if .Values.activeDeadlineSeconds }}
activeDeadlineSeconds: {{ .Values.activeDeadlineSeconds }}
{{- end }}
{{- if .Values.ttlSecondsAfterFinished }}
ttlSecondsAfterFinished: {{ .Values.ttlSecondsAfterFinished }}
{{- end }}
{{- if .Values.backoffLimit }}
backoffLimit: {{ .Values.backoffLimit }}
{{- end }}
{{- if .Values.completions }}
completions: {{ .Values.completions }}
{{- end }}
{{- if .Values.parallelism }}
parallelism: {{ .Values.parallelism }}
{{- end }}
template:
metadata:
{{- with mergeOverwrite .Values.podAnnotations (dict "linkerd.io/inject" "disabled") }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "service.labels" . | nindent 8 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "service.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
shareProcessNamespace: {{ .Values.shareProcessNamespace }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if and .Values.args (ne (len .Values.args) 0) }}
args:
{{- toYaml .Values.args | nindent 12 }}
{{- end }}
{{- if and .Values.command (ne (len .Values.command) 0) }}
command:
{{- toYaml .Values.command | nindent 12 }}
{{- end }}
ports:
- name: http
containerPort: {{ .Values.service.port }}
protocol: TCP
livenessProbe:
{{- include "container.probe" .Values.livenessProbe | nindent 12 }}
readinessProbe:
{{- include "container.probe" .Values.readinessProbe | nindent 12 }}
{{- if eq .Values.startupProbe.enabled true }}
startupProbe:
{{- include "container.probe" (omit .Values.startupProbe "enabled") | nindent 12 }}
{{- end }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- if or (and .Values.persistence.enabled .Values.persistence.mountPath) .Values.volumeMounts}}
volumeMounts:
{{- if and .Values.persistence.enabled .Values.persistence.mountPath }}
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- end }}
{{- if .Values.volumeMounts }}
{{- toYaml .Values.volumeMounts | nindent 12 }}
{{- end }}
{{- end }}
{{- include "service.configuration" . | nindent 10}}
{{- include "service.nonsensitiveEnvVars" (list $global.Values.global .Values) | nindent 10 }}
{{- range $i, $container := .Values.sidecars }}
{{- $imageDict := fromYaml (include "image" $container) }}
{{- $container = mergeOverwrite $container $imageDict }}
{{- with omit $container "envFrom" "env" }}
- {{- toYaml . | nindent 10 }}
{{- include "service.configuration" $job | nindent 10}}
{{- end }}
{{- include "service.nonsensitiveEnvVars" (list $global.Values.global $job.Values $container) | nindent 10 }}
{{- end }}
initContainers:
{{- range $i, $container := .Values.initContainers }}
{{- $imageDict := fromYaml (include "image" $container) }}
{{- $container = mergeOverwrite $container $imageDict }}
{{- with omit $container "envFrom" "env" }}
- {{- toYaml . | nindent 10 }}
{{- include "service.configuration" $job | nindent 10}}
{{- end }}
{{- include "service.nonsensitiveEnvVars" (list $global.Values.global $job.Values $container) | nindent 10 }}
{{- end }}
dnsPolicy: {{ .Values.dnsPolicy }}
{{- if eq .Values.restartPolicy "Always" }}
restartPolicy: OnFailure
{{- else }}
restartPolicy: {{ .Values.restartPolicy }}
{{- end }}
{{- if or .Values.persistence.enabled .Values.volumes}}
volumes:
{{- if .Values.persistence.enabled }}
- name: data
persistentVolumeClaim:
claimName: {{ include "service.claimName" . }}
{{- end }}
{{- if .Values.volumes }}
{{- toYaml .Values.volumes | nindent 8 }}
{{- end }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- if .Values.topologySpreadConstraints }}
{{- with .Values.topologySpreadConstraints }}
topologySpreadConstraints:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- else }}
topologySpreadConstraints:
- maxSkew: 2
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
{{- include "service.selectorLabels" . | nindent 14 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
---
{{ if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "service.serviceAccountName" . }}
labels:
{{- include "service.labels" . | nindent 4 }}
{{- with mergeOverwrite
(dict)
.Values.annotations
.Values.serviceAccount.annotations
}}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
automountServiceAccountToken: {{ .Values.serviceAccount.automount }}
---
{{- end }}
{{- end }}
{{- end }}
2 changes: 1 addition & 1 deletion stack/tests/cronjob_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ tests:
- containsDocument:
apiVersion: v1
kind: ServiceAccount
name: "release-name-stack-job1"
name: "release-name-stack-job2"
not: true

122 changes: 122 additions & 0 deletions stack/tests/job_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json
suite: test jobs
templates:
- job.yaml
tests:
- it: should work
set:
jobs:
job1:
activeDeadlineSeconds: 300
backoffLimit: 2
completions: 5
parallelism: 3
serviceAccount:
create: true
annotations:
"eks.amazonaws.com/role-arn": some-role
image:
repository: my-repo
tag: sha-mytag
command: ["hello-world"]
args: ["arg1", "arg2"]
asserts:
- hasDocuments:
count: 2
- documentIndex: 0
containsDocument:
apiVersion: batch/v1
kind: Job
name: "release-name-stack-job1"
template: job.yaml
- documentIndex: 0
equal:
path: metadata.name
value: "release-name-stack-job1"
- documentIndex: 0
equal:
path: spec.template.spec.containers[0].image
value: "my-repo:sha-mytag"
- documentIndex: 0
equal:
path: spec.template.spec.containers[0].command
value: ["hello-world"]
- documentIndex: 0
equal:
path: spec.template.spec.containers[0].args
value: ["arg1", "arg2"]
- documentIndex: 0
equal:
path: spec.activeDeadlineSeconds
value: 300
- documentIndex: 0
equal:
path: spec.backoffLimit
value: 2
- documentIndex: 0
equal:
path: spec.completions
value: 5
- documentIndex: 0
equal:
path: spec.parallelism
value: 3
- documentIndex: 1
containsDocument:
apiVersion: v1
kind: ServiceAccount
name: "release-name-stack-job1"
- documentIndex: 1
equal:
path: metadata.name
value: "release-name-stack-job1"
- documentIndex: 1
equal:
path: metadata.annotations["eks.amazonaws.com/role-arn"]
value: "some-role"
- documentIndex: 1
equal:
path: metadata.labels["app.kubernetes.io/name"]
value: "stack"
- documentIndex: 1
equal:
path: metadata.labels["app.kubernetes.io/instance"]
value: "RELEASE-NAME"
- documentIndex: 1
equal:
path: metadata.labels["app.kubernetes.io/version"]
value: "1.16.0"
- documentIndex: 1
equal:
path: metadata.labels["app.kubernetes.io/managed-by"]
value: "Helm"
- documentIndex: 1
equal:
path: metadata.labels["app.kubernetes.io/service"]
value: "release-name-stack-job1"
- documentIndex: 1
equal:
path: automountServiceAccountToken
value: true
- it: should not make service account for job
set:
jobs:
job2:
image:
repository: my-repo
tag: sha-mytag
command: ["hello-world"]
args: ["arg1", "arg2"]
asserts:
- hasDocuments:
count: 1
- containsDocument:
apiVersion: batch/v1
kind: Job
name: "release-name-stack-job2"
template: job.yaml
- containsDocument:
apiVersion: v1
kind: ServiceAccount
name: "release-name-stack-job2"
not: true
5 changes: 5 additions & 0 deletions stack/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,11 @@
"type": "object",
"description": "Cron jobs to deploy, all values in the above global section are inherited by the cron jobs and each cron job can override them",
"default": {}
},
"jobs": {
"type": "object",
"description": "Jobs to deploy, all values in the above global section are inherited by the jobs and each job can override them",
"default": {}
}
}
}
18 changes: 18 additions & 0 deletions stack/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,21 @@ cronJobs:
# tag: "latest"
# command: ["command1", "command2"]
# args: ["arg1", "arg2"]

## @param jobs Jobs to deploy, all values in the above global section are inherited by the jobs and each job can override them
jobs:
{}
# job1:
# activeDeadlineSeconds: 300
# backoffLimit: 2
# completions: 5
# parallelism: 3
# serviceAccount:
# create: true
# annotations:
# "eks.amazonaws.com/role-arn": some-role
# image:
# repository: my-repo
# tag: sha-mytag
# command: ["hello-world"]
# args: ["arg1", "arg2"]