Skip to content

Commit 436ba40

Browse files
Merge pull request #7 from stevendborrelli/patch-ordering
Patch ordering
2 parents e6a26c6 + 61e3372 commit 436ba40

17 files changed

+995
-459
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ metadata:
1818
annotations:
1919
render.crossplane.io/runtime: Development
2020
spec:
21-
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.3.0
21+
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.4.0
2222
```
2323
2424
## What this function does

examples/conditional-rendering/functions.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ kind: Function
33
metadata:
44
name: function-conditional-patch-and-transform
55
spec:
6-
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.3.0
6+
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.4.0
77
packagePullPolicy: Always

examples/conditional-resources/composition.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ spec:
1515
input:
1616
apiVersion: conditional-pt.fn.crossplane.io/v1beta1
1717
kind: Resources
18+
condition: observed.composite.resource.spec.render == true
1819
resources:
1920
- name: blue-resource
2021
condition: observed.composite.resource.spec.deployment.blue == true

examples/conditional-resources/functions.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ kind: Function
33
metadata:
44
name: function-conditional-patch-and-transform
55
spec:
6-
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.3.0
6+
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.4.0
77
packagePullPolicy: Always

fn.go

+10-55
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,11 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
131131
}
132132

133133
if input.Environment != nil {
134-
// Run all patches that are from the (observed) XR to the environment.
135-
if err := RenderToEnvironmentPatches(env, oxr.Resource, input.Environment.Patches); err != nil {
134+
// Run all patches that are from the (observed) XR to the environment or from the environment to the (desired) XR.
135+
if err := RenderEnvironmentPatches(env, oxr.Resource, dxr.Resource, input.Environment.Patches); err != nil {
136136
response.Fatal(rsp, errors.Wrapf(err, "cannot render ToEnvironment patches from the composite resource"))
137137
return rsp, nil
138138
}
139-
140-
// Run all patches that are from the environment to the (desired) XR.
141-
if err := RenderFromEnvironmentPatches(dxr.Resource, env, input.Environment.Patches); err != nil {
142-
response.Fatal(rsp, errors.Wrapf(err, "cannot render FromEnvironment patches to the composite resource"))
143-
return rsp, nil
144-
}
145139
}
146140

147141
// Increment this if you emit a warning result.
@@ -228,58 +222,19 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
228222
log.Debug("Found corresponding observed resource",
229223
"ready", ready,
230224
"name", ocd.Resource.GetName())
231-
232-
// TODO(negz): Should failures to patch the XR be terminal? It could
233-
// indicate a required patch failed. A required patch means roughly
234-
// "this patch has to succeed before you mutate the resource". This
235-
// is useful to make sure we never create a composed resource in the
236-
// wrong state. It's less clear how useful it is for the XR, given
237-
// we'll only ever be updating it, not creating it.
238-
239-
// We want to patch the XR from observed composed resources, not
240-
// from desired state. This is because folks will typically be
241-
// patching from a field that is set once the observed resource is
242-
// applied such as its status.
243-
if err := RenderToCompositePatches(dxr.Resource, ocd.Resource, t.Patches); err != nil {
244-
response.Warning(rsp, errors.Wrapf(err, "cannot render ToComposite patches for composed resource %q", t.Name))
245-
log.Info("Cannot render ToComposite patches for composed resource", "warning", err)
246-
warnings++
247-
}
248-
249-
// TODO(negz): Same as above, but for the Environment. What does it
250-
// mean for a required patch to the environment to fail? Should it
251-
// be terminal?
252-
253-
// Run all patches that are from the (observed) composed resource to
254-
// the environment.
255-
if err := RenderToEnvironmentPatches(env, ocd.Resource, t.Patches); err != nil {
256-
response.Warning(rsp, errors.Wrapf(err, "cannot render ToEnvironment patches for composed resource %q", t.Name))
257-
log.Info("Cannot render ToEnvironment patches for composed resource", "warning", err)
258-
warnings++
259-
}
260225
}
261226

262-
// If either of the below renderings return an error, most likely a
263-
// required FromComposite or FromEnvironment patch failed. A required
264-
// patch means roughly "this patch has to succeed before you mutate the
265-
// resource." This is useful to make sure we never create a composed
266-
// resource in the wrong state. To that end, we don't want to add this
267-
// resource to our accumulated desired state.
268-
if err := RenderFromCompositePatches(dcd.Resource, oxr.Resource, t.Patches); err != nil {
269-
response.Warning(rsp, errors.Wrapf(err, "cannot render FromComposite patches for composed resource %q", t.Name))
270-
log.Info("Cannot render FromComposite patches for composed resource", "warning", err)
227+
errs, store := RenderComposedPatches(ocd.Resource, dcd.Resource, oxr.Resource, dxr.Resource, env, t.Patches)
228+
for _, err := range errs {
229+
response.Warning(rsp, errors.Wrapf(err, "cannot render patches for composed resource %q", t.Name))
230+
log.Info("Cannot render patches for composed resource", "warning", err)
271231
warnings++
272-
continue
273-
}
274-
if err := RenderFromEnvironmentPatches(dcd.Resource, env, t.Patches); err != nil {
275-
response.Warning(rsp, errors.Wrapf(err, "cannot render FromEnvironment patches for composed resource %q", t.Name))
276-
log.Info("Cannot render FromEnvironment patches for composed resource", "warning", err)
277-
warnings++
278-
continue
279232
}
280233

281-
// Add or replace our desired resource.
282-
desired[resource.Name(t.Name)] = dcd
234+
if store {
235+
// Add or replace our desired resource.
236+
desired[resource.Name(t.Name)] = dcd
237+
}
283238
}
284239

285240
if err := response.SetDesiredCompositeResource(rsp, dxr); err != nil {

0 commit comments

Comments
 (0)