kubernetes/kops · error

error writing cluster json to stdout: %v

Error message

error writing cluster json to stdout: %v

What it means

RunCreateInstanceGroup prints the newly created InstanceGroup to stdout after creation. When the configured output format is JSON, it calls fullOutputJSON to serialize the InstanceGroup; if that serialization or write fails (e.g. encoding failure or broken stdout writer), kOps wraps the underlying error with this message so the user knows the cluster object was created but could not be displayed.

Source

Thrown at cmd/kops/create_instancegroup.go:248

	if options.DryRun {

		if options.Output == "" {
			return fmt.Errorf("must set output flag; yaml or json")
		}

		// Cluster name is not populated, and we need it
		ig.ObjectMeta.Labels = make(map[string]string)
		ig.ObjectMeta.Labels[kopsapi.LabelClusterName] = cluster.ObjectMeta.Name

		switch options.Output {
		case OutputYaml:
			if err := fullOutputYAML(out, ig); err != nil {
				return fmt.Errorf("error writing cluster yaml to stdout: %v", err)
			}
			return nil
		case OutputJSON:
			if err := fullOutputJSON(out, true, ig); err != nil {
				return fmt.Errorf("error writing cluster json to stdout: %v", err)
			}
			return nil
		default:
			return fmt.Errorf("unsupported output type %q", options.Output)
		}
	}

	if options.Edit {
		edit := editor.NewDefaultEditor(commandutils.EditorEnvs)

		raw, err := kopscodecs.ToVersionedYaml(ig)
		if err != nil {
			return err
		}
		ext := "yaml"

		// launch the editor
		edited, file, err := edit.LaunchTempFile(fmt.Sprintf("%s-edit-", filepath.Base(os.Args[0])), ext, bytes.NewReader(raw))

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check the wrapped underlying error (%v) to identify whether it is an encoding or writer failure
  2. Re-run without piping (direct to terminal) to rule out broken-pipe/stdout issues
  3. Retry with -o yaml to see if YAML output works, isolating the JSON encoding path
  4. Verify free disk space and writable stdout; fix any stdout redirection/pipe problems
  5. If encoding persists, inspect the InstanceGroup fields for unusual values and file an upstream kOps issue

Example fix

// before
kops create instancegroup --name nodes -o json | consumer-exits-early
// after
kops create instancegroup --name nodes -o json > ig.json 2> ig.err || cat ig.err
Defensive patterns

Strategy: try-catch

Validate before calling

case "$OUTPUT" in yaml|json) ;; *) echo "unsupported output: $OUTPUT" >&2; exit 1;; esac

Type guard

func isSupportedOutput(o string) bool { return o == "yaml" || o == "json" }

Try / catch

if err := RunCreateInstanceGroup(ctx, f, out, options); err != nil {
	if strings.Contains(err.Error(), "error writing cluster json to stdout") {
		// fall back to yaml output or log and continue; creation already succeeded
	}
}

Prevention

When it happens

Trigger: Running `kops create instancegroup --name <ig> -o json` where fullOutputJSON returns a non-nil error, most commonly an encoding/marshaling failure of the InstanceGroup object or an error writing to the output writer (out).

Common situations: Piping stdout to a closed pipe or full disk; a codec failure caused by an unexpected field or nil field in the InstanceGroup built from flags; running in a scripted pipeline where the consumer exits early (SIGPIPE-related write failures).

Related errors


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