kubernetes/kops · error

error creating temp file in %q: %v

Error message

error creating temp file in %q: %v

What it means

After ensuring the directory exists, WriteFile creates a temporary file in that directory with os.CreateTemp to stage the write. This error wraps CreateTemp failures such as permission denied on the (now-existing) directory, disk full, or the directory being removed between the MkdirAll and CreateTemp calls.

Source

Thrown at util/pkg/vfs/fs.go:63

}

func (p *FSPath) Join(relativePath ...string) Path {
	args := []string{p.location}
	args = append(args, relativePath...)
	joined := filepath.Join(args...)
	return &FSPath{location: joined}
}

func (p *FSPath) WriteFile(ctx context.Context, data io.ReadSeeker, acl ACL) error {
	dir := filepath.Dir(p.location)
	err := os.MkdirAll(dir, 0o755)
	if err != nil {
		return fmt.Errorf("error creating directories %q: %v", dir, err)
	}

	f, err := os.CreateTemp(dir, "tmp")
	if err != nil {
		return fmt.Errorf("error creating temp file in %q: %v", dir, err)
	}

	// Note from here on in we have to close f and delete or rename the temp file
	tempfile := f.Name()

	_, err = io.Copy(f, data)

	if closeErr := f.Close(); err == nil {
		err = closeErr
	}

	if err == nil {
		err = os.Rename(tempfile, p.location)
		if err != nil {
			err = fmt.Errorf("error during file write of %q: rename failed: %v", p.location, err)
		}
	}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Grant write permission on the target directory for the running user.
  2. Free disk space / check inode usage (df -h, df -i).
  3. Re-check that the directory still exists (no concurrent cleanup job deleting it).
  4. Retry the write after fixing the transient condition.

Example fix

// before
f, err := os.CreateTemp(dir, "tmp") // dir owned by root
// after
sudo chown -R $(whoami) /path/to/dir
f, err := os.CreateTemp(dir, "tmp")
Defensive patterns

Strategy: try-catch

Validate before calling

if err := unix.Access(dir, unix.W_OK); err != nil {
    return fmt.Errorf("directory %s is not writable", dir)
}
if err := checkDiskSpace(dir); err != nil {
    return err
}

Try / catch

if err := p.WriteFile(ctx, data, acl); err != nil && strings.Contains(err.Error(), "error creating temp file") {
    // usually permissions or ENOSPC; surface a targeted hint
    return fmt.Errorf("check write permission and free space in %s: %w", dir, err)
}

Prevention

When it happens

Trigger: Calling CreateFile/WriteFile where os.CreateTemp(dir, "tmp") fails: directory not writable despite existing, no inodes/space left, or dir vanished between MkdirAll and CreateTemp.

Common situations: Directory owned by another user (created earlier as root), full disk or inode exhaustion on the node, or TMPDIR-style restrictions where the fs mount is noexec/nodev with odd permissions.

Related errors


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