Skip to content

Commit

Permalink
Merge pull request #152 from shoplineapp/feature/PLAT-878-argo-workfl…
Browse files Browse the repository at this point in the history
…ow-support-multi-step

Cronjob support multi step
  • Loading branch information
neal-sl authored Jun 27, 2024
2 parents 909e731 + a55a470 commit 09f8f6a
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 68 deletions.
2 changes: 1 addition & 1 deletion cronjob/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v1
description: Helm chart with simple cronjob template
name: cronjob
version: 0.8.0
version: 0.9.0
appVersion: 1.0.0
tillerVersion: ">=2.14.3"
97 changes: 31 additions & 66 deletions cronjob/templates/_argo_cron_workflow.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
counter:
# This increments the counter by 1
value: "1"
entrypoint: entry
entrypoint: {{ .Values.entrypoint }}
# If not exitNotifications config is set, the default exit-handler of the argo server will be used
{{- if .Values.exitNotifications }}
onExit: exit-handler
Expand All @@ -90,71 +90,28 @@
{{- end }}
{{- end }}
templates:
- name: entry
steps:
- - name: step1
template: template
{{- if and (.Values.job) (.Values.job.retries)}}
retryStrategy:
# Limit of retries if the job is fail
limit: {{ .Values.job.retries }}
{{- if .Values.job.retryPolicy }}
# Valid Value: "Always" | "OnFailure" | "OnError" | "OnTransientError", Default: "OnFailure"
retryPolicy: {{ .Values.job.retryPolicy }}
{{- end }}
{{- end }}
- name: template
metadata:
namespace: {{ .Release.Namespace }}
container:
image: '{{ required "image.repository must be provided" .Values.image.repository }}:{{ required "image.tag must be provided" .Values.image.tag }}'
{{- if .Values.command }}
# The command to call the function of the image
command: {{- toYaml ( .Values.command) | nindent 12 }}
{{- end }}
{{- if .Values.args }}
# The args need to pass for the function
args: {{- toYaml ( .Values.args) | nindent 12 }}
{{- end }}
{{- if .Values.resources }}
# The resource will be apply if "resource is set"
resources: {{- toYaml ( .Values.resources) | nindent 12 }}
{{- else }}
# default settings on resources
resources:
limits:
memory: "2Gi"
cpu: "1"
requests:
cpu: "300m"
memory: "1Gi"
{{- end }}
env:
- name: POD_NAME
value: {{ .Values.name }}
{{- range $key, $value := .Values.env }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
{{- range $key, $name := .Values.envSecrets }}
- name: {{ $key }}
valueFrom:
secretKeyRef:
name: {{ $name }}
key: {{ $key | quote }}
{{- end }}
# Apply .Values.envFrom if it is set
{{- if .Values.envFrom }}
envFrom:
{{- range .Values.envFrom.configMapRef }}
- configMapRef:
name: {{ . }}
{{- end }}
{{- range .Values.envFrom.secretRef }}
- secretRef:
name: {{ . }}
{{- end }}
{{- end }}

{{- if .Values.steps }}
{{- range .Values.steps }}
- {{- include "cronjob.argo_cron_workflow.step_template" . | nindent 8 }}
{{- end }}
{{- else }}
{{- /* if no steps, use single step */}}
{{- $defaultValue := merge (fromJson "{\"name\":\"entry\",\"steps\":[[{\"name\":\"step1\",\"template\":\"template\"}]]}") $.Values }}
- {{- include "cronjob.argo_cron_workflow.step_template" $defaultValue | nindent 8 }}
{{- end }}

{{- if .Values.containers }}
{{- range .Values.containers }}
{{ $value := list . $.Release.Namespace $.Values.name }}
- {{- include "cronjob.argo_cron_workflow.container_template" $value | nindent 8}}
{{- end }}
{{- else }}
{{- $defaultValue := merge (fromJson "{\"name\":\"template\"}") $.Values }}
{{- $value := list $defaultValue $.Release.Namespace $.Values.name }}
- {{- include "cronjob.argo_cron_workflow.container_template" $value | nindent 8 }}
{{- end }}

# The template of exist-handler if any .Values.exitNotifications config is set
{{- if .Values.exitNotifications }}
- name: exit-handler
Expand Down Expand Up @@ -212,6 +169,14 @@
{{- end }}
{{- end }}

{{- with .Values.suspends }}
{{- range . }}
- name: {{ .name }}
suspend:
duration: {{ .duration }}
{{- end}}
{{- end }}

{{- $ttl := .Values.ttlStrategy }}
{{- if $ttl }}
ttlStrategy:
Expand Down
74 changes: 74 additions & 0 deletions cronjob/templates/_argo_cron_workflow_container_template.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{{- define "cronjob.argo_cron_workflow.container_template" -}}
{{- $input := index . 0 -}}
{{- $namespace := index . 1 -}}
{{- $workflowName := index . 2 -}}
name: {{ $input.name }}
metadata:
namespace: {{ $namespace }}
{{- with $input.podSpecPatch }}
podSpecPatch: {{ quote . }}
{{- end }}
{{- with $input.inputs }}
inputs: {{ toYaml . | nindent 2 }}
{{- end }}
{{- with $input.outputs }}
outputs: {{ toYaml . | nindent 2 }}
{{- end }}
container:
image: {{ template "cronjob.argo_cron_workflow.image" $input.image }}
{{- with $input.command }}
command: {{- toYaml . | nindent 4 }}
{{- end }}
{{- with $input.args }}
args: {{- toYaml . | nindent 4 }}
{{- end }}
{{- if $input.resources }}
# The resource will be apply if "resource is set"
resources: {{- toYaml $input.resources | nindent 4 }}
{{- else }}
# default settings on resources
resources: {{- include "cronjob.argo_cron_workflow.default_resource" . | nindent 4 }}
{{- end }}
{{- with $input.securityContext }}
securityContext: {{ toYaml . | nindent 4 }}
{{- end }}
env:
- name: POD_NAME
value: {{ $workflowName }}
{{- range $key, $value := $input.env }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
{{- range $key, $name := $input.envSecrets }}
- name: {{ $key }}
valueFrom:
secretKeyRef:
name: {{ $name }}
key: {{ $key | quote }}
{{- end }}
# Apply .envFrom if it is set
{{- if $input.envFrom }}
envFrom:
{{- range $input.envFrom.configMapRef }}
- configMapRef:
name: {{ . }}
{{- end }}
{{- range $input.envFrom.secretRef }}
- secretRef:
name: {{ . }}
{{- end }}
{{- end }}
{{- end -}}

{{- define "cronjob.argo_cron_workflow.default_resource" -}}
limits:
memory: "2Gi"
cpu: "1"
requests:
cpu: "300m"
memory: "1Gi"
{{- end -}}

{{- define "cronjob.argo_cron_workflow.image" -}}
'{{ required "image.repository must be provided" .repository }}:{{ required "image.tag must be provided" .tag }}'
{{- end -}}
19 changes: 19 additions & 0 deletions cronjob/templates/_argo_cron_workflow_step_template.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- define "cronjob.argo_cron_workflow.step_template" -}}
name: {{ .name }}
{{- with .inputs }}
inputs: {{ toYaml . | nindent 2 }}
{{- end }}
{{- with .outputs }}
outputs: {{ toYaml . | nindent 2 }}
{{- end }}
steps: {{ toYaml .steps | nindent 2 }}
{{- if (.job).retries }}
retryStrategy:
# Limit of retries if the job is fail
limit: {{ .job.retries }}
{{- with .job.retryPolicy }}
# Valid Value: "Always" | "OnFailure" | "OnError" | "OnTransientError", Default: "OnFailure"
retryPolicy: {{ . }}
{{- end }}
{{- end }}
{{- end -}}
94 changes: 93 additions & 1 deletion cronjob/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ serviceaccount: {}
# This can be used to suspend the cron workflow by default, set this to true to suspend the cron workflow by default
suspend: false

# container-level security attributes
securityContext: {}

# Holds strategic merge patch to apply
# podSpecPatch: ""


pdb:
# If you just set "enable: true", default will set to "minAvailable: 9999" unless you have set other parameters.
enable: false
Expand All @@ -166,4 +173,89 @@ tolerations: []
# - key: "shop-callback-node"
# operator: "Equal"
# value: "true"
# effect: "NoSchedule"
# effect: "NoSchedule"

# Entrypoint for this argo workflow, default value: entry
entrypoint: "entry"

# Defined container template, used for multi-steps, will override the default container template. Only used on Argo cron workflow
containers: []
# schema:
# - name: Name for this container template, required
# - podSpecPatch: Holds strategic merge patch to apply, will override default podSpecPatch
# - inputs: Describe what inputs parameters and artifacts are supplied to this template, refer to https://argo-workflows.readthedocs.io/en/stable/fields/#inputs
# - outputs: Hold parameters, artifacts, and results, refer to https://argo-workflows.readthedocs.io/en/stable/fields/#outputs
# - image: Image used in this container
# - resources: Resource required for the job
# - securityContext: container-level security attributes
# - args: Arguments required to pass in function
# - command: Command to trigger the function
#
# example
# containers:
# - name: "example-container"
# podSpecPatch: '{"terminationGracePeriodSeconds":5}'
# inputs:
# parameters:
# - name: message
# outputs:
# parameters:
# - name: result
# valueFrom:
# path: /tmp/result.txt
# command:
# - bundle
# - exec
# - rails
# - test
# args:
# - "abc"
# resources:
# requests:
# cpu: "300m"
# memory: "2Gi"
# securityContext:
# runAsNonRoot: true

# Defined steps template, used for multi-steps, optional, Only used on Argo cron workflow
steps: []
# - name: entry
# steps:
# - - name: step1
# template: template
# schema:
# - name: Name for this container template, required
# - inputs: Describe what inputs parameters and artifacts are supplied to this template, refer to https://argo-workflows.readthedocs.io/en/stable/fields/#inputs
# - outputs: Hold parameters, artifacts, and results, refer to https://argo-workflows.readthedocs.io/en/stable/fields/#outputs
# - steps: Define a series of sequential/parallel workflow steps, refer to https://argo-workflows.readthedocs.io/en/stable/fields/#workflowstep
# - job: Retry strategy for this step, object
# - job.retries: Maximum number of retries if error occurred
# - job.retryPolicy: Retry Policy of the cronjob
#
# example
# steps:
# - name: "step-1"
# inputs:
# parameters:
# - name: message
# outputs:
# parameters:
# - name: result
# valueFrom:
# path: /tmp/result.txt
# steps:
# - - name: "step-1-1"
# template: "example-container"
# job:
# limit: 10
# retryPolicy: OnFailure

# Defined suspends template, used for multi-steps and create suspend step
suspends: []
# schema:
# - name: name for this suspends template
# - duration: time to suspends
# example
# suspends:
# - name: "sleep-60s"
# duration: "60"

0 comments on commit 09f8f6a

Please sign in to comment.