From 45b64da732cadf1c737e67c46be5fa090f2850d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Mi=C4=99dzybrodzki?= Date: Mon, 13 Jan 2025 15:58:06 +0100 Subject: [PATCH] feat: 0.5.0 add basic support for crons --- README.md | 4 +- charts/app/Chart.yaml | 2 +- charts/app/templates/_cron.tpl | 5 ++ charts/app/templates/cronjob.yaml | 47 +++++++++++++++++++ .../templates/{service.tpl => service.yaml} | 0 charts/app/values-example.yaml | 9 +++- charts/app/values.schema.json | 15 ++---- 7 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 charts/app/templates/_cron.tpl create mode 100644 charts/app/templates/cronjob.yaml rename charts/app/templates/{service.tpl => service.yaml} (100%) diff --git a/README.md b/README.md index 5975ef9..dc7dfd4 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,14 @@ appVersion: 1.0.0 dependencies: - name: app - version: 0.4.0 + version: 0.5.0 repository: https://wojciechem.github.io/platformex ``` For values please see [values-example.yaml](charts/app/values-example.yaml) ## TODO: - Chart - - [ ] Mimimal stable v1.0.0 + - [ ] Minimal stable v1.0.0 - [ ] Tests - [Ingress routes](charts/app/templates/_ingressroute.tpl) - [ ] custom service definitions for traefik ingressroutes (see https://doc.traefik.io/traefik/routing/providers/kubernetes-crd/#kind-ingressroute) diff --git a/charts/app/Chart.yaml b/charts/app/Chart.yaml index 16b7e55..c232251 100644 --- a/charts/app/Chart.yaml +++ b/charts/app/Chart.yaml @@ -5,7 +5,7 @@ description: Flexible helm chart to deploy your apps to kubernetes cluster. type: application # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.4.0 +version: 0.5.0 appVersion: 1.0.0 diff --git a/charts/app/templates/_cron.tpl b/charts/app/templates/_cron.tpl new file mode 100644 index 0000000..ac1877b --- /dev/null +++ b/charts/app/templates/_cron.tpl @@ -0,0 +1,5 @@ +{{- define "platformex.cron.suspend" }} +{{- if eq .instance.enableCrons false }} +suspend: "true" +{{- end }} +{{- end }} \ No newline at end of file diff --git a/charts/app/templates/cronjob.yaml b/charts/app/templates/cronjob.yaml new file mode 100644 index 0000000..20cf10b --- /dev/null +++ b/charts/app/templates/cronjob.yaml @@ -0,0 +1,47 @@ +apiVersion: v1 +kind: List +items: + {{- range $iname, $instance := .Values.instances }} + {{- range $cname, $cron := $instance.crons }} + {{- $cronContext := dict "iname" $iname "instance" $instance "name" $cname "deployment" $cron "root" $ }} + - apiVersion: batch/v1 + kind: CronJob + metadata: + name: {{ $iname }}-{{$cname}} + labels: + {{- include "platformex.kubernetesAppLabels" $cronContext | indent 10 }} + {{- include "platformex.globalLabels" $ | indent 10 }} + spec: + concurrencyPolicy: {{ $cron.concurrencyPolicy | default "Forbid" }} + failedJobsHistoryLimit: {{ $cron.failedJobsHistoryLimit | default 2 }} + schedule: {{ required "schedule is required!" $cron.schedule | quote }} + timeZone: {{ $cron.timezone | default "UTC" }} + {{- include "platformex.cron.suspend" $cronContext | indent 6 }} + startingDeadlineSeconds: {{ $cron.startingDeadlineSeconds | default 10 }} + successfulJobsHistoryLimit: {{ $cron.successfulJobsHistoryLimit | default 2 }} + jobTemplate: + spec: + backoffLimit: {{ $cron.jobTemplate.backoffLimit | default 2 }} + template: + metadata: + labels: + {{- include "platformex.kubernetesAppLabels" $cronContext | indent 16 }} + spec: + {{- include "platformex.deployment.volumes" $cronContext | indent 14 }} + {{- include "platformex.deployment.nodeSelector" $cronContext | indent 14 }} + {{- include "platformex.deployment.imagePullSecrets" $cronContext | indent 14 }} + restartPolicy: {{ $cron.restartPolicy | default "OnFailure" }} + {{- if $cron.initContainers }} + initContainers: + {{- range $icName, $initContainer := $cron.initContainers }} + {{- $icContext := dict "iname" $iname "instance" $instance "name" $icName "container" $initContainer "root" $ }} + - {{- include "platformex.containerFull" $icContext | indent 14 }} + {{- end }} + {{- end }} + containers: + {{- range $cName, $container := $cron.containers }} + {{- $containerContext := dict "iname" $iname "instance" $instance "name" $cName "container" $container "root" $ }} + - {{- include "platformex.containerFull" $cronContext | indent 14 }} + {{- end }} + {{- end }} + {{- end }} \ No newline at end of file diff --git a/charts/app/templates/service.tpl b/charts/app/templates/service.yaml similarity index 100% rename from charts/app/templates/service.tpl rename to charts/app/templates/service.yaml diff --git a/charts/app/values-example.yaml b/charts/app/values-example.yaml index 4c9570b..d7b6043 100644 --- a/charts/app/values-example.yaml +++ b/charts/app/values-example.yaml @@ -31,7 +31,14 @@ instances: default: enableCrons: true enableWorkers: true - crons: { } + crons: + legendary-cron-at-midnight: + concurrencyPolicy: "Replace" + failedJobsHistoryLimit: 2 + schedule: "0 0 * * *" + timezone: "Europe/Warsaw" + jobTemplate: { } + containers: { } deployments: consumer-xyz: worker: true diff --git a/charts/app/values.schema.json b/charts/app/values.schema.json index 3ac84c8..b9df999 100644 --- a/charts/app/values.schema.json +++ b/charts/app/values.schema.json @@ -51,16 +51,7 @@ }, "Cron": { "type": "object", - "additionalProperties": false, - "properties": { - "CRON_MODE": { - "type": "string", - "format": "integer" - } - }, - "required": [ - "CRON_MODE" - ], + "additionalProperties": true, "title": "Cron" }, "Global": { @@ -158,7 +149,9 @@ }, "Crons": { "type": "object", - "additionalProperties": false, + "additionalProperties": { + "$ref": "#/definitions/Cron" + }, "title": "Crons" }, "Deployments": {