From 6548d75b28a95736a029ff81e7a78ef33b098dcf Mon Sep 17 00:00:00 2001 From: Jean-Francois Remy Date: Thu, 4 Jan 2024 12:20:59 -0800 Subject: [PATCH 1/2] Add documentation note describing behavior with status --- pkg/controller/controllerutil/controllerutil.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/controller/controllerutil/controllerutil.go b/pkg/controller/controllerutil/controllerutil.go index 708d56cbc4..2446402e56 100644 --- a/pkg/controller/controllerutil/controllerutil.go +++ b/pkg/controller/controllerutil/controllerutil.go @@ -270,6 +270,9 @@ const ( // They should complete the sentence "Deployment default/foo has been .. // The MutateFn is called regardless of creating or updating an object. // // It returns the executed operation and an error. +// +// Note: CreateOrUpdate does not update the status of the resource. Any +// change made to it will be ignored func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error) { key := client.ObjectKeyFromObject(obj) if err := c.Get(ctx, key, obj); err != nil { @@ -307,6 +310,11 @@ func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f M // The MutateFn is called regardless of creating or updating an object. // // It returns the executed operation and an error. +// +// Note: CreateOrPatch will patch the status if MutateFn makes change to it +// and the resource already exists. If the resource does not exist, changes +// to the status are ignored. A simple solution in that case is to requeue +// the current element if OperationResult is equal to OperationResultCreated func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error) { key := client.ObjectKeyFromObject(obj) if err := c.Get(ctx, key, obj); err != nil { From 947aa5f06f152426b9243d1b4e7995a9d50b81a6 Mon Sep 17 00:00:00 2001 From: Jean-Francois Remy Date: Thu, 4 Jan 2024 13:10:44 -0800 Subject: [PATCH 2/2] update text --- pkg/controller/controllerutil/controllerutil.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/controller/controllerutil/controllerutil.go b/pkg/controller/controllerutil/controllerutil.go index 2446402e56..05153f74ce 100644 --- a/pkg/controller/controllerutil/controllerutil.go +++ b/pkg/controller/controllerutil/controllerutil.go @@ -271,8 +271,8 @@ const ( // They should complete the sentence "Deployment default/foo has been .. // // It returns the executed operation and an error. // -// Note: CreateOrUpdate does not update the status of the resource. Any -// change made to it will be ignored +// Note: changes made by MutateFn to any sub-resource (status...), will be +// discarded. func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error) { key := client.ObjectKeyFromObject(obj) if err := c.Get(ctx, key, obj); err != nil { @@ -311,10 +311,11 @@ func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f M // // It returns the executed operation and an error. // -// Note: CreateOrPatch will patch the status if MutateFn makes change to it -// and the resource already exists. If the resource does not exist, changes -// to the status are ignored. A simple solution in that case is to requeue -// the current element if OperationResult is equal to OperationResultCreated +// Note: changes to any sub-resource other than status will be ignored. +// Changes to the status sub-resource will only be applied if the object +// already exist. To change the status on object creation, the easiest +// way is to requeue the object in the controller if OperationResult is +// OperationResultCreated func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error) { key := client.ObjectKeyFromObject(obj) if err := c.Get(ctx, key, obj); err != nil {