abiosoft/colima · error

error fetching kubeconfig on guest: %w

Error message

error fetching kubeconfig on guest: %w

What it means

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.

Source

Thrown at environment/container/kubernetes/kubeconfig.go:55

	profile := config.CurrentProfile().ID
	hostKubeDir := filepath.Join(hostHome, ".kube")
	a.Add(func() error {
		return c.host.Run("mkdir", "-p", filepath.Join(hostKubeDir, "."+profile))
	})

	kubeconfFile := filepath.Join(hostKubeDir, "config")
	envKubeConfFile := c.host.Env("KUBECONFIG")
	if envKubeConfFile != "" {
		kubeconfFile = filepath.SplitList(envKubeConfFile)[0]
	}
	tmpkubeconfFile := filepath.Join(hostKubeDir, "."+profile, "colima-temp")

	// manipulate in VM and save to host
	a.Add(func() error {
		kubeconfig, err := c.guest.Read("/etc/rancher/k3s/k3s.yaml")
		if err != nil {
			return fmt.Errorf("error fetching kubeconfig on guest: %w", err)
		}
		// replace name
		kubeconfig = strings.ReplaceAll(kubeconfig, ": default", ": "+profile)

		// replace IP
		if ip != "" && ip != "127.0.0.1" {
			kubeconfig = strings.ReplaceAll(kubeconfig, "https://127.0.0.1:", "https://"+ip+":")
		}

		// save on the host
		return c.host.Write(tmpkubeconfFile, []byte(kubeconfig))
	})

	// merge on host
	a.Add(func() (err error) {
		// prepare new host with right env var.
		envVar := fmt.Sprintf("KUBECONFIG=%s:%s", kubeconfFile, tmpkubeconfFile)
		host := c.host.WithEnv(envVar)

View on GitHub (pinned to c3a5f9184d)

Solutions

  1. 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'.
  2. 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'.
  3. 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.
  4. Ensure the VM has disk space ('colima ssh -- df -h /') — k3s writes kubeconfig+certs at first boot.
  5. Recreate the cluster from scratch if state is corrupt: 'colima delete profile --data kubernetes' / 'colima delete -f && colima start --kubernetes'.
Defensive patterns

Strategy: retry

Validate before calling

// confirm the kubeconfig exists in the guest and is readable before the copy step
if err := guest.Run("sudo", "test", "-r", "/etc/rancher/k3s/k3s.yaml"); err != nil {
    return fmt.Errorf("k3s kubeconfig not ready in guest; is k3s running?")
}

Try / catch

// transient k3s bootstrap: retry the read with backoff
var kubeconfig string
err := retry(10, 3*time.Second, func() error {
    b, err := c.guest.Read("/etc/rancher/k3s/k3s.yaml")
    if err != nil {
        return fmt.Errorf("error fetching kubeconfig on guest: %w", err)
    }
    kubeconfig = string(b)
    return nil
})

Prevention

When it happens

Trigger: '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).

Common situations: 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.

Related errors


AI-assisted analysis of abiosoft/colima@c3a5f9184d (2026-08-15). Data as JSON: /api/errors/3fb8d96671be462a. Report an issue: GitHub.