kubernetes/kops · error

unable to marshal YAML: %v

Error message

unable to marshal YAML: %v

What it means

RunGetAssets uses sigs.k8s.io/yaml.Marshal to serialize the AssetResult (images and file assets) for `kops get assets -o yaml`, and wraps any marshal failure with this message. Because AssetResult contains only plain string-slice structs this is rare, but a marshaling failure (unsupported types, cycle, or writer-side issues surfaced by the YAML library) triggers it.

Source

Thrown at cmd/kops/get_assets.go:161

	}

	if options.Copy {
		err := assetcopy.Copy(updateClusterResults.ImageAssets, updateClusterResults.FileAssets, f.VFSContext(), updateClusterResults.Cluster)
		if err != nil {
			return err
		}
	}

	switch options.Output {
	case OutputTable:
		if err = imageOutputTable(result.Images, out); err != nil {
			return err
		}
		return fileOutputTable(result.Files, out)
	case OutputYaml:
		y, err := yaml.Marshal(result)
		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)
		}
	case OutputJSON:
		j, err := json.Marshal(result)
		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)
		}
	default:
		return fmt.Errorf("unsupported output format: %q", options.Output)
	}

	return nil
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Read the wrapped inner error to identify which value failed to convert
  2. Ensure the asset data comes from a successful RunUpdateCluster dry run (check the preceding error path)
  3. Retry with -o json or the default table output to isolate whether only YAML serialization fails
  4. Update kops if the failure comes from a patched or modified binary

Example fix

// before
y, err := yaml.Marshal(result)
if err != nil { return fmt.Errorf("unable to marshal YAML: %v", err) }
// after
j, err := json.Marshal(result)
if err != nil { return err }
y, err := yaml.JSONToYAML(j) // sigs.k8s.io/yaml goes through JSON
Defensive patterns

Strategy: try-catch

Try / catch

y, err := yaml.Marshal(result)
if err != nil {
    // fall back to JSON which supports more types
    j, jerr := json.Marshal(result)
    if jerr != nil {
        return fmt.Errorf("unable to marshal YAML: %v (json fallback: %v)", err, jerr)
    }
    y, _ = yaml.JSONToYAML(j)
}

Prevention

When it happens

Trigger: `kops get assets -o yaml` where yaml.Marshal(result) returns an error — typically unexpected data types inside the result or a failure of the underlying JSON-based YAML conversion.

Common situations: Modified/patched builds where AssetResult gained unmarshalable fields; corrupted asset URLs (e.g. nil-adjacent data from a dry run) producing values the marshaller cannot handle; unusual plugin-added assets.

Related errors


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