Cleaning kubernetes jobs

7/3/2020

I have a spinnaker pipeline that deploys a db-job to k8s. I would like to be able to delete the job before deploying another one, i.e. to add a spinnaker stage or to somehow configure job so it deletes itself. I know that cronjob would be great for it, but it is in beta and not stable enough to be used for db operations. I have tried to add a stage to spinnaker like this:

{
        "account": "k8s-devops-v2",
        "cloudProvider": "kubernetes",
        "location": "loc",
        "manifestArtifactAccount": "loc-open-source",
        "manifestName": "job my-job-name",
        "mode": "static",
        "name": "Delete Db job",
        "options": {
          "cascading": true,
          "gracePeriodSeconds": null
        },
        "type": "deleteManifest"
      }

but it won't work. I also don't want to use ttl because I wan't the latest job to be present until the new one is created. Are there any other options? What is the best practice for this?

-- Jovana Mihaljcic
devops
kubernetes
kubernetes-jobs
spinnaker

1 Answer

7/21/2020

Depending on what version of kubernetes you're running you can do this in the k8s job itself: https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#jobs-history-limits. The caveat is that once deleted unless you collect the logs via some kind of scraper, you won't be able to see what the job did unless you get to the cluster in time.

-- chinikins
Source: StackOverflow