{"record":{"id":"8394e9f41a7855ec","repo":"derailed/k9s","slug":"unable-to-connect-to-context-q","errorCode":null,"errorMessage":"unable to connect to context %q","messagePattern":"unable to connect to context %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/config/k9s.go","lineNumber":285,"sourceCode":"\tk.setActiveConfig(cfg)\n\n\tif cfg.Context.Proxy != nil {\n\t\tk.ks.SetProxy(func(*http.Request) (*url.URL, error) {\n\t\t\tslog.Debug(\"Using proxy address\", slogs.Address, cfg.Context.Proxy.Address)\n\t\t\treturn url.Parse(cfg.Context.Proxy.Address)\n\t\t})\n\n\t\tif k.conn != nil && k.conn.Config() != nil {\n\t\t\t// We get on this branch when the user switches the context and k9s\n\t\t\t// already has an API connection object so we just set the proxy to\n\t\t\t// avoid recreation using client.InitConnection\n\t\t\tk.conn.Config().SetProxy(func(*http.Request) (*url.URL, error) {\n\t\t\t\tslog.Debug(\"Setting proxy address\", slogs.Address, cfg.Context.Proxy.Address)\n\t\t\t\treturn url.Parse(cfg.Context.Proxy.Address)\n\t\t\t})\n\n\t\t\tif !k.conn.CheckConnectivity() {\n\t\t\t\treturn nil, fmt.Errorf(\"unable to connect to context %q\", contextName)\n\t\t\t}\n\t\t}\n\t}\n\n\tk.Validate(k.conn, contextName, ct.Cluster)\n\t// If the context specifies a namespace, use it!\n\tif ns := ct.Namespace; ns != client.BlankNamespace {\n\t\tk.getActiveConfig().Context.Namespace.Active = ns\n\t} else if k.getActiveConfig().Context.Namespace.Active == \"\" {\n\t\tk.getActiveConfig().Context.Namespace.Active = client.DefaultNamespace\n\t}\n\tif k.getActiveConfig().Context == nil {\n\t\treturn nil, fmt.Errorf(\"context activation failed for: %s\", contextName)\n\t}\n\n\treturn k.getActiveConfig().Context, nil\n}\n","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/config/k9s.go#L267-L303","documentation":"Returned inside K9s.ActivateContext (internal/config/k9s.go:285) on the context-switch fast path: when the context's k9s config declares a proxy and k9s already holds a live connection object, the proxy is swapped onto the existing client (avoiding a full re-dial) and CheckConnectivity() is run immediately. If that check fails, activation aborts with this error naming the context. It fires only in the proxy!=nil branch — non-proxied contexts never hit it.","triggerScenarios":"A context config (contexts/<cluster>/<name>.yaml) with a proxy: block whose address is mistyped, unreachable, or requires auth, combined with switching to that context while k9s already has an API connection (e.g. pressing :ctx mid-session). Also when a corporate proxy goes down between sessions.","commonSituations":"Corporate/air-gapped environments routing cluster traffic through a proxy; proxy address changed by IT; copy-paste of proxy config between contexts with a stale port; VPN dropped so the proxy host no longer resolves.","solutions":["Verify the proxy address in the context yaml (contexts/<cluster>/<ctx>.yaml, proxy.address) and test it: curl -x <address> https://<api-server>/version","Fix the typo/port, or remove the proxy block entirely if the cluster is directly reachable","Restart k9s after fixing — the check only runs on the reuse-connection path, and a fresh start rebuilds the connection through client.InitConnection","If the proxy requires authentication, ensure the address scheme carries the credentials or use an authenticated local forwarder"],"exampleFix":"# before: contexts/prod-c/prod.yaml\ncontext:\n  proxy:\n    address: http://corporate-proxy:808\n\n# after (fix port)\ncontext:\n  proxy:\n    address: http://corporate-proxy:8080\n\n# or remove the block when direct access works","handlingStrategy":"validation","validationCode":"// Before activating a context that declares a proxy, verify the address:\nfunc proxyReachable(address, apiServer string) error {\n\tu, err := url.ParseRequestURI(address)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"bad proxy address %q: %w\", address, err)\n\t}\n\tconn, err := net.DialTimeout(\"tcp\", u.Host, 3*time.Second)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"proxy %q unreachable: %w\", address, err)\n\t}\n\tconn.Close()\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"if _, err := k9s.ActivateContext(name); err != nil {\n\tif strings.Contains(err.Error(), \"unable to connect to context\") {\n\t\t// proxy swapped onto existing conn failed connectivity:\n\t\t// fix/remove proxy address in the context yaml, then restart k9s\n\t\t// (a fresh process rebuilds the connection instead of reusing it)\n\t}\n}","preventionTips":["Smoke-test proxy configs before saving them into a context: curl -x <proxy> <api-server>/version","Prefer DNS names over IPs for proxy addresses so failovers do not break context configs","After changing proxy settings, restart k9s — the connectivity check only reuses the existing connection object on context switch"],"tags":["kubernetes","k9s","proxy","network","context","connectivity"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}