kubernetes/kops · error

error writing to output: %v

Error message

error writing to output: %v

What it means

In RunGetKeypairs' OutputYaml branch, after a successful yaml.Marshal the resulting bytes are written to the output writer. If out.Write(y) fails — broken pipe to a downstream process, closed stream, or an unwritable output file — the error is wrapped as "error writing to output: %v".

Source

Thrown at cmd/kops/get_keypairs.go:244

			if i.HasPrivateKey {
				return "*"
			}
			return ""
		})
		columnNames := []string{"NAME", "ID", "ISSUED", "EXPIRES"}
		if options.Distrusted {
			columnNames = append(columnNames, "DISTRUSTED")
		}
		columnNames = append(columnNames, "PRIMARY", "HASPRIVATE")
		return t.Render(items, out, columnNames...)

	case OutputYaml:
		y, err := yaml.Marshal(items)
		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)
		}
	case OutputJSON:
		j, err := json.Marshal(items)
		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)
		}

	default:
		return fmt.Errorf("unknown output format: %q", options.Output)
	}

	return nil
}

func completeGetKeypairs(ctx context.Context, f commandutils.Factory, options *GetKeypairsOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Inspect the wrapped cause: EPIPE means the downstream consumer closed early — avoid `| head` or tolerate SIGPIPE.
  2. Redirect to a file instead of a pipe to test whether the writer is the problem.
  3. Check disk space/permissions if using an output file.
  4. Retry on a plain terminal; if it still fails on a valid writer, report the underlying error upstream.

Example fix

// before
kops get keypairs -o yaml | head -5   # broken pipe
// after
kops get keypairs -o yaml > keypairs.yaml && head -5 keypairs.yaml
Defensive patterns

Strategy: try-catch

Validate before calling

# ensure the destination is writable before streaming
[ -w "$(pwd)" ] && [ "$(df --output=avail -k . | tail -1)" -gt 1024 ] || exit 2

Try / catch

if err := runKops("get", "keypairs", "-o", "yaml"); err != nil {
	if strings.Contains(err.Error(), "error writing to output") {
		// check wrapped cause for EPIPE; write to a file and retry once
	}
	return err
}

Prevention

When it happens

Trigger: `kops get keypairs -o yaml` piped into a consumer that exits before reading everything (e.g. `| head`), or output redirected to a full/read-only file, causing the io.Writer to return an error.

Common situations: Piping into `head`, `grep -q`, or a script that closes stdin early (EPIPE); `--out` file on a full disk; CI log collector dropping the stream mid-write.

Related errors


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