22import random
33import time
44from asyncio import Future
5-
5+ import traceback
66import yaml
77from krkn_lib .k8s import KrknKubernetes
88from krkn_lib .k8s .pod_monitor import select_and_monitor_by_namespace_pattern_and_label , \
@@ -74,6 +74,7 @@ def run(
7474 return 1
7575
7676 except (RuntimeError , Exception ) as e :
77+ logging .error ("Stack trace:\n %s" , traceback .format_exc ())
7778 logging .error ("PodDisruptionScenariosPlugin exiting due to Exception %s" % e )
7879 return 1
7980 else :
@@ -150,7 +151,7 @@ def _select_pods_with_field_selector(self, name_pattern, label_selector, namespa
150151 field_selector = combined_field_selector
151152 )
152153
153- def get_pods (self , name_pattern , label_selector , namespace , kubecli : KrknKubernetes , field_selector : str = None , node_label_selector : str = None , node_names : list = None , quiet : bool = False ):
154+ def get_pods (self , name_pattern , label_selector , namespace , kubecli : KrknKubernetes , field_selector : str = None , node_label_selector : str = None , node_names : list = None ):
154155 if label_selector and name_pattern :
155156 logging .error ('Only, one of name pattern or label pattern can be specified' )
156157 return []
@@ -161,8 +162,7 @@ def get_pods(self, name_pattern, label_selector, namespace, kubecli: KrknKuberne
161162
162163 # If specific node names are provided, make multiple calls with field selector
163164 if node_names :
164- if not quiet :
165- logging .info (f"Targeting pods on { len (node_names )} specific nodes" )
165+ logging .debug (f"Targeting pods on { len (node_names )} specific nodes" )
166166 all_pods = []
167167 for node_name in node_names :
168168 pods = self ._select_pods_with_field_selector (
@@ -172,20 +172,18 @@ def get_pods(self, name_pattern, label_selector, namespace, kubecli: KrknKuberne
172172 if pods :
173173 all_pods .extend (pods )
174174
175- if not quiet :
176- logging .info (f"Found { len (all_pods )} target pods across { len (node_names )} nodes" )
175+ logging .debug (f"Found { len (all_pods )} target pods across { len (node_names )} nodes" )
177176 return all_pods
178177
179178 # Node label selector approach - use field selectors
180179 if node_label_selector :
181180 # Get nodes matching the label selector first
182181 nodes_with_label = kubecli .list_nodes (label_selector = node_label_selector )
183182 if not nodes_with_label :
184- logging .info (f"No nodes found with label selector: { node_label_selector } " )
183+ logging .debug (f"No nodes found with label selector: { node_label_selector } " )
185184 return []
186185
187- if not quiet :
188- logging .info (f"Targeting pods on { len (nodes_with_label )} nodes with label: { node_label_selector } " )
186+ logging .debug (f"Targeting pods on { len (nodes_with_label )} nodes with label: { node_label_selector } " )
189187 # Use field selector for each node
190188 all_pods = []
191189 for node_name in nodes_with_label :
@@ -196,8 +194,7 @@ def get_pods(self, name_pattern, label_selector, namespace, kubecli: KrknKuberne
196194 if pods :
197195 all_pods .extend (pods )
198196
199- if not quiet :
200- logging .info (f"Found { len (all_pods )} target pods across { len (nodes_with_label )} nodes" )
197+ logging .debug (f"Found { len (all_pods )} target pods across { len (nodes_with_label )} nodes" )
201198 return all_pods
202199
203200 # Standard pod selection (no node targeting)
@@ -207,37 +204,40 @@ def get_pods(self, name_pattern, label_selector, namespace, kubecli: KrknKuberne
207204
208205 def killing_pods (self , config : InputParams , kubecli : KrknKubernetes ):
209206 # region Select target pods
210-
211- namespace = config .namespace_pattern
212- if not namespace :
213- logging .error ('Namespace pattern must be specified' )
214- return 2
207+ try :
208+ namespace = config .namespace_pattern
209+ if not namespace :
210+ logging .error ('Namespace pattern must be specified' )
215211
216- pods = self .get_pods (config .name_pattern ,config .label_selector ,config .namespace_pattern , kubecli , field_selector = "status.phase=Running" , node_label_selector = config .node_label_selector , node_names = config .node_names )
217- exclude_pods = set ()
218- if config .exclude_label :
219- _exclude_pods = self .get_pods ("" ,config .exclude_label ,config .namespace_pattern , kubecli , field_selector = "status.phase=Running" , node_label_selector = config .node_label_selector , node_names = config .node_names )
220- for pod in _exclude_pods :
221- exclude_pods .add (pod [0 ])
212+ pods = self .get_pods (config .name_pattern ,config .label_selector ,config .namespace_pattern , kubecli , field_selector = "status.phase=Running" , node_label_selector = config .node_label_selector , node_names = config .node_names )
213+ exclude_pods = set ()
214+ if config .exclude_label :
215+ _exclude_pods = self .get_pods ("" ,config .exclude_label ,config .namespace_pattern , kubecli , field_selector = "status.phase=Running" , node_label_selector = config .node_label_selector , node_names = config .node_names )
216+ for pod in _exclude_pods :
217+ exclude_pods .add (pod [0 ])
222218
223- pods_count = len (pods )
224- if len (pods ) < config .kill :
225- logging .error ("Not enough pods match the criteria, expected {} but found only {} pods" .format (
226- config .kill , len (pods )))
227- return 2
228-
229- random .shuffle (pods )
230- for i in range (config .kill ):
231- pod = pods [i ]
232- logging .info (pod )
233- if pod [0 ] in exclude_pods :
234- logging .info (f"Excluding { pod [0 ]} from chaos" )
235- else :
236- logging .info (f'Deleting pod { pod [0 ]} ' )
237- kubecli .delete_pod (pod [0 ], pod [1 ])
238-
239- ret = self .wait_for_pods (config .label_selector ,config .name_pattern ,config .namespace_pattern , pods_count , config .duration , config .timeout , kubecli , config .node_label_selector , config .node_names )
240- return ret
219+
220+ pods_count = len (pods )
221+ if len (pods ) < config .kill :
222+ logging .error ("Not enough pods match the criteria, expected {} but found only {} pods" .format (
223+ config .kill , len (pods )))
224+ return 1
225+
226+ random .shuffle (pods )
227+ for i in range (config .kill ):
228+ pod = pods [i ]
229+ logging .info (pod )
230+ if pod [0 ] in exclude_pods :
231+ logging .info (f"Excluding { pod [0 ]} from chaos" )
232+ else :
233+ logging .info (f'Deleting pod { pod [0 ]} ' )
234+ kubecli .delete_pod (pod [0 ], pod [1 ])
235+
236+ return_val = self .wait_for_pods (config .label_selector ,config .name_pattern ,config .namespace_pattern , pods_count , config .duration , config .timeout , kubecli , config .node_label_selector , config .node_names )
237+ except Exception as e :
238+ raise (e )
239+
240+ return return_val
241241
242242 def wait_for_pods (
243243 self , label_selector , pod_name , namespace , pod_count , duration , wait_timeout , kubecli : KrknKubernetes , node_label_selector , node_names
@@ -246,10 +246,10 @@ def wait_for_pods(
246246 start_time = datetime .now ()
247247
248248 while not timeout :
249- pods = self .get_pods (name_pattern = pod_name , label_selector = label_selector ,namespace = namespace , field_selector = "status.phase=Running" , kubecli = kubecli , node_label_selector = node_label_selector , node_names = node_names , quiet = True )
249+ pods = self .get_pods (name_pattern = pod_name , label_selector = label_selector ,namespace = namespace , field_selector = "status.phase=Running" , kubecli = kubecli , node_label_selector = node_label_selector , node_names = node_names )
250250 if pod_count == len (pods ):
251251 return 0
252-
252+
253253 time .sleep (duration )
254254
255255 now_time = datetime .now ()
@@ -258,6 +258,5 @@ def wait_for_pods(
258258 if time_diff .seconds > wait_timeout :
259259 logging .error ("timeout while waiting for pods to come up" )
260260 return 1
261-
262- # should never get to this return
261+
263262 return 0
0 commit comments