Skip to content

[BUG] Runtime crashes caused by bare exception handling and unchecked list indexing #1069

@AR21SM

Description

@AR21SM

Bug Description

Describe the bug

Runtime crashes occur due to unsafe exception handling and list access in multiple scenarios:

  1. 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.

  2. 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).

  3. 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:

  1. Set invalid kubeconfig path
  2. Run krkn
  3. 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:

  1. Configure hog scenario with node selector matching N nodes
  2. Run scenario multiple times
  3. 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:

  1. Have VMI with no network interfaces
  2. Run virt checks
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions