kubernetes/kops · error
failed to marsal %q into json: %w
Error message
failed to marsal %q into json: %w
What it means
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.
Source
Thrown at pkg/applylib/applyset/managedfields.go:111
if fixedManagedFields[i].Subresource != fixedManagedFields[j].Subresource {
return fixedManagedFields[i].Subresource < fixedManagedFields[j].Subresource
}
if fixedManagedFields[i].Operation != fixedManagedFields[j].Operation {
return fixedManagedFields[i].Operation < fixedManagedFields[j].Operation
}
return false
})
meta := &metav1.ObjectMeta{}
meta.SetManagedFields(fixedManagedFields)
patchObject := map[string]interface{}{
"metadata": meta,
}
// MarshalIndent is a little less efficient, but makes this much more readable (also helps tests)
jsonData, err := json.MarshalIndent(patchObject, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to marsal %q into json: %w", currentObject.GetName(), err)
}
return jsonData, nil
}
// fieldManagerKey is the primary key for a ManagedFieldEntry
type fieldManagerKey struct {
Manager string
Operation metav1.ManagedFieldsOperationType
Subresource string
}
// mergeFieldManagers merges the managed fields from identical field managers.
// If we don't do this, the apiserver will not currently construct the union for duplicate keys.
func mergeFieldManagers(managedFields []metav1.ManagedFieldsEntry) ([]metav1.ManagedFieldsEntry, error) {
byKey := make(map[fieldManagerKey][]metav1.ManagedFieldsEntry)
for _, f := range managedFields {
k := fieldManagerKey{
Manager: f.Manager,View on GitHub (pinned to 4c8573c808)
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.
Defensive patterns
Strategy: try-catch
Validate before calling
if _, err := json.MarshalIndent(patchObject, "", " "); err != nil {
return fmt.Errorf("patch for %s not marshalable: %w", name, err)
} Try / catch
if _, err := migrator.Migrate(ctx, obj); err != nil && strings.Contains(err.Error(), "failed to marsal") {
// fall back to recreating the object via server-side apply
return recreateWithSSA(ctx, obj)
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- failed to create managed-fields patch: %w
- failed to patch object managed-fields for %q: %w
- no managed fields supplied
- cannot merge ManagedFieldsEntry apiVersion %q with apiVersio
- failed to marshal object to JSON: %w
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/7b7c5f072e1f81cc.
Report an issue: GitHub.