From 5645c578599b7dc953fd9926e0e92bdcf0e383fe Mon Sep 17 00:00:00 2001 From: xg0719 Date: Sun, 5 Jun 2022 14:47:10 +0800 Subject: [PATCH] add a cronjob crud example --- examples/cronjob_crud.py | 130 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 examples/cronjob_crud.py diff --git a/examples/cronjob_crud.py b/examples/cronjob_crud.py new file mode 100644 index 0000000000..835573d7eb --- /dev/null +++ b/examples/cronjob_crud.py @@ -0,0 +1,130 @@ +#!/usr/bin/python3 +# -*- coding:utf-8 -*- + +import json +import time + +from kubernetes import client, config + +config.load_kube_config() + + +def create_namespaced_cron_job(namespace='default', body=None): + cronjob_json = body + if body is None: + print('body is required!') + exit(0) + name = body['metadata']['name'] + if judge_crontab_exists(namespace, name): + print(f'{name} exists, please do not repeat!') + else: + v1 = client.BatchV1beta1Api() + ret = v1.create_namespaced_cron_job(namespace=namespace, body=cronjob_json, pretty=True, + _preload_content=False, async_req=False) + ret_dict = json.loads(ret.data) + print(f'create succeed\n{json.dumps(ret_dict)}') + + +def delete_namespaced_cron_job(namespace='default', name=None): + if name is None: + print('name is required!') + exit(0) + if not judge_crontab_exists(namespace, name): + print(f"{name} doesn't exists, please enter a new one!") + else: + v1 = client.BatchV1beta1Api() + ret = v1.delete_namespaced_cron_job(name=name, namespace=namespace, _preload_content=False, async_req=False) + ret_dict = json.loads(ret.data) + print(f'delete succeed\n{json.dumps(ret_dict)}') + + +def patch_namespaced_cron_job(namespace='default', body=None): + cronjob_json = body + if body is None: + print('body is required!') + exit(0) + name = body['metadata']['name'] + if judge_crontab_exists(namespace, name): + v1 = client.BatchV1beta1Api() + ret = v1.patch_namespaced_cron_job(name=name, namespace=namespace, body=cronjob_json, + _preload_content=False, async_req=False) + ret_dict = json.loads(ret.data) + print(f'patch succeed\n{json.dumps(ret_dict)}') + else: + print(f"{name} doesn't exists, please enter a new one!") + + +def get_cronjob_list(namespace='default'): + v1 = client.BatchV1beta1Api() + ret = v1.list_namespaced_cron_job(namespace=namespace, pretty=True, _preload_content=False) + cron_job_list = json.loads(ret.data) + print(f'cronjob number={len(cron_job_list["items"])}') + # for cron in cron_job_list['items']: + # print(json.dumps(cron)) + return cron_job_list["items"] + + +def judge_crontab_exists(namespace, name): + cron_job_list = get_cronjob_list(namespace) + for cron_job in cron_job_list: + if name == cron_job['metadata']['name']: + return True + return False + + +def get_cronjob_body(namespace, name, command): + body = { + "apiVersion": "batch/v1beta1", + "kind": "CronJob", + "metadata": { + "name": name, + "namespace": namespace + }, + "spec": { + "schedule": "*/1 * * * *", + "concurrencyPolicy": "Allow", + "suspend": False, + "jobTemplate": { + "spec": { + "template": { + "spec": { + "containers": [ + { + "name": name, + "image": "busybox:1.35", + "command": command + } + ], + "restartPolicy": "Never" + } + } + } + }, + "successfulJobsHistoryLimit": 3, + "failedJobsHistoryLimit": 1 + } + } + return body + + +if __name__ == '__main__': + # get + cronjob_list = get_cronjob_list() + + # delete + delete_namespaced_cron_job('default', 'hostname') + time.sleep(2) + + # create + container_command = [ + "/bin/sh", + "-c", + "date; echo Hello from the Kubernetes cluster; hostname" + ] + hostname_json = get_cronjob_body('default', 'hostname', container_command) + create_namespaced_cron_job('default', hostname_json) + + # update + container_command[2] = "date; echo this is patch; hostname" + hostname_json = get_cronjob_body('default', 'hostname', container_command) + patch_namespaced_cron_job('default', hostname_json)