{"record":{"id":"be5f9564fe7f9969","repo":"derailed/k9s","slug":"getcontext-invalid-context-specified-q","errorCode":null,"errorMessage":"getcontext - invalid context specified: %q","messagePattern":"getcontext - invalid context specified: %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/client/config.go","lineNumber":200,"sourceCode":"func (c *Config) CurrentContext() (*api.Context, error) {\n\tn, err := c.CurrentContextName()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn c.GetContext(n)\n}\n\n// GetContext fetch a given context or error if it does not exist.\nfunc (c *Config) GetContext(n string) (*api.Context, error) {\n\tcfg, err := c.RawConfig()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif c, ok := cfg.Contexts[n]; ok {\n\t\treturn c, nil\n\t}\n\n\treturn nil, fmt.Errorf(\"getcontext - invalid context specified: %q\", n)\n}\n\n// SetProxy sets the proxy function.\nfunc (c *Config) SetProxy(proxy func(*http.Request) (*url.URL, error)) {\n\tc.proxy = proxy\n}\n\n// Contexts fetch all available contexts.\nfunc (c *Config) Contexts() (map[string]*api.Context, error) {\n\tcfg, err := c.RawConfig()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn cfg.Contexts, nil\n}\n\n// DelContext remove a given context from the configuration.","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/client/config.go#L182-L218","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["List what exists: kubectl config get-contexts -o name","Fix the requested name or create the context (kubectl config set-context)","Verify $KUBECONFIG includes the file that defines the context","Restart k9s after kubeconfig mutations so lookups see current data"],"exampleFix":"# before\nctx, err := cfg.GetContext(\"staging\")  // does not exist -> error\n\n# after: check membership first\nctxs, err := cfg.Contexts()\nif err != nil {\n\treturn err\n}\nif _, ok := ctxs[\"staging\"]; !ok {\n\treturn fmt.Errorf(\"unknown context %q; run `kubectl config get-contexts`\", \"staging\")\n}\nctx, err = cfg.GetContext(\"staging\")","handlingStrategy":"validation","validationCode":"func contextExists(cfg *client.Config, name string) (bool, error) {\n\tctxs, err := cfg.Contexts()\n\tif err != nil {\n\t\treturn false, err\n\t}\n\t_, ok := ctxs[name]\n\treturn ok, nil\n}","typeGuard":null,"tryCatchPattern":"if _, err := cfg.GetContext(name); err != nil {\n\tif strings.Contains(err.Error(), \"getcontext - invalid context specified\") {\n\t\t// unknown context: offer a picker fed by cfg.Contexts()\n\t}\n\treturn err\n}","preventionTips":["Fetch the context list once at startup and drive all name inputs from it (UI pickers, not free text)","Treat GetContext errors near the top of any wrapped chain as the root cause — fix that first","Re-validate context names after kubeconfig-mutating operations"],"tags":["kubernetes","kubeconfig","context","lookup"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}