{"record":{"id":"6d583166fcf446cb","repo":"kubernetes/kops","slug":"failed-to-marshal-object-to-json-w","errorCode":null,"errorMessage":"failed to marshal object to JSON: %w","messagePattern":"failed to marshal object to JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/applylib/applyset/applyset.go","lineNumber":144,"sourceCode":"\n\t\t// If the object exists, we need to update any client-side-apply field-managers\n\t\t// Otherwise we often end up with old and new objects combined, which\n\t\t// is unexpected and can be invalid.\n\t\tif currentObj != nil {\n\t\t\tmanagedFields := &ManagedFieldsMigrator{\n\t\t\t\tNewManager: \"kops\",\n\t\t\t\tClient:     client,\n\t\t\t}\n\t\t\tif err := managedFields.Migrate(ctx, currentObj); err != nil {\n\t\t\t\tresults.applyError(gvk, nn, err)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\n\t\tj, err := json.Marshal(expectedObject)\n\t\tif err != nil {\n\t\t\t// TODO: Differentiate between server-fixable vs client-fixable errors?\n\t\t\tresults.applyError(gvk, nn, fmt.Errorf(\"failed to marshal object to JSON: %w\", err))\n\t\t\tcontinue\n\t\t}\n\n\t\tlastApplied, err := client.Patch(ctx, gvk, nn, types.ApplyPatchType, j, a.patchOptions)\n\t\tif err != nil {\n\t\t\tresults.applyError(gvk, nn, fmt.Errorf(\"error from apply: %w\", err))\n\t\t\tcontinue\n\t\t}\n\n\t\ttracker.lastApplied = lastApplied\n\t\tresults.applySuccess(gvk, nn)\n\t\ttracker.isHealthy = isHealthy(lastApplied)\n\t\tresults.reportHealth(gvk, nn, tracker.isHealthy)\n\t}\n\treturn results, nil\n}\n","sourceCodeStart":126,"sourceCodeEnd":161,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/applylib/applyset/applyset.go#L126-L161","documentation":"In applyset's ApplyOnce, each expected object is marshaled to JSON to build a server-side apply patch. If json.Marshal of the unstructured object fails, the error is recorded per-object via results.applyError and the object is skipped. This is a client-fixable serialization failure, so the apply continues with the remaining objects.","triggerScenarios":"An expectedObject whose underlying map contains values json.Marshal cannot encode — e.g. fields holding channels, funcs, NaN/Inf floats, or a map with non-string keys that cannot be converted — injected when the manifest was parsed or built programmatically.","commonSituations":"Building manifests programmatically and inserting runtime values (e.g. resource.Quantity as a struct with unencodable fields in unusual states, or float NaN from a templating bug); decoding YAML into unstructured with plugins that stash Go values in the map.","solutions":["Inspect the object at the reported GVK/name and fix the offending field value so it is JSON-serializable.","Ensure manifests are plain YAML/JSON: decode with sigs.k8s.io/yaml or unstructured decode, not generic YAML libs that keep native types.","Check the applyset results (results.applyError output) for the exact json.Marshal error message to identify the bad field."],"exampleFix":"// before\nobj.Object[\"replicas\"] = math.NaN()\n// after\nobj.Object[\"replicas\"] = int64(3)","handlingStrategy":"validation","validationCode":"if _, err := json.Marshal(expectedObject.Object); err != nil {\n    return fmt.Errorf(\"object %s/%s not JSON-serializable: %w\", expectedObject.GetNamespace(), expectedObject.GetName(), err)\n}","typeGuard":null,"tryCatchPattern":"if err := applyOnce(ctx); err != nil {\n    var marshalErr *json.UnsupportedTypeError\n    if errors.As(err, &marshalErr) {\n        // fix offending field type in the manifest\n    }\n}","preventionTips":["Load manifests via YAML->JSON decoding (sigs.k8s.io/yaml) so only JSON-safe types enter the object map.","Never insert native Go values (NaN, funcs, channels) into unstructured Object maps.","Sanity-render objects to JSON in tests before applying."],"tags":["kubernetes","apply","json-marshal","server-side-apply"],"backgroundTag":"json-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}