kubernetes/kops · error

error writing to output: %v

Error message

error writing to output: %v

What it means

After successfully YAML-marshaling the key list, the command writes the bytes to the output writer. If that write fails (closed pipe, disk full, permission error on a redirect target), it returns "error writing to output: %v".

Source

Thrown at cmd/kops/get_sshpublickeys.go:126

	switch options.Output {

	case OutputTable:
		if len(items) == 0 {
			return fmt.Errorf("no SSH public key found")
		}
		t := &tables.Table{}
		t.AddColumn("ID", func(i *SSHKeyItem) string {
			return i.ID
		})
		return t.Render(items, out, "ID")

	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
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Re-run without the early-exiting pipe consumer, or pipe to `cat`
  2. Check disk space and permissions on the redirect target
  3. Inspect the wrapped error after 'error writing to output' for the OS-level cause
  4. Handle EPIPE in wrappers/scripts rather than treating it as a kOps failure

Example fix

// before
kops get sshpublickeys -o yaml | head -5
// after
kops get sshpublickeys -o yaml | head -5 || true   # tolerate SIGPIPE
Defensive patterns

Strategy: try-catch

Validate before calling

out=/path/to/out.yaml; df -h "$(dirname "$out")" | awk 'NR==2 && $5+0 >= 95 {exit 1}'; [ -w "$(dirname "$out")" ] || exit 1

Try / catch

if err := runGetSSHPublicKeys(opts); err != nil {
	if strings.HasPrefix(err.Error(), "error writing to output:") && strings.Contains(err.Error(), "broken pipe") {
		return nil // consumer closed early; tolerate
	}
	return err
}

Prevention

When it happens

Trigger: `kops get sshpublickeys -o yaml` with stdout piped to a process that exited early (EPIPE), or output redirected to a file on a full filesystem or without write permission.

Common situations: Piping into `head` which closes early; CI logs directory full; redirecting to a read-only file.

Related errors


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