forked from kubernetes-client/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollout-daemonset.py
112 lines (91 loc) · 3.52 KB
/
rollout-daemonset.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
This example covers the following:
- Create daemonset
- Update daemonset
- List contoller revisions which belong to specified daemonset
- Roll out daemonset
"""
from kubernetes import client, config
def create_daemon_set_object():
container = client.V1Container(
name="ds-redis",
image="redis",
image_pull_policy="IfNotPresent",
ports=[client.V1ContainerPort(container_port=6379)],
)
# Template
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app": "redis"}),
spec=client.V1PodSpec(containers=[container]))
# Spec
spec = client.V1DaemonSetSpec(
selector=client.V1LabelSelector(
match_labels={"app": "redis"}
),
template=template)
# DaemonSet
daemonset = client.V1DaemonSet(
api_version="apps/v1",
kind="DaemonSet",
metadata=client.V1ObjectMeta(name="daemonset-redis"),
spec=spec)
return daemonset
def create_daemon_set(apps_v1_api, daemon_set_object):
# Create the Daemonset in default namespace
# You can replace the namespace with you have created
apps_v1_api.create_namespaced_daemon_set(
namespace="default", body=daemon_set_object
)
def update_daemon_set(apps_v1_api, daemonset):
# Update container image
daemonset.spec.template.spec.containers[0].image = "redis:6.2"
daemonset_name = daemonset.metadata.name
# Patch the daemonset
apps_v1_api.patch_namespaced_daemon_set(
name=daemonset_name, namespace="default", body=daemonset
)
def list_controller_revision(apps_v1_api, namespace, daemon_set_name):
# Get all controller revisions in specified namespace
controller_revision_list = apps_v1_api.list_namespaced_controller_revision(
namespace)
# Get all controller revisions which belong to specified daemonset.
controller_revision_belong_to_ds = []
for controller_revision in controller_revision_list.items:
owner_kind = controller_revision.metadata.owner_references[0].kind
owner_name = controller_revision.metadata.owner_references[0].name
if owner_kind == "DaemonSet" and owner_name == daemon_set_name:
controller_revision_belong_to_ds.append(
(controller_revision.metadata.name, controller_revision.revision))
return sorted(controller_revision_belong_to_ds, key=lambda x: x[1])
def rollout_namespaced_daemon_set(
apps_v1_api,
name,
namespace,
controller_revision_name):
# Get the specified controller revision object
_controller_revision = apps_v1_api.read_namespaced_controller_revision(
controller_revision_name, namespace)
# Roll out daemonset to the specified revision
apps_v1_api.patch_namespaced_daemon_set(
name, namespace, body=_controller_revision.data)
def main():
# Loading the local kubeconfig
config.load_kube_config()
apps_v1_api = client.AppsV1Api()
core_v1_api = client.CoreV1Api()
daemon_set_obj = create_daemon_set_object()
create_daemon_set(apps_v1_api, daemon_set_obj)
update_daemon_set(apps_v1_api, daemon_set_obj)
# Wait for finishing creation of controller revision
import time
time.sleep(15)
# List the controller revison
controller_revisions = list_controller_revision(
apps_v1_api, "default", "daemonset-redis")
rollout_namespaced_daemon_set(
apps_v1_api,
"daemonset-redis",
"default",
controller_revisions[0][0])
if __name__ == "__main__":
main()