derailed/k9s · error
unable to locate associated cluster for context %q: %w
Error message
unable to locate associated cluster for context %q: %w
What it means
Returned from Config.Save (internal/config/config.go:305) when resolving the cluster for the currently active context via ActiveClusterName(contextName) fails. Save needs a context->cluster pair to pick the per-cluster config directory; if the active context name (held in k9s state) cannot be resolved to a cluster — usually because the context no longer exists in the kubeconfig, or the kubeconfig cannot be loaded — the save is aborted.
Source
Thrown at internal/config/config.go:305
if err := yaml.Unmarshal(bb, &cfg); err != nil {
errs = errors.Join(errs, fmt.Errorf("main config.yaml load failed: %w", err))
}
c.Merge(&cfg)
return errs
}
// Save configuration to disk.
func (c *Config) Save(force bool) error {
contextName := c.K9s.ActiveContextName()
// Skip saving if no context is configured
if contextName == "" {
slog.Debug("No context configured, skipping config save")
return nil
}
clusterName, err := c.ActiveClusterName(contextName)
if err != nil {
return fmt.Errorf("unable to locate associated cluster for context %q: %w", contextName, err)
}
c.Validate(contextName, clusterName)
if err := c.K9s.Save(contextName, clusterName, force); err != nil {
return err
}
if _, err := os.Stat(AppConfigFile); errors.Is(err, fs.ErrNotExist) {
return c.SaveFile(AppConfigFile)
}
return nil
}
// SaveFile K9s configuration to disk.
func (c *Config) SaveFile(path string) error {
if err := data.EnsureDirPath(path, data.DefaultDirMod); err != nil {
return err
}
View on GitHub (pinned to 2d3ccc6ba2)
Solutions
- Verify the active context still exists: kubectl config get-contexts (compare against the name in the error)
- Restore or recreate the context in the kubeconfig, then retry the save
- If the kubeconfig is fine, restart k9s so internal state re-syncs with disk
- If the context is permanently gone, accept that its k9s preferences will not persist and switch to a valid context first
Example fix
# before: context deleted mid-session, k9s tries to save on exit kubectl config delete-context prod # k9s exit -> 'unable to locate associated cluster for context "prod": ...' # after: recreate or switch before exiting kubectl config set-context prod --cluster=prod-c --user=me # or: inside k9s, :ctx <valid-context> first, then exit
Defensive patterns
Strategy: validation
Validate before calling
func canSave(cfg *config.Config, kubeCfg *client.Config, ctxName string) error {
ctxs, err := kubeCfg.Contexts()
if err != nil {
return fmt.Errorf("kubeconfig unreadable, save skipped: %w", err)
}
if _, ok := ctxs[ctxName]; !ok {
return fmt.Errorf("active context %q missing from kubeconfig; save skipped", ctxName)
}
return nil
} Try / catch
if err := cfg.Save(force); err != nil {
if strings.Contains(err.Error(), "unable to locate associated cluster") {
// non-fatal: preferences for this context will not persist;
// warn and continue instead of aborting shutdown
}
} Prevention
- Do not delete/rename the active kubeconfig context while a k9s session is live — switch inside k9s first
- Restart k9s after kubeconfig churn so saved state matches reality
- Treat Save failures as warnings in shutdown paths; losing favorites beats losing the session
When it happens
Trigger: The kubeconfig changed while k9s was running: the active context was deleted or renamed via kubectl, KUBECONFIG now points elsewhere, or the config file became unreadable — then any action triggering Save (exit, settings change) hits this.
Common situations: Long-running k9s session across kubeconfig churn; kubectx rename mid-session; cluster-login tools rewriting the kubeconfig; automated cleanup scripts deleting stale contexts.
Related errors
- invalid current context specified: %q
- current-cluster - invalid context specified: %q
- no active context available
- context %q does not exist
- getcontext - invalid context specified: %q
AI-assisted analysis of derailed/k9s@2d3ccc6ba2 (2026-08-15).
Data as JSON: /api/errors/310c16d21f5398c1.
Report an issue: GitHub.