kubernetes/kops · error

error marshaling %s to yaml: %v

Error message

error marshaling %s to yaml: %v

What it means

kOps' pkg/kubemanifest Object.Reparse extracts a nested subobject of a YAML manifest and round-trips it (yaml.Marshal then yaml.Unmarshal) into a typed Go struct. This error fires when yaml.Marshal fails on the subobject's generic map data. Marshal of map[string]interface{} data rarely fails, so this usually indicates non-serializable content (e.g. values of unsupported Go types injected programmatically).

Source

Thrown at pkg/kubemanifest/manifest.go:240

	humanFields := strings.Join(fields, ".")

	current := m.data
	for _, field := range fields {
		v, found := current[field]
		if !found {
			return fmt.Errorf("field %q in %s not found", field, humanFields)
		}

		m, ok := v.(map[string]interface{})
		if !ok {
			return fmt.Errorf("field %q in %s was not an object, was %T", field, humanFields, v)
		}
		current = m
	}

	b, err := yaml.Marshal(current)
	if err != nil {
		return fmt.Errorf("error marshaling %s to yaml: %v", humanFields, err)
	}

	if err := yaml.Unmarshal(b, obj); err != nil {
		return fmt.Errorf("error unmarshaling subobject %s: %v", humanFields, err)
	}

	return nil
}

// Set mutates a subfield to the newValue
func (m *Object) Set(newValue interface{}, fieldPath ...string) error {
	humanFields := strings.Join(fieldPath, ".")

	current := m.data
	if len(fieldPath) >= 2 {
		for _, field := range fieldPath[:len(fieldPath)-1] {
			v, found := current[field]
			if !found {

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Inspect the value at the field path and ensure only YAML-safe types (string, bool, float64, map[string]interface{}, []interface{}) are stored in the manifest map
  2. Marshal the subobject yourself in a test to reproduce and identify the offending key/value
  3. If the value came from a parsed YAML file, re-validate the source manifest file for corruption or exotic YAML tags

Example fix

// before: injecting a raw Go struct into the manifest
manifest.Set(myCustomStruct{}, "spec", "template")
// after: convert to a YAML-safe map first
b, _ := yaml.Marshal(myCustomStruct)
m := map[string]interface{}{}
yaml.Unmarshal(b, &m)
manifest.Set(m, "spec", "template")
Defensive patterns

Strategy: validation

Validate before calling

if _, err := yaml.Marshal(subObj); err != nil {
    // do not call Reparse; sanitize the map first
}

Type guard

func isYAMLSafe(v interface{}) bool {
    switch v.(type) {
    case nil, string, bool, float64, int, map[string]interface{}, []interface{}:
        return true
    }
    return false
}

Try / catch

if err := obj.Reparse(target, "spec", "template"); err != nil {
    return fmt.Errorf("reparse spec.template failed: %w", err)
}

Prevention

When it happens

Trigger: Calling Object.Reparse(obj, fields...) where the subobject at the given field path contains data yaml.Marshal cannot serialize, such as channels, funcs, or other non-YAML-representable Go values placed into the manifest map in code.

Common situations: Custom mutation code that inserts raw Go values (e.g. a struct with private fields or a non-marshalable type) into a kubemanifest Object before reparsing into a typed pod spec; corrupted manifest data built programmatically rather than parsed from YAML.

Related errors


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