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

⚠️ Validate controller names are unique #2902

Merged
merged 1 commit into from
Aug 5, 2024

Conversation

alvaroaleman
Copy link
Member

When re-using the same name among multiple controllers, they will report to the same metrics and use the same logger. This can be very confusing, might not be obvious and can happen accidentally.

Validate controller names are unique at runtime to avoid all of this.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 3, 2024
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 3, 2024
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Aug 3, 2024
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 3, 2024
@troy0820
Copy link
Member

troy0820 commented Aug 5, 2024

/lgtm

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

LGTM label has been added.

Git tree hash: 88bba7827300bc12364a32520eea18dc109de046

@alvaroaleman
Copy link
Member Author

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 5, 2024
pkg/controller/name.go Outdated Show resolved Hide resolved
@sbueringer
Copy link
Member

Otherwise lgtm from my side

When re-using the same name among multiple controllers, they will report
to the same metrics and use the same logger. This can be very confusing,
might not be obvious and can happen accidentally.

Validate controller names are unique at runtime to avoid all of this.
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 5, 2024
@alvaroaleman
Copy link
Member Author

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 5, 2024
@sbueringer
Copy link
Member

Thx!

/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 Aug 5, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 4f7a6ac50b301922450983aa632b2baaebf1a055

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, sbueringer

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:
  • OWNERS [alvaroaleman,sbueringer]

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 merged commit 3efd16a into kubernetes-sigs:main Aug 5, 2024
9 checks passed
@mythi
Copy link
Contributor

mythi commented Aug 12, 2024

FWIW, this seems to trigger an error in our envtest based tests where a new manager is started BeforeEach and its context cancelled AfterEach. I might have a race or doing something wrong, though. 🤔

AFAUI, the manager gets closed

...
I0812 16:39:27.717706 1182770 internal.go:550] "Stopping and waiting for caches"
I0812 16:39:27.718212 1182770 internal.go:554] "Stopping and waiting for webhooks"
I0812 16:39:27.718251 1182770 internal.go:557] "Stopping and waiting for HTTP servers"
I0812 16:39:27.718270 1182770 internal.go:561] "Wait completed, proceeding to shutdown the manager"

but the next test returns an error added here.

  [FAILED] Expected
      <*errors.errorString | 0xc000804160>: 
      controller with name <myname> already exists. Controller names must be unique to avoid multiple controllers reporting to the same metric
      {
          s: "controller with name <myname> already exists. Controller names must be unique to avoid multiple controllers reporting to the same metric",
      }
  to be nil

@sbueringer
Copy link
Member

sbueringer commented Aug 12, 2024

I think it's not a race condition. The problem is that the check is a singleton, but it has to be because the queue metrics are also singletons.

What you can do today is to use different controller names for each test run.

Just discussed this with @alvaroaleman, we would add an option to controller & manager (the controller option defaults to the one on the manager) to skip the name check.
(so this will be on

type TypedOptions[request comparable] struct {
&
type Controller struct {
)

I assume this would help in your case?

@sbueringer
Copy link
Member

@mythi @alvaroaleman PR: #2918

@mythi
Copy link
Contributor

mythi commented Aug 12, 2024

@mythi @alvaroaleman PR: #2918

thanks! I'll give it a try on Tue.

defo89 added a commit to ironcore-dev/metal-operator that referenced this pull request Sep 13, 2024
afritzler added a commit to ironcore-dev/metal-operator that referenced this pull request Sep 16, 2024
* Bump `k8s.io/*` deps to v0.31.1

- Bump `k8s.io/*` deps to v0.31.1
- Bump `controller-tools` to v0.16.3

* enable controller SkipNameValidation in test

ref kubernetes-sigs/controller-runtime#2902 (comment)

* Bump `k8s.io/*` deps to v0.31.1

- Bump `k8s.io/*` deps to v0.31.1
- Bump `controller-tools` to v0.16.3

* Use 1.31 testenv cluster

* Make probe agent backoff timeout configurable

---------

Co-authored-by: Dmitri Fedotov <dmitri.fedotov@sap.com>
sxd added a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Sep 26, 2024
Previous to the version v0.19.0 of controller-runtime it was possible
to have the same name for every controller, but in the new version this
is not allow anymore by the following PR kubernetes-sigs/controller-runtime#2902

Now we have a specific name for every controller and this may improve the
logging process.

Closes #5640

Signed-off-by: Jonathan Gonzalez V. <jonathan.gonzalez@enterprisedb.com>
gbartolini pushed a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Sep 27, 2024
Previous to the version v0.19.0 of controller-runtime it was possible
to have the same name for every controller, but in the new version this
is not allow anymore by the following PR kubernetes-sigs/controller-runtime#2902

Now we have a specific name for every controller and this may improve the
logging process.

Closes #5640

Signed-off-by: Jonathan Gonzalez V. <jonathan.gonzalez@enterprisedb.com>
gbartolini pushed a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Sep 27, 2024
Controller-runtime v0.19.0 errors when a controller manager detects
multiple controllers with the same name; this is done to improve the
clarity of the log messages.

This patch adds a name for every controller in the operator, complying
with this rule.

For more information, see kubernetes-sigs/controller-runtime#2902

Closes #5640

Signed-off-by: Jonathan Gonzalez V. <jonathan.gonzalez@enterprisedb.com>
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
Co-authored-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
leonardoce pushed a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Sep 27, 2024
Controller-runtime v0.19.0 errors when a controller manager detects
multiple controllers with the same name; this is done to improve the
clarity of the log messages.

This patch adds a name for every controller in the operator, complying
with this rule.

For more information, see kubernetes-sigs/controller-runtime#2902

Closes #5640

Signed-off-by: Jonathan Gonzalez V. <jonathan.gonzalez@enterprisedb.com>
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
Co-authored-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
(cherry picked from commit 6e28aa8)
leonardoce pushed a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Sep 27, 2024
Controller-runtime v0.19.0 errors when a controller manager detects
multiple controllers with the same name; this is done to improve the
clarity of the log messages.

This patch adds a name for every controller in the operator, complying
with this rule.

For more information, see kubernetes-sigs/controller-runtime#2902

Closes #5640

Signed-off-by: Jonathan Gonzalez V. <jonathan.gonzalez@enterprisedb.com>
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
Co-authored-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
(cherry picked from commit 6e28aa8)
leonardoce pushed a commit to cloudnative-pg/cloudnative-pg that referenced this pull request Sep 27, 2024
Controller-runtime v0.19.0 errors when a controller manager detects
multiple controllers with the same name; this is done to improve the
clarity of the log messages.

This patch adds a name for every controller in the operator, complying
with this rule.

For more information, see kubernetes-sigs/controller-runtime#2902

Closes #5640

Signed-off-by: Jonathan Gonzalez V. <jonathan.gonzalez@enterprisedb.com>
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
Co-authored-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
(cherry picked from commit 6e28aa8)
toshipp added a commit to topolvm/topolvm that referenced this pull request Oct 18, 2024
As of controller-runtime 0.19.0, it validates that controller names
should be unique[1]. In controller tests, we instantiate a controller
multiple times, therefore, the validation fails. To avoid it, disable
it.

[1]: kubernetes-sigs/controller-runtime#2902

Signed-off-by: Toshikuni Fukaya <toshikuni-fukaya@cybozu.co.jp>
toshipp added a commit to topolvm/topolvm that referenced this pull request Oct 18, 2024
As of controller-runtime 0.19.0, it validates that controller names
should be unique[1]. In controller tests, we instantiate a controller
multiple times, therefore, the validation fails. To avoid it, disable
it.

[1]: kubernetes-sigs/controller-runtime#2902

Signed-off-by: Toshikuni Fukaya <toshikuni-fukaya@cybozu.co.jp>
)

var nameLock sync.Mutex
var usedNames sets.Set[string]
Copy link
Member

Choose a reason for hiding this comment

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

Hi @alvaroaleman @sbueringer
Holding controller name with a global variable seems not a good idea, because the variable will be shared between tests in a test suite or in a test package.

To be more specific, see following the test sample below:

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mgr, err := controllerruntime.NewManager(&rest.Config{}, controllerruntime.Options{})

			controller := &FooController{}
			err = controller.SetupWithManager(mgr)
			assert.NoError(t, err) // raise error: Controller names must be unique to avoid multiple controllers reporting to the same metric

			// ... others
		})
	}

Even though each test creates a brand new controller manager, but the usedNamed is shared unexpected.

Copy link
Member

Choose a reason for hiding this comment

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

My two cents:
The used controller names should be stored in a specific manager instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

@RainbowMango my tests have the following: ctrl.NewManager(cfg, ctrl.Options{Scheme: scheme.Scheme, Metrics: metricsserver.Options{BindAddress: "0"}, Controller: config.Controller{SkipNameValidation: &yes}})

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, thanks. @mythi
It's not a blocker for me.
I noticed the option as well, but I think it's more like a workaround.

Copy link

Choose a reason for hiding this comment

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

@mythi @RainbowMango my 2 cents there as well
I did raise the same question at this issue and I really like the idea of storing controller names within one manager maybe? Or at least provide some mechanism of cleaning this map instead of disabling the check completely

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. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants