-
Notifications
You must be signed in to change notification settings - Fork 10
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
chore: remove duplicated code and fix minor bugs #326
Conversation
8538d09
to
878085e
Compare
878085e
to
e249792
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left few comments
cleanupF := func(ctx context.Context) error { | ||
return r.deleteExternalResources(ctx, components, logger) | ||
} | ||
finish, err := checkAndHandleResourceDeletion(ctx, r.Client, &server, operatorFinalizer, cleanupF, logger) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personally prefer to leave the refactored code and structure it slightly differently. The checkandHandleResourceDeletion
is misleading, since it also adds the finalizer if one doesn't exist. This helper function is doing too much for something that is easy to read. As a suggestion, I'd prefer the following:
if !server.ObjectMeta.DeletionTimestamp.IsZero() {
// The object is being deleted
if !controllerutil.ContainsFinalizer(&as, operatorFinalizer) {
return ctrl.Result{}, nil
}
logger.Info("Running cleanup function for ArmadaServer cluster-scoped components", "finalizer", operatorFinalizer)
if err := r.deleteExternalResources(ctx, components, logger); err != nil {
// if fail to delete the external dependency here, return with error
// so that it can be retried
return ctrl.Result{}, err
}
// remove our finalizer from the list and update it.
logger.Info("Removing finalizer from ArmadaServer object", "finalizer", operatorFinalizer)
controllerutil.RemoveFinalizer(&as, operatorFinalizer)
err = r.Update(ctx, &as)
return ctrl.Result{}, err
}
if !controllerutil.ContainsFinalizer(&as, operatorFinalizer) {
logger.Info("Attaching finalizer to As object", "finalizer", operatorFinalizer)
controllerutil.AddFinalizer(&as, operatorFinalizer)
if err := r.Update(ctx, &as); err != nil {
return ctrl.Result{}, err
}
}
To me, this is much easier to read, you can easily follow the state machine and what is being applied, and the added benefit is when you decide to add another finalizer, the generic cleanup function would have to be refactored to cover that case, which would be more complicated than reading this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is a snippet which is duplicated in 8 contollers with around 99% similarity.
I'd rather have it in a single function which is well-tested and reused in all controllers.
I refactored it a bit so the function seems smaller and a bit cleaner, maybe checkAndHandleResourceDeletion
can be renamed a bit better but I don't have a better idea currently.
e249792
to
bf81d58
Compare
Signed-off-by: Dejan Zele Pejchev <pejcev.dejan@gmail.com>
bf81d58
to
14bdf50
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice refactor - ship it!
Pull Request Template
Description
Please include a summary of the changes and the related issue (if any). Describe your changes in detail to help reviewers understand your contribution.
Fixes # (issue)
Type of change
Please select the type of change your PR introduces:
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions to reproduce these tests. List any relevant details for your test configuration.
Test Configuration:
Test Steps:
Checklist: