-
Notifications
You must be signed in to change notification settings - Fork 1
/
pykubectl.py
403 lines (343 loc) · 16.6 KB
/
pykubectl.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
from kubernetes import client,config, dynamic
from kubernetes.client import api_client
import datetime
#[input]
#api_version, e.g., autoscaling/v2, split.smi-spec.io/v1alpha1, openfaas.com/v1, apps/v1
#kind, e.g., Deployment, Function, Pod, HorizontalPodAutoscaler, TrafficSplit
#object_name, e.g., sample_deployment
#namespace, e.g., openfaas-fn
#manifest, e.g., a json object for create, replace or patch operation
#operation, e.g., replace, safe_replace (if object not exists, first create it), create, patch, safe_patch (if object not exists, first create it), delete, get, get-json
#patch_type, e.g., application/merge-patch+json, application/json-patch+json, application/apply-patch+yaml
#required arguments:
#api_version, kind, object_name,
#semi-required arguments:
#manifest = {}
#optional arguments:
#namespace="default", operation= "safe_replace", patch_type= "application/merge-patch+json",
#gets a resource as dict and returns object
import threading
lock = threading.Lock()
def apply(**kwargs):
with lock:
object_input = kwargs
res, msg, err = run(**object_input)
return res, msg, err
def run(**kwargs):
# with lock:
#start
error = ""; msg = ""; results = None
msg += 'Kubectl apply started \nInput:' + ("\n**kwargs= " + str({k:v for k,v in kwargs.items()}) if len(kwargs)>0 else "")
#check input requirements
if 'api_version' not in kwargs or 'kind' not in kwargs or 'object_name' not in kwargs:
error += '\nBasic input is not given, i.e., api_version, kind, or object_name'
return results, msg, error
#manifest sometimes is required, i.e., for replace, create or patch operations.
if 'operation' in kwargs and ('replace' in kwargs['operation'] or 'create' in kwargs['operation'] or 'patch' in kwargs['operation']):
if 'manifest' not in kwargs or kwargs['manifest'] == {}:
error += '\nManifest is not given, but is required.'
return results, msg, error
#set optional arguments to defaults, if not given by kwargs
namespace = kwargs['namespace'] if 'namespace' in kwargs else "default"
operation = kwargs['operation'] if 'operation' in kwargs else "safe_replace"
patch_type = kwargs['patch_type'] if 'patch_type' in kwargs else "application/merge-patch+json"
manifest = kwargs['manifest'] if 'manifest' in kwargs else {}
#print the input
msg +=("Collected arguments:"
+ "\n- api_version= " + kwargs['api_version']
+ "\n- kind= " + kwargs['kind']
+ "\n- object_name= " + kwargs['object_name']
+ "\n- namespace= " + namespace
+ "\n- operation= " + operation
+ "\n- patch_type= " + patch_type)
#client
try:
# setup the kubernetes API
client = dynamic.DynamicClient(api_client.ApiClient(configuration=config.load_kube_config()))
api_resources = client.resources.get(api_version=kwargs['api_version'], kind=kwargs['kind'])
except Exception as e:
error += str(e) + "\nFailed to create a client using the kubeconfig file"
return results, msg, error
#operation
#If the operation is 'replace/patch', verify if the object exists; otherwise a 'create' will be requested automatically if a safe operation is requested.
object_already_exists = False
if 'replace' in operation or 'patch' in operation or 'get' in operation or 'safe-delete' in operation:
#for get operations only
found_object = None
#get all objects
for item in api_resources.get(namespace=namespace).items:
#Check each by name
#either as dictionery/json
object_under_check = api_resources.get(name=item.metadata.name, namespace=namespace)
object_under_check_name = object_under_check["metadata"]["name"]
#or as Yaml
object_under_check_name = item.metadata.name
if object_under_check_name == kwargs['object_name']:
object_already_exists = True
msg += "\nObject " + kwargs['object_name'] + " exists."
#for get operations only
found_object = object_under_check
break
#check done
if not object_already_exists:
msg += "\nObject " + kwargs['object_name'] + " does NOT exists."
#get operation
if 'get' in operation:
# error += '\nObject not found'
return results, msg, error
#only for get operation
else:
if 'get' in operation:
if found_object == None:
msg += '\nfound_object == None'
elif 'json' in operation:
found_object = found_object.to_dict()
return found_object, msg, error
#apply
#replace or patch
if 'replace' in operation or 'patch' in operation:
#object exists
if object_already_exists:
#replace
if 'replace' in operation:
msg += "\nA replace operation is taken."
try:
results = api_resources.replace(body=manifest, namespace=namespace, name=kwargs['object_name'])
except Exception as e:
error += '\napi_resources.replace ' + str(e)
#patch
elif 'patch' in operation:
msg += "\nA patch operation is taken."
try:
results = api_resources.patch(body=manifest, namespace=namespace, name=kwargs['object_name'], content_type=patch_type)
except Exception as e:
error += '\napi_resources.patch ' + str(e)
#object NOT exists
else:
#if safe,
#create one for a requested replace
if 'safe' in operation and 'replace' in operation:
msg += "\nA create operation is taken."
try:
results = api_resources.create(body=manifest, namespace=namespace)
except Exception as e:
error += '\napi_resources.create ' + str(e)
#create one for a requested patch
elif 'safe' in operation and 'patch' in operation:
msg += "\nA create operation is taken."
try:
results = api_resources.create(body=manifest, namespace=namespace)
except Exception as e:
error += '\napi_resources.create ' + str(e)
#no operation
else:
error += "\nThe action is dismissed since the object does not exist and operation is not a safe one."
#create
elif 'create' in operation:
msg += "\nA create operation is taken."
try:
results = api_resources.create(body=manifest, namespace=namespace)
except Exception as e:
error += '\napi_resources.create ' + str(e)
#delete
elif 'delete' in operation or 'safe-delete' in operation:
if 'safe-delete' in operation and not object_already_exists:
msg += '\nObject did not exist, so the deletion is safely ignored'
else:
msg += "\nA delete operation is taken."
try:
results = api_resources.delete(name=kwargs['object_name'], body={}, namespace=namespace)
except Exception as e:
error += '\n api_resources.delete' + str(e)
#unknown
else:
error += "\nOperation =" + operation + " is not known"
msg +='\nkubectl apply stopped'
#return
return results, msg, error
#sample trafficsplit
#create
'''
object_name= "function-split"
api_version= "split.smi-spec.io/v1alpha2"
kind= "TrafficSplit"
namespace="openfaas-fn"
operation= "safe_patch"
patch_type= "application/merge-patch+json"
#extra
service= "ssd-tpu"
manifest = {
"apiVersion": api_version,
"kind": kind,
"metadata": {
"name": object_name,
"namespace": namespace
},
"spec": {
"backends": [],
"service": service
}
}
'''
#patch
'''
manifest = {"spec": {
"backends": [
{
"service": "ssd-tpu-blue",
"weight": 1000
}
],
"service": "ssd-tpu"
}
}
'''
#alternative way of patching a deployment
'''
from kubernetes import client, config
DEPLOYMENT_NAME = "ssd-tpu"
client.configuration.debug = True
config.load_kube_config()
apps_v1 = client.AppsV1Api()
#'{"spec": {"template": {"spec": {"containers": [{"name": "ssd-tpu","image": "aslanpour/ssd:cpu-tpu", "securityContext": {"allowPrivilegeEscalation": true}}]}}}}'
# patch = [{"op": "replace", "value": True, "path": "/spec/template/spec/containers/0/securityContext/allowPrivilegeEscalation"}]
#'{"spec": {"template": {"spec": {"volumes": [{"name": "usb-devices", "hostPath": {"path": "/dev/bus/usb"}}]}}}}'
patch = [{"op": "replace", "value": [{"name": "usb-devices", "hostPath": {"path": "/dev/bus/usb"}}], "path": "/spec/template/spec/volumes"}]
# patch the deployment
resp = apps_v1.patch_namespaced_deployment(
name=DEPLOYMENT_NAME, namespace="openfaas-fn", body=patch
)
'''
#alternative way of patching by kubectl
'''
#Sample patch by kubectl
kubectl patch deployment -n openfaas-fn function_name --patch '{"spec": {"template": {"spec": {"containers": [{"name": "ssd-tpu","image": "aslanpour/ssd:cpu-tpu", "securityContext": {"privileged": true}}]}}}}'
'''
#patch by curl
#curl -X POST -i http://localhost:5000/config/write -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"config":{"Model": {"run_on": "gpu"}}}'
####################
def patch_kubernetes_deployment(name, namespace = "default", enabling_tpu_gpu_access = False):
from kubernetes import config, dynamic
from kubernetes.client import api_client
import datetime
import pytz
error = ""
# Creating a dynamic client
try:
dynamic_api_client = dynamic.DynamicClient(
api_client.ApiClient(configuration=config.load_kube_config())
)
except Exception as e:
error += '\n' + str(e)
error= 'Unable to get kubeconfig'
return error, 400
#Does the requested deployment exist?
exists = False
from kubernetes import client
apps_v1 = client.AppsV1Api()
deployments = apps_v1.list_namespaced_deployment(namespace=namespace)
for i in deployments.items:
#print(f'{i.status.available_replicas}\t\t{i.spec.replicas}\t\t{i.metadata.namespace}\t{i.metadata.name}')
if i.metadata.name == name:
exists = True
if exists == False:
error += '\n The requested deployment name=' + name + ' is not found in namespace=' + namespace
return error, 400
# fetching the deployment api
dynamic_api = dynamic_api_client.resources.get(api_version="apps/v1", kind="Deployment")
#get the deployment by name
try:
deployment = dynamic_api.get(name=name, namespace=namespace)
except Exception as e:
error += '\n' + str(e)
error += '\nFailed to get the requested deployment -- name=' + name + ' namespace=' + namespace
return error, 400
if enabling_tpu_gpu_access:
#update the fileds
#allowPrivilegeEscalation = True
#'{"spec": {"template": {"spec": {"containers": [{"name": "ssd-tpu","image": "aslanpour/ssd:cpu-tpu", "securityContext": {"allowPrivilegeEscalation": true}}]}}}}'
deployment.spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation = True
#privileged = True
#'{"spec": {"template": {"spec": {"containers": [{"name": "ssd-tpu","image": "aslanpour/ssd:cpu-tpu", "securityContext": {"privileged": true}}]}}}}'
deployment.spec.template.spec.containers[0].securityContext.privileged = True
#runAsUser = 0
#'{"spec": {"template": {"spec": {"containers": [{"name": "ssd-tpu","image": "aslanpour/ssd:cpu-tpu", "securityContext": {"runAsUser": 0}}]}}}}'
deployment.spec.template.spec.containers[0].securityContext.runAsUser = 0
#volumes hostPath
#'{"spec": {"template": {"spec": {"volumes": [{"name": "usb-devices", "hostPath": {"path": "/dev/bus/usb"}}]}}}}'
deployment.spec.template.spec.volumes = [{"name": "usb-devices", "hostPath": {"path": "/dev/bus/usb"}}]
#volumeMounts mountPath
#'{"spec": {"template": {"spec": {"containers": [{"name": "ssd-tpu", "volumeMounts":[{"mountPath": "/dev/bus/usb", "name": "usb-devices"}]}]}}}}'
deployment.spec.template.spec.containers[0].volumeMounts = [{"mountPath": "/dev/bus/usb", "name": "usb-devices"}]
#get Pod info as env into the container
deployment.spec.template.spec.containers[0].env = [{"name": "GREETING", "value": "WARM_GREETING"},
{"name": "NODE_NAME", "valueFrom": {"fieldRef":{"fieldPath": "spec.nodeName"}}},
{"name": "POD_NAME", "valueFrom": {"fieldRef":{"fieldPath": "metadata.name"}}},
{"name": "POD_NAMESPACE", "valueFrom": {"fieldRef":{"fieldPath": "metadata.namespace"}}},
{"name": "POD_IP", "valueFrom": {"fieldRef":{"fieldPath": "status.podIP"}}},
{"name": "POD_IPS", "valueFrom": {"fieldRef":{"fieldPath": "status.podIPs"}}},
{"name": "POD_HOST_IP", "valueFrom": {"fieldRef":{"fieldPath": "status.hostIP"}}},
{"name": "DEPLOYMENT_NAME", "valueFrom": {"fieldRef":{"fieldPath": "metadata.labels['app']"}}},
{"name": "POD_UID", "valueFrom": {"fieldRef":{"fieldPath": "metadata.uid"}}}]
else:
error += '\nPatching this field is not implemented yet.'
return error, 400
#patch
try:
result = dynamic_api.patch(body=deployment, name=name, namespace=namespace)
except Exception as e:
error += '\n' + str(e)
error += '\nPatch failed -- name=' + name + ' namespace=' + namespace + ' body=' + deployment
return error, 400
return error, 200
#alternative way of patching a deployment by Kubectl. ref: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#patch-deployment-v1-apps
'''
kubectl patch deployment deployment-example -p \
'{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.16"}]}}}}'
'''
#alternative way of patching a deployment by Curl. Ref: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#patch-deployment-v1-apps
'''
kubectl proxy
$ curl -X PATCH -H 'Content-Type: application/strategic-merge-patch+json' --data '
{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.16"}]}}}}' \
'http://127.0.0.1:8001/apis/apps/v1/namespaces/default/deployments/deployment-example'
'''
#alternative way of patching a deployment in Python
'''
from kubernetes import client, config
DEPLOYMENT_NAME = "ssd-tpu"
client.configuration.debug = True
config.load_kube_config()
apps_v1 = client.AppsV1Api()
#'{"spec": {"template": {"spec": {"containers": [{"name": "ssd-tpu","image": "aslanpour/ssd:cpu-tpu", "securityContext": {"allowPrivilegeEscalation": true}}]}}}}'
# patch = [{"op": "replace", "value": True, "path": "/spec/template/spec/containers/0/securityContext/allowPrivilegeEscalation"}]
#'{"spec": {"template": {"spec": {"volumes": [{"name": "usb-devices", "hostPath": {"path": "/dev/bus/usb"}}]}}}}'
patch = [{"op": "replace", "value": [{"name": "usb-devices", "hostPath": {"path": "/dev/bus/usb"}}], "path": "/spec/template/spec/volumes"}]
# patch the deployment
resp = apps_v1.patch_namespaced_deployment(
name=DEPLOYMENT_NAME, namespace="saas-fn", body=patch
)
'''
#######################
# patch_kubernetes_deployment('ssd-tpu', namespace = "openfaas-fn", enabling_tpu_gpu_access = True)
# data = {'type': 'trafficsplit',
# 'interval': 60,
# 'algorithm': 'even',
# 'backends':[{'service':'w5-ssd','weight': 1000}],
# 'api_version': 'split.smi-spec.io/v1alpha2',
# 'kind': 'TrafficSplit',
# 'object_name': 'my-traffic-split',
# 'namespace': 'openfaas-fn',
# 'service': 'gw-func',
# 'operation': 'safe-patch',
# }
# import pymanifest
# manifest, msg, error = pymanifest.manifest_builder(**data)
# print('1111111111111111111')
# print(manifest)
# data['manifest'] = manifest
# results, msg, error = apply(**data)
# if not error:
# print('RRRRRRRR:' + str(results))
# print('MMMMMMM:' + msg)
# print('ERRRRRRRR:' + error)