EDITD / kubetools

:nut_and_bolt: Kubetools is a tool and processes for developing and deploying microservices to Kubernetes.
MIT License
13 stars 2 forks source link

Add cron job object #118

Closed DilaraOflaz closed 2 years ago

DilaraOflaz commented 2 years ago

## Purpose of PR I added CronJob object to Kubetools.

## Parts of the app this will impact With this Branch we can run the following commands:

## How to test it

  1. if you have a Mac, you can test it locally by enabling Kubernetes in Docker-Desktop ( Open Docker -> Preferences -> Kubernetes -> Check Enable Kubernetes -> Apply & Restart) Otherwise, by pointing to the staging Kubernetes cluster

  2. To get the name of the context, run this following command: kubectl config current-context

  3. Checkout to this branch, create a python Virtual Environment, run the following command: pip install -e .[dev] Note: Run pip install -e ".[dev]" if you use zsh

  4. Create a kubetools.yml file. Content example:

name: my-app
namespace: cronjob-test
cronjobs:
  my-cronjob:
    schedule: "*/1 * * * *"
    concurrency_policy: "Replace"
    containers:
      hello:
        image: busybox
        command: [/bin/sh, -c, date; echo Hello from the Kubernetes cluster]
  1. Run this command: kubetools --context <CONTEXT NAME> deploy cronjob-test This command can take 20s to finish running (it waits until the first job is created)

  2. The deploy command should create the cronjob-test namespace and the my-cronjob CronJob, you can check by running this command: kubetools --context <CONTEXT NAME> show cronjob-test

  3. Run this command to delete the CronJob: kubetools --context <CONTEXT NAME> remove cronjob-test After running this command, if you run this following command kubetools --context <CONTEXT NAME> show cronjob-test, the output should be Nothing to be found here 👀!

  4. To clean the succeeded/completed pods, you can run this following command: kubetools --context <CONTEXT NAME> cleanup cronjob-test

## Additional information With the current code, the kubetools cleanup command cleans only if the pods (created by the CronJob) are orphaned. If the CronJob still exists (Not removed), even if the pods are completed they are not cleaned.

ryan109 commented 2 years ago

Before we merge this can we squash a lot of these commits? For example a lot of the "cleaning", and "testing" ones could be pushed into one or merged with feature commits. Sam will be covering exactly this in the git meeting tomorrow, but here's a general tutorial on it: https://www.baeldung.com/ops/git-squash-commits

It keeps the commit tree in a readable state when looking through history, so a commit should be named exactly as it is "Adding functionality X to support feature Y" - as an example.

Let me know if you have questions about it or want to go over it together.

DilaraOflaz commented 2 years ago

I found this parameter 'ttlSecondsAfterFinished': X, to remove the pods automatically X seconds after completion

I can add it inside the file kubetools/kubernetes/config/cronjob.py inside the cronjob spec: spec.jobTemplate.spec

So Kubernetes will handle the deletion of the completed pods, we won't have to modify the delete_cronjob function

Please let me know your thoughts ?

edmond-edited commented 2 years ago

PR looks good to me, I think we maybe we should also consider these parameters for job history retention spec.successfulJobsHistoryLimit and spec.failedJobsHistoryLimit? By default, successfulJobsHistoryLimit is set to 3 and failedJobsHistoryLimit is set to 1 for Kubernetes

ryan109 commented 2 years ago

Great stuff!