kubernetes/kops · error

error reading file %q: %v

Error message

error reading file %q: %v

What it means

kops `kops replace -f FILE` reads each manifest through the VFS layer (vfsContext.ReadFile) before decoding. This error wraps any read failure - missing file, permission denied, or a VFS scheme (s3://, gs://) that failed or is not configured.

Source

Thrown at cmd/kops/replace.go:104

func RunReplace(ctx context.Context, f *util.Factory, out io.Writer, c *ReplaceOptions) error {
	clientset, err := f.KopsClient()
	if err != nil {
		return err
	}

	vfsContext := f.VFSContext()

	for _, f := range c.Filenames {
		var contents []byte
		if f == "-" {
			contents, err = ConsumeStdin()
			if err != nil {
				return err
			}
		} else {
			contents, err = vfsContext.ReadFile(f)
			if err != nil {
				return fmt.Errorf("error reading file %q: %v", f, err)
			}
		}
		sections := text.SplitContentToSections(contents)

		for _, section := range sections {
			o, gvk, err := kopscodecs.Decode(section, nil)
			if err != nil {
				return fmt.Errorf("error parsing file %q: %v", f, err)
			}

			switch v := o.(type) {
			case *kopsapi.Cluster:
				{
					// Retrieve the current status of the cluster.  This will eventually be part of the cluster object.
					cloud, err := cloudup.BuildCloud(v)
					if err != nil {
						return err
					}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Verify the file path exists and is readable: ls -l <file>; fix typos in the -f argument
  2. Use an absolute path or run from the correct directory
  3. For remote paths (s3://, gs://), confirm the object exists and cloud credentials/env are configured
  4. If intending stdin, pass exactly `-f -` and pipe the manifest
  5. Check the underlying error text in the message for the precise cause (no such file, permission denied, etc.)

Example fix

// before
kops replace -f ./cluter.yaml
// after
kops replace -f ./cluster.yaml
Defensive patterns

Strategy: validation

Validate before calling

for _, f := range filenames {
    if f != "-" {
        if _, err := os.Stat(f); err != nil {
            return fmt.Errorf("manifest not readable: %w", err)
        }
    }
}
// For remote VFS paths, ensure credentials and the target object exist first.

Try / catch

if err != nil {
    var perr *fs.PathError
    if errors.As(err, &perr) { /* handle missing/unreadable file */ }
    return err
}

Prevention

When it happens

Trigger: Running `kops replace -f` with a path that does not exist, a typo'd path, no read permission, an unreadable remote VFS path (e.g. s3:// bucket not present/accessible), or stdin handled elsewhere while a bad filename is passed.

Common situations: Running the command from a different working directory than expected; forgetting the file was deleted or renamed; ~/.aws or gcloud credentials missing so a cloud-backed VFS path cannot be read; using a relative path in CI.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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