kubernetesterraformkubernetes-helmkubernetes-jobs

Can I use helm for fire and forget k8s Jobs?


How do I use helm to create an ephemeral Kubernetes Job that is "gone" after completion.

Currently, we have a big ol yaml string in a terraform HCL .tf file that gets passed to an in house orchestrator that uses python kubernetes client to create the Job.

I've always felt that variable k8s resources should be defined in helm charts instead of directly in terraform. This is already the case for every other resource type. I know about using helm hooks for Jobs, but this isn't about some lifecycle hook, it's an arbitrary trigger in a fire-and-forget fashion. Argo Workflows seems really heavy for this use case and would be better as a wholesale replacement for our orchestrator, but baby steps. Is this possible with helm?

PS: I don't buy comparisons with other package manages like apt as helm is also a template engine which absolutely makes sense for this use case.


Solution

  • It seems to be enough to set the ttlSecondsAfterFinished: property of your Job.

    # templates/job.yaml
    apiVersion: batch/v1
    kind: Job
    spec:
      ttlSecondsAfterFinished: 5  # or 0 or {{ .Values.jobRetentionSeconds }}
    

    Running helm upgrade --install on a chart containing this will recreate the Job if it doesn't exist, and then the ttlSecondsAfterFinished: second will cause the Job to delete itself after it completes.

    Helm has a couple of features that you don't especially care about here: helm history your-job-chart shows you when it's been run, and helm get manifest your-job-chart will show you the Job YAML. You can ignore these, though they'll take up a small amount of space in the cluster. If you're not using Helm's templating either, then kubectl apply might be more straightforward.