-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s.py
63 lines (48 loc) · 2.09 KB
/
k8s.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from kubernetes import client, config
def get_follower_nodes(api):
ret = api.list_node(watch = False)
return [node for node in ret.items if 'node-role.kubernetes.io/control-plane' not in node.metadata.labels]
def get_node(api, node_name):
return api.read_node(node_name)
def add_label_to_node(api, node_name, key, value):
body = {"metadata":{"labels":{key: value}}}
api.patch_node(node_name, body)
def remove_label_from_node(api, node_name, key):
body = {"metadata":{"labels":{key: None}}}
api.patch_node(node_name, body)
def get_pods(api):
ret = api.list_pod_for_all_namespaces(watch = False)
return [pod for pod in ret.items if pod.metadata.namespace == 'default']
def get_pod(api, pod_name, namespace):
return api.read_namespaced_pod(pod_name, namespace)
def get_pods_for_node(api, node_name):
field_selector = 'spec.nodeName=' + node_name
ret = api.list_pod_for_all_namespaces(watch = False, field_selector = field_selector)
return [pod for pod in ret.items if pod.metadata.namespace == 'default']
def add_label_to_pod(api, pod_name, namespace, key, value):
body = {"metadata":{"labels":{key: value}}}
api.patch_namespaced_pod(pod_name, namespace, body)
def remove_label_from_pod(api, pod_name, namespace, key, value):
body = {"metadata":{"labels":{key: None}}}
api.patch_namespaced_pod(pod_name, namespace, body)
def create_new_pod(api, pod, new_pod_name, new_node_name=None):
metadata = client.V1ObjectMeta(namespace = pod.metadata.namespace, name = new_pod_name, labels = pod.metadata.labels)
spec = pod.spec
spec.node_name = new_node_name
new_pod = client.V1Pod(metadata = metadata, spec = pod.spec)
ret = api.create_namespaced_pod(pod.metadata.namespace, new_pod)
return ret
def is_pod_ready(api, pod):
if pod.status.phase == "Pending":
return False
if pod.status.conditions is None:
return False
for condition in pod.status.conditions:
if not condition.status:
return False
for container in pod.status.container_statuses:
if not container.ready:
return False
return True
def delete_pod(api, pod):
ret = api.delete_namespaced_pod(pod.metadata.name, pod.metadata.namespace)