derailed/k9s · error
invalid current context specified: %q
Error message
invalid current context specified: %q
What it means
Returned by Config.CurrentClusterName (internal/client/config.go:143) when no --cluster flag is set and the kubeconfig's current-context key names a context that does not exist in the contexts map. The kubeconfig is internally inconsistent: current-context points at a context that was never defined or has been removed. Resolution of the cluster name cannot proceed.
Source
Thrown at internal/client/config.go:143
flags.Timeout = c.Flags().Timeout
flags.KubeConfig = c.Flags().KubeConfig
return flags, nil
}
// CurrentClusterName returns the currently active cluster name.
func (c *Config) CurrentClusterName() (string, error) {
if isSet(c.flags.ClusterName) {
return *c.flags.ClusterName, nil
}
cfg, err := c.RawConfig()
if err != nil {
return "", err
}
ct, ok := cfg.Contexts[cfg.CurrentContext]
if !ok {
return "", fmt.Errorf("invalid current context specified: %q", cfg.CurrentContext)
}
if isSet(c.flags.Context) {
ct, ok = cfg.Contexts[*c.flags.Context]
if !ok {
return "", fmt.Errorf("current-cluster - invalid context specified: %q", *c.flags.Context)
}
}
return ct.Cluster, nil
}
// CurrentContextName returns the currently active config context.
func (c *Config) CurrentContextName() (string, error) {
if isSet(c.flags.Context) {
return *c.flags.Context, nil
}
cfg, err := c.RawConfig()
if err != nil {View on GitHub (pinned to 2d3ccc6ba2)
Solutions
- Run kubectl config get-contexts and check the CURRENT column against the contexts listed
- Point current-context at a real context: kubectl config use-context <existing-context>
- Or edit the kubeconfig to fix/remove the dangling current-context key
- If multiple files are merged via KUBECONFIG, fix the file that actually carries the stale current-context
Example fix
# before: kubeconfig has current-context: deleted-ctx contexts: - name: real-ctx # after kubectl config use-context real-ctx # current-context: real-ctx
Defensive patterns
Strategy: validation
Validate before calling
func validateCurrentContext(cfg *client.Config) error {
raw, err := cfg.RawConfig()
if err != nil {
return err
}
if _, ok := raw.Contexts[raw.CurrentContext]; raw.CurrentContext != "" && !ok {
return fmt.Errorf("kubeconfig current-context %q is dangling; run: kubectl config use-context <name>", raw.CurrentContext)
}
return nil
} Prevention
- Never delete the active context without immediately running kubectl config use-context <other>
- Lint kubeconfigs in CI (kubeconform or a small YAML+references check) before shipping them to users
- Prefer kubectl config use-context over hand-editing current-context in the YAML
When it happens
Trigger: cfg.CurrentContext (from the merged raw kubeconfig) is not a key in cfg.Contexts — e.g. someone deleted the active context with kubectl config delete-context without running use-context afterwards, or hand-edited the YAML leaving a dangling current-context.
Common situations: Manual kubeconfig editing; scripts that delete contexts; kubeconfig generated by a tool (oidc-login, kubectx rename) that wrote current-context before writing the context entry; truncated files.
Related errors
- no active context available
- context %q does not exist
- current-cluster - invalid context specified: %q
- unable to locate associated cluster for context %q: %w
- fail to load rawConfig: %w
AI-assisted analysis of derailed/k9s@2d3ccc6ba2 (2026-08-15).
Data as JSON: /api/errors/686716f9aab52ab9.
Report an issue: GitHub.