{"record":{"id":"6f109ee965b26f2c","repo":"derailed/k9s","slug":"k8sflags-unable-to-activate-context-q-w","errorCode":null,"errorMessage":"k8sflags. unable to activate context %q: %w","messagePattern":"k8sflags\\. unable to activate context %q: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/config/config.go","lineNumber":103,"sourceCode":"\tflags.Timeout = &v\n}\n\n// Refine the configuration based on cli args.\nfunc (c *Config) Refine(flags *genericclioptions.ConfigFlags, k9sFlags *Flags, cfg *client.Config) error {\n\tif flags == nil {\n\t\treturn nil\n\t}\n\n\tif !isStringSet(flags.Timeout) {\n\t\tif d, err := time.ParseDuration(c.K9s.APIServerTimeout); err == nil {\n\t\t\tsetK8sTimeout(flags, d)\n\t\t} else {\n\t\t\tsetK8sTimeout(flags, client.DefaultCallTimeoutDuration)\n\t\t}\n\t}\n\tif isStringSet(flags.Context) {\n\t\tif _, err := c.K9s.ActivateContext(*flags.Context); err != nil {\n\t\t\treturn fmt.Errorf(\"k8sflags. unable to activate context %q: %w\", *flags.Context, err)\n\t\t}\n\t} else {\n\t\tn, err := cfg.CurrentContextName()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to retrieve kubeconfig current context %q: %w\", n, err)\n\t\t}\n\n\t\tif n != \"\" {\n\t\t\t_, err = c.K9s.ActivateContext(n)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"unable to activate context %q: %w\", n, err)\n\t\t\t}\n\t\t} else {\n\t\t\tslog.Debug(\"No context set, skipping context activation\")\n\t\t}\n\t}\n\tif c.K9s.ActiveContextName() != \"\" {\n\t\tslog.Debug(\"Using active context\", slogs.Context, c.K9s.ActiveContextName())","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/config/config.go#L85-L121","documentation":"Returned from the k8s-flag bootstrap in internal/config/config.go:103 when a --context flag is set and c.K9s.ActivateContext(*flags.Context) fails. This is a wrapper: the real failure is in the wrapped error (%w) and can be any activation-stage error — context missing from kubeconfig (getcontext error), per-context config load failure (dir.Load), or proxy connectivity failure. The 'k8sflags.' prefix marks the flag-driven path, distinguishing it from the implicit-context variant at line 114.","triggerScenarios":"Launching k9s with --context <name> where the name is not in the kubeconfig, or where the context's k9s config file (contexts/<cluster>/<name>.yaml) is corrupt, or where the context config sets a proxy address that is unreachable when k9s already holds a live connection.","commonSituations":"Startup scripts pinning --context to a renamed/deleted context; leftover context yaml from an older k9s version after upgrade; corporate proxy address changed; KUBECONFIG pointing at a different file than the one defining the context.","solutions":["Read the wrapped chain first (errors.Unwrap / fmt.Printf with %v shows the full chain) — fix the root cause, not this wrapper","Verify the context exists: kubectl config get-contexts against the same KUBECONFIG k9s uses","Inspect the per-context file under $K9S_CONFIG_DIR/contexts/... for YAML damage or a bad proxy address","As a last resort remove the per-context yaml so k9s regenerates defaults"],"exampleFix":"# before\nk9s --context prod  # fails: 'k8sflags. unable to activate context \"prod\": ...'\n\n# after: unwrap root cause, e.g. proxy unreachable\ngrep -A2 'proxy:' ~/.local/share/k9s/clusters/*/prod.yaml\n# fix address or delete the file, then retry\nk9s --context prod","handlingStrategy":"try-catch","validationCode":"// Before startup, confirm the flag context resolves end-to-end:\nfunc preflightContextFlag(k9sCfg *config.Config, kubeCfg *client.Config, flagCtx string) error {\n\tif _, err := kubeCfg.GetContext(flagCtx); err != nil {\n\t\treturn fmt.Errorf(\"--context %q invalid: %w\", flagCtx, err)\n\t}\n\treturn nil // per-context yaml/proxy issues still surface at activation\n}","typeGuard":null,"tryCatchPattern":"if err := cfg.Init(k8sFlags); err != nil {\n\t// walk the chain: k8sflags wrapper -> activation -> root cause\n\tfor e := err; e != nil; e = errors.Unwrap(e) {\n\t\tswitch {\n\t\tcase strings.Contains(e.Error(), \"getcontext\"):\n\t\t\t// fix context name / kubeconfig\n\t\tcase strings.Contains(e.Error(), \"yaml load failed\"):\n\t\t\t// fix per-context config file\n\t\tcase strings.Contains(e.Error(), \"unable to connect\"):\n\t\t\t// fix proxy address\n\t\t}\n\t}\n}","preventionTips":["On startup failure, always read the whole wrapped chain — the k8sflags prefix is just the outermost layer","Validate --context values in wrapper scripts against kubectl config get-contexts","After k9s upgrades, smoke-test one context activation before rolling the new binary to users"],"tags":["kubernetes","k9s","context","startup","flags","wrapped-error"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}