Skip to content

controller code/test refactoring/additions - #107

Closed
joelanford wants to merge 2 commits into
operator-framework:mainfrom
joelanford:refactor-plus-unit-tests
Closed

controller code/test refactoring/additions#107
joelanford wants to merge 2 commits into
operator-framework:mainfrom
joelanford:refactor-plus-unit-tests

Conversation

@joelanford

@joelanfordjoelanford commented Jan 28, 2023

Copy link
Copy Markdown
Member

I noticed y'all made some great progress this week, and got the itch to stay in the loop and pitch in. This PR is a whole bunch of changes all wrapped up together, so I have no illusions about it being merged as is. I'm curious what folks think!

Main bullet points are:

  • remove unnecessary RBAC rules for create/update/patch/delete Operator
    objects. this leaves only get, list, watch, update status, and update
    finalizers. (reduce RBAC permissions of operator-controller #116)
  • use server-side apply to manage bundle deployments (Use server-side apply to ensure the desired bundle deployments exist #114)
  • unit test refactoring (refactor and add unit tests for operator reconciler #115)
    • move reconciler unit tests to a separate file from the suite. if we add other controllers in the future, we'll likely want separate test files for each controller.
    • export the reconciler's resolver field to make it easy to swap out for tests (also add a test entity source)
    • remove the manager setup in the unit tests. this ensures we can test individual runs of the Reconcile function, which is good for two reasons:
      1. no need to use Eventually, no polling, etc. so it speeds up the tests
      2. it ensures that we fully understand the individual transitions that occur from a beginning state to an end state when multiple Reconcile calls are necessary. (e.g. r1: create BD, r2: update Op based on BD status change to unpacking, r3: update Op again based on BD status change to installed successfully)
    • added more unit tests to increase code coverage.
    • moved conditions and reasons checks to invariants that run after every test of reconciling an existing Operator object.
  • decrease nesting in main reconcile code by setting conditions and returning early when handling unexpected errors. (Reduce reconcile complexity #118)
  • make certain bundle entity properties optional. not all bundles will provide/require GVKs or require other packages. lookups for those entities should return nil slices rather than an error.

EDIT: I split each of these main things out into separate PRs now that it seems like folks are generally onboard.

@openshift-ciopenshift-ciBot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 28, 2023
@joelanford
joelanfordforce-pushed the refactor-plus-unit-tests branch from 683b79e to 3dfea44CompareFebruary 1, 2023 17:26
@joelanford
joelanford marked this pull request as ready for review February 1, 2023 17:32
@openshift-ciopenshift-ciBot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 1, 2023
Comment threadconfig/rbac/role.yaml
@joelanford
joelanfordforce-pushed the refactor-plus-unit-tests branch from 3dfea44 to 5d27194CompareFebruary 1, 2023 19:31
Comment threadcontrollers/operator_controller.go
main bullet points are:
- decrease nesting in main reconcile code by setting conditions and
returning early when handling unexpected errors.
- move reconciler unit tests to a separate file from the suite. if we
add other controllers in the future, we'll likely want separate test
files for each controller.
- export the reconciler's resolver field to make it easy to swap out for
tests (also add and use a test entity source)
- remove the manager setup in the unit tests. this ensures we can test
individual runs of the Reconcile function, which is good for two
reasons:
1. no need to use `Eventually`, no polling, etc. so it speeds up the tests
2. it ensures that we fully understand the transitions that occur from
a beginning state to and end state.
- added more unit tests to increase code coverage.
- moved conditions and reasons checks to invariants that run after every
test of reconciling an existing Operator object.
- make certain bundle entity properties optional. not all bundles will
provide/require GVKs or require other packages. lookups for those
entities should return nil slices rather than an error.
- remove unnecessary RBAC rules for create/update/patch/delete Operator
objects. this leaves only get, list, watch, update status, and update
finalizers.
@joelanford
joelanfordforce-pushed the refactor-plus-unit-tests branch from 5d27194 to bc627b6CompareFebruary 1, 2023 20:01
Comment threadcontrollers/operator_controller.go Outdated
Comment threadcontrollers/operator_controller.go
1. factor bundle lookup into separate function, which makes for less
complex main reconcile function
2. use server-side apply to ensure bundle deployment is up-to-date
3. add more unit test scenarios
@joelanford

Copy link
Copy Markdown
MemberAuthor

I added another commit, which I think improves the controller code a bit more. It also adds more unit test scenarios. I'm fairly happy with those, but they do introduce some duplication that I think could be reduced a bit, so I think its worth a follow up to focus on that.

Comment threadmain.go
if err = (&controllers.OperatorReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Resolver: resolution.NewOperatorResolver(mgr.GetClient(), resolution.HardcodedEntitySource),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

much better!

@openshift-merge-robot

Copy link
Copy Markdown

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

defer b.mu.Unlock()
if b.requiredGVKs == nil {
requiredGVKs, err := loadFromEntity[[]GVKRequired](b.Entity, property.TypeGVKRequired)
requiredGVKs, err := loadFromEntity[[]GVKRequired](b.Entity, property.TypeGVKRequired, false)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as we discussed online, I think we should only push these booleans to constants to make it a little more readable and that's basically the cherry on top ^^

might be worth splitting the suite_test changes to a different PR. But not strictly necesssary...

@joelanford
joelanford deleted the refactor-plus-unit-tests branch February 10, 2023 03:25
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-rebaseIndicates a PR cannot be merged because it has merge conflicts with HEAD.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@joelanford@openshift-merge-robot@perdasilva@dtfranz@tmshort