@@ -46,7 +46,7 @@ def run(
4646 try :
4747 with open (scenario , "r" ) as f :
4848 scenario_config = yaml .full_load (f )
49-
49+
5050 self .init_clients (lib_telemetry .get_lib_kubernetes ())
5151 pods_status = PodsStatus ()
5252 for config in scenario_config ["scenarios" ]:
@@ -71,67 +71,6 @@ def init_clients(self, k8s_client: KrknKubernetes):
7171 self .custom_object_client = k8s_client .custom_object_client
7272 logging .info ("Successfully initialized Kubernetes client for KubeVirt operations" )
7373
74- def get_vmi (self , name : str , namespace : str ) -> Optional [Dict ]:
75- """
76- Get a Virtual Machine Instance by name and namespace.
77-
78- :param name: Name of the VMI to retrieve
79- :param namespace: Namespace of the VMI
80- :return: The VMI object if found, None otherwise
81- """
82- try :
83- vmi = self .custom_object_client .get_namespaced_custom_object (
84- group = "kubevirt.io" ,
85- version = "v1" ,
86- namespace = namespace ,
87- plural = "virtualmachineinstances" ,
88- name = name
89- )
90- return vmi
91- except ApiException as e :
92- if e .status == 404 :
93- logging .warning (f"VMI { name } not found in namespace { namespace } " )
94- return None
95- else :
96- logging .error (f"Error getting VMI { name } : { e } " )
97- raise
98- except Exception as e :
99- logging .error (f"Unexpected error getting VMI { name } : { e } " )
100- raise
101-
102- def get_vmis (self , regex_name : str , namespace : str ) -> Optional [Dict ]:
103- """
104- Get a Virtual Machine Instance by name and namespace.
105-
106- :param name: Name of the VMI to retrieve
107- :param namespace: Namespace of the VMI
108- :return: The VMI object if found, None otherwise
109- """
110- try :
111- namespaces = self .k8s_client .list_namespaces_by_regex (namespace )
112- for namespace in namespaces :
113- vmis = self .custom_object_client .list_namespaced_custom_object (
114- group = "kubevirt.io" ,
115- version = "v1" ,
116- namespace = namespace ,
117- plural = "virtualmachineinstances" ,
118- )
119-
120- for vmi in vmis .get ("items" ):
121- vmi_name = vmi .get ("metadata" ,{}).get ("name" )
122- match = re .match (regex_name , vmi_name )
123- if match :
124- self .vmis_list .append (vmi )
125- except ApiException as e :
126- if e .status == 404 :
127- logging .warning (f"VMI { regex_name } not found in namespace { namespace } " )
128- return []
129- else :
130- logging .error (f"Error getting VMI { regex_name } : { e } " )
131- raise
132- except Exception as e :
133- logging .error (f"Unexpected error getting VMI { regex_name } : { e } " )
134- raise
13574
13675 def execute_scenario (self , config : Dict [str , Any ], scenario_telemetry : ScenarioTelemetry ) -> int :
13776 """
@@ -154,7 +93,7 @@ def execute_scenario(self, config: Dict[str, Any], scenario_telemetry: ScenarioT
15493 logging .error ("vm_name parameter is required" )
15594 return 1
15695 self .pods_status = PodsStatus ()
157- self .get_vmis (vm_name ,namespace )
96+ self .vmis_list = self . k8s_client . get_vmis (vm_name ,namespace )
15897 for _ in range (kill_count ):
15998
16099 rand_int = random .randint (0 , len (self .vmis_list ) - 1 )
@@ -166,7 +105,7 @@ def execute_scenario(self, config: Dict[str, Any], scenario_telemetry: ScenarioT
166105 if not self .validate_environment (vmi_name , vmi_namespace ):
167106 return 1
168107
169- vmi = self .get_vmi (vmi_name , vmi_namespace )
108+ vmi = self .k8s_client . get_vmi (vmi_name , vmi_namespace )
170109 self .affected_pod = AffectedPod (
171110 pod_name = vmi_name ,
172111 namespace = vmi_namespace ,
@@ -212,15 +151,13 @@ def validate_environment(self, vm_name: str, namespace: str) -> bool:
212151 """
213152 try :
214153 # Check if KubeVirt CRDs exist
215- crd_list = self .custom_object_client .list_namespaced_custom_object ("kubevirt.io" ,"v1" ,namespace ,"virtualmachines" )
216- kubevirt_crds = [crd for crd in crd_list .items () ]
217-
154+ kubevirt_crds = self .k8s_client .get_vms (vm_name , namespace )
218155 if not kubevirt_crds :
219156 logging .error ("KubeVirt CRDs not found. Ensure KubeVirt/CNV is installed in the cluster" )
220157 return False
221158
222159 # Check if VMI exists
223- vmi = self .get_vmi (vm_name , namespace )
160+ vmi = self .k8s_client . get_vmi (vm_name , namespace )
224161 if not vmi :
225162 logging .error (f"VMI { vm_name } not found in namespace { namespace } " )
226163 return False
@@ -243,28 +180,15 @@ def patch_vm_spec(self, vm_name: str, namespace: str, running: bool) -> bool:
243180 """
244181 try :
245182 # Get the VM object first to get its current spec
246- vm = self .custom_object_client .get_namespaced_custom_object (
247- group = "kubevirt.io" ,
248- version = "v1" ,
249- namespace = namespace ,
250- plural = "virtualmachines" ,
251- name = vm_name
252- )
183+ vm = self .k8s_client .get_vm (vm_name , namespace )
253184
254185 # Update the running state
255186 if 'spec' not in vm :
256187 vm ['spec' ] = {}
257188 vm ['spec' ]['running' ] = running
258189
259190 # Apply the patch
260- self .custom_object_client .patch_namespaced_custom_object (
261- group = "kubevirt.io" ,
262- version = "v1" ,
263- namespace = namespace ,
264- plural = "virtualmachines" ,
265- name = vm_name ,
266- body = vm
267- )
191+ self .k8s_client .patch_vm (vm_name ,namespace ,vm )
268192 return True
269193
270194 except ApiException as e :
@@ -293,26 +217,12 @@ def delete_vmi(self, vm_name: str, namespace: str, disable_auto_restart: bool =
293217 " - proceeding with deletion but VM may auto-restart" )
294218 start_creation_time = self .original_vmi .get ('metadata' , {}).get ('creationTimestamp' )
295219 start_time = time .time ()
296- try :
297- self .custom_object_client .delete_namespaced_custom_object (
298- group = "kubevirt.io" ,
299- version = "v1" ,
300- namespace = namespace ,
301- plural = "virtualmachineinstances" ,
302- name = vm_name
303- )
304- except ApiException as e :
305- if e .status == 404 :
306- logging .warning (f"VMI { vm_name } not found during deletion" )
307- return 1
308- else :
309- logging .error (f"API error during VMI deletion: { e } " )
310- return 1
220+ self .k8s_client .delete_vmi (vm_name , namespace )
311221
312222 # Wait for the VMI to be deleted
313223
314224 while time .time () - start_time < timeout :
315- deleted_vmi = self .get_vmi (vm_name , namespace )
225+ deleted_vmi = self .k8s_client . get_vmi (vm_name , namespace )
316226 if deleted_vmi :
317227 if start_creation_time != deleted_vmi .get ('metadata' , {}).get ('creationTimestamp' ):
318228 logging .info (f"VMI { vm_name } successfully recreated" )
@@ -337,7 +247,7 @@ def wait_for_running(self, vm_name: str, namespace: str, timeout: int = 120) ->
337247 while time .time () - start_time < timeout :
338248
339249 # Check current state once since we've already waited for the duration
340- vmi = self .get_vmi (vm_name , namespace )
250+ vmi = self .k8s_client . get_vmi (vm_name , namespace )
341251
342252 if vmi :
343253 if vmi .get ('status' , {}).get ('phase' ) == "Running" :
@@ -378,13 +288,7 @@ def recover(self, vm_name: str, namespace: str, disable_auto_restart: bool = Fal
378288 del metadata [field ]
379289
380290 # Create the VMI
381- self .custom_object_client .create_namespaced_custom_object (
382- group = "kubevirt.io" ,
383- version = "v1" ,
384- namespace = namespace ,
385- plural = "virtualmachineinstances" ,
386- body = vmi_dict
387- )
291+ self .k8s_client .create_vmi (vm_name , namespace , vmi_dict )
388292 logging .info (f"Successfully recreated VMI { vm_name } " )
389293
390294 # Wait for VMI to start running
0 commit comments