kubernetes/kops · error

unable to marshal YAML: %v

Error message

unable to marshal YAML: %v

What it means

This error is raised when yaml.Marshal(result) fails while rendering validation results in --output yaml mode. yaml.Marshal of a plain validation result struct essentially never fails in practice; failure implies an unmarshalable type in the result (e.g. channel, func field) or a corrupted result produced by the validator.

Source

Thrown at cmd/kops/validate_cluster.go:213

			consecutive = 0
			if options.wait > 0 {
				klog.Warningf("(will retry): unexpected error during validation: %v", err)
				time.Sleep(options.interval)
				continue
			} else {
				return nil, fmt.Errorf("unexpected error during validation: %v", err)
			}
		}

		switch options.output {
		case OutputTable:
			if err := validateClusterOutputTable(result, cluster, instanceGroups, out); err != nil {
				return nil, err
			}
		case OutputYaml:
			y, err := yaml.Marshal(result)
			if err != nil {
				return nil, fmt.Errorf("unable to marshal YAML: %v", err)
			}
			if _, err := out.Write(y); err != nil {
				return nil, fmt.Errorf("error writing to output: %v", err)
			}
		case OutputJSON:
			j, err := json.Marshal(result)
			if err != nil {
				return nil, fmt.Errorf("unable to marshal JSON: %v", err)
			}
			if _, err := out.Write(j); err != nil {
				return nil, fmt.Errorf("error writing to output: %v", err)
			}
		default:
			return nil, fmt.Errorf("unknown output format: %q", options.output)
		}

		if len(result.Failures) == 0 {
			consecutive++

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Re-run with default table output or -o json to get results and file a bug if only yaml fails.
  2. Confirm kops binary version matches the cluster's kops version; upgrade or rebuild the binary.
  3. Check for locally patched kops builds or plugins altering the validation result type.
  4. Report to kops maintainers with the printed %v cause if it persists with official binaries.

Example fix

// before
kops validate cluster -o yaml
// after
kops validate cluster -o json
Defensive patterns

Strategy: try-catch

Try / catch

result, err := runValidateCluster(ctx, options)
if err != nil && strings.Contains(err.Error(), "unable to marshal YAML") {
	// fall back to JSON or table output
	return runValidateCluster(ctx, withOutput(options, OutputJSON))
}

Prevention

When it happens

Trigger: `kops validate cluster -o yaml` where the validation result struct contains a value the YAML encoder cannot serialize.

Common situations: A kops version mismatch or patched binary injecting an unsupported field into the result struct; a custom validator plugin returning unserializable data; extremely rare — usually indicates a kops bug.

Related errors


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