cronjobs

by SickHub

Helm chartv0.4.1App latestVerified publisher

A generic helm cronjob chart for kubernetes

About this chart

cronjobs Helm Chart

A chart that lets you setup one or multiple jobs with ease.

Components:

  • Per job, one busybox (or your image) pod that runs at scheduled times
  • Optional configMaps that contain your scripts

Deploy cronjobs

helm repo add sickhub https://sickhub.github.io/charts
helm repo update
helm search repo sickhub
helm upgrade --create-namespace --namespace test --install --values values.yaml my-jobs sickhub/cronjobs

Setup

Setup multiple simple cronjobs

  • You can use your own images, e.g. simple, small, task based images
  • You can run simple shell commands out-of-the-box with command and args
  • You can include small scripts through configMaps and run them on a generic image

values.yaml:

jobs:
  job1:
    schedule: "*/5 * * * *"
    command: ["/bin/sh"]
    args:
      - "/configMaps/scripts/script.sh"
  curl-30-min:
    schedule: "*/30 * * * *"
    image:
      registry: docker.io
      repository: alpine
      tag: 3.13
    command:
      - "/bin/sh"
      - "-c"
    args:
      - "apk add --no-cache curl; curl -I https://google.com"

ConfigMaps and Secrets for scripts and data

  • mounted as /configMaps/{{ name }} and /secrets/{{ name }} respectively

Existing configMap

Use an existing configMap in your cronjobs, it will be available at /configMaps/global-configmap/

customConfigMap: global-configmap

Files from strings

Hint: you can load larger files from separate yaml files.

Example: helm template example cronjobs --debug --values cronjobs/ci/scripts-values.yaml,cronjobs/ci/config-test.yaml

configMaps:
  scripts:
    data:
      script.sh: |-
        #!/bin/sh
        echo "I am started in Job1"
        sleep 5

Files from files which are within the chart directory

Helm can access files, but only when they are within the chart directory, see https://helm.sh/docs/chart_template_guide/accessing_files/

configMaps:
  from-files:
    files:
      example.sh: ci/files/example.sh

secrets:
  ssh-creds:
    files:
      id-rsa.pub: ci/files/id-rsa.pub

Kubernetes API Permissions

By default, a Kubernetes ServiceAccount object is created and used by the Pods that are generated by the Cronjobs/Jobs. Optionally, you can attach a Role or ClusterRole to this ServiceAccount in order to grant permissions within Kubernetes, e.g., to restart deployments.

role:
  create: true
  rules:
  - apiGroups: ["", "extensions", "apps"]
    resources: ["deployments", "replicasets", "pods", "pods/attach", "pods/exec", "pods/log"]
    verbs: ["list", "get", "watch", "create", "update", "patch", "delete"]

Missing features - help appreciated

  • [ ] allow overwriting nodeSelector, affinity, resources, ...
  • [x] add support for scripts + secrets via ConfigMap and Secret
  • [ ] add support for volumes + volumeMounts
  • [ ] add some specific examples

Contribute

  • Create issues: Be specific. What do you expect? How do you suggest we get there?
  • Create pull requests: Don't ask, just create a PR. Small improvements at a time please.
  • See Contributing

Testing

  • Run chart-testing locally (requires brew install chart-testing yamllint; pip3 install yamale)
helm repo add common https://charts.bitnami.com/bitnami
ct lint --remote origin --chart-dirs=$PWD/sickhub --all
  • Run chart-testing in docker
docker run --rm -it -v $PWD:/data quay.io/helmpack/chart-testing  bash -c \
  "helm repo add common https://charts.bitnami.com/bitnami; ct lint --remote origin --chart-dirs=/data/sickhub --all"

Credits

Deploy cronjobs with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has cronjobs on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartcronjobs is pinned at v0.4.1 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply