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

Encapsulate KCM controllers with their metadata #120371

Merged
merged 5 commits into from
Oct 27, 2023

Conversation

atiratree
Copy link
Member

@atiratree atiratree commented Sep 1, 2023

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

this is a followup to following PR and a discussion: #115813 (comment)

  • These metadata can be used to handle controllers in a generic way.
  • This enables showing feature gated controllers in kube-controller-manager's help.
  • It is possible to obtain a controllerName in the InitFunc so it can be passed down to and used by the controller.
  • include ServiceAccountTokenController in the NewRegistrableControllers to make it more generic
  • move start controller pre- and post- checks/actions out of StartControllers into StartController function
metadata about a controller:
- name
- requiredFeatureGates
- aliases
- isDisabledByDefault
- isCloudProviderController
- requiresSpecialHandling

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

similar thing can be done for cloud-controller-manager, but it is better to open a new separate PR for that

Does this PR introduce a user-facing change?

kube-controller-manager's help will include controllers behind a feature gate in `--controllers` flag

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Sep 1, 2023
@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Sep 1, 2023
cmd/kube-controller-manager/app/controllermanager.go Outdated Show resolved Hide resolved
cmd/kube-controller-manager/app/controllermanager.go Outdated Show resolved Hide resolved
type serviceAccountTokenControllerStarter struct {
rootClientBuilder clientbuilder.ControllerClientBuilder
// RegistrableController map so that it can always run first.
func newServiceAccountTokenRegistrableController(rootClientBuilder clientbuilder.ControllerClientBuilder) *registrableController {
Copy link
Member Author

Choose a reason for hiding this comment

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

can we move this controller to a new file?

@atiratree
Copy link
Member Author

Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

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

This was just a skim, but I personally like the direction. I'd emphasize the encapsulation by segregating this code behind a clean API (even if in the same pkg, but a different file) and making the code that calls it as simple and streamlined as possible.

cmd/kube-controller-manager/app/controllermanager.go Outdated Show resolved Hide resolved
cmd/kube-controller-manager/app/controllermanager.go Outdated Show resolved Hide resolved
cmd/kube-controller-manager/app/controllermanager.go Outdated Show resolved Hide resolved
cmd/kube-controller-manager/app/controllermanager.go Outdated Show resolved Hide resolved
@alexzielenski
Copy link
Contributor

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Sep 5, 2023
@atiratree
Copy link
Member Author

as a result of the discussions, I have made the following changes:

  • renamed RegistrableController to ControllerDescriptor
  • removed ControllerDescriptor interface in favor of a struct
  • moved start controller pre- and post- checks/actions out of StartControllers into StartController function

return &ControllerDescriptor{
name: names.ValidatingAdmissionPolicyStatusController,
initFunc: startValidatingAdmissionPolicyStatusController,
requiredFeatureGates: []featuregate.Feature{
Copy link
Member

Choose a reason for hiding this comment

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

I don´t have the feeling this is a good idea, because right now it looks pretty neat and clean, but as soon as the code evolves and new features are added this is just yet another place to update the feature gates ... or what happen if the controller has only one part of the logic with a feature gate? we need to run the controller and we still need to add a feature gate inside the controller logic

some food for thoughts

Copy link
Member Author

Choose a reason for hiding this comment

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

It depends on what tradeoff we want to make. If we want to have all the controller metadata and logic sprinkled everywhere or together. We already have feature gate logic sprinkled everywhere, so IMO this would not change that much. That is, one has to search for all occurrences of a feature gate to get the full picture.

or what happen if the controller has only one part of the logic with a feature gate? we need to run the controller and we still need to add a feature gate inside the controller logic

I think we would solve this the same way we do today. We can put a feature gate call inside the init function or inside the controller itself.

For the usual cases, we could reason about controllers without running them. That is, we could for example show which controllers are gated by which feature gates in kube-controller-manager's help

Copy link
Member

@aojea aojea Sep 19, 2023

Choose a reason for hiding this comment

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

the logic is always going to be sprinkled everywhere, the feature gated code can be anything, a function or just part of a function ...

For the usual cases, we could reason about controllers without running them. That is, we could for example show which controllers are gated by which feature gates in kube-controller-manager's help

I think that the intent is good, but I'm afraid that the reality is different and it soon will get out of sync and we'll have an additional place interacting with the code that is not obvious for everybody

Copy link
Member Author

@atiratree atiratree Sep 19, 2023

Choose a reason for hiding this comment

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

I know it only helps a little, but I have added a unit test to make sure people do not mix the old and the new approach.

@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 19, 2023
@atiratree
Copy link
Member Author

/test pull-kubernetes-conformance-kind-ga-only-parallel

@atiratree
Copy link
Member Author

#119208 shows a use case for this refactoring and #119208 (comment) discusses how controller name should be consumed.

@atiratree
Copy link
Member Author

@thockin could you please review again?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 15, 2023
@atiratree atiratree force-pushed the encapsulate-kcm-controllers branch 2 times, most recently from 280db77 to c3b3fb6 Compare October 16, 2023 18:35
@atiratree
Copy link
Member Author

rebased

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 20, 2023
@thockin thockin self-assigned this Oct 25, 2023
Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

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

really minor questions

- These metadata can be used to handle controllers in a generic way.
- This enables showing feature gated controllers in kube-controller-manager's help.
- It is possible to obtain a controllerName in the InitFunc so it can be passed down to and used by the controller.

metadata about a controller:
- name
- requiredFeatureGates
- isDisabledByDefault
- isCloudProviderController
… to make it more generic

- pass a map of controllerDescriptors instead of a function
…ollers

into StartController function

the function is reused by ServiceAccountTokenController
- controller descriptors should not be feature gated
- aliases should not be defined for new controllers and have only a
  canonical name
@thockin
Copy link
Member

thockin commented Oct 27, 2023

Thanks!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 27, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 2652230cbcb0ffbf6673bdab4f6cb7022ae35a07

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: atiratree, thockin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 27, 2023
@k8s-ci-robot k8s-ci-robot merged commit ee474e6 into kubernetes:master Oct 27, 2023
6 of 14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.29 milestone Oct 27, 2023
@atiratree
Copy link
Member Author

Thanks for the review @thockin. Unfortunately I missed controllers enabled by default in one test case. And just ran into it during a review #119208 (comment).

I have opened a small PR for this: #121611

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants