{"record":{"id":"566b3d8fd45b657f","repo":"derailed/k9s","slug":"current-cluster-invalid-context-specified-q","errorCode":null,"errorMessage":"current-cluster - invalid context specified: %q","messagePattern":"current-cluster - invalid context specified: %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/client/config.go","lineNumber":148,"sourceCode":"\n// CurrentClusterName returns the currently active cluster name.\nfunc (c *Config) CurrentClusterName() (string, error) {\n\tif isSet(c.flags.ClusterName) {\n\t\treturn *c.flags.ClusterName, nil\n\t}\n\tcfg, err := c.RawConfig()\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tct, ok := cfg.Contexts[cfg.CurrentContext]\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"invalid current context specified: %q\", cfg.CurrentContext)\n\t}\n\tif isSet(c.flags.Context) {\n\t\tct, ok = cfg.Contexts[*c.flags.Context]\n\t\tif !ok {\n\t\t\treturn \"\", fmt.Errorf(\"current-cluster - invalid context specified: %q\", *c.flags.Context)\n\t\t}\n\t}\n\n\treturn ct.Cluster, nil\n}\n\n// CurrentContextName returns the currently active config context.\nfunc (c *Config) CurrentContextName() (string, error) {\n\tif isSet(c.flags.Context) {\n\t\treturn *c.flags.Context, nil\n\t}\n\tcfg, err := c.RawConfig()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"fail to load rawConfig: %w\", err)\n\t}\n\n\treturn cfg.CurrentContext, nil\n}","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/client/config.go#L130-L166","documentation":"Returned by Config.CurrentClusterName (internal/client/config.go:148) in the branch where a --context override flag is set (isSet(c.flags.Context)). Even though the kubeconfig's own current-context resolved fine, the explicitly requested context is not present in the contexts map, so its cluster cannot be determined. The 'current-cluster - ' prefix distinguishes this from the current-context variant at line 143.","triggerScenarios":"Launching k9s (or constructing Config) with --context <name> where <name> is not a key in the merged kubeconfig contexts: a typo, a stale flag/alias referencing a renamed context, or a wrapper script hardcoding a context that no longer exists.","commonSituations":"Shell aliases or launcher scripts pinned to an old context name; kubectx renames while muscle memory types the old name; CI job templates with hardcoded --context values after cluster migration.","solutions":["Compare the flag value against kubectl config get-contexts output and fix the spelling","Drop the --context flag entirely to fall back to the kubeconfig's current-context","If the context is expected, create or restore it in the kubeconfig that is actually being loaded (check KUBECONFIG)"],"exampleFix":"# before\nk9s --context prod-eu-wrong\n\n# after\nkubectl config get-contexts   # find exact name e.g. 'prod-eu1'\nk9s --context prod-eu1","handlingStrategy":"validation","validationCode":"func validateContextFlag(cfg *client.Config, flagCtx string) error {\n\tctxs, err := cfg.Contexts()\n\tif err != nil {\n\t\treturn err\n\t}\n\tif _, ok := ctxs[flagCtx]; !ok {\n\t\treturn fmt.Errorf(\"--context %q unknown; available: %v\", flagCtx, maps.Keys(ctxs))\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate --context values against kubectl config get-contexts in wrapper scripts before exec'ing k9s","Avoid pinning context names in aliases; resolve them dynamically at launch","After renaming contexts (kubectx), grep your shell config and scripts for the old name"],"tags":["kubernetes","kubeconfig","context","flags","cluster"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}