@@ -35,6 +35,7 @@ import (
3535 "sigs.k8s.io/controller-runtime/pkg/builder"
3636 "sigs.k8s.io/controller-runtime/pkg/client"
3737 crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
38+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3839 "sigs.k8s.io/controller-runtime/pkg/event"
3940 crhandler "sigs.k8s.io/controller-runtime/pkg/handler"
4041 "sigs.k8s.io/controller-runtime/pkg/log"
@@ -48,6 +49,7 @@ import (
4849 ocv1 "github.com/operator-framework/operator-controller/api/v1"
4950 "github.com/operator-framework/operator-controller/internal/operator-controller/conditionsets"
5051 "github.com/operator-framework/operator-controller/internal/operator-controller/labels"
52+ k8sutil "github.com/operator-framework/operator-controller/internal/shared/util/k8s"
5153)
5254
5355const (
@@ -135,25 +137,28 @@ func (r *ClusterExtensionReconciler) Reconcile(ctx context.Context, req ctrl.Req
135137 updateFinalizers := ! equality .Semantic .DeepEqual (existingExt .Finalizers , reconciledExt .Finalizers )
136138
137139 // If any unexpected fields have changed, panic before updating the resource
138- unexpectedFieldsChanged := checkForUnexpectedClusterExtensionFieldChange ( * existingExt , * reconciledExt )
140+ unexpectedFieldsChanged := k8sutil . CheckForUnexpectedFieldChange ( existingExt , reconciledExt )
139141 if unexpectedFieldsChanged {
140142 panic ("spec or metadata changed by reconciler" )
141143 }
142144
143145 // Save the finalizers off to the side. If we update the status, the reconciledExt will be updated
144146 // to contain the new state of the ClusterExtension, which contains the status update, but (critically)
145- // does not contain the finalizers. After the status update, we need to re-add the finalizers to the
146- // reconciledExt before updating the object.
147+ // does not contain the finalizers. After the status update, we will use the saved finalizers in the
148+ // CreateOrPatch()
147149 finalizers := reconciledExt .Finalizers
148150 if updateStatus {
149151 if err := r .Client .Status ().Update (ctx , reconciledExt ); err != nil {
150152 reconcileErr = errors .Join (reconcileErr , fmt .Errorf ("error updating status: %v" , err ))
151153 }
152154 }
153- reconciledExt .Finalizers = finalizers
154155
155156 if updateFinalizers {
156- if err := r .Update (ctx , reconciledExt ); err != nil {
157+ // Use CreateOrPatch to update finalizers on the server
158+ if _ , err := controllerutil .CreateOrPatch (ctx , r .Client , reconciledExt , func () error {
159+ reconciledExt .Finalizers = finalizers
160+ return nil
161+ }); err != nil {
157162 reconcileErr = errors .Join (reconcileErr , fmt .Errorf ("error updating finalizers: %v" , err ))
158163 }
159164 }
@@ -179,13 +184,6 @@ func ensureAllConditionsWithReason(ext *ocv1.ClusterExtension, reason v1alpha1.C
179184 }
180185}
181186
182- // Compare resources - ignoring status & metadata.finalizers
183- func checkForUnexpectedClusterExtensionFieldChange (a , b ocv1.ClusterExtension ) bool {
184- a .Status , b .Status = ocv1.ClusterExtensionStatus {}, ocv1.ClusterExtensionStatus {}
185- a .Finalizers , b .Finalizers = []string {}, []string {}
186- return ! equality .Semantic .DeepEqual (a , b )
187- }
188-
189187// SetDeprecationStatus will set the appropriate deprecation statuses for a ClusterExtension
190188// based on the provided bundle
191189func SetDeprecationStatus (ext * ocv1.ClusterExtension , bundleName string , deprecation * declcfg.Deprecation ) {
0 commit comments