derailed/k9s · error

no active context available

Error message

no active context available

What it means

Returned by K9s.ActiveContext (internal/config/k9s.go:220) when there is no cached active *data.Config and ActiveContextName() returns an empty string. The active context name comes from the k9s config's currentContext, falling back to the kubeconfig's current-context; empty means neither source names a context, so k9s cannot determine which cluster configuration to load.

Source

Thrown at internal/config/k9s.go:220

		return "", err
	}

	return act.Namespace.Active, nil
}

// ActiveContextName returns the active context name.
func (k *K9s) ActiveContextName() string {
	return k.getActiveContextName()
}

// ActiveContext returns the currently active context.
func (k *K9s) ActiveContext() (*data.Context, error) {
	if cfg := k.getActiveConfig(); cfg != nil && cfg.Context != nil {
		return cfg.Context, nil
	}
	ctxName := k.ActiveContextName()
	if ctxName == "" {
		return nil, errors.New("no active context available")
	}
	ct, err := k.ActivateContext(ctxName)

	return ct, err
}

func (k *K9s) setActiveConfig(c *data.Config) {
	k.mx.Lock()
	defer k.mx.Unlock()

	k.activeConfig = c
}

func (k *K9s) getActiveConfig() *data.Config {
	k.mx.RLock()
	defer k.mx.RUnlock()

	return k.activeConfig

View on GitHub (pinned to 2d3ccc6ba2)

Solutions

  1. Set a current context in kubeconfig: kubectl config use-context <name>, then restart
  2. Ensure KUBECONFIG points to an existing file with defined contexts (kubectl config get-contexts)
  3. Pass the context explicitly on startup (k9s --context <name>) or set it in the k9s config so ActiveContextName() is never empty
  4. If multiple kubeconfig files are merged, verify the merged result still yields a current-context

Example fix

# before
$ KUBECONFIG=/dev/null k9s
# -> no active context available

# after
$ kubectl config use-context minikube
$ k9s --context minikube
Defensive patterns

Strategy: validation

Validate before calling

if k9sCfg.ActiveContextName() == "" {
    return fmt.Errorf("select a context first (k9s --context <name>)")
}
ct, err := k9sCfg.ActiveContext()

Try / catch

ct, err := k9sCfg.ActiveContext()
if err != nil && strings.Contains(err.Error(), "no active context") {
    // prompt user to pick a context or exit with guidance
}

Prevention

When it happens

Trigger: First run with no kubeconfig or a kubeconfig whose current-context is unset and no K9s context configured; environment where KUBECONFIG points to an empty/invalid file; config state cleared mid-session.

Common situations: Fresh machine or CI container without kubectl setup; KUBECONFIG env var pointing to a wrong path; kubeconfig hand-edited and current-context line removed; automated environments that never selected a context.

Related errors


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