{"record":{"id":"eb054f12beec3e8a","repo":"ahmetb/kubectx","slug":"failed-to-save-kubeconfig-file-w","errorCode":null,"errorMessage":"failed to save kubeconfig file: %w","messagePattern":"failed to save kubeconfig file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubens/switch.go","lineNumber":91,"sourceCode":"\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\n}\n\nfunc namespaceExists(kc *kubeconfig.Kubeconfig, ns string) (bool, error) {\n\t// for tests\n\tif os.Getenv(\"_MOCK_NAMESPACES\") != \"\" {\n\t\treturn ns == \"ns1\" || ns == \"ns2\", nil\n\t}\n\n\tclientset, err := newKubernetesClientSet(kc)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"failed to initialize k8s REST client: %w\", err)","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubens/switch.go#L73-L109","documentation":"After mutating the namespace in memory, switchNamespace persists the kubeconfig to disk via kc.Save(). This error wraps any failure writing the kubeconfig file — I/O errors, permission problems, or serialization failures. The in-memory change succeeded but was not committed. Thrown at cmd/kubens/switch.go:91.","triggerScenarios":"kc.Save() errors because the kubeconfig file or its parent directory is not writable (read-only filesystem, wrong owner), disk is full, or an immutable attribute / LSM policy blocks the write.","commonSituations":"~/.kube/config owned by root after running kubectl under sudo; read-only container filesystem; disk quota/full disk; the config file mounted read-only from a Kubernetes secret or ConfigMap in a pod; EACCES from SELinux/AppArmor.","solutions":["Fix ownership/permissions: sudo chown $USER ~/.kube/config && chmod 600 ~/.kube/config","Check disk space and quota: df -h ~/.kube","If the file is a read-only mount (secret/configmap), write to a writable copy and point KUBECONFIG at it","Verify no policy blocks writes: check mount options (mount | grep kube) and SELinux denials"],"exampleFix":"// before\n-rw------- 1 root root ~/.kube/config   # kubens cannot write as normal user\n// after\nsudo chown $USER:$USER ~/.kube/config\nkubens production","handlingStrategy":"validation","validationCode":"cfgPath := kubeconfigPath()\nif fi, err := os.Stat(cfgPath); err == nil {\n    if err := syscall.Access(cfgPath, os.O_RDWR); err != nil {\n        return fmt.Errorf(\"kubeconfig %s not writable by %s: %v\", cfgPath, os.Getenv(\"USER\"), err)\n    }\n    _ = fi\n}","typeGuard":null,"tryCatchPattern":"if err := kc.Save(); err != nil {\n    var perr *os.PathError\n    if errors.As(err, &perr) {\n        return fmt.Errorf(\"cannot write %s (check ownership/permissions/disk space): %w\", perr.Path, err)\n    }\n    return err\n}","preventionTips":["Never run kubectl/kubens under sudo — it leaves root-owned config files","Keep 0600/user-owned permissions on ~/.kube/config","Monitor disk space on machines running cluster tooling","In pods, do not mount kubeconfig from a read-only secret if tools must update it"],"tags":["filesystem","kubeconfig","permissions"],"backgroundTag":"kubeconfig-write-failed","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}