kubernetes/kops · error

error writing to output: %v

Error message

error writing to output: %v

What it means

After successfully YAML-marshaling the instance list, RunGetInstances writes the bytes to the command's output writer (stdout, or a file via redirection). If out.Write fails, the command returns "error writing to output: %v". This is an I/O failure of the destination stream, not a data problem.

Source

Thrown at cmd/kops/get_instances.go:165

		return err
	}

	for _, cg := range cloudGroups {
		cloudInstances = append(cloudInstances, cg.Ready...)
		cloudInstances = append(cloudInstances, cg.NeedUpdate...)
		cg.AdjustNeedUpdate()
	}

	switch options.Output {
	case OutputTable:
		return instanceOutputTable(cloudInstances, out)
	case OutputYaml:
		y, err := yaml.Marshal(asRenderable(cloudInstances))
		if err != nil {
			return fmt.Errorf("unable to marshal YAML: %v", err)
		}
		if _, err := out.Write(y); err != nil {
			return fmt.Errorf("error writing to output: %v", err)
		}
		return nil
	case OutputJSON:
		j, err := json.Marshal(asRenderable(cloudInstances))
		if err != nil {
			return fmt.Errorf("unable to marshal JSON: %v", err)
		}
		if _, err := out.Write(j); err != nil {
			return fmt.Errorf("error writing to output: %v", err)
		}
		return nil
	default:
		return fmt.Errorf("unsupported output format: %q", options.Output)
	}
}

func instanceOutputTable(instances []*cloudinstances.CloudInstance, out io.Writer) error {
	fmt.Println("")

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check the wrapped inner error: EPIPE means the downstream reader closed — use `cat` or drop the early-closing consumer
  2. Verify disk space (df -h) and file permissions when redirecting output to a file
  3. Retry in a terminal where stdout is a valid tty or an open regular file
  4. In scripts, avoid commands that exit early (e.g. use `head -n 100 || true` semantics or full output capture)

Example fix

// before
kops get instances -o yaml | head -5   # SIGPIPE kills the writer
// after
kops get instances -o yaml > /tmp/instances.yaml && head -5 /tmp/instances.yaml
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure output destination is writable before running
df -h . | awk 'NR==2 {exit ($5+0 > 95 ? 1 : 0)}' || { echo "disk nearly full"; exit 1; }

Type guard

if w, ok := out.(*os.File); ok {
    if _, err := w.Stat(); err != nil {
        return fmt.Errorf("output target unusable: %w", err)
    }
}

Try / catch

if _, err := out.Write(y); err != nil {
    if errors.Is(err, syscall.EPIPE) {
        klog.V(2).Infof("downstream consumer closed the pipe")
        return nil
    }
    return fmt.Errorf("error writing to output: %w", err)
}

Prevention

When it happens

Trigger: `kops get instances -o yaml` where the io.Writer passed to RunGetInstances errors on Write at get_instances.go:164-165 — e.g. stdout closed (EPIPE from `| head`), disk full when redirecting to a file, or a closed custom writer.

Common situations: Piping into `head` or `less -F` that closes the pipe early (broken pipe); redirecting to a file on a full filesystem or read-only mount; writing to a closed pipe in scripted/CI contexts.

Related errors


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