kubernetes/kops · error

error writing temp file: %v

Error message

error writing temp file: %v

What it means

After creating the temp dir, KubectlApplier.Apply writes the manifest bytes to <tmpdir>/manifest.yaml with os.WriteFile so kubectl can read it. If the write fails, this error is returned. Like the temp-dir error, it points to a local filesystem/environment problem rather than a cluster problem.

Source

Thrown at channels/pkg/channels/kubectlapplier.go:49

// Apply calls kubectl apply to apply the manifest.
// We will likely in future change this to create things directly (or more likely embed this logic into kubectl itself)
func (*KubectlApplier) Apply(ctx context.Context, data []byte) error {
	// We copy the manifest to a temp file because it is likely e.g. an s3 URL, which kubectl can't read
	tmpDir, err := os.MkdirTemp("", "channel")
	if err != nil {
		return fmt.Errorf("error creating temp dir: %v", err)
	}

	defer func() {
		if err := os.RemoveAll(tmpDir); err != nil {
			klog.Warningf("error deleting temp dir %q: %v", tmpDir, err)
		}
	}()

	localManifestFile := path.Join(tmpDir, "manifest.yaml")
	if err := os.WriteFile(localManifestFile, data, 0o600); err != nil {
		return fmt.Errorf("error writing temp file: %v", err)
	}
	// First do an apply. This may fail when removing things from lists/arrays and required fields are not removed.
	{
		_, err := execKubectl(ctx, "apply", "-f", localManifestFile, "--server-side", "--force-conflicts", "--field-manager=kops")
		if err != nil {
			klog.Errorf("failed to apply the manifest: %v", err)
		}

	}

	// Replace will force ownership on all fields to kops. But on some k8s versions, this will fail on e.g trying to set clusterIP to "".
	{
		_, err := execKubectl(ctx, "replace", "-f", localManifestFile, "--field-manager=kops")
		if err != nil {
			klog.Errorf("failed to replace manifest: %v", err)
		}
	}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check disk space on the temp filesystem (df -h) and free space if full.
  2. Re-run the command — transient disk/cleaner races usually resolve on retry.
  3. Inspect the wrapped %v error (ENOSPC, EACCES, ENOENT) to target the fix.
  4. Disable/rule out tmp-cleaners or security agents interfering with the temp dir.
  5. Point TMPDIR at a different, stable writable filesystem.

Example fix

// before
export TMPDIR=/var/tmp-full-disk
kops ... # error writing temp file: no space left on device
// after
export TMPDIR=/mnt/big-disk-tmp
kops ...
Defensive patterns

Strategy: retry

Validate before calling

tmp := os.TempDir()
if st, err := os.Stat(tmp); err != nil || !st.IsDir() {
    return fmt.Errorf("temp dir %q unusable: %v", tmp, err)
}
// optional free-space check
// syscall.Statfs(tmp, &buf); if buf.Bavail*uint64(buf.Bsize) < minFree { ... }

Type guard

func isTempWriteFailure(err error) bool {
    return strings.Contains(err.Error(), "error writing temp file")
}

Try / catch

if err := applier.Apply(ctx, data); err != nil {
    if isTempWriteFailure(err) {
        // check df/free space, then retry after cleanup
        return retryApplyAfterDiskCheck(ctx, applier, data)
    }
    return err
}

Prevention

When it happens

Trigger: os.WriteFile failing because the disk filled up between MkdirTemp and WriteFile, the temp dir was concurrently removed, permissions changed, or the manifest data write exceeded available space/inodes.

Common situations: Disk filling up mid-operation; aggressive tmp cleaners (systemd-tmpfiles) racing the write; read-only remount of the temp filesystem; security tooling blocking file creation.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/5cda8300edb82e45. Report an issue: GitHub.