kubernetes/kops · error

error creating directories %q: %v

Error message

error creating directories %q: %v

What it means

FSPath.WriteFile writes files atomically by first creating the target's parent directories with os.MkdirAll. This error wraps any MkdirAll failure — permission denied, read-only filesystem, a non-directory file occupying a path component, etc. The library cannot even create a temp file until the directory exists.

Source

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

	_ HasHash = &FSPath{}
)

func NewFSPath(location string) *FSPath {
	return &FSPath{location: location}
}

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)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Fix permissions on the parent path (chown/chmod) or run the process as a user with write access.
  2. Ensure no regular file occupies a directory component of the target path.
  3. Check the filesystem is writable and not full (mount options, disk space).
  4. Point the VFS local path at a directory that exists or can be created.

Example fix

// before
FSPath{location: "/etc/kops/config"} // /etc not writable by current user
// after
sudo chown $(whoami) /etc/kops || FSPath{location: "$HOME/.kops/config"}
Defensive patterns

Strategy: try-catch

Validate before calling

dir := filepath.Dir(target)
if fi, err := os.Stat(dir); err == nil && !fi.IsDir() {
    return fmt.Errorf("%s exists and is not a directory", dir)
}
if err := unix.Access(filepath.Dir(dir), unix.W_OK); err != nil {
    return fmt.Errorf("no write permission on %s: %w", dir, err)
}

Try / catch

if err := p.WriteFile(ctx, data, acl); err != nil && strings.Contains(err.Error(), "error creating directories") {
    var pe *fs.PathError
    if errors.As(err, &pe) && errors.Is(pe.Err, fs.ErrPermission) {
        return fmt.Errorf("run with write access to %s or pick another path", filepath.Dir(target))
    }
    return err
}

Prevention

When it happens

Trigger: Calling CreateFile/WriteFile on a FSPath whose parent directory cannot be created: permission denied on the parent, a path component exists as a regular file, or the filesystem is read-only/full.

Common situations: Running kops as a non-root user against /etc or another root-owned directory, state store local dir pointed at a file rather than a directory, or containers with read-only root filesystems.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


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