-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_workers_deployments.py
183 lines (150 loc) · 5.11 KB
/
create_workers_deployments.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import datetime
import pytz
from kubernetes import client, config
import argparse
def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
usage="%(prog)s [OPTION] [FILE]...",
description="Creates and destroys"
)
parser.add_argument(
"--version", action="version",
version = f"{parser.prog} version 0.1.0"
)
parser.add_argument(
"--create", help="Create deployment", action='store_true'
)
parser.add_argument(
"--delete", help="Delete deployment", action='store_true'
)
parser.add_argument(
"--update", help="Update deployment", action='store_true'
)
parser.add_argument(
"--restart", help="Restart deployment", action='store_true'
)
return parser
DEPLOYMENT_NAME = "worker-deployment"
def create_deployment_object():
# Configure Pod template container
# You can create multiple containers and add to pod spec.
container = client.V1Container(
name="worker",
image="localhost:5001/worker",
ports=[client.V1ContainerPort(container_port=80)],
# resources=client.V1ResourceRequirements(
# requests={"cpu": "100m", "memory": "200Mi"},
# limits={"cpu": "500m", "memory": "500Mi"},
# ),
)
# Create and configure a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app": "worker"}),
spec=client.V1PodSpec(containers=[container]),
)
# Create the specification of deployment
spec = client.V1DeploymentSpec(
replicas=3,
template=template,
selector={
"matchLabels":{"app": "worker"}
}
)
# Instantiate the deployment object
deployment = client.V1Deployment(
api_version="apps/v1",
kind="Deployment",
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
spec=spec,
)
return deployment
def create_deployment(api, deployment):
# Create deployment
resp = api.create_namespaced_deployment(
body=deployment, namespace="default"
)
print("\n[INFO] deployment `worker-deployment` created.\n")
print("%s\t%s\t\t\t%s\t%s" % ("NAMESPACE", "NAME", "REVISION", "IMAGE"))
print(
"%s\t\t%s\t%s\t\t%s\n"
% (
resp.metadata.namespace,
resp.metadata.name,
resp.metadata.generation,
resp.spec.template.spec.containers[0].image,
)
)
def update_deployment(api, deployment):
# Update container image
deployment.spec.template.spec.containers[0].image = "localhost:5001/worker"
# patch the deployment
resp = api.patch_namespaced_deployment(
name=DEPLOYMENT_NAME, namespace="default", body=deployment
)
print("\n[INFO] deployment's container image updated.\n")
print("%s\t%s\t\t\t%s\t%s" % ("NAMESPACE", "NAME", "REVISION", "IMAGE"))
print(
"%s\t\t%s\t%s\t\t%s\n"
% (
resp.metadata.namespace,
resp.metadata.name,
resp.metadata.generation,
resp.spec.template.spec.containers[0].image,
)
)
def restart_deployment(api, deployment):
# update `spec.template.metadata` section
# to add `kubectl.kubernetes.io/restartedAt` annotation
deployment.spec.template.metadata.annotations = {
"kubectl.kubernetes.io/restartedAt": datetime.datetime.utcnow()
.replace(tzinfo=pytz.UTC)
.isoformat()
}
# patch the deployment
resp = api.patch_namespaced_deployment(
name=DEPLOYMENT_NAME, namespace="default", body=deployment
)
print("\n[INFO] deployment `worker-deployment` restarted.\n")
print("%s\t\t\t%s\t%s" % ("NAME", "REVISION", "RESTARTED-AT"))
print(
"%s\t%s\t\t%s\n"
% (
resp.metadata.name,
resp.metadata.generation,
resp.spec.template.metadata.annotations,
)
)
def delete_deployment(api):
# Delete deployment
resp = api.delete_namespaced_deployment(
name=DEPLOYMENT_NAME,
namespace="default",
body=client.V1DeleteOptions(
propagation_policy="Foreground", grace_period_seconds=5
),
)
print("\n[INFO] deployment `worker-deployment` deleted.")
def main():
parser = init_argparse()
args = parser.parse_args()
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
apps_v1 = client.AppsV1Api()
# Uncomment the following lines to enable debug logging
# c = client.Configuration()
# c.debug = True
# apps_v1 = client.AppsV1Api(api_client=client.ApiClient(configuration=c))
# Create a deployment object with client-python API.
deployment = create_deployment_object()
if args.delete:
delete_deployment(apps_v1)
if args.create:
create_deployment(apps_v1, deployment)
if args.update:
update_deployment(apps_v1, deployment)
if args.restart:
restart_deployment(apps_v1, deployment)
if __name__ == "__main__":
main()