Skip to content

fix: bare except clause silently swallows errors during Kubernetes client initialization in run_kraken.py #1298

@v0idheaven

Description

@v0idheaven

Summary

In run_kraken.py around line 231, a bare except: clause is used when initializing
the Kubernetes clients (KrknKubernetes and KrknOpenshift). This is a well-known
Python anti-pattern that silently catches all exceptions, including SystemExit,
KeyboardInterrupt, and NameError making failures invisible and extremely hard
to debug.

The Problem

The original code:

try:
    kubeconfig_path = str(kubeconfig_path)
    kubecli = KrknKubernetes(kubeconfig_path=kubeconfig_path)
    ocpcli = KrknOpenshift(kubeconfig_path=kubeconfig_path)
    except:
        kubecli.initialize_clients(None)  # NameError if kubecli was never assigned

Four issues with this:

  1. Bare except: catches everything, including KeyboardInterrupt and SystemExit,
    which should never be swallowed.
  2. Silent failure, if KrknKubernetes() raises an exception, the error is swallowed
    with no log message. You get zero feedback about what went wrong.
  3. NameError in the except block, if the exception is raised before kubecli is
    assigned, then kubecli.initialize_clients(None) raises a NameError. That
    secondary exception is also silently lost.
  4. kubeconfig_path bare expression kubeconfig_path on its own does nothing useful;
    it was likely intended as an existence check but has no effect.

Expected Behaviour

  • Catch only specific, expected exceptions (e.g. Exception)
  • Log a clear error message when client initialization fails
  • Exit gracefully with a non-zero code instead of proceeding with an undefined kubecli

Proposed Fix

try:
    kubeconfig_path = str(kubeconfig_path)
    kubecli = KrknKubernetes(kubeconfig_path=kubeconfig_path)
    ocpcli = KrknOpenshift(kubeconfig_path=kubeconfig_path)
    except Exception as e:
        logging.error(
            "Failed to initialize Kubernetes client with kubeconfig %s: %s",
            kubeconfig_path, e,
        )
        return -1

Steps to Reproduce

  1. Provide an invalid or inaccessible kubeconfig path in config.yaml
  2. Run python run_kraken.py
  3. No error is logged execution continues with an undefined kubecli, leading to
    a confusing AttributeError or NameError later in the run

Impact

  • Makes debugging initialization failures very difficult
  • Can cause misleading secondary errors far from the actual root cause
  • Violates PEP 8 and Python best practices

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions