Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5645c57

Browse files
committed
add a cronjob crud example
1 parent be9a47e commit 5645c57

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

examples/cronjob_crud.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.BatchV1beta1Api()
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.BatchV1beta1Api()
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.BatchV1beta1Api()
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.BatchV1beta1Api()
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+
# for cron in cron_job_list['items']:
63+
# print(json.dumps(cron))
64+
return cron_job_list["items"]
65+
66+
67+
def judge_crontab_exists(namespace, name):
68+
cron_job_list = get_cronjob_list(namespace)
69+
for cron_job in cron_job_list:
70+
if name == cron_job['metadata']['name']:
71+
return True
72+
return False
73+
74+
75+
def get_cronjob_body(namespace, name, command):
76+
body = {
77+
"apiVersion": "batch/v1beta1",
78+
"kind": "CronJob",
79+
"metadata": {
80+
"name": name,
81+
"namespace": namespace
82+
},
83+
"spec": {
84+
"schedule": "*/1 * * * *",
85+
"concurrencyPolicy": "Allow",
86+
"suspend": False,
87+
"jobTemplate": {
88+
"spec": {
89+
"template": {
90+
"spec": {
91+
"containers": [
92+
{
93+
"name": name,
94+
"image": "busybox:1.35",
95+
"command": command
96+
}
97+
],
98+
"restartPolicy": "Never"
99+
}
100+
}
101+
}
102+
},
103+
"successfulJobsHistoryLimit": 3,
104+
"failedJobsHistoryLimit": 1
105+
}
106+
}
107+
return body
108+
109+
110+
if __name__ == '__main__':
111+
# get
112+
cronjob_list = get_cronjob_list()
113+
114+
# delete
115+
delete_namespaced_cron_job('default', 'hostname')
116+
time.sleep(2)
117+
118+
# create
119+
container_command = [
120+
"/bin/sh",
121+
"-c",
122+
"date; echo Hello from the Kubernetes cluster; hostname"
123+
]
124+
hostname_json = get_cronjob_body('default', 'hostname', container_command)
125+
create_namespaced_cron_job('default', hostname_json)
126+
127+
# update
128+
container_command[2] = "date; echo this is patch; hostname"
129+
hostname_json = get_cronjob_body('default', 'hostname', container_command)
130+
patch_namespaced_cron_job('default', hostname_json)

0 commit comments

Comments
 (0)