derailed/k9s · error

unable to retrieve kubeconfig current context

Error message

unable to retrieve kubeconfig current context %q: %w

What it means

Returned from internal/config/config.go:108 when no --context flag is set and cfg.CurrentContextName() fails while trying to derive the implicit context. The underlying cause is a RawConfig failure (kubeconfig unreadable/unparseable), which surfaces here wrapped. Note a formatting quirk: the %q prints the empty name n, because the failure occurs before any name resolves — the name in the message is always "".

Solutions

  1. Reproduce with kubectl config view to get the exact parse error and file
  2. Fix or remove the offending file from $KUBECONFIG (yamllint helps pinpoint the line)
  3. Restore the kubeconfig from a backup or regenerate it with kubectl config set-* commands
  4. Workaround: pass --context explicitly to bypass implicit resolution — but fix the kubeconfig regardless

Example fix

# before
k9s  # 'unable to retrieve kubeconfig current context "": ...'

# after: find and fix the corrupt file
kubectl config view --raw 2>&1 | head   # shows parse error + path
yamllint ~/.kube/config                  # fix reported line
k9s
Defensive patterns

Strategy: validation

Validate before calling

func kubeconfigPreflight() error {
	out, err := exec.Command("kubectl", "config", "view", "-o", "jsonpath={.current-context}").CombinedOutput()
	if err != nil {
		return fmt.Errorf("kubeconfig broken: %v: %s", err, out)
	}
	return nil
}

Try / catch

if err := cfg.Init(nil); err != nil {
	if strings.Contains(err.Error(), "unable to retrieve kubeconfig current context") {
		// root cause is the wrapped rawconfig error: broken kubeconfig file
		// run kubectl config view to locate it
	}
}

Prevention

When it happens

Trigger: Starting k9s without --context with a broken kubeconfig: malformed YAML in a KUBECONFIG file, unreadable file permissions, or an invalid multi-file merge. CurrentContextName's only failure mode in this branch is the rawconfig load.

Common situations: Hand-edited kubeconfig with syntax errors; concurrent write by kubelogin/oidc tools corrupting the file; KUBECONFIG referencing a removed path with bad content restored from a partial backup.

Related errors


AI-assisted analysis of derailed/k9s@2d3ccc6ba2 (2026-08-15). Data as JSON: /api/errors/7b274ec85ed04343. Report an issue: GitHub.

Appendix: source

Thrown at internal/config/config.go:108

	if flags == nil {
		return nil
	}

	if !isStringSet(flags.Timeout) {
		if d, err := time.ParseDuration(c.K9s.APIServerTimeout); err == nil {
			setK8sTimeout(flags, d)
		} else {
			setK8sTimeout(flags, client.DefaultCallTimeoutDuration)
		}
	}
	if isStringSet(flags.Context) {
		if _, err := c.K9s.ActivateContext(*flags.Context); err != nil {
			return fmt.Errorf("k8sflags. unable to activate context %q: %w", *flags.Context, err)
		}
	} else {
		n, err := cfg.CurrentContextName()
		if err != nil {
			return fmt.Errorf("unable to retrieve kubeconfig current context %q: %w", n, err)
		}

		if n != "" {
			_, err = c.K9s.ActivateContext(n)
			if err != nil {
				return fmt.Errorf("unable to activate context %q: %w", n, err)
			}
		} else {
			slog.Debug("No context set, skipping context activation")
		}
	}
	if c.K9s.ActiveContextName() != "" {
		slog.Debug("Using active context", slogs.Context, c.K9s.ActiveContextName())
	}

	var ns string
	switch {
	case k9sFlags != nil && IsBoolSet(k9sFlags.AllNamespaces):

View on GitHub (pinned to 2d3ccc6ba2)