kubernetes/kops · error

unable to marshal YAML: %v

Error message

unable to marshal YAML: %v

What it means

When `kops get instances --output yaml` runs, the cloud instance list is converted with asRenderable and serialized with sigs.k8s.io/yaml.Marshal. If marshaling fails, the command returns "unable to marshal YAML: %v". sigs.k8s.io/yaml converts to JSON first, so this almost always means the data cannot be JSON-encoded (e.g. unsupported types such as channels, funcs, or NaN), which is extremely rare for the plain-string renderable struct used here.

Source

Thrown at cmd/kops/get_instances.go:162

	cloudGroups, err := cloud.GetCloudGroups(cluster, instanceGroups, false, nodeList.Items)
	if err != nil {
		return err
	}

	for _, cg := range cloudGroups {
		cloudInstances = append(cloudInstances, cg.Ready...)
		cloudInstances = append(cloudInstances, cg.NeedUpdate...)
		cg.AdjustNeedUpdate()
	}

	switch options.Output {
	case OutputTable:
		return instanceOutputTable(cloudInstances, out)
	case OutputYaml:
		y, err := yaml.Marshal(asRenderable(cloudInstances))
		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)
		}
		return nil
	case OutputJSON:
		j, err := json.Marshal(asRenderable(cloudInstances))
		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)
		}
		return nil
	default:
		return fmt.Errorf("unsupported output format: %q", options.Output)
	}
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check which instance/field fails: print instances with `kops get instances` (table) to isolate the offending instance
  2. Inspect the instance metadata in your cloud console for malformed characters (NaN, invalid UTF-8) and fix at the source
  3. Use `--output json` to see whether JSON marshaling succeeds, narrowing the problem to the YAML path
  4. Upgrade kops; if reproducible with clean metadata, file a bug with the wrapped error text

Example fix

// before
y, err := yaml.Marshal(asRenderable(cloudInstances))
// after: inspect what fails
j, jsonErr := json.Marshal(asRenderable(cloudInstances))
if jsonErr != nil {
    klog.Warningf("JSON marshal also failed: %v", jsonErr) // pinpoints non-serializable field
}
y, err := yaml.Marshal(asRenderable(cloudInstances))
Defensive patterns

Strategy: try-catch

Type guard

func marshalable(instances []*cloudinstances.CloudInstance) bool {
    _, err := json.Marshal(asRenderable(instances))
    return err == nil
}

Try / catch

y, err := yaml.Marshal(asRenderable(cloudInstances))
if err != nil {
    return fmt.Errorf("unable to marshal YAML: %w", err)
}

Prevention

When it happens

Trigger: `kops get instances -o yaml` where yaml.Marshal(asRenderable(cloudInstances)) returns an error at get_instances.go:160-162 — e.g. a renderableCloudInstance field carrying a value not representable in JSON/YAML (NaN float, invalid UTF-8 from cloud provider metadata).

Common situations: Cloud provider returns instance metadata with invalid UTF-8 bytes that end up in a string field; a corrupted custom build injecting non-serializable values; tampered vendored types.

Related errors


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