@@ -90,7 +90,12 @@ func (r *genericNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Reque
9090 // Request object not found, could have been deleted after reconcile request.
9191 // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
9292 // Return and don't requeue
93- return reconcile.Result {}, nil
93+ return reconcile.Result {}, r .garbageCollect (ctx )
94+ }
95+
96+ err = r .cleanOldFinalizers (ctx , instance )
97+ if err != nil {
98+ return reconcile.Result {}, err
9499 }
95100
96101 if instance .NetworkNamespace () != "" && instance .GetNamespace () != vars .Namespace {
@@ -104,20 +109,6 @@ func (r *genericNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Reque
104109 return reconcile.Result {}, nil
105110 }
106111
107- // examine DeletionTimestamp to determine if object is under deletion
108- if instance .GetDeletionTimestamp ().IsZero () {
109- // The object is not being deleted, so if it does not have our finalizer,
110- // then lets add the finalizer and update the object. This is equivalent
111- // registering our finalizer.
112- err = r .updateFinalizers (ctx , instance )
113- if err != nil {
114- return reconcile.Result {}, err
115- }
116- } else {
117- // The object is being deleted
118- err = r .cleanResourcesAndFinalizers (ctx , instance )
119- return reconcile.Result {}, err
120- }
121112 raw , err := instance .RenderNetAttDef ()
122113 if err != nil {
123114 return reconcile.Result {}, err
@@ -250,24 +241,6 @@ func (r *genericNetworkReconciler) namespaceHandlerCreate(ctx context.Context, e
250241 })
251242}
252243
253- // deleteNetAttDef deletes the generated net-att-def CR
254- func (r * genericNetworkReconciler ) deleteNetAttDef (ctx context.Context , cr NetworkCRInstance ) error {
255- // Fetch the NetworkAttachmentDefinition instance
256- namespace := cr .NetworkNamespace ()
257- if namespace == "" {
258- namespace = cr .GetNamespace ()
259- }
260- instance := & netattdefv1.NetworkAttachmentDefinition {ObjectMeta : metav1.ObjectMeta {Name : cr .GetName (), Namespace : namespace }}
261- err := r .Delete (ctx , instance )
262- if err != nil {
263- if errors .IsNotFound (err ) {
264- return nil
265- }
266- return err
267- }
268- return nil
269- }
270-
271244// fetchObject tries to fectch the request object in its own namespace. If it is not found, then tries
272245// to fetch from the operator's namespace
273246func (r * genericNetworkReconciler ) fetchObject (ctx context.Context , req ctrl.Request ) (NetworkCRInstance , error ) {
@@ -296,43 +269,66 @@ func (r *genericNetworkReconciler) fetchObject(ctx context.Context, req ctrl.Req
296269 return nil , nil
297270}
298271
299- func (r * genericNetworkReconciler ) updateFinalizers (ctx context.Context , instance NetworkCRInstance ) error {
300- if instance .GetNamespace () != vars .Namespace {
301- // If the resource is in a namespace different than the operator one, then the NetworkAttachmentDefinition will
302- // be created in the same namespace and its deletion can be handled by OwnerReferences. There is no need for finalizers
303- return nil
272+ // garbageCollect searches all NetworkAttachmentDefinition objects that has a `sriovnetwork.openshift.io/owner`
273+ // annotation pointing to non existing objects
274+ func (r * genericNetworkReconciler ) garbageCollect (ctx context.Context ) error {
275+ logger := log .Log .WithName ("garbageCollect" )
276+
277+ netAttachDefs := & netattdefv1.NetworkAttachmentDefinitionList {}
278+ err := r .Client .List (ctx , netAttachDefs )
279+ if err != nil {
280+ return fmt .Errorf ("garbageCollect: failed to list NetworkAttachmentDefinition: %w" , err )
304281 }
305282
306- instanceFinalizers := instance .GetFinalizers ()
307- if ! sriovnetworkv1 .StringInArray (sriovnetworkv1 .NETATTDEFFINALIZERNAME , instanceFinalizers ) {
308- instance .SetFinalizers (append (instanceFinalizers , sriovnetworkv1 .NETATTDEFFINALIZERNAME ))
309- if err := r .Update (ctx , instance ); err != nil {
310- return err
283+ for _ , netAttDef := range netAttachDefs .Items {
284+ owner , ok := netAttDef .GetAnnotations ()[consts .OwnerAnnotation ]
285+ if ! ok {
286+ continue
287+ }
288+
289+ obj , namespace , name , err := sriovnetworkv1 .StringToOwnerRef (owner )
290+ if err != nil {
291+ logger .Error (err , "bad value for `sriovnetwork.openshift.io/owner`" , "owner" , owner )
292+ continue
293+ }
294+
295+ err = r .Get (ctx , types.NamespacedName {Name : name , Namespace : namespace }, obj )
296+ if err != nil {
297+ if ! errors .IsNotFound (err ) {
298+ logger .Error (err , "failed to get owner object" , "owner" , owner )
299+ continue
300+ }
301+
302+ logger .Info ("owner object not found. deleting NetworkAttachmentDefinition" , "obj" , netAttDef )
303+
304+ err := r .Delete (ctx , & netAttDef )
305+ if err != nil {
306+ logger .Error (err , "can't delete NetworkAttachmentDefinition" , "obj" , netAttDef )
307+ }
311308 }
312309 }
313310
314311 return nil
315312}
316313
317- func (r * genericNetworkReconciler ) cleanResourcesAndFinalizers (ctx context.Context , instance NetworkCRInstance ) error {
314+ func (r * genericNetworkReconciler ) cleanOldFinalizers (ctx context.Context , instance NetworkCRInstance ) error {
318315 instanceFinalizers := instance .GetFinalizers ()
319316
320- if sriovnetworkv1 .StringInArray (sriovnetworkv1 .NETATTDEFFINALIZERNAME , instanceFinalizers ) {
321- // our finalizer is present, so lets handle any external dependency
322- log .FromContext (ctx ).Info ("delete NetworkAttachmentDefinition CR" , "Namespace" , instance .NetworkNamespace (), "Name" , instance .GetName ())
323- if err := r .deleteNetAttDef (ctx , instance ); err != nil {
324- // if fail to delete the external dependency here, return with error
325- // so that it can be retried
317+ if ! sriovnetworkv1 .StringInArray (sriovnetworkv1 .NETATTDEFFINALIZERNAME , instanceFinalizers ) {
318+ return nil
319+ }
320+
321+ // remove our finalizer from the list and update it.
322+ newFinalizers , found := sriovnetworkv1 .RemoveString (sriovnetworkv1 .NETATTDEFFINALIZERNAME , instanceFinalizers )
323+ if found {
324+ logger := log .Log .WithName ("cleanFinalizers" )
325+
326+ instance .SetFinalizers (newFinalizers )
327+ logger .Info ("Updating network instance to remove finalizer `netattdef.finalizers.sriovnetwork.openshift.io`" , "obj" , instance )
328+ if err := r .Update (ctx , instance ); err != nil {
326329 return err
327330 }
328- // remove our finalizer from the list and update it.
329- newFinalizers , found := sriovnetworkv1 .RemoveString (sriovnetworkv1 .NETATTDEFFINALIZERNAME , instanceFinalizers )
330- if found {
331- instance .SetFinalizers (newFinalizers )
332- if err := r .Update (ctx , instance ); err != nil {
333- return err
334- }
335- }
336331 }
332+
337333 return nil
338334}
0 commit comments