forked from RedHatQE/openshift-virtualization-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
442 lines (396 loc) · 14.5 KB
/
utils.py
File metadata and controls
442 lines (396 loc) · 14.5 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import logging
from collections import defaultdict
from kubernetes.dynamic.exceptions import NotFoundError
from ocp_resources.pod import Pod
from ocp_resources.resource import ResourceEditor
from timeout_sampler import TimeoutExpiredError, TimeoutSampler
from utilities.constants import (
BRIDGE_MARKER,
CDI_APISERVER,
CDI_DEPLOYMENT,
CDI_OPERATOR,
CDI_UPLOADPROXY,
CLUSTER_NETWORK_ADDONS_OPERATOR,
HCO_OPERATOR,
HCO_WEBHOOK,
HYPERCONVERGED_CLUSTER_CLI_DOWNLOAD,
IMAGE_CRON_STR,
KUBE_CNI_LINUX_BRIDGE_PLUGIN,
KUBEMACPOOL_MAC_CONTROLLER_MANAGER,
NODE_ROLE_KUBERNETES_IO,
SSP_OPERATOR,
TIMEOUT_4MIN,
TIMEOUT_5MIN,
TIMEOUT_5SEC,
TIMEOUT_10MIN,
TIMEOUT_30SEC,
VIRT_CONTROLLER,
VIRT_HANDLER,
VIRT_OPERATOR,
VIRT_TEMPLATE_VALIDATOR,
WORKER_NODE_LABEL_KEY,
)
from utilities.hco import wait_for_hco_post_update_stable_state
LOGGER = logging.getLogger(__name__)
SELECTORS = [
("infra-comp", "infra1"),
("infra-comp", "infra2"),
("infra-comp", "infra3"),
("work-comp", "work1"),
("work-comp", "work2"),
("work-comp", "work3"),
("op-comp", "op1"),
("op-comp", "op2"),
("op-comp", "op3"),
]
INFRA_LABEL_1 = {"nodePlacement": {"nodeSelector": {"infra-comp": "infra1"}}}
INFRA_LABEL_2 = {"nodePlacement": {"nodeSelector": {"infra-comp": "infra2"}}}
INFRA_LABEL_3 = {"nodePlacement": {"nodeSelector": {"infra-comp": "infra3"}}}
WORK_LABEL_1 = {"nodePlacement": {"nodeSelector": {"work-comp": "work1"}}}
WORK_LABEL_2 = {"nodePlacement": {"nodeSelector": {"work-comp": "work2"}}}
WORK_LABEL_3 = {"nodePlacement": {"nodeSelector": {"work-comp": "work3"}}}
SUBSCRIPTION_NODE_SELCTOR_1 = {"op-comp": "op1"}
SUBSCRIPTION_NODE_SELCTOR_2 = {"op-comp": "op2"}
SUBSCRIPTION_NODE_SELCTOR_3 = {"op-comp": "op3"}
SUBSCRIPTION_TOLERATIONS = [
{
"effect": "NoSchedule",
"key": f"{NODE_ROLE_KUBERNETES_IO}/master",
"operator": "Exists",
}
]
NODE_PLACEMENT_INFRA = {
"nodePlacement": {
"affinity": {
"nodeAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{
"key": "infra-comp",
"operator": "In",
"values": ["infra1", "infra2"],
}
]
}
]
}
}
},
"nodeSelector": {"infra-comp": "infra1"},
"tolerations": [
{
"effect": "NoSchedule",
"key": WORKER_NODE_LABEL_KEY,
"operator": "Exists",
}
],
}
}
NODE_PLACEMENT_WORKLOADS = {
"nodePlacement": {
"affinity": {
"nodeAffinity": {
"preferredDuringSchedulingIgnoredDuringExecution": [
{
"preference": {
"matchExpressions": [
{
"key": "work-comp",
"operator": "In",
"values": ["work1", "work2"],
}
]
},
"weight": 1,
}
],
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{
"key": "work-comp",
"operator": "In",
"values": ["work1", "work2"],
}
]
}
]
},
}
},
"nodeSelector": {"work-comp": "work2"},
"tolerations": [
{
"effect": "NoSchedule",
"key": WORKER_NODE_LABEL_KEY,
"operator": "Exists",
}
],
}
}
# Below list consists of Infrastructure and Workloads pods based on Daemonset and Deployments.
CNV_INFRA_PODS_COMPONENTS = [
VIRT_CONTROLLER,
VIRT_TEMPLATE_VALIDATOR,
KUBEMACPOOL_MAC_CONTROLLER_MANAGER,
CDI_APISERVER,
CDI_DEPLOYMENT,
CDI_UPLOADPROXY,
]
CNV_WORKLOADS_PODS_COMPONENTS = [
VIRT_HANDLER,
BRIDGE_MARKER,
KUBE_CNI_LINUX_BRIDGE_PLUGIN,
]
CNV_OPERATOR_PODS_COMPONENTS = [
CDI_OPERATOR,
CLUSTER_NETWORK_ADDONS_OPERATOR,
HCO_OPERATOR,
HCO_WEBHOOK,
SSP_OPERATOR,
VIRT_OPERATOR,
]
def find_components_on_node(component_list, node_name, admin_client, hco_namespace):
"""
This function is used to check the Pod on given node. It breaks the loop once it finds Pod from the given list.
Args:
component_list (list): list of components to be matched
node_name (str): Name of the node
admin_client(DynamicClient): DynamicClient object
hco_namespace(Namespace): Namespace object
Returns:
list, list: list of matched components, list of unmatched components for a given node
"""
pods_on_node = get_pod_per_nodes(
admin_client=admin_client,
hco_namespace=hco_namespace,
filter_pods_by_name=IMAGE_CRON_STR,
)
found_components = []
missing_components = []
if node_name not in pods_on_node:
LOGGER.warning(f"Node: {node_name}, does not have any associated pods.")
return found_components, missing_components
for component_name in component_list:
for pod_name in pods_on_node[node_name]:
if pod_name.startswith(component_name):
found_components.append(component_name)
break
else:
missing_components.append(component_name)
LOGGER.info(
f"For node: {node_name}, found components: {found_components}, missing components: {missing_components}"
)
return found_components, missing_components
def verify_all_components_on_node(component_list, node_name, admin_client, hco_namespace):
"""
This function validates that actual pods associated with a given node matches with the list of expected pods for
same node
Args:
component_list (list): list of components to be matched
node_name (str): Name of the node
admin_client(DynamicClient): DynamicClient object
hco_namespace(Namespace): Namespace object
raise:
TimeoutExpiredError: if a match is not found
"""
LOGGER.info(f"Validating that following pod types: {component_list} are present for node: {node_name}")
samples = TimeoutSampler(
wait_timeout=TIMEOUT_5MIN,
sleep=TIMEOUT_5SEC,
func=find_components_on_node,
component_list=component_list,
node_name=node_name,
admin_client=admin_client,
hco_namespace=hco_namespace,
)
found_components = None
missing_components = None
try:
for found_components, missing_components in samples:
if not missing_components:
return
except TimeoutExpiredError:
LOGGER.error(
f"For Node:{node_name}, verified components {found_components}, failed components {missing_components}"
)
raise
def verify_no_components_on_nodes(
component_list,
node_names,
admin_client,
hco_namespace,
):
"""
This function validates that a list of pods are not associated with any of node from a given list
Args:
component_list (list): list of components to be matched
node_names (list): Name of the nodes
admin_client(DynamicClient): DynamicClient object
hco_namespace(Namespace): Namespace object
raise:
TimeoutExpiredError: if a match is found
"""
LOGGER.info(f"Validating following pod types: {component_list} are not present on nodes: {node_names}")
def _check_found_components_all_nodes():
node_results = {}
for node_name in node_names:
found_components, missing_components = find_components_on_node(
component_list=component_list,
node_name=node_name,
admin_client=admin_client,
hco_namespace=hco_namespace,
)
node_results[node_name] = {
"found": found_components,
"missing": missing_components,
}
LOGGER.debug(f"On node: {node_name}, found: {found_components}, missing_components: {missing_components}")
return {
node_name: node_results[node_name]["found"] for node_name in node_names if node_results[node_name]["found"]
}
samples = TimeoutSampler(
wait_timeout=TIMEOUT_4MIN,
sleep=TIMEOUT_5SEC,
func=_check_found_components_all_nodes,
)
sample = None
try:
for sample in samples:
if not sample:
return
except TimeoutExpiredError:
LOGGER.error(f"Timed out waiting for no matching components on nodes:{node_names}, actual results {sample}")
raise
def verify_components_exist_only_on_selected_node(
hco_pods_per_nodes,
component_list,
selected_node,
admin_client,
hco_namespace,
):
"""
This function validates only expected pods have been spin'ed up on a given node.
Args:
hco_pods_per_nodes(dict): dictionary with node names as keys and associated list of pod apps as values
component_list (list): list of components to be matched
selected_node (str): Name of the selected node
admin_client(DynamicClient): DynamicClient object
hco_namespace(Namespace): Namespace object
"""
unselected_nodes = [node_name for node_name in hco_pods_per_nodes.keys() if node_name != selected_node]
verify_all_components_on_node(
component_list=component_list,
node_name=selected_node,
admin_client=admin_client,
hco_namespace=hco_namespace,
)
verify_no_components_on_nodes(
component_list=component_list,
node_names=unselected_nodes,
admin_client=admin_client,
hco_namespace=hco_namespace,
)
def get_pod_per_nodes(admin_client, hco_namespace, filter_pods_by_name=None):
"""
This function creates a dictionary, with nodes as keys and associated list of pod apps as values
Args:
admin_client(DynamicClient): DynamicClient object
hco_namespace(Namespace): Namespace object
filter_pods_by_name(str): string to filter pod names by
Returns:
dict: a dictionary, with nodes as keys and associated list of pod apps as values
"""
def _get_pods_per_nodes(_filter_pods_by_name):
pods_per_nodes = defaultdict(list)
for pod in Pod.get(
dyn_client=admin_client,
namespace=hco_namespace.name,
):
if _filter_pods_by_name and _filter_pods_by_name in pod.name:
LOGGER.warning(f"Ignoring pod: {pod.name} for placement")
continue
try:
# field_selector="status.phase==Running" is not always reliable
# to filter out terminating pods, see: https://github.com/kubernetes/kubectl/issues/450
if pod.instance.metadata.get("deletionTimestamp") is None:
pods_per_nodes[pod.node.name].append(pod)
except NotFoundError:
LOGGER.warning(f"Ignoring pods that disappeared during the query. node={pod.node.name} pod={pod.name}")
return pods_per_nodes
pod_names_per_nodes = {}
samples = TimeoutSampler(
wait_timeout=TIMEOUT_5MIN,
sleep=TIMEOUT_30SEC,
func=_get_pods_per_nodes,
_filter_pods_by_name=filter_pods_by_name,
exceptions_dict={NotFoundError: []},
)
try:
for sample in samples:
if all(pod.exists and pod.status == Pod.Status.RUNNING for pods in sample.values() for pod in pods):
pod_names_per_nodes = {node: [pod.name for pod in pods] for node, pods in sample.items()}
return pod_names_per_nodes
except TimeoutExpiredError:
LOGGER.error(f"Timeout waiting for pods to be ready {pod_names_per_nodes}.")
raise
def update_subscription_config(admin_client, hco_namespace, subscription, config):
"""
Updates CNV subscription spec.config
Args:
admin_client(DynamicClient): DynamicClient object
hco_namespace (Resource): hco_namespace
subscription(Resource): subscription resource
config(dict): config dict to be used for patch operation
Raises:
TimeoutExpiredError: if appropriate pods are not re-spinned
"""
editor = ResourceEditor(
patches={
subscription: {
"spec": {
"config": config,
}
}
},
)
editor.update(backup_resources=False)
LOGGER.info("Waiting for CNV HCO to be Ready.")
wait_for_hco_post_update_stable_state(
admin_client=admin_client,
hco_namespace=hco_namespace,
exclude_deployments=[HYPERCONVERGED_CLUSTER_CLI_DOWNLOAD],
)
def pods_with_node_selector(namespace_name, node_selectors):
pods_with_labels = []
for pod in list(Pod.get(namespace=namespace_name)):
node_selectors_from_pod = pod.instance.spec.get("nodeSelector", [])
LOGGER.info(f"Node selector for pod {pod.name}: {node_selectors_from_pod}")
if node_selectors_from_pod and (set(node_selectors_from_pod.keys()).intersection(node_selectors)):
pods_with_labels.append(f"Pod: {pod.name} with labels: {node_selectors_from_pod} still exists")
return pods_with_labels
def wait_for_pod_node_selector_clean_up(namespace_name):
node_selectors = set(list(zip(*SELECTORS))[0])
LOGGER.info(f"Looking for pods with nodeSelectors keys: {node_selectors}")
samples = TimeoutSampler(
wait_timeout=TIMEOUT_10MIN,
sleep=TIMEOUT_30SEC,
func=pods_with_node_selector,
namespace_name=namespace_name,
node_selectors=node_selectors,
exceptions_dict={NotFoundError: []},
)
sample = None
try:
for sample in samples:
if sample:
LOGGER.info(f"Following pods still has labels: {sample}")
else:
return
except TimeoutExpiredError:
if sample:
LOGGER.error(f"Following pods still has labels: {sample}")
raise