7
7
from typing import Any , Callable , Dict , List , Optional
8
8
9
9
import kubernetes .client
10
- from kubernetes import client , utils
11
-
12
10
from kubeobject import CustomObject
11
+ from kubernetes import client , utils
13
12
from kubetester .kubetester import run_periodically , running_locally
14
13
15
14
# Re-exports
19
18
assert_pod_container_security_context ,
20
19
assert_pod_security_context ,
21
20
)
22
- from kubernetes import client
23
21
24
22
25
23
def create_secret (
@@ -30,9 +28,7 @@ def create_secret(
30
28
api_client : Optional [client .ApiClient ] = None ,
31
29
) -> str :
32
30
"""Creates a Secret with `name` in `namespace`. String contents are passed as the `data` parameter."""
33
- secret = client .V1Secret (
34
- metadata = client .V1ObjectMeta (name = name ), string_data = data , type = type
35
- )
31
+ secret = client .V1Secret (metadata = client .V1ObjectMeta (name = name ), string_data = data , type = type )
36
32
37
33
client .CoreV1Api (api_client = api_client ).create_namespaced_secret (namespace , secret )
38
34
@@ -63,14 +59,10 @@ def update_secret(
63
59
):
64
60
"""Updates a secret in a given namespace with the given name and data—handles base64 encoding."""
65
61
secret = client .V1Secret (metadata = client .V1ObjectMeta (name = name ), string_data = data )
66
- client .CoreV1Api (api_client = api_client ).patch_namespaced_secret (
67
- name , namespace , secret
68
- )
62
+ client .CoreV1Api (api_client = api_client ).patch_namespaced_secret (name , namespace , secret )
69
63
70
64
71
- def delete_secret (
72
- namespace : str , name : str , api_client : Optional [kubernetes .client .ApiClient ] = None
73
- ):
65
+ def delete_secret (namespace : str , name : str , api_client : Optional [kubernetes .client .ApiClient ] = None ):
74
66
client .CoreV1Api (api_client = api_client ).delete_namespaced_secret (name , namespace )
75
67
76
68
@@ -88,18 +80,12 @@ def create_or_update_service(
88
80
api_client : Optional [kubernetes .client .ApiClient ] = None ,
89
81
):
90
82
"""Creates a service with `name` in `namespace`"""
91
- service = client .V1Service (
92
- metadata = client .V1ObjectMeta (name = name , namespace = namespace ), spec = spec
93
- )
83
+ service = client .V1Service (metadata = client .V1ObjectMeta (name = name , namespace = namespace ), spec = spec )
94
84
try :
95
- client .CoreV1Api (api_client = api_client ).create_namespaced_service (
96
- namespace = namespace , body = service
97
- )
85
+ client .CoreV1Api (api_client = api_client ).create_namespaced_service (namespace = namespace , body = service )
98
86
except kubernetes .client .ApiException as e :
99
87
if e .status == 409 :
100
- client .CoreV1Api (api_client = api_client ).patch_namespaced_service (
101
- name , namespace , body = service
102
- )
88
+ client .CoreV1Api (api_client = api_client ).patch_namespaced_service (name , namespace , body = service )
103
89
else :
104
90
raise e
105
91
@@ -117,9 +103,7 @@ def get_service(
117
103
:return None if the service does not exist
118
104
"""
119
105
try :
120
- return client .CoreV1Api (api_client = api_client ).read_namespaced_service (
121
- name , namespace
122
- )
106
+ return client .CoreV1Api (api_client = api_client ).read_namespaced_service (name , namespace )
123
107
except kubernetes .client .ApiException as e :
124
108
if e .status == 404 :
125
109
return None
@@ -129,24 +113,16 @@ def get_service(
129
113
130
114
def delete_pvc (namespace : str , name : str ):
131
115
"""Deletes a persistent volument claim(pvc) with `name` in `namespace`"""
132
- client .CoreV1Api ().delete_namespaced_persistent_volume_claim (
133
- namespace = namespace , name = name
134
- )
116
+ client .CoreV1Api ().delete_namespaced_persistent_volume_claim (namespace = namespace , name = name )
135
117
136
118
137
119
def create_object_from_dict (data , namespace : str ) -> List :
138
120
k8s_client = client .ApiClient ()
139
121
return utils .create_from_dict (k8s_client = k8s_client , data = data , namespace = namespace )
140
122
141
123
142
- def read_configmap (
143
- namespace : str , name : str , api_client : Optional [client .ApiClient ] = None
144
- ) -> Dict [str , str ]:
145
- return (
146
- client .CoreV1Api (api_client = api_client )
147
- .read_namespaced_config_map (name , namespace )
148
- .data
149
- )
124
+ def read_configmap (namespace : str , name : str , api_client : Optional [client .ApiClient ] = None ) -> Dict [str , str ]:
125
+ return client .CoreV1Api (api_client = api_client ).read_namespaced_config_map (name , namespace ).data
150
126
151
127
152
128
def create_configmap (
@@ -156,9 +132,7 @@ def create_configmap(
156
132
api_client : Optional [kubernetes .client .ApiClient ] = None ,
157
133
):
158
134
configmap = client .V1ConfigMap (metadata = client .V1ObjectMeta (name = name ), data = data )
159
- client .CoreV1Api (api_client = api_client ).create_namespaced_config_map (
160
- namespace , configmap
161
- )
135
+ client .CoreV1Api (api_client = api_client ).create_namespaced_config_map (namespace , configmap )
162
136
163
137
164
138
def update_configmap (
@@ -168,9 +142,7 @@ def update_configmap(
168
142
api_client : Optional [kubernetes .client .ApiClient ] = None ,
169
143
):
170
144
configmap = client .V1ConfigMap (metadata = client .V1ObjectMeta (name = name ), data = data )
171
- client .CoreV1Api (api_client = api_client ).replace_namespaced_config_map (
172
- name , namespace , configmap
173
- )
145
+ client .CoreV1Api (api_client = api_client ).replace_namespaced_config_map (name , namespace , configmap )
174
146
175
147
176
148
def create_or_update_configmap (
@@ -201,9 +173,7 @@ def create_service(
201
173
202
174
service = client .V1Service (
203
175
metadata = client .V1ObjectMeta (name = name , namespace = namespace ),
204
- spec = client .V1ServiceSpec (
205
- ports = ports , cluster_ip = cluster_ip , selector = selector
206
- ),
176
+ spec = client .V1ServiceSpec (ports = ports , cluster_ip = cluster_ip , selector = selector ),
207
177
)
208
178
client .CoreV1Api ().create_namespaced_service (namespace , service )
209
179
@@ -242,26 +212,18 @@ def read_service(
242
212
name : str ,
243
213
api_client : Optional [client .ApiClient ] = None ,
244
214
) -> client .V1Service :
245
- return client .CoreV1Api (api_client = api_client ).read_namespaced_service (
246
- name , namespace
247
- )
215
+ return client .CoreV1Api (api_client = api_client ).read_namespaced_service (name , namespace )
248
216
249
217
250
218
def read_secret (
251
219
namespace : str ,
252
220
name : str ,
253
221
api_client : Optional [client .ApiClient ] = None ,
254
222
) -> Dict [str , str ]:
255
- return decode_secret (
256
- client .CoreV1Api (api_client = api_client )
257
- .read_namespaced_secret (name , namespace )
258
- .data
259
- )
223
+ return decode_secret (client .CoreV1Api (api_client = api_client ).read_namespaced_secret (name , namespace ).data )
260
224
261
225
262
- def delete_pod (
263
- namespace : str , name : str , api_client : Optional [kubernetes .client .ApiClient ] = None
264
- ):
226
+ def delete_pod (namespace : str , name : str , api_client : Optional [kubernetes .client .ApiClient ] = None ):
265
227
client .CoreV1Api (api_client = api_client ).delete_namespaced_pod (name , namespace )
266
228
267
229
@@ -282,9 +244,7 @@ def create_or_update_namespace(
282
244
client .CoreV1Api (api_client = api_client ).create_namespace (namespace_resource )
283
245
except kubernetes .client .ApiException as e :
284
246
if e .status == 409 :
285
- client .CoreV1Api (api_client = api_client ).patch_namespace (
286
- namespace , namespace_resource
287
- )
247
+ client .CoreV1Api (api_client = api_client ).patch_namespace (namespace , namespace_resource )
288
248
289
249
290
250
def delete_namespace (name : str ):
@@ -312,14 +272,10 @@ def get_statefulset(
312
272
name : str ,
313
273
api_client : Optional [client .ApiClient ] = None ,
314
274
) -> client .V1StatefulSet :
315
- return client .AppsV1Api (api_client = api_client ).read_namespaced_stateful_set (
316
- name , namespace
317
- )
275
+ return client .AppsV1Api (api_client = api_client ).read_namespaced_stateful_set (name , namespace )
318
276
319
277
320
- def statefulset_is_deleted (
321
- namespace : str , name : str , api_client : Optional [client .ApiClient ]
322
- ):
278
+ def statefulset_is_deleted (namespace : str , name : str , api_client : Optional [client .ApiClient ]):
323
279
try :
324
280
get_statefulset (namespace , name , api_client = api_client )
325
281
return False
@@ -338,13 +294,9 @@ def delete_cluster_role(name: str, api_client: Optional[client.ApiClient] = None
338
294
raise e
339
295
340
296
341
- def delete_cluster_role_binding (
342
- name : str , api_client : Optional [client .ApiClient ] = None
343
- ):
297
+ def delete_cluster_role_binding (name : str , api_client : Optional [client .ApiClient ] = None ):
344
298
try :
345
- client .RbacAuthorizationV1Api (
346
- api_client = api_client
347
- ).delete_cluster_role_binding (name )
299
+ client .RbacAuthorizationV1Api (api_client = api_client ).delete_cluster_role_binding (name )
348
300
except client .rest .ApiException as e :
349
301
if e .status != 404 :
350
302
raise e
@@ -367,9 +319,7 @@ def get_pod_when_running(
367
319
time .sleep (3 )
368
320
369
321
try :
370
- pods = client .CoreV1Api (api_client = api_client ).list_namespaced_pod (
371
- namespace , label_selector = label_selector
372
- )
322
+ pods = client .CoreV1Api (api_client = api_client ).list_namespaced_pod (namespace , label_selector = label_selector )
373
323
try :
374
324
pod = pods .items [0 ]
375
325
except IndexError :
@@ -397,17 +347,13 @@ def get_pod_when_ready(
397
347
cnt = 0
398
348
399
349
while True and cnt < default_retry :
400
- print (
401
- f"get_pod_when_ready: namespace={ namespace } , label_selector={ label_selector } "
402
- )
350
+ print (f"get_pod_when_ready: namespace={ namespace } , label_selector={ label_selector } " )
403
351
404
352
if cnt > 0 :
405
353
time .sleep (1 )
406
354
cnt += 1
407
355
try :
408
- pods = client .CoreV1Api (api_client = api_client ).list_namespaced_pod (
409
- namespace , label_selector = label_selector
410
- )
356
+ pods = client .CoreV1Api (api_client = api_client ).list_namespaced_pod (namespace , label_selector = label_selector )
411
357
412
358
if len (pods .items ) == 0 :
413
359
continue
@@ -440,13 +386,9 @@ def is_pod_ready(
440
386
if it does not exist or there is any other kind of error.
441
387
This function is intended to check if installing third party components is needed.
442
388
"""
443
- print (
444
- f"Checking if pod is ready: namespace={ namespace } , label_selector={ label_selector } "
445
- )
389
+ print (f"Checking if pod is ready: namespace={ namespace } , label_selector={ label_selector } " )
446
390
try :
447
- pods = client .CoreV1Api (api_client = api_client ).list_namespaced_pod (
448
- namespace , label_selector = label_selector
449
- )
391
+ pods = client .CoreV1Api (api_client = api_client ).list_namespaced_pod (namespace , label_selector = label_selector )
450
392
451
393
if len (pods .items ) == 0 :
452
394
return None
@@ -507,9 +449,7 @@ def create_or_update(resource: CustomObject) -> CustomObject:
507
449
while tries < 10 :
508
450
if tries > 0 : # The first try we don't need to do client-side merge apply
509
451
# do a client-side-apply
510
- new_back_obj_to_apply = copy .deepcopy (
511
- resource .backing_obj
512
- ) # resource and changes we want to apply
452
+ new_back_obj_to_apply = copy .deepcopy (resource .backing_obj ) # resource and changes we want to apply
513
453
514
454
resource .load () # resource from the server overwrites resource.backing_obj
515
455
@@ -521,13 +461,9 @@ def create_or_update(resource: CustomObject) -> CustomObject:
521
461
# we want to apply has them. But that is highly unlikely, and we can add that code in case that happens.
522
462
resource ["spec" ] = new_back_obj_to_apply ["spec" ]
523
463
if "metadata" in resource and "annotations" in resource ["metadata" ]:
524
- resource ["metadata" ]["annotations" ].update (
525
- new_back_obj_to_apply ["metadata" ]["annotations" ]
526
- )
464
+ resource ["metadata" ]["annotations" ].update (new_back_obj_to_apply ["metadata" ]["annotations" ])
527
465
if "metadata" in resource and "labels" in resource ["metadata" ]:
528
- resource ["metadata" ]["labels" ].update (
529
- new_back_obj_to_apply ["metadata" ]["labels" ]
530
- )
466
+ resource ["metadata" ]["labels" ].update (new_back_obj_to_apply ["metadata" ]["labels" ])
531
467
try :
532
468
resource .update ()
533
469
break
@@ -541,9 +477,7 @@ def create_or_update(resource: CustomObject) -> CustomObject:
541
477
)
542
478
tries += 1
543
479
if tries == 10 :
544
- raise Exception (
545
- "Tried client side merge 10 times and did not succeed"
546
- )
480
+ raise Exception ("Tried client side merge 10 times and did not succeed" )
547
481
548
482
return resource
549
483
0 commit comments