@@ -23,6 +23,7 @@ import (
2323 "path/filepath"
2424 "sort"
2525 "strings"
26+ "time"
2627
2728 "k8s.io/apimachinery/pkg/runtime"
2829 "k8s.io/client-go/kubernetes"
@@ -250,7 +251,7 @@ func (r *OpenStackReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
250251 // now that CRDs have been updated (with old olm.managed references removed)
251252 // we can finally cleanup the old operators
252253 if err := r .postCleanupObsoleteResources (ctx , instance ); err != nil {
253- return ctrl.Result {}, err
254+ return ctrl.Result {RequeueAfter : time . Duration ( 5 ) * time . Second }, err
254255 }
255256
256257 // Check if all deployments are running
@@ -479,6 +480,10 @@ func (r *OpenStackReconciler) cleanupObsoleteResources(ctx context.Context, inst
479480 if isServiceOperatorResource (csv .GetName ()) {
480481 err = r .Client .Delete (ctx , & csv )
481482 if err != nil {
483+ if apierrors .IsNotFound (err ) {
484+ Log .Info ("CSV not found on delete. Continuing..." , "name" , csv .GetName ())
485+ continue
486+ }
482487 return err
483488 }
484489 Log .Info ("CSV deleted successfully" , "name" , csv .GetName ())
@@ -496,6 +501,10 @@ func (r *OpenStackReconciler) cleanupObsoleteResources(ctx context.Context, inst
496501 if isServiceOperatorResource (subscription .GetName ()) {
497502 err = r .Client .Delete (ctx , & subscription )
498503 if err != nil {
504+ if apierrors .IsNotFound (err ) {
505+ Log .Info ("Subscription not found on delete. Continuing..." , "name" , subscription .GetName ())
506+ continue
507+ }
499508 return err
500509 }
501510 Log .Info ("Subscription deleted successfully" , "name" , subscription .GetName ())
@@ -523,6 +532,10 @@ func (r *OpenStackReconciler) cleanupObsoleteResources(ctx context.Context, inst
523532 if isServiceOperatorResource (csvNames [0 ].(string )) {
524533 err = r .Client .Delete (ctx , & installPlan )
525534 if err != nil {
535+ if apierrors .IsNotFound (err ) {
536+ Log .Info ("Installplane not found on delete. Continuing..." , "name" , installPlan .GetName ())
537+ continue
538+ }
526539 return err
527540 }
528541 Log .Info ("Installplan deleted successfully" , "name" , installPlan .GetName ())
@@ -558,6 +571,46 @@ func (r *OpenStackReconciler) postCleanupObsoleteResources(ctx context.Context,
558571 for _ , operator := range operatorList .Items {
559572 Log .Info ("Found Operator" , "name" , operator .GetName ())
560573 if isServiceOperatorResource (operator .GetName ()) {
574+
575+ refs , found , err := uns .NestedSlice (operator .Object , "status" , "components" , "refs" )
576+ if err != nil {
577+ return err
578+ }
579+ if found {
580+
581+ // The horizon-operator.openstack-operators has references to old roles/bindings
582+ // the code below will delete those references before continuing
583+ for _ , ref := range refs {
584+ refData := ref .(map [string ]interface {})
585+ Log .Info ("Deleting operator reference" , "Reference" , ref )
586+ obj := uns.Unstructured {}
587+ obj .SetName (refData ["name" ].(string ))
588+ obj .SetNamespace (refData ["namespace" ].(string ))
589+ apiParts := strings .Split (refData ["apiVersion" ].(string ), "/" )
590+ objGvk := schema.GroupVersionResource {
591+ Group : apiParts [0 ],
592+ Version : apiParts [1 ],
593+ Resource : refData ["kind" ].(string ),
594+ }
595+ obj .SetGroupVersionKind (objGvk .GroupVersion ().WithKind (refData ["kind" ].(string )))
596+
597+ // references from CRD's should be removed before this function is called
598+ // but this is a safeguard as we do not want to delete them
599+ if refData ["kind" ].(string ) != "CustomResourceDefinition" {
600+ err = r .Client .Delete (ctx , & obj )
601+ if err != nil {
602+ if apierrors .IsNotFound (err ) {
603+ Log .Info ("Object not found on delete. Continuing..." , "name" , obj .GetName ())
604+ continue
605+ }
606+ return err
607+ }
608+ }
609+ }
610+
611+ return fmt .Errorf ("Requeuing/Found references for operator name: %s, refs: %v" , operator .GetName (), refs )
612+ }
613+ // no refs found so we should be able to successfully delete the operator
561614 err = r .Client .Delete (ctx , & operator )
562615 if err != nil {
563616 return err
0 commit comments