Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MGDAPI-3467 tenant deletion support #715

Merged
merged 10 commits into from
Feb 24, 2022
Merged

Conversation

MStokluska
Copy link
Contributor

@MStokluska MStokluska commented Feb 11, 2022

Verification:

For RHOAM:

  • install RHOAM locally from this branch: WIP MGDAPI-3177 wip pr for making 3scale cluster scoped integr8ly/integreatly-operator#2409 but replace the manifests/3scale/0.9.0 values for containerImage and image from quay.io/austincunningham/3scale-operator:cluster-scoped to quay.io/mstoklus/3scale-operator:v0.12.0
  • wait for install to complete
  • create tenant via CR
  • confirm tenant got created in 3scale
  • delete tenant CR
  • confirm tenant has been marked for deletion in 3scale

@MStokluska MStokluska force-pushed the MGDAPI-3467-2 branch 3 times, most recently from ecf96b7 to 61e0c1b Compare February 14, 2022 11:53
@MStokluska MStokluska changed the title [WIP] MGDAPI-3467 tenant deletion support MGDAPI-3467 tenant deletion support Feb 14, 2022
@MStokluska MStokluska force-pushed the MGDAPI-3467-2 branch 2 times, most recently from 12d978c to 47764fc Compare February 14, 2022 12:06
@maksymvavilov
Copy link

Installed RHOAM using the quay.io/mstoklus/3scale-operator:v0.9.0 3scale image:
Screenshot from 2022-02-15 11-33-57
Created a tenant CR:
Screenshot from 2022-02-15 11-34-46
And watched a tenant being created in the 3scale:
Screenshot from 2022-02-15 11-43-43
After deleting a tenant CR the tenant was scheduled for a deletion:
Screenshot from 2022-02-15 11-45-16

@maksymvavilov
Copy link

Other failed checks are related to the k8s.io/utils licence approval 🤔 Anyway, the code looks good and performs well, hence

/lgtm

@MStokluska MStokluska mentioned this pull request Feb 16, 2022
Copy link
Member

@eguzki eguzki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the tenant deletion support should be done at the tenant_controller.go on the entrypoint of reconciler.

The finalizer pattern used across other controllers we are working on is:

func (r *TenantReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {                                  
    _ = context.Background()                                                                                   
    reqLogger := r.Log.WithValues("tenant", req.NamespacedName)                                                
                                                                                                               
    // Fetch the Tenant instance                                                                               
    tenantR := &capabilitiesv1alpha1.Tenant{}                                                                  
    err := r.Client.Get(context.TODO(), req.NamespacedName, tenantR)                                           
    if err != nil {                                                                                            
        if errors.IsNotFound(err) {                                                                            
            // Request object not found, could have been deleted after reconcile request.                      
            // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. 
            // Return and don't requeue                                                                        
            reqLogger.Info("Tenant resource not found")                                                        
            return ctrl.Result{}, nil                                                                          
        }                                                                                                      
        // Error reading the object - requeue the request.                                                     
        return ctrl.Result{}, err                                                                              
    }       
    // Tenant has been marked for deletion                                                      
    if tenantR.GetDeletionTimestamp() != nil && controllerutil.ContainsFinalizer(tenantR, tenantFinalizer) {
         // your deletion handling
         .....
         
         // 
         //Remove finalizer and update the object.             
         controllerutil.RemoveFinalizer(tenantR, tenantFinalizer)   
         err = r.Client.Update(ctx, tenantR)                     
         logger.Info("Removing finalizer", "error", err)       
         return ctrl.Result{Requeue: true}, err                
     }

       // Ignore deleted resources, this can happen when foregroundDeletion is enabled                                     
 // https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#foreground-cascading-deletion      
        if tenantR.GetDeletionTimestamp() != nil {                                                                             
              return ctrl.Result{}, nil                                                                                       
         } 
 
          if !controllerutil.ContainsFinalizer(tenantR, tenantFinalizer) {                    
                   controllerutil.AddFinalizer(tenantR, tenantFinalizer)                           
                    err := r.Client.Update(ctx, tenantR)                                         
                    logger.Info("Adding finalizer", "error", err)                              
                    return ctrl.Result{Requeue: true}, err                                     
            }                                                                              
            ...
                                                                                                                           

controllers/capabilities/tenant_internal_reconciler.go Outdated Show resolved Hide resolved
pkg/controller/helper/controllerUtil.go Outdated Show resolved Hide resolved
@MStokluska MStokluska force-pushed the MGDAPI-3467-2 branch 3 times, most recently from 79b7a20 to 71bc0bb Compare February 16, 2022 14:56
@MStokluska MStokluska force-pushed the MGDAPI-3467-2 branch 2 times, most recently from 8fb7842 to 7147ee4 Compare February 16, 2022 15:54
@MStokluska
Copy link
Contributor Author

@eguzki hey, sorry for all the updates, I was trying to make the codeClimate a bit happier!

controllers/capabilities/tenant_controller.go Show resolved Hide resolved
controllers/capabilities/tenant_controller.go Show resolved Hide resolved
controllers/capabilities/tenant_controller.go Show resolved Hide resolved
controllers/capabilities/tenant_controller.go Outdated Show resolved Hide resolved
controllers/capabilities/tenant_internal_reconciler.go Outdated Show resolved Hide resolved
pkg/controller/helper/finalizers.go Outdated Show resolved Hide resolved
pkg/controller/helper/finalizers.go Outdated Show resolved Hide resolved
controllers/capabilities/tenant_controller.go Outdated Show resolved Hide resolved
@MStokluska
Copy link
Contributor Author

@eguzki I've addressed all your comments and explained the reasoning behind having it done in a certain way in the first place, I would appreciate a review again.
The only thing that I question is around the requeue if by any chance the 3scale api call fails on removing the tenant. But it might be easier to go through the scenarios on call.
Thanks for the reviews :)

Copy link
Member

@eguzki eguzki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good

I left some comments

controllers/capabilities/tenant_controller.go Show resolved Hide resolved
- tenant
- portaClient
*/
func DeleteTenant(tenant *capabilitiesv1alpha1.Tenant, portaClient *porta_client_pkg.ThreeScaleClient) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: this method does not add anything, does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially it could be moved, although I was thinking it would be nice to have it in a single file, I was thinking of possibility moving the tenant create here too, but happy to remove it directly if that's what you preffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

controllers/capabilities/tenant_controller.go Outdated Show resolved Hide resolved
@eguzki
Copy link
Member

eguzki commented Feb 23, 2022

Looks good

I think we should tackle the issue of cascade deletion of 3scale tenant objects (DeveloperUser, DeveloperAccount, Products, Backends, ActiveDocs) in another PR.

I am aware that the code coverage in the capabilites code is near to zero, but it is prepared to have tests (https://github.com/3scale/3scale-operator/blob/62f241c29eb4136e055269cf31ab7062c16e6bfa/controllers/capabilities/suite_test.go). If you are willing to add some tests, feel free.

Regarding upstream doc, I think it would be nice to have a note in the capabilities doc about the new feature. Also will be used by tech writers team for the official doc.

@codeclimate
Copy link

codeclimate bot commented Feb 24, 2022

Code Climate has analyzed commit fa0538e and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 2

View more on Code Climate.

@eguzki eguzki merged commit ee3e331 into 3scale:master Feb 24, 2022
@eguzki
Copy link
Member

eguzki commented Feb 24, 2022

good job 🎖️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants