Skip to content

Commit ac1e15c

Browse files
committed
feat: ✨ add job command override support
1 parent ac66dd0 commit ac1e15c

File tree

9 files changed

+67
-68
lines changed

9 files changed

+67
-68
lines changed

charts/dso-ansible-job/templates/cronjob.yaml

Lines changed: 0 additions & 11 deletions
This file was deleted.

charts/dso-ansible-job/templates/job.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apiVersion: v2
2-
name: cpn-ansible-job
3-
description: Creates Job for Socle services post-configuration.
2+
name: cpn-job
3+
description: Creates Job in Kubernetes cluster.
44
type: application
5-
version: 1.6.0
5+
version: 1.0.0
66
appVersion: 1.1.0
77
maintainers:
88
- name: cloud-pi-native
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# cpn-ansible-job
1+
# cpn-job
22

3-
![Version: 1.6.0](https://img.shields.io/badge/Version-1.6.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 1.1.0](https://img.shields.io/badge/AppVersion-1.1.0-informational?style=flat-square)
3+
![Version: 1.0.0](https://img.shields.io/badge/Version-1.0.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 1.1.0](https://img.shields.io/badge/AppVersion-1.1.0-informational?style=flat-square)
44

5-
Creates Job for Socle services post-configuration.
5+
Creates Job in Kubernetes cluster.
66

77
## Maintainers
88

@@ -14,27 +14,25 @@ Creates Job for Socle services post-configuration.
1414

1515
| Key | Type | Default | Description |
1616
|-----|------|---------|-------------|
17+
| cronJob.command | list | `[]` | Command to run in the job container. |
1718
| cronJob.create | bool | `false` | Create a cronJob. |
18-
| cronJob.name | string | `""` | Playbook name if different from job. |
1919
| cronJob.schedule | string | `"0 */6 * * *"` | CronJob schedule. |
2020
| fullnameOverride | string | `""` | String to fully override the default application name. |
2121
| global.platform | string | `"kubernetes"` | For security context depending of the cluster distribution. |
2222
| job.activeDeadlineSeconds | string | `nil` | Optional maximum time in seconds before de Pod is killed (no matter of the backoffLimit). |
2323
| job.annotations | object | `{"argocd.argoproj.io/hook":"PostSync","argocd.argoproj.io/hook-delete-policy":"HookSucceeded"}` | Kubernetes annotations to apply to the job resource. |
2424
| job.backoffLimit | int | `6` | Number of retries before setting de Job status to failed. |
25-
| job.dscName | string | `nil` | Name of the DsoSocleConfig to use as source-of-truth. |
25+
| job.command | list | `[]` | Command to run in the job container. |
2626
| job.extraEnv | Optional | `[]` | Extra environment variables to pass to the job container. |
2727
| job.extraVolumeMounts | Optional | `[]` | Extra volume mounts for the job container. |
2828
| job.extraVolumes | Optional | `[]` | Extra volumes to mount into the job container. |
2929
| job.image.pullPolicy | string | `"IfNotPresent"` | Pull policy for the job image. |
30-
| job.image.repository | string | `"ghcr.io/cloud-pi-native/git-ansible"` | Repository to use for the job. |
30+
| job.image.repository | string | `""` | Repository to use for the job. |
3131
| job.image.tag | string | `""` | Tag to use for the job. # Overrides the image tag whose default is the chart appVersion. |
32-
| job.name | string | `"example-playbook"` | Playbook name. |
3332
| job.restartPolicy | string | `"OnFailure"` | Job restart policy (e.g., Never or OnFailure). |
3433
| job.serviceAccount.annotations | object | `{}` | Annotations applied to created service account. |
3534
| job.serviceAccount.create | bool | `true` | Create a service account for the job. |
3635
| job.serviceAccount.name | string | `""` | Service account name. |
37-
| job.targetRevision | string | `"main"` | Git branch or tag to checkout before applying the Ansible playbook. |
3836
| nameOverride | string | `""` | Provide a name in place of the default application name. |
3937

4038
----------------------------------------------

charts/dso-ansible-job/templates/_helpers.tpl renamed to charts/dso-job/templates/_helpers.tpl

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{{/*
22
Expand the name of the chart.
33
*/}}
4-
{{- define "cpnAnsibleJob.name" -}}
4+
{{- define "cpnJob.name" -}}
55
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
66
{{- end }}
77

88

99
{{/*
1010
Create chart name and version as used by the chart label.
1111
*/}}
12-
{{- define "cpnAnsibleJob.chart" -}}
12+
{{- define "cpnJob.chart" -}}
1313
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
1414
{{- end }}
1515

@@ -19,7 +19,7 @@ Create a default fully qualified app name.
1919
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
2020
If release name contains chart name it will be used as a full name.
2121
*/}}
22-
{{- define "cpnAnsibleJob.fullname" -}}
22+
{{- define "cpnJob.fullname" -}}
2323
{{- if .Values.fullnameOverride }}
2424
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
2525
{{- else }}
@@ -36,15 +36,19 @@ If release name contains chart name it will be used as a full name.
3636
{{/*
3737
Create the name of the service account to use
3838
*/}}
39-
{{- define "cpnAnsibleJob.job.serviceAccountName" -}}
40-
{{- default (include "cpnAnsibleJob.name" .) .Values.job.serviceAccount.name }}
39+
{{- define "cpnJob.job.serviceAccountName" -}}
40+
{{- default (include "cpnJob.name" .) .Values.job.serviceAccount.name }}
4141
{{- end }}
4242

4343

4444
{{/*
4545
Generate the common job pod spec used in both Job and CronJob templates.
46+
This helper accepts an optional dict to override parts of the pod spec,
47+
such as the command.
48+
Usage:
49+
{{ include "cpnJob.jobPodSpec" (dict "Values" .Values "Chart" .Chart "command" .Values.cronJob.command) }}
4650
*/}}
47-
{{- define "cpnAnsibleJob.jobPodSpec" -}}
51+
{{- define "cpnJob.jobPodSpec" -}}
4852
spec:
4953
{{- if .Values.job.activeDeadlineSeconds }}
5054
activeDeadlineSeconds: {{ .Values.job.activeDeadlineSeconds }}
@@ -53,26 +57,22 @@ spec:
5357
template:
5458
metadata:
5559
labels:
56-
{{- include "cpnAnsibleJob.job.selectorLabels" . | nindent 8 }}
60+
{{- include "cpnJob.job.selectorLabels" . | nindent 8 }}
5761
{{- with .Values.job.podLabels }}
5862
{{- toYaml . | nindent 8 }}
5963
{{- end }}
6064
spec:
61-
serviceAccountName: {{ include "cpnAnsibleJob.job.serviceAccountName" . | default .Values.job.serviceAccount.name}}
65+
serviceAccountName: {{ include "cpnJob.job.serviceAccountName" . | default .Values.job.serviceAccount.name }}
6266
restartPolicy: {{ .Values.job.restartPolicy }}
6367
containers:
64-
- name: {{ .playbookName }}-post-conf
68+
- name: dso-job
6569
image: {{ .Values.job.image.repository }}:{{ .Values.job.image.tag | default .Chart.AppVersion }}
6670
imagePullPolicy: {{ .Values.job.image.pullPolicy }}
71+
{{- with .command | default .Values.job.command }}
6772
command:
68-
- /bin/sh
69-
- -c
70-
- |
71-
git clone https://github.com/cloud-pi-native/socle.git && \
72-
cd socle && \
73-
git checkout {{ .Values.job.targetRevision }} && \
74-
ansible-playbook post-install/{{ .playbookName }}.yaml -e dsc_cr={{ .Values.job.dscName }}
75-
{{- include "cpnAnsibleJob.fullSecurityContext" . | indent 10 }}
73+
{{- toYaml . | nindent 12 }}
74+
{{- end }}
75+
{{- include "cpnJob.fullSecurityContext" . | indent 10 }}
7676
{{- with .Values.job.extraEnv }}
7777
env:
7878
{{- toYaml . | nindent 12 }}
@@ -91,7 +91,7 @@ spec:
9191
{{/*
9292
Add securityContext depending on platform.
9393
*/}}
94-
{{- define "cpnAnsibleJob.fullSecurityContext" -}}
94+
{{- define "cpnJob.fullSecurityContext" -}}
9595
{{- if eq .Values.global.platform "kubernetes" }}
9696
securityContext:
9797
allowPrivilegeEscalation: false
@@ -110,24 +110,24 @@ securityContext:
110110
{{/*
111111
Common labels
112112
*/}}
113-
{{- define "cpnAnsibleJob.common.labels" -}}
114-
helm.sh/chart: {{ include "cpnAnsibleJob.chart" . }}
113+
{{- define "cpnJob.common.labels" -}}
114+
helm.sh/chart: {{ include "cpnJob.chart" . }}
115115
{{- if .Chart.AppVersion }}
116116
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
117117
{{- end }}
118118
app.kubernetes.io/managed-by: {{ .Release.Service }}
119119
{{- end }}
120120

121-
{{- define "cpnAnsibleJob.job.labels" -}}
122-
{{ include "cpnAnsibleJob.common.labels" . }}
123-
{{ include "cpnAnsibleJob.job.selectorLabels" . }}
121+
{{- define "cpnJob.job.labels" -}}
122+
{{ include "cpnJob.common.labels" . }}
123+
{{ include "cpnJob.job.selectorLabels" . }}
124124
{{- end }}
125125

126126

127127
{{/*
128128
Selector labels
129129
*/}}
130-
{{- define "cpnAnsibleJob.job.selectorLabels" -}}
131-
app.kubernetes.io/name: {{ include "cpnAnsibleJob.name" . }}
130+
{{- define "cpnJob.job.selectorLabels" -}}
131+
app.kubernetes.io/name: {{ include "cpnJob.name" . }}
132132
app.kubernetes.io/instance: {{ .Release.Name }}
133133
{{- end }}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{{- if .Values.cronJob.create -}}
2+
apiVersion: batch/v1
3+
kind: CronJob
4+
metadata:
5+
name: {{ include "cpnJob.fullname" . }}
6+
spec:
7+
schedule: {{ .Values.cronJob.schedule }}
8+
jobTemplate:
9+
{{ include "cpnJob.jobPodSpec" (dict "Values" .Values "Chart" .Chart "Release" .Release "command" .Values.cronJob.command) | indent 4 }}
10+
{{- end }}

charts/dso-job/templates/job.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: {{ include "cpnJob.fullname" . }}
5+
annotations:
6+
{{- range $key, $value := .Values.job.annotations }}
7+
{{ $key }}: {{ $value | quote }}
8+
{{- end }}
9+
{{ include "cpnJob.jobPodSpec" (dict "Values" .Values "Chart" .Chart "Release" .Release) }}

charts/dso-ansible-job/templates/serviceaccount.yaml renamed to charts/dso-job/templates/serviceaccount.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
apiVersion: v1
33
kind: ServiceAccount
44
metadata:
5-
name: {{ include "cpnAnsibleJob.job.serviceAccountName" . }}
5+
name: {{ include "cpnJob.job.serviceAccountName" . }}
66
labels:
7-
{{- include "cpnAnsibleJob.job.labels" . | nindent 4 }}
7+
{{- include "cpnJob.job.labels" . | nindent 4 }}
88
{{- with .Values.job.serviceAccount.annotations }}
99
annotations:
1010
{{- toYaml . | nindent 4 }}
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ global:
99

1010
# Job configuration.
1111
job:
12-
# -- Playbook name.
13-
name: "example-playbook"
14-
# -- (string) Name of the DsoSocleConfig to use as source-of-truth.
15-
dscName: null
16-
# -- Git branch or tag to checkout before applying the Ansible playbook.
17-
targetRevision: "main"
1812
serviceAccount:
1913
# -- Create a service account for the job.
2014
create: true
@@ -30,12 +24,17 @@ job:
3024
backoffLimit: 6
3125
image:
3226
# -- Repository to use for the job.
33-
repository: ghcr.io/cloud-pi-native/git-ansible
27+
repository: ""
3428
# -- Tag to use for the job.
3529
## Overrides the image tag whose default is the chart appVersion.
3630
tag: ""
3731
# -- Pull policy for the job image.
3832
pullPolicy: IfNotPresent
33+
# -- Command to run in the job container.
34+
command: []
35+
# - /bin/sh
36+
# - -c
37+
# - "echo hello"
3938
# -- Kubernetes annotations to apply to the job resource.
4039
annotations:
4140
argocd.argoproj.io/hook: PostSync
@@ -58,7 +57,11 @@ job:
5857
cronJob:
5958
# -- Create a cronJob.
6059
create: false
61-
# -- Playbook name if different from job.
62-
name: ""
6360
# -- CronJob schedule.
6461
schedule: "0 */6 * * *"
62+
# -- Command to run in the job container.
63+
command: []
64+
# - /bin/sh
65+
# - -c
66+
# - "echo hello"
67+
# -- Kubernetes annotations to apply to the job resource.

0 commit comments

Comments
 (0)