Skip to content

Commit 566a3fc

Browse files
committed
update toFieldPath policies with options compatible to upstream mergeOptions
Signed-off-by: ravilr <raviprasad_lr@yahoo.com>
1 parent 387842b commit 566a3fc

File tree

6 files changed

+83
-24
lines changed

6 files changed

+83
-24
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,14 @@ These fields are now required. This makes P&T configuration less ambiguous:
130130
Also, the `resources[i].patches[i].policy.mergeOptions` field is no longer
131131
supported. The same capability can be achieved by setting
132132
`resources[i].patches[i].policy.toFieldPath` to:
133-
- `MergeObject` - equivalent to
133+
- `MergeObjects` - equivalent to
134134
`resources[i].patches[i].policy.mergeOptions.keepMapValues: true`
135-
- `AppendArray` - equivalent to
136-
`resources[i].patches[i].policy.mergeOptions.appendSlice: false`
135+
- `MergeObjectsAppendArrays` - equivalent to
136+
`resources[i].patches[i].policy.mergeOptions{keepMapValues: true, appendSlice: true}`
137+
- `ForceMergeObjects` - equivalent to
138+
`resources[i].patches[i].policy.mergeOptions.keepMapValues: false`
139+
- `ForceMergeObjectsAppendArrays` - equivalent to
140+
`resources[i].patches[i].policy.mergeOptions.appendSlice: true`
137141

138142
## Developing this function
139143

@@ -163,4 +167,4 @@ $ crossplane xpkg build -f package --embed-runtime-image=runtime
163167
[#4746]: https://github.com/crossplane/crossplane/issues/4746
164168
[go]: https://go.dev
165169
[docker]: https://www.docker.com
166-
[cli]: https://docs.crossplane.io/latest/cli
170+
[cli]: https://docs.crossplane.io/latest/cli

fn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ func TestRunFunction(t *testing.T) {
482482
FromFieldPath: ptr.To[string]("spec.sourceObject"),
483483
ToFieldPath: ptr.To[string]("spec.targetObject"),
484484
Policy: &v1beta1.PatchPolicy{
485-
ToFieldPath: ptr.To(v1beta1.ToFieldPathPolicyMergeObject),
485+
ToFieldPath: ptr.To(v1beta1.ToFieldPathPolicyMergeObjects),
486486
},
487487
},
488488
},

input/v1beta1/resources_patches.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ type ToFieldPathPolicy string
3737

3838
// ToFieldPath patch policies.
3939
const (
40-
ToFieldPathPolicyReplace ToFieldPathPolicy = "Replace"
40+
ToFieldPathPolicyReplace ToFieldPathPolicy = "Replace"
41+
ToFieldPathPolicyMergeObjects ToFieldPathPolicy = "MergeObjects"
42+
ToFieldPathPolicyMergeObjectsAppendArrays ToFieldPathPolicy = "MergeObjectsAppendArrays"
43+
ToFieldPathPolicyForceMergeObjects ToFieldPathPolicy = "ForceMergeObjects"
44+
ToFieldPathPolicyForceMergeObjectsAppendArrays ToFieldPathPolicy = "ForceMergeObjectsAppendArrays"
45+
46+
// Deprecated: Use MergeObjects, which is functionally identical.
4147
ToFieldPathPolicyMergeObject ToFieldPathPolicy = "MergeObject"
48+
// Deprecated: Use ForceMergeObjectsAppendArrays, which is functionally identical.
4249
ToFieldPathPolicyAppendArray ToFieldPathPolicy = "AppendArray"
4350
)
4451

@@ -54,10 +61,17 @@ type PatchPolicy struct {
5461

5562
// ToFieldPath specifies how to patch to a field path. The default is
5663
// 'Replace', which means the patch will completely replace the target field,
57-
// or create it if it does not exist. Use 'MergeObject' to merge the patch
58-
// object with the target object, or 'AppendArray' to append the patch array
59-
// to the target array.
60-
// +kubebuilder:validation:Enum=Replace;MergeObject;AppendArray
64+
// or create it if it does not exist. Use 'MergeObjects' to recursively merge the patch
65+
// object with the target object, while keeping target object keys, but overwriting any array values, or use
66+
// 'MergeObjectsAppendArrays' to recursively merge the patch object with the target object, while keeping
67+
// target object keys and appending any array values to target array values, or use
68+
// 'ForceMergeObjects' to recursively merge the patch object with the target object, overwriting
69+
// any target object keys, including array values, or use
70+
// 'ForceMergeObjectsAppendArrays' to recursively merge the patch object with the target object,
71+
// overwriting target object keys, and appending any array values to target array values.
72+
// 'MergeObject' is deprecated, use 'MergeObjects' instead, which is functionally identical.
73+
// 'AppendArray' is deprecated, use 'ForceMergeObjectsAppendArrays' instead, which is functionally identical.
74+
// +kubebuilder:validation:Enum=Replace;MergeObjects;MergeObjectsAppendArrays;ForceMergeObjects;ForceMergeObjectsAppendArrays;MergeObject;AppendArray
6175
// +optional
6276
ToFieldPath *ToFieldPathPolicy `json:"toFieldPath,omitempty"`
6377
}

package/input/pt.fn.crossplane.io_resources.yaml

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,22 @@ spec:
121121
description: |-
122122
ToFieldPath specifies how to patch to a field path. The default is
123123
'Replace', which means the patch will completely replace the target field,
124-
or create it if it does not exist. Use 'MergeObject' to merge the patch
125-
object with the target object, or 'AppendArray' to append the patch array
126-
to the target array.
124+
or create it if it does not exist. Use 'MergeObjects' to recursively merge the patch
125+
object with the target object, while keeping target object keys, but overwriting any array values, or use
126+
'MergeObjectsAppendArrays' to recursively merge the patch object with the target object, while keeping
127+
target object keys and appending any array values to target array values, or use
128+
'ForceMergeObjects' to recursively merge the patch object with the target object, overwriting
129+
any target object keys, including array values, or use
130+
'ForceMergeObjectsAppendArrays' to recursively merge the patch object with the target object,
131+
overwriting target object keys, and appending any array values to target array values.
132+
'MergeObject' is deprecated, use 'MergeObjects' instead, which is functionally identical.
133+
'AppendArray' is deprecated, use 'ForceMergeObjectsAppendArrays' instead, which is functionally identical.
127134
enum:
128135
- Replace
136+
- MergeObjects
137+
- MergeObjectsAppendArrays
138+
- ForceMergeObjects
139+
- ForceMergeObjectsAppendArrays
129140
- MergeObject
130141
- AppendArray
131142
type: string
@@ -464,11 +475,22 @@ spec:
464475
description: |-
465476
ToFieldPath specifies how to patch to a field path. The default is
466477
'Replace', which means the patch will completely replace the target field,
467-
or create it if it does not exist. Use 'MergeObject' to merge the patch
468-
object with the target object, or 'AppendArray' to append the patch array
469-
to the target array.
478+
or create it if it does not exist. Use 'MergeObjects' to recursively merge the patch
479+
object with the target object, while keeping target object keys, but overwriting any array values, or use
480+
'MergeObjectsAppendArrays' to recursively merge the patch object with the target object, while keeping
481+
target object keys and appending any array values to target array values, or use
482+
'ForceMergeObjects' to recursively merge the patch object with the target object, overwriting
483+
any target object keys, including array values, or use
484+
'ForceMergeObjectsAppendArrays' to recursively merge the patch object with the target object,
485+
overwriting target object keys, and appending any array values to target array values.
486+
'MergeObject' is deprecated, use 'MergeObjects' instead, which is functionally identical.
487+
'AppendArray' is deprecated, use 'ForceMergeObjectsAppendArrays' instead, which is functionally identical.
470488
enum:
471489
- Replace
490+
- MergeObjects
491+
- MergeObjectsAppendArrays
492+
- ForceMergeObjects
493+
- ForceMergeObjectsAppendArrays
472494
- MergeObject
473495
- AppendArray
474496
type: string
@@ -871,11 +893,22 @@ spec:
871893
description: |-
872894
ToFieldPath specifies how to patch to a field path. The default is
873895
'Replace', which means the patch will completely replace the target field,
874-
or create it if it does not exist. Use 'MergeObject' to merge the patch
875-
object with the target object, or 'AppendArray' to append the patch array
876-
to the target array.
896+
or create it if it does not exist. Use 'MergeObjects' to recursively merge the patch
897+
object with the target object, while keeping target object keys, but overwriting any array values, or use
898+
'MergeObjectsAppendArrays' to recursively merge the patch object with the target object, while keeping
899+
target object keys and appending any array values to target array values, or use
900+
'ForceMergeObjects' to recursively merge the patch object with the target object, overwriting
901+
any target object keys, including array values, or use
902+
'ForceMergeObjectsAppendArrays' to recursively merge the patch object with the target object,
903+
overwriting target object keys, and appending any array values to target array values.
904+
'MergeObject' is deprecated, use 'MergeObjects' instead, which is functionally identical.
905+
'AppendArray' is deprecated, use 'ForceMergeObjectsAppendArrays' instead, which is functionally identical.
877906
enum:
878907
- Replace
908+
- MergeObjects
909+
- MergeObjectsAppendArrays
910+
- ForceMergeObjects
911+
- ForceMergeObjectsAppendArrays
879912
- MergeObject
880913
- AppendArray
881914
type: string

patches.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ func toMergeOption(p PatchInterface) (mo *xpv1.MergeOptions, err error) {
102102
switch pp.GetToFieldPathPolicy() {
103103
case v1beta1.ToFieldPathPolicyReplace:
104104
// nothing to do, this is the default
105-
case v1beta1.ToFieldPathPolicyAppendArray:
106-
mo = &xpv1.MergeOptions{AppendSlice: ptr.To(true)}
107-
case v1beta1.ToFieldPathPolicyMergeObject:
105+
case v1beta1.ToFieldPathPolicyMergeObjects, v1beta1.ToFieldPathPolicyMergeObject: //nolint:staticcheck // MergeObject is deprecated but we must still support it.
108106
mo = &xpv1.MergeOptions{KeepMapValues: ptr.To(true)}
107+
case v1beta1.ToFieldPathPolicyMergeObjectsAppendArrays:
108+
mo = &xpv1.MergeOptions{KeepMapValues: ptr.To(true), AppendSlice: ptr.To(true)}
109+
case v1beta1.ToFieldPathPolicyForceMergeObjects:
110+
mo = &xpv1.MergeOptions{KeepMapValues: ptr.To(false)}
111+
case v1beta1.ToFieldPathPolicyForceMergeObjectsAppendArrays, v1beta1.ToFieldPathPolicyAppendArray: //nolint:staticcheck // AppendArray is deprecated but we must still support it.
112+
mo = &xpv1.MergeOptions{AppendSlice: ptr.To(true)}
109113
default:
110114
// should never happen
111115
return nil, errors.Errorf(errFmtInvalidPatchPolicy, pp.GetToFieldPathPolicy())

validate.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,12 @@ func ValidatePatch(p PatchInterface) *field.Error { //nolint: gocyclo // This is
202202
if pp := p.GetPolicy(); pp != nil {
203203
switch pp.GetToFieldPathPolicy() {
204204
case v1beta1.ToFieldPathPolicyReplace,
205-
v1beta1.ToFieldPathPolicyAppendArray,
206-
v1beta1.ToFieldPathPolicyMergeObject:
205+
v1beta1.ToFieldPathPolicyMergeObjects,
206+
v1beta1.ToFieldPathPolicyMergeObjectsAppendArrays,
207+
v1beta1.ToFieldPathPolicyForceMergeObjects,
208+
v1beta1.ToFieldPathPolicyForceMergeObjectsAppendArrays,
209+
v1beta1.ToFieldPathPolicyMergeObject, //nolint:staticcheck // MergeObject is deprecated but we must still support it.
210+
v1beta1.ToFieldPathPolicyAppendArray: //nolint:staticcheck // AppendArray is deprecated but we must still support it.
207211
// ok
208212
default:
209213
return field.Invalid(field.NewPath("policy", "toFieldPathPolicy"), pp.GetToFieldPathPolicy(), "unknown toFieldPathPolicy")

0 commit comments

Comments
 (0)