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

Refactor CAPV controller context #2295

Closed
18 tasks done
zhanggbj opened this issue Aug 29, 2023 · 28 comments
Closed
18 tasks done

Refactor CAPV controller context #2295

zhanggbj opened this issue Aug 29, 2023 · 28 comments
Assignees
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@zhanggbj
Copy link
Contributor

zhanggbj commented Aug 29, 2023

/kind feature

Describe the solution you'd like
[A clear and concise description of what you want to happen.]
Currently CAPV has a customized Controller Context which wraps context.Context along with other essential controller-related information as here. This context is propagated across controllers and reconcilers. However, this approach comes with drawbacks:

  • Not align with the recommended practices outlined by the containedctx lint, which emphasizes that 'Contexts should not be stored inside a struct type, but instead passed to each function that needs it'.
  • Hard to understand the function and may need further investigation for better comprehension about what it needs, for example func (r clusterReconciler) reconcileNormal(ctx *context.ClusterContext)
  • contains mixed usage of CAPV controller context and golang context, sometimes capv will initialize new golang context to call some API. It is difficult to enable contextual logging and tracing.

Given the sensitivity of context usage, particularly its impact across controllers, propose a refactoring of the controller context, aiming to split it into golang context and the CAPV controller context. This will be achieved incrementally through the following steps:

Anything else you would like to add:
[Miscellaneous information that will assist in solving the issue.]

Environment:

  • Cluster-api-provider-vsphere version:
  • Kubernetes version: (use kubectl version):
  • OS (e.g. from /etc/os-release):
@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label Aug 29, 2023
@sbueringer
Copy link
Member

Sounds perfect. Thank you!

@sbueringer
Copy link
Member

Introduce new ctx context.Context parameters to funcs, also fix Unit Test. We can break this down by controllers as outlined below, but let's begin and be open to adjustments as needed.

Just stating the obvious probably. The idea would be basically to use the ctx that controller-runtime hands us over in the Reconcile func and then pass it down to wherever we need it. This would be:

  • places where today we use the ctx from our CAPV contexts
  • places where we use context.TODO/context.Background (in a lot of cases this is when we make a Get/List/... call via the controller-runtime client)

@zhanggbj
Copy link
Contributor Author

zhanggbj commented Sep 8, 2023

/assign
/help wanted

@zhanggbj
Copy link
Contributor Author

zhanggbj commented Sep 8, 2023

The first PR to pass down context to controller/reconciler are in review #2339.

Please feel free to pick up other tasks listed above once it is merged. We can assign this issue to multiple assignees :) Thanks!

@sbueringer
Copy link
Member

sbueringer commented Sep 13, 2023

Let's hold this for a bit (hopefully only a few days). I started to unblock the logging work and had to add more context handling across half the repo. Let's get this one in first to avoid duplicate work: #2352

@sbueringer sbueringer mentioned this issue Sep 18, 2023
12 tasks
@sbueringer
Copy link
Member

sbueringer commented Sep 18, 2023

Updated the tasks above with the controllers already refactored via #2352

@chrischdi
Copy link
Member

Added task:

  • check that there are no leftover context.TODO()'s (indicates we missed something to refactor above)

@sbueringer
Copy link
Member

#2352 merged

@zhanggbj I think we can continue here

cc @Madhur97 @Ankitasw If you have time to pick up some of the tasks here (let us know if you need more context)

@zhanggbj
Copy link
Contributor Author

Also CC @MrDXY @xiujuanx feel free to pick up tasks, this is fundamental work for tracing and logging :-)

@MrDXY
Copy link
Contributor

MrDXY commented Sep 19, 2023

I'll take this one! 😄

  • controllers/vsphereclusteridentity_controller.go

@Ankitasw
Copy link
Member

I can pick controllers/vspheremachine_controller.go

@zhanggbj
Copy link
Contributor Author

zhanggbj commented Sep 19, 2023

/assign @Ankitasw

@zhanggbj
Copy link
Contributor Author

zhanggbj commented Sep 19, 2023

I'll continue with this one.

  • controllers/vspheredeploymentzone_controller.go

@xiujuanx
Copy link
Contributor

I would like to help on this one:

  • controllers/vspherevm_controller.go

@sbueringer
Copy link
Member

sbueringer commented Sep 19, 2023

Thx folks! I've updated the list above accordingly

@Madhur97
Copy link
Contributor

I can pick controllers/vspherevm_ipaddress_reconciler.go

@zhanggbj
Copy link
Contributor Author

/assign @xiujuanx

@zhanggbj
Copy link
Contributor Author

/assign @Madhur97

@zhanggbj
Copy link
Contributor Author

/assign @MrDXY

@zhanggbj
Copy link
Contributor Author

About the logger which may relate to context refactoring for each controller, I think we could leave them for separate PRs to keep our PR easy to review, and logging refactoring are tracking by #2076
CC @MrDXY @Ankitasw @xiujuanx @Madhur97

@sbueringer
Copy link
Member

Absolutely. Let's not start using contextual logging in these PRs.

@chrischdi
Copy link
Member

Re-assigning controllers/vspherevm_ipaddress_reconciler.go to @xiujuanx because its included in #2398 .

@adityabhatia
Copy link
Contributor

I believe first 3 entries: controllers/clustermodule_reconciler.go, controllers/serviceaccount_controller.go, controllers/servicediscovery_controller.go were already fixed in 09ff688

@zhanggbj
Copy link
Contributor Author

@adityabhatia yes, just linked them in case of any confusion.

@chrischdi
Copy link
Member

#2398 should be merging now too.

Afterwards we can do the final steps and verify that we didn't miss a case :-)

@chrischdi
Copy link
Member

Thanks for taking the next tasks @zhanggbj !

@zhanggbj
Copy link
Contributor Author

As we're holding #2462 and trying to reproduce the flaky error, it may take a long time, so move it out of this list to #2506 for tracking.
I'll close this, thanks a lot for all the contributors and also reviewers! :-)

@sbueringer
Copy link
Member

Sounds good, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

No branches or pull requests

9 participants