@@ -131,17 +131,11 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
131
131
}
132
132
133
133
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 {
136
136
response .Fatal (rsp , errors .Wrapf (err , "cannot render ToEnvironment patches from the composite resource" ))
137
137
return rsp , nil
138
138
}
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
- }
145
139
}
146
140
147
141
// Increment this if you emit a warning result.
@@ -228,58 +222,19 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
228
222
log .Debug ("Found corresponding observed resource" ,
229
223
"ready" , ready ,
230
224
"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
- }
260
225
}
261
226
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 )
271
231
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
279
232
}
280
233
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
+ }
283
238
}
284
239
285
240
if err := response .SetDesiredCompositeResource (rsp , dxr ); err != nil {
0 commit comments