ApplyFromFieldPathPatch patches the "to" resource, using a source field on the "from" resource. Values may be transformed if any are defined on the patch.
(p PatchInterface, from, to runtime.Object)
| 63 | // on the "from" resource. Values may be transformed if any are defined on |
| 64 | // the patch. |
| 65 | func ApplyFromFieldPathPatch(p PatchInterface, from, to runtime.Object) error { |
| 66 | fromMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(from) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | in, err := fieldpath.Pave(fromMap).GetValue(p.GetFromFieldPath()) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | // Apply transform pipeline |
| 77 | out, err := ResolveTransforms(p.GetTransforms(), in) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | // Round-trip the "from" source field value through Kubernetes JSON decoder, |
| 83 | // so that the json integers are unmarshalled as int64 consistent with "to"/dest value handling. |
| 84 | // Kubernetes JSON decoder will get us a map[string]any where number values are int64, |
| 85 | // but protojson and structpb will get us one where number values are float64. |
| 86 | // https://pkg.go.dev/sigs.k8s.io/json#UnmarshalCaseSensitivePreserveInts |
| 87 | v, err := toValidJSON(out) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | mo, err := toMergeOption(p) |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | |
| 97 | // ComposedPatch all expanded fields if the ToFieldPath contains wildcards |
| 98 | if strings.Contains(p.GetToFieldPath(), "[*]") { |
| 99 | return patchFieldValueToMultiple(p.GetToFieldPath(), v, to, mo) |
| 100 | } |
| 101 | |
| 102 | return errors.Wrap(patchFieldValueToObject(p.GetToFieldPath(), v, to, mo), "cannot patch to object") |
| 103 | } |
| 104 | |
| 105 | func toValidJSON(value any) (any, error) { |
| 106 | var v any |