1
1
import json
2
- import unittest
3
2
import time
4
- import timeout_decorator
5
3
import subprocess
6
4
import warnings
7
- import os
8
- import yaml
9
5
10
- from datetime import datetime
11
6
from kubernetes import client , config
12
7
from kubernetes .client .rest import ApiException
13
8
14
9
15
10
def to_selector (labels ):
16
- return "," .join (["=" .join (l ) for l in labels .items ()])
11
+ return "," .join (["=" .join (lbl ) for lbl in labels .items ()])
17
12
18
13
19
14
class K8sApi :
@@ -43,8 +38,8 @@ class K8s:
43
38
44
39
def __init__ (self , labels = 'x=y' , namespace = 'default' ):
45
40
self .api = K8sApi ()
46
- self .labels = labels
47
- self .namespace = namespace
41
+ self .labels = labels
42
+ self .namespace = namespace
48
43
49
44
def get_pg_nodes (self , pg_cluster_name , namespace = 'default' ):
50
45
master_pod_node = ''
@@ -81,7 +76,7 @@ def get_operator_pod(self):
81
76
'default' , label_selector = 'name=postgres-operator'
82
77
).items
83
78
84
- pods = list (filter (lambda x : x .status .phase == 'Running' , pods ))
79
+ pods = list (filter (lambda x : x .status .phase == 'Running' , pods ))
85
80
86
81
if len (pods ):
87
82
return pods [0 ]
@@ -110,7 +105,6 @@ def wait_for_pod_start(self, pod_labels, namespace='default'):
110
105
111
106
time .sleep (self .RETRY_TIMEOUT_SEC )
112
107
113
-
114
108
def get_service_type (self , svc_labels , namespace = 'default' ):
115
109
svc_type = ''
116
110
svcs = self .api .core_v1 .list_namespaced_service (namespace , label_selector = svc_labels , limit = 1 ).items
@@ -213,8 +207,8 @@ def wait_for_logical_backup_job_creation(self):
213
207
self .wait_for_logical_backup_job (expected_num_of_jobs = 1 )
214
208
215
209
def delete_operator_pod (self , step = "Delete operator pod" ):
216
- # patching the pod template in the deployment restarts the operator pod
217
- self .api .apps_v1 .patch_namespaced_deployment ("postgres-operator" ,"default" , {"spec" :{"template" :{"metadata" :{"annotations" :{"step" :"{}-{}" .format (step , datetime . fromtimestamp ( time .time () ))}}}}})
210
+ # patching the pod template in the deployment restarts the operator pod
211
+ self .api .apps_v1 .patch_namespaced_deployment ("postgres-operator" , "default" , {"spec" : {"template" : {"metadata" : {"annotations" : {"step" : "{}-{}" .format (step , time .time ())}}}}})
218
212
self .wait_for_operator_pod_start ()
219
213
220
214
def update_config (self , config_map_patch , step = "Updating operator deployment" ):
@@ -237,7 +231,7 @@ def exec_with_kubectl(self, pod, cmd):
237
231
238
232
def get_patroni_state (self , pod ):
239
233
r = self .exec_with_kubectl (pod , "patronictl list -f json" )
240
- if not r .returncode == 0 or not r .stdout .decode ()[0 :1 ]== "[" :
234
+ if not r .returncode == 0 or not r .stdout .decode ()[0 :1 ] == "[" :
241
235
return []
242
236
return json .loads (r .stdout .decode ())
243
237
@@ -248,7 +242,7 @@ def get_operator_state(self):
248
242
pod = pod .metadata .name
249
243
250
244
r = self .exec_with_kubectl (pod , "curl localhost:8080/workers/all/status/" )
251
- if not r .returncode == 0 or not r .stdout .decode ()[0 :1 ]== "{" :
245
+ if not r .returncode == 0 or not r .stdout .decode ()[0 :1 ] == "{" :
252
246
return None
253
247
254
248
return json .loads (r .stdout .decode ())
@@ -277,7 +271,7 @@ def get_effective_pod_image(self, pod_name, namespace='default'):
277
271
'''
278
272
pod = self .api .core_v1 .list_namespaced_pod (
279
273
namespace , label_selector = "statefulset.kubernetes.io/pod-name=" + pod_name )
280
-
274
+
281
275
if len (pod .items ) == 0 :
282
276
return None
283
277
return pod .items [0 ].spec .containers [0 ].image
@@ -305,8 +299,8 @@ class K8sBase:
305
299
306
300
def __init__ (self , labels = 'x=y' , namespace = 'default' ):
307
301
self .api = K8sApi ()
308
- self .labels = labels
309
- self .namespace = namespace
302
+ self .labels = labels
303
+ self .namespace = namespace
310
304
311
305
def get_pg_nodes (self , pg_cluster_labels = 'cluster-name=acid-minimal-cluster' , namespace = 'default' ):
312
306
master_pod_node = ''
@@ -434,10 +428,10 @@ def count_deployments_with_label(self, labels, namespace='default'):
434
428
def count_pdbs_with_label (self , labels , namespace = 'default' ):
435
429
return len (self .api .policy_v1_beta1 .list_namespaced_pod_disruption_budget (
436
430
namespace , label_selector = labels ).items )
437
-
431
+
438
432
def count_running_pods (self , labels = 'application=spilo,cluster-name=acid-minimal-cluster' , namespace = 'default' ):
439
433
pods = self .api .core_v1 .list_namespaced_pod (namespace , label_selector = labels ).items
440
- return len (list (filter (lambda x : x .status .phase == 'Running' , pods )))
434
+ return len (list (filter (lambda x : x .status .phase == 'Running' , pods )))
441
435
442
436
def wait_for_pod_failover (self , failover_targets , labels , namespace = 'default' ):
443
437
pod_phase = 'Failing over'
@@ -484,14 +478,14 @@ def exec_with_kubectl(self, pod, cmd):
484
478
485
479
def get_patroni_state (self , pod ):
486
480
r = self .exec_with_kubectl (pod , "patronictl list -f json" )
487
- if not r .returncode == 0 or not r .stdout .decode ()[0 :1 ]== "[" :
481
+ if not r .returncode == 0 or not r .stdout .decode ()[0 :1 ] == "[" :
488
482
return []
489
483
return json .loads (r .stdout .decode ())
490
484
491
485
def get_patroni_running_members (self , pod ):
492
486
result = self .get_patroni_state (pod )
493
- return list (filter (lambda x : x ["State" ]== "running" , result ))
494
-
487
+ return list (filter (lambda x : x ["State" ] == "running" , result ))
488
+
495
489
def get_statefulset_image (self , label_selector = "application=spilo,cluster-name=acid-minimal-cluster" , namespace = 'default' ):
496
490
ssets = self .api .apps_v1 .list_namespaced_stateful_set (namespace , label_selector = label_selector , limit = 1 )
497
491
if len (ssets .items ) == 0 :
@@ -505,7 +499,7 @@ def get_effective_pod_image(self, pod_name, namespace='default'):
505
499
'''
506
500
pod = self .api .core_v1 .list_namespaced_pod (
507
501
namespace , label_selector = "statefulset.kubernetes.io/pod-name=" + pod_name )
508
-
502
+
509
503
if len (pod .items ) == 0 :
510
504
return None
511
505
return pod .items [0 ].spec .containers [0 ].image
@@ -514,10 +508,13 @@ def get_effective_pod_image(self, pod_name, namespace='default'):
514
508
"""
515
509
Inspiriational classes towards easier writing of end to end tests with one cluster per test case
516
510
"""
511
+
512
+
517
513
class K8sOperator (K8sBase ):
518
514
def __init__ (self , labels = "name=postgres-operator" , namespace = "default" ):
519
515
super ().__init__ (labels , namespace )
520
516
517
+
521
518
class K8sPostgres (K8sBase ):
522
519
def __init__ (self , labels = "cluster-name=acid-minimal-cluster" , namespace = "default" ):
523
520
super ().__init__ (labels , namespace )
0 commit comments