derailed/k9s · error

k8sflags. unable to activate context %q: %w

Error message

k8sflags. unable to activate context %q: %w

What it means

Returned from the k8s-flag bootstrap in internal/config/config.go:103 when a --context flag is set and c.K9s.ActivateContext(*flags.Context) fails. This is a wrapper: the real failure is in the wrapped error (%w) and can be any activation-stage error — context missing from kubeconfig (getcontext error), per-context config load failure (dir.Load), or proxy connectivity failure. The 'k8sflags.' prefix marks the flag-driven path, distinguishing it from the implicit-context variant at line 114.

Source

Thrown at internal/config/config.go:103

	flags.Timeout = &v
}

// Refine the configuration based on cli args.
func (c *Config) Refine(flags *genericclioptions.ConfigFlags, k9sFlags *Flags, cfg *client.Config) error {
	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())

View on GitHub (pinned to 2d3ccc6ba2)

Solutions

  1. Read the wrapped chain first (errors.Unwrap / fmt.Printf with %v shows the full chain) — fix the root cause, not this wrapper
  2. Verify the context exists: kubectl config get-contexts against the same KUBECONFIG k9s uses
  3. Inspect the per-context file under $K9S_CONFIG_DIR/contexts/... for YAML damage or a bad proxy address
  4. As a last resort remove the per-context yaml so k9s regenerates defaults

Example fix

# before
k9s --context prod  # fails: 'k8sflags. unable to activate context "prod": ...'

# after: unwrap root cause, e.g. proxy unreachable
grep -A2 'proxy:' ~/.local/share/k9s/clusters/*/prod.yaml
# fix address or delete the file, then retry
k9s --context prod
Defensive patterns

Strategy: try-catch

Validate before calling

// Before startup, confirm the flag context resolves end-to-end:
func preflightContextFlag(k9sCfg *config.Config, kubeCfg *client.Config, flagCtx string) error {
	if _, err := kubeCfg.GetContext(flagCtx); err != nil {
		return fmt.Errorf("--context %q invalid: %w", flagCtx, err)
	}
	return nil // per-context yaml/proxy issues still surface at activation
}

Try / catch

if err := cfg.Init(k8sFlags); err != nil {
	// walk the chain: k8sflags wrapper -> activation -> root cause
	for e := err; e != nil; e = errors.Unwrap(e) {
		switch {
		case strings.Contains(e.Error(), "getcontext"):
			// fix context name / kubeconfig
		case strings.Contains(e.Error(), "yaml load failed"):
			// fix per-context config file
		case strings.Contains(e.Error(), "unable to connect"):
			// fix proxy address
		}
	}
}

Prevention

When it happens

Trigger: Launching k9s with --context <name> where the name is not in the kubeconfig, or where the context's k9s config file (contexts/<cluster>/<name>.yaml) is corrupt, or where the context config sets a proxy address that is unreachable when k9s already holds a live connection.

Common situations: Startup scripts pinning --context to a renamed/deleted context; leftover context yaml from an older k9s version after upgrade; corporate proxy address changed; KUBECONFIG pointing at a different file than the one defining the context.

Related errors


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