Skip to content

Commit 74aefe6

Browse files
committed
fix: correct patches in input CRD
Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
1 parent 7fb1c72 commit 74aefe6

File tree

10 files changed

+586
-349
lines changed

10 files changed

+586
-349
lines changed

fn_test.go

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,25 @@ func TestRunFunction(t *testing.T) {
156156
{
157157
Name: "cool-resource",
158158
Base: &runtime.RawExtension{Raw: []byte(`{"apiVersion":"example.org/v1","kind":"CD"}`)},
159-
Patches: []v1beta1.Patch{
159+
Patches: []v1beta1.ComposedPatch{
160160
{
161-
Type: v1beta1.PatchTypeFromCompositeFieldPath,
162-
FromFieldPath: ptr.To[string]("spec.widgets"),
163-
ToFieldPath: ptr.To[string]("spec.watchers"),
164-
Transforms: []v1beta1.Transform{
165-
{
166-
Type: v1beta1.TransformTypeConvert,
167-
Convert: &v1beta1.ConvertTransform{
168-
ToType: v1beta1.TransformIOTypeInt64,
161+
Type: v1beta1.PatchTypeFromCompositeFieldPath,
162+
Patch: v1beta1.Patch{
163+
FromFieldPath: ptr.To[string]("spec.widgets"),
164+
ToFieldPath: ptr.To[string]("spec.watchers"),
165+
Transforms: []v1beta1.Transform{
166+
{
167+
Type: v1beta1.TransformTypeConvert,
168+
Convert: &v1beta1.ConvertTransform{
169+
ToType: v1beta1.TransformIOTypeInt64,
170+
},
169171
},
170-
},
171-
{
172-
Type: v1beta1.TransformTypeMath,
173-
Math: &v1beta1.MathTransform{
174-
Type: v1beta1.MathTransformTypeMultiply,
175-
Multiply: ptr.To[int64](3),
172+
{
173+
Type: v1beta1.TransformTypeMath,
174+
Math: &v1beta1.MathTransform{
175+
Type: v1beta1.MathTransformTypeMultiply,
176+
Multiply: ptr.To[int64](3),
177+
},
176178
},
177179
},
178180
},
@@ -221,23 +223,25 @@ func TestRunFunction(t *testing.T) {
221223
// patch the resource named "cool-resource" in
222224
// the desired resources array.
223225
Name: "cool-resource",
224-
Patches: []v1beta1.Patch{
226+
Patches: []v1beta1.ComposedPatch{
225227
{
226-
Type: v1beta1.PatchTypeFromCompositeFieldPath,
227-
FromFieldPath: ptr.To[string]("spec.widgets"),
228-
ToFieldPath: ptr.To[string]("spec.watchers"),
229-
Transforms: []v1beta1.Transform{
230-
{
231-
Type: v1beta1.TransformTypeConvert,
232-
Convert: &v1beta1.ConvertTransform{
233-
ToType: v1beta1.TransformIOTypeInt64,
228+
Type: v1beta1.PatchTypeFromCompositeFieldPath,
229+
Patch: v1beta1.Patch{
230+
FromFieldPath: ptr.To[string]("spec.widgets"),
231+
ToFieldPath: ptr.To[string]("spec.watchers"),
232+
Transforms: []v1beta1.Transform{
233+
{
234+
Type: v1beta1.TransformTypeConvert,
235+
Convert: &v1beta1.ConvertTransform{
236+
ToType: v1beta1.TransformIOTypeInt64,
237+
},
234238
},
235-
},
236-
{
237-
Type: v1beta1.TransformTypeMath,
238-
Math: &v1beta1.MathTransform{
239-
Type: v1beta1.MathTransformTypeMultiply,
240-
Multiply: ptr.To[int64](3),
239+
{
240+
Type: v1beta1.TransformTypeMath,
241+
Math: &v1beta1.MathTransform{
242+
Type: v1beta1.MathTransformTypeMultiply,
243+
Multiply: ptr.To[int64](3),
244+
},
241245
},
242246
},
243247
},
@@ -291,11 +295,13 @@ func TestRunFunction(t *testing.T) {
291295
// patch the resource named "cool-resource" in
292296
// the desired resources array.
293297
Name: "cool-resource",
294-
Patches: []v1beta1.Patch{
298+
Patches: []v1beta1.ComposedPatch{
295299
{
296-
Type: v1beta1.PatchTypeFromCompositeFieldPath,
297-
FromFieldPath: ptr.To[string]("spec.widgets"),
298-
ToFieldPath: ptr.To[string]("spec.watchers"),
300+
Type: v1beta1.PatchTypeFromCompositeFieldPath,
301+
Patch: v1beta1.Patch{
302+
FromFieldPath: ptr.To[string]("spec.widgets"),
303+
ToFieldPath: ptr.To[string]("spec.watchers"),
304+
},
299305
},
300306
},
301307
},
@@ -387,25 +393,29 @@ func TestRunFunction(t *testing.T) {
387393
// patch the resource named "cool-resource" in
388394
// the desired resources array.
389395
Name: "cool-resource",
390-
Patches: []v1beta1.Patch{
396+
Patches: []v1beta1.ComposedPatch{
391397
{
392398
// This patch should work.
393-
Type: v1beta1.PatchTypeFromCompositeFieldPath,
394-
FromFieldPath: ptr.To[string]("spec.widgets"),
395-
ToFieldPath: ptr.To[string]("spec.watchers"),
399+
Type: v1beta1.PatchTypeFromCompositeFieldPath,
400+
Patch: v1beta1.Patch{
401+
FromFieldPath: ptr.To[string]("spec.widgets"),
402+
ToFieldPath: ptr.To[string]("spec.watchers"),
403+
},
396404
},
397405
{
398406
// This patch should return an error,
399407
// because the required path does not
400408
// exist.
401-
Type: v1beta1.PatchTypeFromCompositeFieldPath,
402-
FromFieldPath: ptr.To[string]("spec.doesNotExist"),
403-
ToFieldPath: ptr.To[string]("spec.explode"),
404-
Policy: &v1beta1.PatchPolicy{
405-
FromFieldPath: func() *v1beta1.FromFieldPathPolicy {
406-
r := v1beta1.FromFieldPathPolicyRequired
407-
return &r
408-
}(),
409+
Type: v1beta1.PatchTypeFromCompositeFieldPath,
410+
Patch: v1beta1.Patch{
411+
FromFieldPath: ptr.To[string]("spec.doesNotExist"),
412+
ToFieldPath: ptr.To[string]("spec.explode"),
413+
Policy: &v1beta1.PatchPolicy{
414+
FromFieldPath: func() *v1beta1.FromFieldPathPolicy {
415+
r := v1beta1.FromFieldPathPolicyRequired
416+
return &r
417+
}(),
418+
},
409419
},
410420
},
411421
},
@@ -572,23 +582,25 @@ func TestRunFunction(t *testing.T) {
572582
{
573583
Name: "cool-resource",
574584
Base: &runtime.RawExtension{Raw: []byte(`{"apiVersion":"example.org/v1","kind":"CD"}`)},
575-
Patches: []v1beta1.Patch{
585+
Patches: []v1beta1.ComposedPatch{
576586
{
577-
Type: v1beta1.PatchTypeToCompositeFieldPath,
578-
FromFieldPath: ptr.To[string]("spec.widgets"),
579-
ToFieldPath: ptr.To[string]("spec.watchers"),
580-
Transforms: []v1beta1.Transform{
581-
{
582-
Type: v1beta1.TransformTypeConvert,
583-
Convert: &v1beta1.ConvertTransform{
584-
ToType: v1beta1.TransformIOTypeInt64,
587+
Type: v1beta1.PatchTypeToCompositeFieldPath,
588+
Patch: v1beta1.Patch{
589+
FromFieldPath: ptr.To[string]("spec.widgets"),
590+
ToFieldPath: ptr.To[string]("spec.watchers"),
591+
Transforms: []v1beta1.Transform{
592+
{
593+
Type: v1beta1.TransformTypeConvert,
594+
Convert: &v1beta1.ConvertTransform{
595+
ToType: v1beta1.TransformIOTypeInt64,
596+
},
585597
},
586-
},
587-
{
588-
Type: v1beta1.TransformTypeMath,
589-
Math: &v1beta1.MathTransform{
590-
Type: v1beta1.MathTransformTypeMultiply,
591-
Multiply: ptr.To[int64](3),
598+
{
599+
Type: v1beta1.TransformTypeMath,
600+
Math: &v1beta1.MathTransform{
601+
Type: v1beta1.MathTransformTypeMultiply,
602+
Multiply: ptr.To[int64](3),
603+
},
592604
},
593605
},
594606
},

input/v1beta1/resources_common.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ type PatchSet struct {
2828
Name string `json:"name"`
2929

3030
// Patches will be applied as an overlay to the base resource.
31-
Patches []Patch `json:"patches"`
31+
Patches []PatchSetPatch `json:"patches"`
32+
}
33+
34+
// GetComposedPatches returns the composed patches from the patch set.
35+
func (ps *PatchSet) GetComposedPatches() []ComposedPatch {
36+
out := make([]ComposedPatch, len(ps.Patches))
37+
for i, p := range ps.Patches {
38+
out[i] = ComposedPatch{
39+
Type: p.GetType(),
40+
Patch: p.Patch,
41+
}
42+
}
43+
return out
3244
}
3345

3446
// ComposedTemplate is used to provide information about how the composed
@@ -49,7 +61,7 @@ type ComposedTemplate struct {
4961

5062
// Patches to and from the composed resource.
5163
// +optional
52-
Patches []Patch `json:"patches,omitempty"`
64+
Patches []ComposedPatch `json:"patches,omitempty"`
5365

5466
// ConnectionDetails lists the propagation secret keys from this composed
5567
// resource to the composition instance connection secret.

input/v1beta1/resources_patches.go

Lines changed: 92 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,91 @@ type Environment struct {
5757
// composition's resources are composed. These patches are between the XR
5858
// and the Environment. Either from the Environment to the XR, or vice
5959
// versa.
60-
Patches []Patch `json:"patches,omitempty"`
60+
Patches []EnvironmentPatch `json:"patches,omitempty"`
6161
}
6262

63-
// Patch objects are applied between composite and composed resources. Their
64-
// behaviour depends on the Type selected. The default Type,
65-
// FromCompositeFieldPath, copies a value from the composite resource to
66-
// the composed resource, applying any defined transformers.
67-
type Patch struct {
63+
// EnvironmentPatch objects are applied between the composite resource and
64+
// the environment. Their behaviour depends on the Type selected. The default
65+
// Type, FromCompositeFieldPath, copies a value from the composite resource
66+
// to the environment, applying any defined transformers.
67+
type EnvironmentPatch struct {
6868
// Type sets the patching behaviour to be used. Each patch type may require
6969
// its own fields to be set on the Patch object.
7070
// +optional
71-
// +kubebuilder:validation:Enum=FromCompositeFieldPath;PatchSet;ToCompositeFieldPath;CombineFromComposite;CombineToComposite
71+
// +kubebuilder:validation:Enum=FromCompositeFieldPath;ToCompositeFieldPath;CombineFromComposite;CombineToComposite
7272
// +kubebuilder:default=FromCompositeFieldPath
7373
Type PatchType `json:"type,omitempty"`
7474

75+
Patch `json:",inline"`
76+
}
77+
78+
// GetType returns the patch type. If the type is not set, it returns the default type.
79+
func (ep *EnvironmentPatch) GetType() PatchType {
80+
if ep.Type == "" {
81+
return PatchTypeFromCompositeFieldPath
82+
}
83+
return ep.Type
84+
}
85+
86+
// ComposedPatch objects are applied between composite and composed resources.
87+
// Their behaviour depends on the Type selected. The default Type,
88+
// FromCompositeFieldPath, copies a value from the composite resource to the
89+
// composed resource, applying any defined transformers.
90+
type ComposedPatch struct {
91+
// Type sets the patching behaviour to be used. Each patch type may require
92+
// its own fields to be set on the ComposedPatch object.
93+
// +optional
94+
// +kubebuilder:validation:Enum=FromCompositeFieldPath;PatchSet;ToCompositeFieldPath;CombineFromComposite;CombineToComposite;FromEnvironmentFieldPath;ToEnvironmentFieldPath;CombineFromEnvironment;CombineToEnvironment
95+
// +kubebuilder:default=FromCompositeFieldPath
96+
Type PatchType `json:"type,omitempty"`
97+
98+
// PatchSetName to include patches from. Required when type is PatchSet.
99+
// +optional
100+
PatchSetName *string `json:"patchSetName,omitempty"`
101+
102+
Patch `json:",inline"`
103+
}
104+
105+
// GetType returns the patch type. If the type is not set, it returns the default type.
106+
func (p *ComposedPatch) GetType() PatchType {
107+
if p.Type == "" {
108+
return PatchTypeFromCompositeFieldPath
109+
}
110+
return p.Type
111+
}
112+
113+
// GetPatchSetName returns the PatchSetName for this ComposedPatch, or an empty
114+
// string if it is nil.
115+
func (p *ComposedPatch) GetPatchSetName() string {
116+
if p.PatchSetName == nil {
117+
return ""
118+
}
119+
return *p.PatchSetName
120+
}
121+
122+
// PatchSetPatch defines a set of Patches that can be referenced by name by
123+
// other patches of type PatchSet.
124+
type PatchSetPatch struct {
125+
// Type sets the patching behaviour to be used. Each patch type may require
126+
// its own fields to be set on the ComposedPatch object.
127+
// +optional
128+
// +kubebuilder:validation:Enum=FromCompositeFieldPath;ToCompositeFieldPath;CombineFromComposite;CombineToComposite;FromEnvironmentFieldPath;ToEnvironmentFieldPath;CombineFromEnvironment;CombineToEnvironment
129+
// +kubebuilder:default=FromCompositeFieldPath
130+
Type PatchType `json:"type,omitempty"`
131+
132+
Patch `json:",inline"`
133+
}
134+
135+
// GetType returns the patch type. If the type is not set, it returns the default type.
136+
func (psp *PatchSetPatch) GetType() PatchType {
137+
if psp.Type == "" {
138+
return PatchTypeFromCompositeFieldPath
139+
}
140+
return psp.Type
141+
}
142+
143+
// Patch defines a patch between a source and destination.
144+
type Patch struct {
75145
// FromFieldPath is the path of the field on the resource whose value is
76146
// to be used as input. Required when type is FromCompositeFieldPath or
77147
// ToCompositeFieldPath.
@@ -89,10 +159,6 @@ type Patch struct {
89159
// +optional
90160
ToFieldPath *string `json:"toFieldPath,omitempty"`
91161

92-
// PatchSetName to include patches from. Required when type is PatchSet.
93-
// +optional
94-
PatchSetName *string `json:"patchSetName,omitempty"`
95-
96162
// Transforms are the list of functions that are used as a FIFO pipe for the
97163
// input to be transformed.
98164
// +optional
@@ -114,17 +180,25 @@ func (p *Patch) GetFromFieldPath() string {
114180
// GetToFieldPath returns the ToFieldPath for this Patch, or an empty string if it is nil.
115181
func (p *Patch) GetToFieldPath() string {
116182
if p.ToFieldPath == nil {
117-
return ""
183+
// Default to patching the same field on the composed resource.
184+
return p.GetFromFieldPath()
118185
}
119186
return *p.ToFieldPath
120187
}
121188

122-
// GetType returns the patch type. If the type is not set, it returns the default type.
123-
func (p *Patch) GetType() PatchType {
124-
if p.Type == "" {
125-
return PatchTypeFromCompositeFieldPath
126-
}
127-
return p.Type
189+
// GetCombine returns the Combine for this ComposedPatch, or nil if it is nil.
190+
func (p *Patch) GetCombine() *Combine {
191+
return p.Combine
192+
}
193+
194+
// GetTransforms returns the Transforms for this ComposedPatch, or nil if it is nil.
195+
func (p *Patch) GetTransforms() []Transform {
196+
return p.Transforms
197+
}
198+
199+
// GetPolicy returns the PatchPolicy for this ComposedPatch, or nil if it is nil.
200+
func (p *Patch) GetPolicy() *PatchPolicy {
201+
return p.Policy
128202
}
129203

130204
// A CombineVariable defines the source of a value that is combined with

0 commit comments

Comments
 (0)