Bug Description
Describe the bug
Runtime crashes occur due to unsafe exception handling and list access in multiple scenarios:
-
run_kraken.py line 173: Bare except block that catches kubecli initialization failure but then attempts to call kubecli.initialize_clients(None) - kubecli doesn't exist at this point, causing NameError.
-
hogs_scenario_plugin.py line 56: random.randint(0, len(available_nodes)) can return an index out of range when len(available_nodes) equals the upper bound (should be len(available_nodes) - 1).
-
VirtChecker.py line 52: Assumes vmi.get("status",{}).get("interfaces",[]) always has at least one element, but accesses [0].get("ipAddress") without bounds checking - crashes when interfaces list is empty.
To Reproduce
Issue 1: run_kraken.py crash
# Any config.yaml will trigger this if kubeconfig is invalid
kubeconfig_path: /invalid/path/to/config
Steps:
- Set invalid kubeconfig path
- Run krkn
- Observe
NameError: name 'kubecli' is not defined at line 173
Issue 2: hogs_scenario_plugin.py crash
# scenarios/hogs.yaml
node_selector: "worker=true"
number_of_nodes: 5 # when exactly 5 nodes match selector
Steps:
- Configure hog scenario with node selector matching N nodes
- Run scenario multiple times
- When randint(0, N) returns N (probability: 1/(N+1)), crash occurs
Issue 3: VirtChecker.py crash
# kubevirt_check config
namespace: "default"
name: "test-vm"
Steps:
- Have VMI with no network interfaces
- Run virt checks
- Observe
IndexError: list index out of range at line 52
Expected behavior
- Issue 1: Should log error and exit gracefully, not crash with
NameError
- Issue 2: Should correctly select random node from available list
- Issue 3: Should handle VMIs with empty interfaces list gracefully
Krkn Output
Issue 1 output:
Traceback (most recent call last):
File "run_kraken.py", line 173
kubecli.initialize_clients(None)
NameError: name 'kubecli' is not defined
Issue 2 output:
Traceback (most recent call last):
File "hogs_scenario_plugin.py", line 56
available_nodes = [available_nodes[random.randint(0, len(available_nodes))]]
IndexError: list index out of range
Issue 3 output:
Traceback (most recent call last):
File "VirtChecker.py", line 52
ip_address = vmi.get("status",{}).get("interfaces",[])[0].get("ipAddress")
IndexError: list index out of range
Additional context
Inconsistency detected:
The IndexError in hogs_scenario_plugin.py is an deviation from the correct pattern used elsewhere in the codebase. Other plugins correctly handle the inclusive upper bound of random.randint by subtracting 1.
Evidence of correct usage in other files:
krkn/scenario_plugins/node_actions/common_node_functions.py: Uses random.randint(0, len(nodes) - 1)
krkn/scenario_plugins/kubevirt_vm_outage/kubevirt_vm_outage_scenario_plugin.py: Uses random.randint(0, len(self.vmis_list) - 1)
The fix in hogs_scenario_plugin.py should align with these established safe patterns.
I’m working on this and will submit a PR soon.
Bug Description
Describe the bug
Runtime crashes occur due to unsafe exception handling and list access in multiple scenarios:
run_kraken.pyline 173: Bareexceptblock that catcheskubecliinitialization failure but then attempts to callkubecli.initialize_clients(None)-kubeclidoesn't exist at this point, causingNameError.hogs_scenario_plugin.pyline 56:random.randint(0, len(available_nodes))can return an index out of range whenlen(available_nodes)equals the upper bound (should belen(available_nodes) - 1).VirtChecker.pyline 52: Assumesvmi.get("status",{}).get("interfaces",[])always has at least one element, but accesses[0].get("ipAddress")without bounds checking - crashes wheninterfaceslist is empty.To Reproduce
Issue 1:
run_kraken.pycrashSteps:
NameError: name 'kubecli' is not definedat line 173Issue 2:
hogs_scenario_plugin.pycrashSteps:
Issue 3:
VirtChecker.pycrashSteps:
IndexError: list index out of rangeat line 52Expected behavior
NameErrorKrkn Output
Issue 1 output:
Issue 2 output:
Issue 3 output:
Additional context
Inconsistency detected:
The
IndexErrorinhogs_scenario_plugin.pyis an deviation from the correct pattern used elsewhere in the codebase. Other plugins correctly handle the inclusive upper bound ofrandom.randintby subtracting 1.Evidence of correct usage in other files:
krkn/scenario_plugins/node_actions/common_node_functions.py: Usesrandom.randint(0, len(nodes) - 1)krkn/scenario_plugins/kubevirt_vm_outage/kubevirt_vm_outage_scenario_plugin.py: Usesrandom.randint(0, len(self.vmis_list) - 1)The fix in
hogs_scenario_plugin.pyshould align with these established safe patterns.I’m working on this and will submit a PR soon.