derailed/k9s · error

getcontext - invalid context specified: %q

Error message

getcontext - invalid context specified: %q

What it means

Returned by Config.GetContext (internal/client/config.go:200), the central context-lookup helper: after RawConfig succeeds, the requested name n is not a key in cfg.Contexts. Because nearly every context API (SwitchContext, ActivateContext chains, CurrentContextNamespace) funnels through GetContext, this message often appears as the wrapped root cause of higher-level errors.

Source

Thrown at internal/client/config.go:200

func (c *Config) CurrentContext() (*api.Context, error) {
	n, err := c.CurrentContextName()
	if err != nil {
		return nil, err
	}
	return c.GetContext(n)
}

// GetContext fetch a given context or error if it does not exist.
func (c *Config) GetContext(n string) (*api.Context, error) {
	cfg, err := c.RawConfig()
	if err != nil {
		return nil, err
	}
	if c, ok := cfg.Contexts[n]; ok {
		return c, nil
	}

	return nil, fmt.Errorf("getcontext - invalid context specified: %q", n)
}

// SetProxy sets the proxy function.
func (c *Config) SetProxy(proxy func(*http.Request) (*url.URL, error)) {
	c.proxy = proxy
}

// Contexts fetch all available contexts.
func (c *Config) Contexts() (map[string]*api.Context, error) {
	cfg, err := c.RawConfig()
	if err != nil {
		return nil, err
	}

	return cfg.Contexts, nil
}

// DelContext remove a given context from the configuration.

View on GitHub (pinned to 2d3ccc6ba2)

Solutions

  1. List what exists: kubectl config get-contexts -o name
  2. Fix the requested name or create the context (kubectl config set-context)
  3. Verify $KUBECONFIG includes the file that defines the context
  4. Restart k9s after kubeconfig mutations so lookups see current data

Example fix

# before
ctx, err := cfg.GetContext("staging")  // does not exist -> error

# after: check membership first
ctxs, err := cfg.Contexts()
if err != nil {
	return err
}
if _, ok := ctxs["staging"]; !ok {
	return fmt.Errorf("unknown context %q; run `kubectl config get-contexts`", "staging")
}
ctx, err = cfg.GetContext("staging")
Defensive patterns

Strategy: validation

Validate before calling

func contextExists(cfg *client.Config, name string) (bool, error) {
	ctxs, err := cfg.Contexts()
	if err != nil {
		return false, err
	}
	_, ok := ctxs[name]
	return ok, nil
}

Try / catch

if _, err := cfg.GetContext(name); err != nil {
	if strings.Contains(err.Error(), "getcontext - invalid context specified") {
		// unknown context: offer a picker fed by cfg.Contexts()
	}
	return err
}

Prevention

When it happens

Trigger: Direct call GetContext("staging") with no such key in the merged kubeconfig; or indirect via SwitchContext/ActivateContext after the context was deleted or the KUBECONFIG set changed since the process loaded it.

Common situations: Context deleted via kubectl config delete-context while k9s runs; kubeconfig swapped by a cluster-login tool (sso, oidc); typo'd name; context defined only in a file not on the KUBECONFIG path.

Related errors


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