kubernetes/kops · error

error writing cluster json to stdout: %v

Error message

error writing cluster json to stdout: %v

What it means

Wraps a failure of fullOutputJSON while `kops toolbox instance-selector --dry-run -o json` serializes a generated instance group to stdout. The dry-run output stream write failed, not the instance-group computation.

Source

Thrown at cmd/kops/toolbox_instance-selector.go:335

		newInstanceGroups = append(newInstanceGroups, ig)

		if igCount != 1 {
			doubledRatio := (*mutatedFilters.VCpusToMemoryRatio) * 2
			mutatedFilters.VCpusToMemoryRatio = &doubledRatio
		}
	}

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

	for _, ig := range newInstanceGroups {
		_, err = clientset.InstanceGroupsFor(cluster).Create(ctx, ig, metav1.CreateOptions{})
		if err != nil {
			return fmt.Errorf("error storing InstanceGroup: %v", err)
		}

		if err := fullOutputYAML(out, ig); err != nil {
			return fmt.Errorf("error writing cluster yaml to stdout: %v", err)
		}
	}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. If the cause is EPIPE, rerun writing to a file instead of a pipe.
  2. Confirm disk space for the redirected output target.
  3. Try --output yaml to check whether only the JSON path fails.
  4. Inspect the wrapped encoder error for the offending field.

Example fix

// before
kops toolbox instance-selector --output json ... | jq '.[0]' | head
// after
kops toolbox instance-selector --output json ... > igs.json
Defensive patterns

Strategy: try-catch

Validate before calling

if options.Output != OutputJSON {
	return fmt.Errorf("expected --output json for this path")
}

Try / catch

if err := fullOutputJSON(out, true, ig); err != nil {
	if errors.Is(err, syscall.EPIPE) {
		log.Println("stdout closed by consumer; output discarded")
		return nil
	}
	return fmt.Errorf("error writing cluster json to stdout: %v", err)
}

Prevention

When it happens

Trigger: fullOutputJSON returns an error during JSON encoding of the InstanceGroup or writing to the out writer (broken pipe, encoder failure).

Common situations: Downstream consumer of the pipe exits before consuming all JSON; stdout redirected to a full filesystem; JSON encoder choking on a field produced by an unusual spec decoration.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


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