{"record":{"id":"7b7c5f072e1f81cc","repo":"kubernetes/kops","slug":"failed-to-marsal-q-into-json-w","errorCode":null,"errorMessage":"failed to marsal %q into json: %w","messagePattern":"failed to marsal %q into json: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/applylib/applyset/managedfields.go","lineNumber":111,"sourceCode":"\t\tif fixedManagedFields[i].Subresource != fixedManagedFields[j].Subresource {\n\t\t\treturn fixedManagedFields[i].Subresource < fixedManagedFields[j].Subresource\n\t\t}\n\t\tif fixedManagedFields[i].Operation != fixedManagedFields[j].Operation {\n\t\t\treturn fixedManagedFields[i].Operation < fixedManagedFields[j].Operation\n\t\t}\n\t\treturn false\n\t})\n\n\tmeta := &metav1.ObjectMeta{}\n\tmeta.SetManagedFields(fixedManagedFields)\n\tpatchObject := map[string]interface{}{\n\t\t\"metadata\": meta,\n\t}\n\n\t// MarshalIndent is a little less efficient, but makes this much more readable (also helps tests)\n\tjsonData, err := json.MarshalIndent(patchObject, \"\", \"  \")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marsal %q into json: %w\", currentObject.GetName(), err)\n\t}\n\treturn jsonData, nil\n}\n\n// fieldManagerKey is the primary key for a ManagedFieldEntry\ntype fieldManagerKey struct {\n\tManager     string\n\tOperation   metav1.ManagedFieldsOperationType\n\tSubresource string\n}\n\n// mergeFieldManagers merges the managed fields from identical field managers.\n// If we don't do this, the apiserver will not currently construct the union for duplicate keys.\nfunc mergeFieldManagers(managedFields []metav1.ManagedFieldsEntry) ([]metav1.ManagedFieldsEntry, error) {\n\tbyKey := make(map[fieldManagerKey][]metav1.ManagedFieldsEntry)\n\tfor _, f := range managedFields {\n\t\tk := fieldManagerKey{\n\t\t\tManager:     f.Manager,","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/applylib/applyset/managedfields.go#L93-L129","documentation":"createManagedFieldPatch marshals the constructed patchObject (containing apiVersion, kind, and metadata with the merged managed fields) using json.MarshalIndent for readability. A failure here is wrapped with a typo'd message \"failed to marsal %q into json\" naming the current object. This aborts creation of the migration patch.","triggerScenarios":"json.MarshalIndent fails on the patchObject map — typically because a value placed in the patch structure (e.g. an entry from the object's managedFields or a FieldsV1-parsed set) contains a type Go's encoding/json cannot encode, such as NaN floats or non-marshalable internal types.","commonSituations":"An API server or library version returning managedFields data that, after conversion to fieldpath.Set and back, yields unencodable values; a bug in constructing the patchObject; rarely seen in practice since inputs come from the API server's own types.","solutions":["Check the wrapped error for the unencodable type and which object (%q) triggered it.","Upgrade/align k8s.io/apimachinery and sigs.k8s.io/structured-merge-diff versions so FieldsV1 round-trips cleanly.","As a workaround, re-create the object with server-side apply from scratch (kubectl apply --server-side --force-conflicts) instead of migrating in place."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if _, err := json.MarshalIndent(patchObject, \"\", \"  \"); err != nil {\n    return fmt.Errorf(\"patch for %s not marshalable: %w\", name, err)\n}","typeGuard":null,"tryCatchPattern":"if _, err := migrator.Migrate(ctx, obj); err != nil && strings.Contains(err.Error(), \"failed to marsal\") {\n    // fall back to recreating the object via server-side apply\n    return recreateWithSSA(ctx, obj)\n}","preventionTips":["Keep apimachinery and structured-merge-diff versions aligned so FieldsV1 round-trips cleanly.","Build patch objects only from API-server-typed data, never raw decoded maps.","If marshaling persistently fails, recreate the object with kubectl apply --server-side instead of in-place migration."],"tags":["kubernetes","managed-fields","json-marshal","migration"],"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"}