{"record":{"id":"3fb8d96671be462a","repo":"abiosoft/colima","slug":"error-fetching-kubeconfig-on-guest-w","errorCode":null,"errorMessage":"error fetching kubeconfig on guest: %w","messagePattern":"error fetching kubeconfig on guest: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"environment/container/kubernetes/kubeconfig.go","lineNumber":55,"sourceCode":"\n\tprofile := config.CurrentProfile().ID\n\thostKubeDir := filepath.Join(hostHome, \".kube\")\n\ta.Add(func() error {\n\t\treturn c.host.Run(\"mkdir\", \"-p\", filepath.Join(hostKubeDir, \".\"+profile))\n\t})\n\n\tkubeconfFile := filepath.Join(hostKubeDir, \"config\")\n\tenvKubeConfFile := c.host.Env(\"KUBECONFIG\")\n\tif envKubeConfFile != \"\" {\n\t\tkubeconfFile = filepath.SplitList(envKubeConfFile)[0]\n\t}\n\ttmpkubeconfFile := filepath.Join(hostKubeDir, \".\"+profile, \"colima-temp\")\n\n\t// manipulate in VM and save to host\n\ta.Add(func() error {\n\t\tkubeconfig, err := c.guest.Read(\"/etc/rancher/k3s/k3s.yaml\")\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error fetching kubeconfig on guest: %w\", err)\n\t\t}\n\t\t// replace name\n\t\tkubeconfig = strings.ReplaceAll(kubeconfig, \": default\", \": \"+profile)\n\n\t\t// replace IP\n\t\tif ip != \"\" && ip != \"127.0.0.1\" {\n\t\t\tkubeconfig = strings.ReplaceAll(kubeconfig, \"https://127.0.0.1:\", \"https://\"+ip+\":\")\n\t\t}\n\n\t\t// save on the host\n\t\treturn c.host.Write(tmpkubeconfFile, []byte(kubeconfig))\n\t})\n\n\t// merge on host\n\ta.Add(func() (err error) {\n\t\t// prepare new host with right env var.\n\t\tenvVar := fmt.Sprintf(\"KUBECONFIG=%s:%s\", kubeconfFile, tmpkubeconfFile)\n\t\thost := c.host.WithEnv(envVar)","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/abiosoft/colima/blob/c3a5f9184d83a197184f897a9f07eb3c01b3bc88/environment/container/kubernetes/kubeconfig.go#L37-L73","documentation":"Produced when copying the freshly generated k3s kubeconfig out of the guest: c.guest.Read('/etc/rancher/k3s/k3s.yaml') failed. The kubeconfig manipulation step (rename cluster to the colima profile, rewrite 127.0.0.1 to the VM IP, write temp file on host) cannot proceed without the guest file, so the chain aborts with the wrapped guest error.","triggerScenarios":"'colima start --kubernetes' where k3s was installed but the k3s service has not yet written /etc/rancher/k3s/k3s.yaml (installed but service still starting); k3s failed to start inside the VM (check 'colima ssh -- sudo journalctl -u k3s' / k3s.log); SSH/limactl copy transport failing; the file exists but is unreadable due to permissions (guest.Read runs as the default non-root user and k3s.yaml is root-owned 0600 unless --write-kubeconfig-mode 644 is in effect, which colima sets during install — a partial or interrupted install can miss it).","commonSituations":"Racing the k3s service: running kubectl context setup too soon after start; k3s crashing in the guest due to low memory/CPU allocation; interrupted first provisioning run leaving k3s half-installed; guest disk full so k3s cannot write its config; VM clock skew breaking k3s bootstrap.","solutions":["Confirm k3s actually runs in the guest: 'colima ssh -- sudo systemctl status k3s' (or 'sudo k3s kubectl get nodes') and give it a moment if it is still starting, then re-run 'colima start --kubernetes'.","If k3s is crash-looping, read its logs ('colima ssh -- sudo journalctl -u k3s -n 100' or /var/log/k3s.log) and fix the root cause — commonly insufficient 'colima start --memory/--cpu'.","Check the file and its permissions directly: 'colima ssh -- ls -l /etc/rancher/k3s/k3s.yaml'; if missing, 'colima delete --data kubernetes' (older builds) or reinstall kubernetes via 'colima start --kubernetes=false' then re-enable.","Ensure the VM has disk space ('colima ssh -- df -h /') — k3s writes kubeconfig+certs at first boot.","Recreate the cluster from scratch if state is corrupt: 'colima delete profile --data kubernetes' / 'colima delete -f && colima start --kubernetes'."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// confirm the kubeconfig exists in the guest and is readable before the copy step\nif err := guest.Run(\"sudo\", \"test\", \"-r\", \"/etc/rancher/k3s/k3s.yaml\"); err != nil {\n    return fmt.Errorf(\"k3s kubeconfig not ready in guest; is k3s running?\")\n}","typeGuard":null,"tryCatchPattern":"// transient k3s bootstrap: retry the read with backoff\nvar kubeconfig string\nerr := retry(10, 3*time.Second, func() error {\n    b, err := c.guest.Read(\"/etc/rancher/k3s/k3s.yaml\")\n    if err != nil {\n        return fmt.Errorf(\"error fetching kubeconfig on guest: %w\", err)\n    }\n    kubeconfig = string(b)\n    return nil\n})","preventionTips":["Allocate enough memory/cpu for k3s ('colima start --memory 4') so it finishes bootstrap.","Don't script kubectl/context setup immediately at first kubernetes start; poll for the file.","Keep guest disk from filling — k3s writes certs+config on first boot."],"tags":["kubeconfig","kubernetes","k3s","guest","ssh"],"backgroundTag":null,"analyzedSha":"c3a5f9184d83a197184f897a9f07eb3c01b3bc88","analyzedAt":"2026-08-15T18:58:08.334Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}