From 464bd6dcc5c8f37e99ca59ad4a764372fa56ab73 Mon Sep 17 00:00:00 2001 From: Bian <717098+masterginger@users.noreply.github.com> Date: Wed, 29 Nov 2023 15:54:18 -0500 Subject: [PATCH] feat(charts/simple-workflow): Workflow Janitor (#248) --- .github/workflows/ci.yaml | 2 +- charts/simple-workflow/Chart.yaml | 2 +- charts/simple-workflow/README.md | 2 +- .../templates/workflow-janitor.yaml | 39 +++++++++++++++++++ .../templates/workflow-pipeline.yaml | 8 +++- 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 charts/simple-workflow/templates/workflow-janitor.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 63c523a8..969f631c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -31,7 +31,7 @@ jobs: - name: Set up Helm uses: azure/setup-helm@v3 with: - version: v3.5.0 + version: v3.12.2 - uses: actions/setup-python@v4 with: diff --git a/charts/simple-workflow/Chart.yaml b/charts/simple-workflow/Chart.yaml index 517e5b66..81be09c6 100644 --- a/charts/simple-workflow/Chart.yaml +++ b/charts/simple-workflow/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 name: simple-workflow description: Default Argo Workflow Helm Chart type: application -version: 0.0.11 +version: 0.0.12 appVersion: latest maintainers: - name: masterginger diff --git a/charts/simple-workflow/README.md b/charts/simple-workflow/README.md index 75bd1125..63e7afe7 100644 --- a/charts/simple-workflow/README.md +++ b/charts/simple-workflow/README.md @@ -2,7 +2,7 @@ Default Argo Workflow Helm Chart -![Version: 0.0.11](https://img.shields.io/badge/Version-0.0.11-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: latest](https://img.shields.io/badge/AppVersion-latest-informational?style=flat-square) +![Version: 0.0.12](https://img.shields.io/badge/Version-0.0.12-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: latest](https://img.shields.io/badge/AppVersion-latest-informational?style=flat-square) ## Values diff --git a/charts/simple-workflow/templates/workflow-janitor.yaml b/charts/simple-workflow/templates/workflow-janitor.yaml new file mode 100644 index 00000000..79b24789 --- /dev/null +++ b/charts/simple-workflow/templates/workflow-janitor.yaml @@ -0,0 +1,39 @@ +{{- if and .Values.workflowJanitor .Values.workflowJanitor.enabled }} +apiVersion: batch/v1 +kind: Job +metadata: + name: {{ .Release.Name }}-keep-recent-n-workflows + namespace: {{ .Release.Namespace }} + annotations: + argocd.argoproj.io/hook: PreSync + argocd.argoproj.io/hook-delete-policy: BeforeHookCreation + argocd.argoproj.io/sync-wave: "-1" +spec: + backoffLimit: 1 + template: + metadata: + labels: + mesh.nextdoor.com/dropMetrics: 'true' + annotations: + sidecar.istio.io/inject: 'false' + spec: + serviceAccountName: {{ .Values.workflow.serviceAccount.name }} + restartPolicy: Never + activeDeadlineSeconds: 120 + containers: + - name: workflow-janitor + image: bitnami/kubectl:1.28.4 + command: ["/bin/bash"] + args: + - -c + - >- + kubectl get workflow --sort-by=.metadata.creationTimestamp && + echo "Total workflows: $(kubectl get workflow --sort-by=.metadata.creationTimestamp -o go-template --template {{ "'{{" }}range .items}}{{ "{{" }}.metadata.name}}{{ "{{" }}"\n"}}{{ "{{" }}end}}' | wc -l)" && + echo "Keeping {{ .Values.workflowJanitor.keepRecent | default 30 }}" && + while [[ $(kubectl get workflow --sort-by=.metadata.creationTimestamp -o go-template --template {{ "'{{" }}range .items}}{{ "{{" }}.metadata.name}}{{ "{{" }}"\n"}}{{ "{{" }}end}}' | wc -l) -gt {{ .Values.workflowJanitor.keepRecent | default 30 }} ]]; + do + sleep 3; + kubectl delete workflow $(kubectl get workflow --sort-by=.metadata.creationTimestamp -o go-template --template {{ "'{{" }}range .items}}{{ "{{" }}.metadata.name}}{{ "{{" }}"\n"}}{{ "{{" }}end}}' | head -n 1); + kubectl get workflow --sort-by=.metadata.creationTimestamp; + done +{{- end }} diff --git a/charts/simple-workflow/templates/workflow-pipeline.yaml b/charts/simple-workflow/templates/workflow-pipeline.yaml index 6551a61b..4395fabb 100644 --- a/charts/simple-workflow/templates/workflow-pipeline.yaml +++ b/charts/simple-workflow/templates/workflow-pipeline.yaml @@ -11,7 +11,13 @@ metadata: {{- tpl (toYaml .) $ | nindent 4 }} {{- end }} spec: - {{- if .Values.workflow.ttlStrategy }} + {{- if and .Values.workflowJanitor .Values.workflowJanitor.enabled }} + # Set a 30-day ttl because workflow janitor is enabled + ttlStrategy: + secondsAfterCompletion: 2592000 + secondsAfterFailure: 2592000 + secondsAfterSuccess: 2592000 + {{- else if .Values.workflow.ttlStrategy }} ttlStrategy: {{- with $.Values.workflow.ttlStrategy }} {{- tpl (toYaml .) $ | nindent 4 }}