{"record":{"id":"310c16d21f5398c1","repo":"derailed/k9s","slug":"unable-to-locate-associated-cluster-for-context-q","errorCode":null,"errorMessage":"unable to locate associated cluster for context %q: %w","messagePattern":"unable to locate associated cluster for context %q: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/config/config.go","lineNumber":305,"sourceCode":"\tif err := yaml.Unmarshal(bb, &cfg); err != nil {\n\t\terrs = errors.Join(errs, fmt.Errorf(\"main config.yaml load failed: %w\", err))\n\t}\n\tc.Merge(&cfg)\n\n\treturn errs\n}\n\n// Save configuration to disk.\nfunc (c *Config) Save(force bool) error {\n\tcontextName := c.K9s.ActiveContextName()\n\t// Skip saving if no context is configured\n\tif contextName == \"\" {\n\t\tslog.Debug(\"No context configured, skipping config save\")\n\t\treturn nil\n\t}\n\tclusterName, err := c.ActiveClusterName(contextName)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to locate associated cluster for context %q: %w\", contextName, err)\n\t}\n\tc.Validate(contextName, clusterName)\n\tif err := c.K9s.Save(contextName, clusterName, force); err != nil {\n\t\treturn err\n\t}\n\tif _, err := os.Stat(AppConfigFile); errors.Is(err, fs.ErrNotExist) {\n\t\treturn c.SaveFile(AppConfigFile)\n\t}\n\n\treturn nil\n}\n\n// SaveFile K9s configuration to disk.\nfunc (c *Config) SaveFile(path string) error {\n\tif err := data.EnsureDirPath(path, data.DefaultDirMod); err != nil {\n\t\treturn err\n\t}\n","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/config/config.go#L287-L323","documentation":"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.","triggerScenarios":"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.","commonSituations":"Long-running k9s session across kubeconfig churn; kubectx rename mid-session; cluster-login tools rewriting the kubeconfig; automated cleanup scripts deleting stale contexts.","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"],"exampleFix":"# before: context deleted mid-session, k9s tries to save on exit\nkubectl config delete-context prod\n# k9s exit -> 'unable to locate associated cluster for context \"prod\": ...'\n\n# after: recreate or switch before exiting\nkubectl config set-context prod --cluster=prod-c --user=me\n# or: inside k9s, :ctx <valid-context> first, then exit","handlingStrategy":"validation","validationCode":"func canSave(cfg *config.Config, kubeCfg *client.Config, ctxName string) error {\n\tctxs, err := kubeCfg.Contexts()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"kubeconfig unreadable, save skipped: %w\", err)\n\t}\n\tif _, ok := ctxs[ctxName]; !ok {\n\t\treturn fmt.Errorf(\"active context %q missing from kubeconfig; save skipped\", ctxName)\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"if err := cfg.Save(force); err != nil {\n\tif strings.Contains(err.Error(), \"unable to locate associated cluster\") {\n\t\t// non-fatal: preferences for this context will not persist;\n\t\t// warn and continue instead of aborting shutdown\n\t}\n}","preventionTips":["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"],"tags":["kubernetes","k9s","context","cluster","kubeconfig","save"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}