kubernetes/kops · error

error opening file %q: %v

Error message

error opening file %q: %v

What it means

HashAlgorithm.HashFile opens the file at path p read-only before hashing. If os.OpenFile fails with an error other than not-exist (permission denied, path is a directory, I/O error), it is wrapped in this error. Note: a genuinely missing file returns the raw os.ErrNotExist without this wrapper.

Source

Thrown at util/pkg/hashing/hash.go:143

	return ha.FromString(s)
}

func (ha HashAlgorithm) Hash(r io.Reader) (*Hash, error) {
	hasher := ha.NewHasher()
	_, err := copyToHasher(hasher, r)
	if err != nil {
		return nil, fmt.Errorf("error while hashing resource: %v", err)
	}
	return &Hash{Algorithm: ha, HashValue: hasher.Sum(nil)}, nil
}

func (ha HashAlgorithm) HashFile(p string) (*Hash, error) {
	f, err := os.OpenFile(p, os.O_RDONLY, 0)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, err
		}
		return nil, fmt.Errorf("error opening file %q: %v", p, err)
	}
	defer try.CloseFile(f)
	return ha.Hash(f)
}

func copyToHasher(dest io.Writer, src io.Reader) (int64, error) {
	n, err := io.Copy(dest, src)
	if err != nil {
		return n, fmt.Errorf("error hashing data: %v", err)
	}
	return n, nil
}

func (l *Hash) Equal(r *Hash) bool {
	return (l.Algorithm == r.Algorithm) && bytes.Equal(l.HashValue, r.HashValue)
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check the file exists and is a regular file, and that the current user has read permission (ls -l)
  2. Fix permissions (chmod/chown) or run the command as a user with access
  3. If the file may legitimately be absent, handle os.IsNotExist first — it is returned unwrapped

Example fix

// before
h, err := algo.HashFile(dirPath) // dirPath is a directory
// after
fi, err := os.Stat(dirPath)
if err != nil { return err }
if fi.IsDir() { return fmt.Errorf("%s is a directory", dirPath) }
h, err := algo.HashFile(dirPath)
Defensive patterns

Strategy: validation

Validate before calling

fi, err := os.Stat(p)
if err != nil { return err }
if fi.IsDir() { return fmt.Errorf("%s is a directory, not a file", p) }
if f, err := os.Open(p); err != nil {
	return fmt.Errorf("no read permission for %s: %w", p, err)
} else { f.Close() }

Type guard

null

Try / catch

h, err := algo.HashFile(p)
if err != nil {
	if os.IsNotExist(err) {
		return fmt.Errorf("file %s not found; re-download or fix path", p)
	}
	return fmt.Errorf("cannot open %s for hashing: %w", p, err)
}

Prevention

When it happens

Trigger: Calling HashAlgorithm.HashFile("/path/to/file") where the path exists but is unreadable (wrong permissions), is a directory, or the device returns an I/O error; also via fileHasHash / Hash call sites.

Common situations: Running kops as a non-root user against root-owned node files; pointing hash verification at a directory instead of a file; NFS/readonly-mount failures; SELinux denials.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


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