@@ -34,12 +34,12 @@ import (
3434 ctrl "sigs.k8s.io/controller-runtime"
3535 "sigs.k8s.io/controller-runtime/pkg/client"
3636 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
37- crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
3837 "sigs.k8s.io/controller-runtime/pkg/log"
3938 "sigs.k8s.io/controller-runtime/pkg/reconcile"
4039
4140 ocv1 "github.com/operator-framework/operator-controller/api/v1"
4241 "github.com/operator-framework/operator-controller/internal/catalogd/storage"
42+ finalizerutil "github.com/operator-framework/operator-controller/internal/shared/util/finalizer"
4343 imageutil "github.com/operator-framework/operator-controller/internal/shared/util/image"
4444)
4545
@@ -59,8 +59,6 @@ type ClusterCatalogReconciler struct {
5959
6060 Storage storage.Instance
6161
62- finalizers crfinalizer.Finalizers
63-
6462 // TODO: The below storedCatalogs fields are used for a quick a hack that helps
6563 // us correctly populate a ClusterCatalog's status. The fact that we need
6664 // these is indicative of a larger problem with the design of one or both
@@ -106,33 +104,18 @@ func (r *ClusterCatalogReconciler) Reconcile(ctx context.Context, req ctrl.Reque
106104
107105 // Do checks before any Update()s, as Update() may modify the resource structure!
108106 updateStatus := ! equality .Semantic .DeepEqual (existingCatsrc .Status , reconciledCatsrc .Status )
109- updateFinalizers := ! equality .Semantic .DeepEqual (existingCatsrc .Finalizers , reconciledCatsrc .Finalizers )
110107 unexpectedFieldsChanged := checkForUnexpectedFieldChange (existingCatsrc , * reconciledCatsrc )
111108
112109 if unexpectedFieldsChanged {
113110 panic ("spec or metadata changed by reconciler" )
114111 }
115112
116- // Save the finalizers off to the side. If we update the status, the reconciledCatsrc will be updated
117- // to contain the new state of the ClusterCatalog, which contains the status update, but (critically)
118- // does not contain the finalizers. After the status update, we need to re-add the finalizers to the
119- // reconciledCatsrc before updating the object.
120- finalizers := reconciledCatsrc .Finalizers
121-
122113 if updateStatus {
123114 if err := r .Client .Status ().Update (ctx , reconciledCatsrc ); err != nil {
124115 reconcileErr = errors .Join (reconcileErr , fmt .Errorf ("error updating status: %v" , err ))
125116 }
126117 }
127118
128- reconciledCatsrc .Finalizers = finalizers
129-
130- if updateFinalizers {
131- if err := r .Update (ctx , reconciledCatsrc ); err != nil {
132- reconcileErr = errors .Join (reconcileErr , fmt .Errorf ("error updating finalizers: %v" , err ))
133- }
134- }
135-
136119 return res , reconcileErr
137120}
138121
@@ -142,10 +125,6 @@ func (r *ClusterCatalogReconciler) SetupWithManager(mgr ctrl.Manager) error {
142125 defer r .storedCatalogsMu .Unlock ()
143126 r .storedCatalogs = make (map [string ]storedCatalogData )
144127
145- if err := r .setupFinalizers (); err != nil {
146- return fmt .Errorf ("failed to setup finalizers: %v" , err )
147- }
148-
149128 return ctrl .NewControllerManagedBy (mgr ).
150129 For (& ocv1.ClusterCatalog {}).
151130 Named ("catalogd-clustercatalog-controller" ).
@@ -171,32 +150,47 @@ func (r *ClusterCatalogReconciler) reconcile(ctx context.Context, catalog *ocv1.
171150 return ctrl.Result {}, err
172151 }
173152
174- // Set status.conditions[type=Progressing] to False as we are done with
175- // all that needs to be done with the catalog
176- updateStatusProgressingUserSpecifiedUnavailable (& catalog .Status , catalog .GetGeneration ())
177-
178153 // Remove the fbcDeletionFinalizer as we do not want a finalizer attached to the catalog
179154 // when it is disabled. Because the finalizer serves no purpose now.
180- controllerutil .RemoveFinalizer (catalog , fbcDeletionFinalizer )
155+ if err := finalizerutil .RemoveFinalizer (ctx , r .Client , catalog , fbcDeletionFinalizer ); err != nil {
156+ return ctrl.Result {}, fmt .Errorf ("error removing finalizer: %v" , err )
157+ }
158+
159+ // Set status.conditions[type=Progressing] to True as we are done with
160+ // all that needs to be done with the catalog
161+ updateStatusProgressingUserSpecifiedUnavailable (& catalog .Status , catalog .GetGeneration ())
162+ // Clear URLs, ResolvedSource, and LastUnpacked since catalog is unavailable
163+ catalog .Status .ResolvedSource = nil
164+ catalog .Status .URLs = nil
165+ catalog .Status .LastUnpacked = nil
181166
182167 return ctrl.Result {}, nil
183168 }
184169
185- finalizeResult , err := r .finalizers .Finalize (ctx , catalog )
186- if err != nil {
187- return ctrl.Result {}, err
188- }
189- if finalizeResult .Updated || finalizeResult .StatusUpdated {
190- // On create: make sure the finalizer is applied before we do anything
191- // On delete: make sure we do nothing after the finalizer is removed
170+ // Handle deletion
171+ if catalog .GetDeletionTimestamp () != nil {
172+ if ! controllerutil .ContainsFinalizer (catalog , fbcDeletionFinalizer ) {
173+ // All finalizers removed, nothing more to do
174+ return ctrl.Result {}, nil
175+ }
176+ if err := r .deleteCatalogCache (ctx , catalog ); err != nil {
177+ return ctrl.Result {}, fmt .Errorf ("finalizer %q failed: %w" , fbcDeletionFinalizer , err )
178+ }
179+ if err := finalizerutil .RemoveFinalizer (ctx , r .Client , catalog , fbcDeletionFinalizer ); err != nil {
180+ return ctrl.Result {}, fmt .Errorf ("error removing finalizer: %v" , err )
181+ }
182+ // Update status to reflect that catalog is no longer serving
183+ updateStatusNotServing (& catalog .Status , catalog .GetGeneration ())
192184 return ctrl.Result {}, nil
193185 }
194186
195- if catalog .GetDeletionTimestamp () != nil {
196- // If we've gotten here, that means the cluster catalog is being deleted, we've handled all of
197- // _our_ finalizers (above), but the cluster catalog is still present in the cluster, likely
198- // because there are _other_ finalizers that other controllers need to handle, (e.g. the orphan
199- // deletion finalizer).
187+ // Ensure finalizer is present
188+ finalizerAdded , err := finalizerutil .EnsureFinalizer (ctx , r .Client , catalog , fbcDeletionFinalizer )
189+ if err != nil {
190+ return ctrl.Result {}, fmt .Errorf ("error ensuring finalizer: %v" , err )
191+ }
192+ // On create: make sure the finalizer is applied before we do anything else
193+ if finalizerAdded {
200194 return ctrl.Result {}, nil
201195 }
202196
@@ -419,30 +413,16 @@ func (r *ClusterCatalogReconciler) needsPoll(lastSuccessfulPoll time.Time, catal
419413func checkForUnexpectedFieldChange (a , b ocv1.ClusterCatalog ) bool {
420414 a .Status , b .Status = ocv1.ClusterCatalogStatus {}, ocv1.ClusterCatalogStatus {}
421415 a .Finalizers , b .Finalizers = []string {}, []string {}
422- return ! equality .Semantic .DeepEqual (a , b )
423- }
424-
425- type finalizerFunc func (ctx context.Context , obj client.Object ) (crfinalizer.Result , error )
426-
427- func (f finalizerFunc ) Finalize (ctx context.Context , obj client.Object ) (crfinalizer.Result , error ) {
428- return f (ctx , obj )
429- }
430-
431- func (r * ClusterCatalogReconciler ) setupFinalizers () error {
432- f := crfinalizer .NewFinalizers ()
433- err := f .Register (fbcDeletionFinalizer , finalizerFunc (func (ctx context.Context , obj client.Object ) (crfinalizer.Result , error ) {
434- catalog , ok := obj .(* ocv1.ClusterCatalog )
435- if ! ok {
436- panic ("could not convert object to clusterCatalog" )
437- }
438- err := r .deleteCatalogCache (ctx , catalog )
439- return crfinalizer.Result {StatusUpdated : true }, err
440- }))
441- if err != nil {
442- return err
416+ a .ManagedFields , b .ManagedFields = nil , nil
417+ a .ResourceVersion , b .ResourceVersion = "" , ""
418+ // Remove kubectl's last-applied-configuration annotation which may be added by the API server
419+ if a .Annotations != nil {
420+ delete (a .Annotations , "kubectl.kubernetes.io/last-applied-configuration" )
443421 }
444- r .finalizers = f
445- return nil
422+ if b .Annotations != nil {
423+ delete (b .Annotations , "kubectl.kubernetes.io/last-applied-configuration" )
424+ }
425+ return ! equality .Semantic .DeepEqual (a , b )
446426}
447427
448428func (r * ClusterCatalogReconciler ) deleteStoredCatalog (catalogName string ) {
0 commit comments