{"record":{"id":"accea82601779e74","repo":"ahmetb/kubectx","slug":"failed-to-query-if-namespace-exists-is-cluster-ac","errorCode":null,"errorMessage":"failed to query if namespace exists (is cluster accessible?): %w","messagePattern":"failed to query if namespace exists \\(is cluster accessible\\?\\): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubens/switch.go","lineNumber":80,"sourceCode":"\t}\n\n\tf := NewNSFile(ctx)\n\tprev, err := f.Load()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to load previous namespace from file: %w\", err)\n\t}\n\n\tif ns == \"-\" {\n\t\tif prev == \"\" {\n\t\t\treturn \"\", fmt.Errorf(\"No previous namespace found for current context (%s)\", ctx)\n\t\t}\n\t\tns = prev\n\t}\n\n\tif !force {\n\t\tok, err := namespaceExists(kc, ns)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to query if namespace exists (is cluster accessible?): %w\", err)\n\t\t}\n\t\tif !ok {\n\t\t\treturn \"\", fmt.Errorf(\"no namespace exists with name \\\"%s\\\"\", ns)\n\t\t}\n\t}\n\n\tif err := kc.SetNamespace(ctx, ns); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to change to namespace \\\"%s\\\": %w\", ns, err)\n\t}\n\tif err := kc.Save(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to save kubeconfig file: %w\", err)\n\t}\n\tif curNS != ns {\n\t\tif err := f.Save(curNS); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to save the previous namespace to file: %w\", err)\n\t\t}\n\t}\n\treturn ns, nil","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubens/switch.go#L62-L98","documentation":"Unless --force is passed, switchNamespace verifies the target namespace actually exists by querying the cluster API via namespaceExists. This error wraps a failure of that existence check itself — the tool could not determine whether the namespace exists, usually because the cluster is unreachable. The message explicitly hints 'is cluster accessible?'. Thrown at cmd/kubens/switch.go:80.","triggerScenarios":"namespaceExists(kc, ns) returns an error: newKubernetesClientSet fails or the CoreV1 Namespaces().Get call fails with a non-NotFound error (connection refused, TLS failure, auth error, timeout, DNS failure).","commonSituations":"VPN disconnected; cluster behind a bastion with a stale kubectl-proxy requirement; kubeconfig server URL points to an old API endpoint; expired client certificate or token; DNS name for the cluster no longer resolves; air-gapped/offline machine.","solutions":["Test cluster connectivity: kubectl cluster-info or kubectl get ns — fix network/VPN if it fails","Verify the kubeconfig server URL matches the current cluster endpoint (IPs change after cluster recreation)","Refresh credentials: re-login (cloud provider auth plugin), rotate an expired client cert or token","If you intentionally want to switch without cluster verification, pass --force to kubens"],"exampleFix":"// before\nkubens prod-ns            # offline: existence check fails\n// after\nkubens --force prod-ns    # skip cluster-side existence check (switch offline)","handlingStrategy":"retry","validationCode":"// cheap pre-flight before the existence check\nconn, err := net.DialTimeout(\"tcp\", hostPortFromKubeconfig(kc), 5*time.Second)\nif err != nil { return fmt.Errorf(\"cluster unreachable: %w\", err) }\nconn.Close()","typeGuard":null,"tryCatchPattern":"ok, err := namespaceExists(kc, ns)\nif err != nil {\n    if isTransientNetErr(err) { // timeouts, conn refused\n        time.Sleep(2 * time.Second)\n        ok, err = namespaceExists(kc, ns)\n    }\n    if err != nil { return fmt.Errorf(\"cluster check failed (use --force to skip): %w\", err) }\n}","preventionTips":["Run `kubectl cluster-info` as a pre-flight before automated kubens calls","Keep VPN/bastion connections alive in scripts that switch namespaces","Use --force when switching offline is intentional","Update server URLs after cluster recreation (IP/DNS changes)"],"tags":["network","kubernetes-api","kubens"],"backgroundTag":"cluster-unreachable","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}