|
| 1 | +#!/usr/bin/python3 |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | + |
| 4 | +import json |
| 5 | +import time |
| 6 | + |
| 7 | +from kubernetes import client, config |
| 8 | + |
| 9 | +config.load_kube_config() |
| 10 | + |
| 11 | + |
| 12 | +def create_namespaced_cron_job(namespace='default', body=None): |
| 13 | + cronjob_json = body |
| 14 | + if body is None: |
| 15 | + print('body is required!') |
| 16 | + exit(0) |
| 17 | + name = body['metadata']['name'] |
| 18 | + if judge_crontab_exists(namespace, name): |
| 19 | + print(f'{name} exists, please do not repeat!') |
| 20 | + else: |
| 21 | + v1 = client.BatchV1Api() |
| 22 | + ret = v1.create_namespaced_cron_job(namespace=namespace, body=cronjob_json, pretty=True, |
| 23 | + _preload_content=False, async_req=False) |
| 24 | + ret_dict = json.loads(ret.data) |
| 25 | + print(f'create succeed\n{json.dumps(ret_dict)}') |
| 26 | + |
| 27 | + |
| 28 | +def delete_namespaced_cron_job(namespace='default', name=None): |
| 29 | + if name is None: |
| 30 | + print('name is required!') |
| 31 | + exit(0) |
| 32 | + if not judge_crontab_exists(namespace, name): |
| 33 | + print(f"{name} doesn't exists, please enter a new one!") |
| 34 | + else: |
| 35 | + v1 = client.BatchV1Api() |
| 36 | + ret = v1.delete_namespaced_cron_job(name=name, namespace=namespace, _preload_content=False, async_req=False) |
| 37 | + ret_dict = json.loads(ret.data) |
| 38 | + print(f'delete succeed\n{json.dumps(ret_dict)}') |
| 39 | + |
| 40 | + |
| 41 | +def patch_namespaced_cron_job(namespace='default', body=None): |
| 42 | + cronjob_json = body |
| 43 | + if body is None: |
| 44 | + print('body is required!') |
| 45 | + exit(0) |
| 46 | + name = body['metadata']['name'] |
| 47 | + if judge_crontab_exists(namespace, name): |
| 48 | + v1 = client.BatchV1Api() |
| 49 | + ret = v1.patch_namespaced_cron_job(name=name, namespace=namespace, body=cronjob_json, |
| 50 | + _preload_content=False, async_req=False) |
| 51 | + ret_dict = json.loads(ret.data) |
| 52 | + print(f'patch succeed\n{json.dumps(ret_dict)}') |
| 53 | + else: |
| 54 | + print(f"{name} doesn't exists, please enter a new one!") |
| 55 | + |
| 56 | + |
| 57 | +def get_cronjob_list(namespace='default'): |
| 58 | + v1 = client.BatchV1Api() |
| 59 | + ret = v1.list_namespaced_cron_job(namespace=namespace, pretty=True, _preload_content=False) |
| 60 | + cron_job_list = json.loads(ret.data) |
| 61 | + print(f'cronjob number={len(cron_job_list["items"])}') |
| 62 | + return cron_job_list["items"] |
| 63 | + |
| 64 | + |
| 65 | +def judge_crontab_exists(namespace, name): |
| 66 | + cron_job_list = get_cronjob_list(namespace) |
| 67 | + for cron_job in cron_job_list: |
| 68 | + if name == cron_job['metadata']['name']: |
| 69 | + return True |
| 70 | + return False |
| 71 | + |
| 72 | + |
| 73 | +def get_cronjob_body(namespace, name, command): |
| 74 | + body = { |
| 75 | + "apiVersion": "batch/v1", |
| 76 | + "kind": "CronJob", |
| 77 | + "metadata": { |
| 78 | + "name": name, |
| 79 | + "namespace": namespace |
| 80 | + }, |
| 81 | + "spec": { |
| 82 | + "schedule": "*/1 * * * *", |
| 83 | + "concurrencyPolicy": "Allow", |
| 84 | + "suspend": False, |
| 85 | + "jobTemplate": { |
| 86 | + "spec": { |
| 87 | + "template": { |
| 88 | + "spec": { |
| 89 | + "containers": [ |
| 90 | + { |
| 91 | + "name": name, |
| 92 | + "image": "busybox:1.35", |
| 93 | + "command": command |
| 94 | + } |
| 95 | + ], |
| 96 | + "restartPolicy": "Never" |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + "successfulJobsHistoryLimit": 3, |
| 102 | + "failedJobsHistoryLimit": 1 |
| 103 | + } |
| 104 | + } |
| 105 | + return body |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + # get |
| 110 | + cronjob_list = get_cronjob_list() |
| 111 | + |
| 112 | + # delete |
| 113 | + delete_namespaced_cron_job('default', 'hostname') |
| 114 | + time.sleep(2) |
| 115 | + |
| 116 | + # create |
| 117 | + container_command = [ |
| 118 | + "/bin/sh", |
| 119 | + "-c", |
| 120 | + "date; echo Hello from the Kubernetes cluster; hostname" |
| 121 | + ] |
| 122 | + hostname_json = get_cronjob_body('default', 'hostname', container_command) |
| 123 | + create_namespaced_cron_job('default', hostname_json) |
| 124 | + |
| 125 | + # update |
| 126 | + container_command[2] = "date; echo this is patch; hostname" |
| 127 | + hostname_json = get_cronjob_body('default', 'hostname', container_command) |
| 128 | + patch_namespaced_cron_job('default', hostname_json) |
0 commit comments