kubernetes/kops · error

error marshaling manifest to yaml: %w

Error message

error marshaling manifest to yaml: %w

What it means

Object.ToYAML marshals the object's generic data map with sigs.k8s.io/yaml-compatible yaml.Marshal and wraps any marshal failure with this message. Because the data came from parsed YAML, failures are rare and typically indicate non-string map keys or values that cannot be represented in YAML/JSON.

Source

Thrown at pkg/kubemanifest/manifest.go:131

		if object.IsEmptyObject() {
			continue
		}

		y, err := object.ToYAML()
		if err != nil {
			return nil, fmt.Errorf("error re-marshaling manifest: %v", err)
		}

		yamls = append(yamls, y)
	}

	return bytes.Join(yamls, yamlSeparator), nil
}

func (m *Object) ToYAML() ([]byte, error) {
	b, err := yaml.Marshal(m.data)
	if err != nil {
		return nil, fmt.Errorf("error marshaling manifest to yaml: %w", err)
	}
	return b, nil
}

func (m *Object) MarshalJSON() ([]byte, error) {
	b, err := json.Marshal(m.data)
	if err != nil {
		return nil, fmt.Errorf("error marshaling manifest to json: %w", err)
	}
	return b, nil
}

func (m *Object) accept(visitor Visitor) error {
	err := visit(visitor, m.data, []string{}, func(v interface{}) {
		klog.Fatal("cannot mutate top-level data")
	})
	return err
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Ensure all values in Object data are JSON/YAML-compatible (string keys, scalars, slices, maps)
  2. Print/inspect m.data (Object implements MarshalJSON) to find the offending value
  3. Re-load the object from the original YAML to discard bad in-memory mutations
  4. Check the wrapped error for the type and path that failed
Defensive patterns

Strategy: try-catch

Validate before calling

// same yamlSafe check as errorIndex 1165, applied to m.data before marshaling
if !yamlSafe(objData) { return fmt.Errorf("object data contains non-YAML values") }

Try / catch

b, err := obj.ToYAML()
if err != nil {
	return fmt.Errorf("failed to serialize manifest object: %w", err)
}

Prevention

When it happens

Trigger: Calling Object.ToYAML directly, or any ObjectList.ToYAML/Build/Replace path, when m.data contains a value yaml.Marshal rejects (e.g. non-string-keyed maps, invalid types inserted programmatically).

Common situations: Custom manifest rewriting tools that put ints/complex values where the original YAML had different types; cyclic data structures from careless mutation.

Related errors


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