{"record":{"id":"901ee58988f486e4","repo":"derailed/k9s","slug":"context-q-does-not-exist","errorCode":null,"errorMessage":"context %q does not exist","messagePattern":"context %q does not exist","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/client/config.go","lineNumber":94,"sourceCode":"func (c *Config) Flags() *genericclioptions.ConfigFlags {\n\treturn c.flags\n}\n\nfunc (c *Config) RawConfig() (api.Config, error) {\n\treturn c.clientConfig().RawConfig()\n}\n\nfunc (c *Config) clientConfig() clientcmd.ClientConfig {\n\treturn c.flags.ToRawKubeConfigLoader()\n}\n\nfunc (*Config) reset() {}\n\n// SwitchContext changes the kubeconfig context to a new cluster.\nfunc (c *Config) SwitchContext(name string) error {\n\tct, err := c.GetContext(name)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"context %q does not exist\", name)\n\t}\n\t// !!BOZO!! Do you need to reset the flags?\n\tflags := genericclioptions.NewConfigFlags(UsePersistentConfig)\n\tflags.Context, flags.ClusterName = &name, &ct.Cluster\n\tflags.Namespace = c.flags.Namespace\n\tflags.Timeout = c.flags.Timeout\n\tflags.KubeConfig = c.flags.KubeConfig\n\tflags.Impersonate = c.flags.Impersonate\n\tflags.ImpersonateGroup = c.flags.ImpersonateGroup\n\tflags.ImpersonateUID = c.flags.ImpersonateUID\n\tflags.Insecure = c.flags.Insecure\n\tflags.BearerToken = c.flags.BearerToken\n\n\tc.flags = flags\n\n\treturn nil\n}\n","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/client/config.go#L76-L112","documentation":"Returned by Config.SwitchContext (internal/client/config.go:94) when the helper GetContext(name) cannot find the requested context in the merged kubeconfig. SwitchContext deliberately wraps the miss with the requested name because GetContext's own message ('getcontext - invalid context specified') lacks the operation context. The switch is aborted before any kubeconfig mutation happens.","triggerScenarios":"Calling SwitchContext(\"prod\") when 'prod' is not a key in the contexts map of the merged KUBECONFIG files — typo in the name, the kubeconfig file was replaced/deleted after load, or the context lives in a kubeconfig file not listed in the KUBECONFIG search path.","commonSituations":"Renaming or deleting a context with kubectl config delete-context while k9s is running; a typo in the :ctx command; KUBECONFIG env var differing between the shell that launched k9s and the one used to create the context.","solutions":["List valid names with kubectl config get-contexts and retry with the exact spelling (names are case-sensitive)","Verify which files are merged: echo $KUBECONFIG — the context may exist in a file outside the merge order","Create the context if it should exist: kubectl config set-context prod --cluster=... --user=...","Restart k9s after kubeconfig changes so the in-memory view is refreshed"],"exampleFix":"# before\nkubectl config use-context prodd   # typo, fails later in k9s too\n\n# after\nkubectl config get-contexts         # confirm exact name 'prod'\nkubectl config use-context prod","handlingStrategy":"validation","validationCode":"func safeSwitchContext(cfg *client.Config, name string) error {\n\tctxs, err := cfg.Contexts()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"kubeconfig unreadable: %w\", err)\n\t}\n\tif _, ok := ctxs[name]; !ok {\n\t\treturn fmt.Errorf(\"context %q not in kubeconfig (have: %v)\",\n\t\t\tname, maps.Keys(ctxs))\n\t}\n\treturn cfg.SwitchContext(name)\n}","typeGuard":null,"tryCatchPattern":"if err := cfg.SwitchContext(name); err != nil {\n\tif strings.Contains(err.Error(), \"does not exist\") {\n\t\t// prompt the user to pick from `kubectl config get-contexts`\n\t}\n\treturn err\n}","preventionTips":["Always source context names from cfg.Contexts()/kubectl config get-contexts instead of hardcoding strings","After any kubectl config delete-context/rename, restart k9s before switching contexts","Echo $KUBECONFIG in launch scripts so context lookups hit the same files you edit with kubectl"],"tags":["kubernetes","kubeconfig","context","config"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}