kubernetes/kops · warning

error writing to stdout: %v

Error message

error writing to stdout: %v

What it means

writeYAMLSep writes the "\n---\n\n" document separator used when concatenating multiple resources into one YAML stream for `kops get -o yaml`. If the io.Writer (os.Stdout, or a redirected pipe/file) returns an error, it is wrapped as "error writing to stdout".

Source

Thrown at cmd/kops/get.go:85

	cmd.AddCommand(NewCmdGetCluster(f, out, options))
	cmd.AddCommand(NewCmdGetInstanceGroups(f, out, options))
	cmd.AddCommand(NewCmdGetInstances(f, out, options))
	cmd.AddCommand(NewCmdGetKeypairs(f, out, options))
	cmd.AddCommand(NewCmdGetSecrets(f, out, options))
	cmd.AddCommand(NewCmdGetSSHPublicKeys(f, out, options))

	return cmd
}

func RunGet(ctx context.Context, f commandutils.Factory, out io.Writer, options *GetOptions) error {
	klog.Warning("`kops get [CLUSTER]` is deprecated: use `kops get all [CLUSTER]`")
	return RunGetAll(ctx, f, out, &GetAllOptions{options})
}

func writeYAMLSep(out io.Writer) error {
	_, err := out.Write([]byte("\n---\n\n"))
	if err != nil {
		return fmt.Errorf("error writing to stdout: %v", err)
	}
	return nil
}

type marshalFunc func(obj runtime.Object) ([]byte, error)

func marshalToWriter(obj runtime.Object, marshal marshalFunc, w io.Writer) error {
	b, err := marshal(obj)
	if err != nil {
		return err
	}
	_, err = w.Write(b)
	if err != nil {
		return fmt.Errorf("error writing to stdout: %v", err)
	}
	return nil
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check why stdout failed: ensure the downstream pipe consumer stays alive (use `head -c` alternatives or temp files)
  2. Verify redirect target has space: df -h on the output file's filesystem
  3. Rerun writing to a file instead of a pipe: kops get -o yaml > out.yaml (if out.yaml is on a healthy fs)
  4. Ignore if caused by expected SIGPIPE in shell pipelines (bash reports 141)

Example fix

// before (breaks)
kops get ig -o yaml | head -1
// after
kops get ig -o yaml > ig.yaml && head -1 ig.yaml
Defensive patterns

Strategy: fallback

Validate before calling

// check stdout is a healthy writable target before running
if fi, err := os.Stdout.Stat(); err == nil && (fi.Mode()&os.ModeCharDevice) == 0 {
    // piped: ensure downstream consumer will not exit early
}

Try / catch

out, err := exec.Command("kops", "get", "ig", "-o", "yaml").Output()
if err != nil {
    if strings.Contains(err.Error(), "error writing to stdout") || exitCode(err) == 141 {
        // broken pipe: write to a file instead and read it
    }
}

Prevention

When it happens

Trigger: `kops get <type> -o yaml` (fullOutputYAML path) when stdout is closed or the write fails — e.g. output piped to a command that exited early (SIGPIPE/EPIPE), disk-full redirect target, or closed descriptor.

Common situations: `kops get clusters -o yaml | head -1` causing head to exit and the pipe to break; redirecting to a file on a full filesystem; stdout connected to a dead process in a CI pipeline.

Related errors


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