kubernetes/kops · error

error marshaling into JSON: %v

Error message

error marshaling into JSON: %v

What it means

KubeObjectToApplyYAML in pkg/kubemanifest/yaml.go inlines sigs.k8s.io/yaml.Marshal: it first json.Marshal's a runtime.Object (a Kubernetes API object) before converting to a YAML map and deleting some fields. This error fires when the Go standard json.Marshal fails on the runtime.Object.

Source

Thrown at pkg/kubemanifest/yaml.go:39

	"fmt"

	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/klog/v2"
	"sigs.k8s.io/yaml"
)

// KubeObjectToApplyYAML returns the kubernetes object converted to YAML, with "noisy" fields removed.
//
// We remove:
//   - status (can't be applied, shouldn't be specified)
//   - metadata.creationTimestamp (can't be applied, shouldn't be specified)
func KubeObjectToApplyYAML(data runtime.Object) (string, error) {
	// This logic is inlined sigs.k8s.io/yaml.Marshal, but we delete some fields in the middle.

	// Convert the object to JSON bytes
	j, err := json.Marshal(data)
	if err != nil {
		return "", fmt.Errorf("error marshaling into JSON: %v", err)
	}

	// Convert the JSON to a map.
	jsonObj := make(map[string]interface{})
	if err := yaml.Unmarshal(j, &jsonObj); err != nil {
		return "", err
	}

	// Remove status (can't be applied, shouldn't be specified)
	delete(jsonObj, "status")

	// Remove metadata.creationTimestamp (can't be applied, shouldn't be specified)
	metadataObj, found := jsonObj["metadata"]
	if found {
		if metadata, ok := metadataObj.(map[string]interface{}); ok {
			delete(metadata, "creationTimestamp")
		} else {
			klog.Warningf("unexpected type for object metadata: %T", metadataObj)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Validate the runtime.Object with json.Marshal in a test to find the offending field
  2. Fix or zero out the non-serializable field values before conversion
  3. Ensure the object is a properly formed typed k8s API object (e.g. *corev1.Pod) rather than a wrapper/unregistered type

Example fix

// before: NaN quantity
resources.Limits["cpu"] = math.NaN()
obj.KubeObjectToApplyYAML(pod)
// after
resources.Limits["cpu"] = resource.MustParse("500m")
obj.KubeObjectToApplyYAML(pod)
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := json.Marshal(data); err != nil {
    // object is not JSON-serializable; fix fields before calling KubeObjectToApplyYAML
}

Type guard

func isJSONSafe(v interface{}) bool {
    var hasBad func(interface{}) bool
    hasBad = func(x interface{}) bool {
        switch x.(type) {
        case chan interface{}, func():
            return true
        }
        return false
    }
    return !hasBad(v)
}

Try / catch

yamlStr, err := kubemanifest.KubeObjectToApplyYAML(pod)
if err != nil {
    return fmt.Errorf("cannot render apply yaml for %T: %w", pod, err)
}

Prevention

When it happens

Trigger: Calling KubeObjectToApplyYAML with an object containing fields json.Marshal cannot encode: invalid types (chan, func, NaN/Inf floats), or a runtime.Object whose underlying Go value fails marshaling; also a nil or malformed typed object.

Common situations: Passing a partially-initialized k8s API struct with unsupported values, or a custom runtime.Object whose marshaling logic errors; objects built programmatically with bad values (e.g. NaN in resource quantities stored as float).

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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