Coverage for ocp_resources/node_network_state.py: 0%
66 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 10:48 +0200
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 10:48 +0200
1import time
3from kubernetes.dynamic.exceptions import ConflictError
5from ocp_resources.utils.constants import TIMEOUT_4MINUTES
6from ocp_resources.resource import Resource
7from timeout_sampler import TimeoutSampler
9SLEEP = 1
12class NodeNetworkState(Resource):
13 api_group = Resource.ApiGroup.NMSTATE_IO
15 def __init__(
16 self,
17 name=None,
18 client=None,
19 teardown=True,
20 yaml_file=None,
21 delete_timeout=TIMEOUT_4MINUTES,
22 **kwargs,
23 ):
24 super().__init__(
25 name=name,
26 client=client,
27 teardown=teardown,
28 yaml_file=yaml_file,
29 delete_timeout=delete_timeout,
30 **kwargs,
31 )
32 status = self.instance.to_dict()["status"]
33 self.desired_state = status.get("desiredState", {"interfaces": []})
35 def set_interface(self, interface):
36 # First drop the interface is's already in the list
37 interfaces = [iface for iface in self.desired_state["interfaces"] if iface["name"] != interface["name"]]
39 # Add the interface
40 interfaces.append(interface)
41 self.desired_state["interfaces"] = interfaces
43 def to_dict(self) -> None:
44 super().to_dict()
45 if not self.kind_dict and not self.yaml_file:
46 self.res.update({
47 "spec": {
48 "nodeName": self.name,
49 "managed": True,
50 "desiredState": self.desired_state,
51 }
52 })
54 def apply(self):
55 retries_on_conflict = 3
56 while True:
57 try:
58 self.res["metadata"] = self.instance.to_dict()["metadata"]
59 self.update(self.res)
60 break
61 except ConflictError as e:
62 retries_on_conflict -= 1
63 if retries_on_conflict == 0:
64 raise e
65 time.sleep(1)
67 def wait_until_up(self, name):
68 def _find_up_interface():
69 iface = self.get_interface(name=name)
70 if iface.get("state") == self.Interface.State.UP:
71 return iface
73 return None
75 self.logger.info(f"Checking if interface {name} is up -- {self.name}")
76 samples = TimeoutSampler(wait_timeout=TIMEOUT_4MINUTES, sleep=SLEEP, func=_find_up_interface)
77 for sample in samples:
78 if sample:
79 return
81 def wait_until_deleted(self, name):
82 self.logger.info(f"Checking if interface {name} is deleted -- {self.name}")
83 samples = TimeoutSampler(
84 wait_timeout=self.delete_timeout,
85 sleep=SLEEP,
86 func=self.get_interface,
87 name=name,
88 )
89 for sample in samples:
90 if not sample:
91 return
93 @property
94 def interfaces(self):
95 return self.instance.to_dict()["status"]["currentState"]["interfaces"]
97 @property
98 def routes(self):
99 return self.instance.status.currentState.routes
101 def ipv4(self, iface):
102 for interface in self.interfaces:
103 if interface["name"] == iface:
104 addresses = interface["ipv4"]["address"]
105 if addresses:
106 return interface["ipv4"]["address"][0]["ip"]
108 def get_interface(self, name):
109 for interface in self.interfaces:
110 if interface["name"] == name:
111 return interface
112 return {}