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

ListOptions step on each other, HasLabels and MatchingLabels compete #2098

Closed
jsilvela opened this issue Dec 13, 2022 · 12 comments
Closed

ListOptions step on each other, HasLabels and MatchingLabels compete #2098

jsilvela opened this issue Dec 13, 2022 · 12 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@jsilvela
Copy link

jsilvela commented Dec 13, 2022

When trying to get a List using the variadic ListOptions argument, I ran to unexpected behavior if applying both HasLabels and MatchingLabels.

List(ctx, podList, client.InNamespace(namespace)
    client.MatchingLabels{
        utils.ClusterLabelName: clusterName,
    },
    client.HasLabels{"role"}, // this ensures we are getting instance pods only
)

When watching the code in action, more pods than expected were being returned.
It seemed as if the ListOptions were being OR'ed rather than AND'ed.

Looking into the implementation of MatchingLabels and HasLabels, in their ApplyToList(opts *ListOptions),
both will overwrite whatever values the LabelSelector had before.

opts.LabelSelector = sel

This means, likely, that if we apply both HasLabels and MatchingLabels, the last one applied will "win".
This is rather un-intuitive behavior.
If it is so by design, I think it should be documented.
If possible, I would think a logical AND would be the intuitive expectation.

@jsilvela jsilvela changed the title ListOptions step on each other, HasLabels and MatchinLabels compete ListOptions step on each other, HasLabels and MatchingLabels compete Dec 13, 2022
mnencia pushed a commit to cloudnative-pg/cloudnative-pg that referenced this issue Dec 16, 2022
We have found that using HasLabels and MatchingLabels in the same Kubernetes List call leads
to only one of them being applied. See kubernetes-sigs/controller-runtime#2098

This patch fixes one occurrence of this double usage in the operator code, 
avoiding some unneeded reconciliation cycles.

Signed-off-by: Jaime Silvela <jaime.silvela@enterprisedb.com>
cnpg-bot pushed a commit to cloudnative-pg/cloudnative-pg that referenced this issue Dec 16, 2022
We have found that using HasLabels and MatchingLabels in the same Kubernetes List call leads
to only one of them being applied. See kubernetes-sigs/controller-runtime#2098

This patch fixes one occurrence of this double usage in the operator code,
avoiding some unneeded reconciliation cycles.

Signed-off-by: Jaime Silvela <jaime.silvela@enterprisedb.com>
(cherry picked from commit 2aaa31c)
@alvaroaleman
Copy link
Member

/kind bug

Feel free to contribute a fix

@k8s-ci-robot k8s-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Dec 19, 2022
sxd pushed a commit to cloudnative-pg/cloudnative-pg that referenced this issue Dec 20, 2022
We have found that using HasLabels and MatchingLabels in the same Kubernetes List call leads
to only one of them being applied. See kubernetes-sigs/controller-runtime#2098

This patch fixes one occurrence of this double usage in the operator code,
avoiding some unneeded reconciliation cycles.

Signed-off-by: Jaime Silvela <jaime.silvela@enterprisedb.com>
sxd pushed a commit to cloudnative-pg/cloudnative-pg that referenced this issue Dec 20, 2022
We have found that using HasLabels and MatchingLabels in the same Kubernetes List call leads
to only one of them being applied. See kubernetes-sigs/controller-runtime#2098

This patch fixes one occurrence of this double usage in the operator code,
avoiding some unneeded reconciliation cycles.

Signed-off-by: Jaime Silvela <jaime.silvela@enterprisedb.com>
@jsilvela
Copy link
Author

Yep @alvaroaleman would like to, going to try to make time for this

@jsilvela
Copy link
Author

I posted in Slack, but will add my question here, I think it may be relevant:

I am working on a PR for this Issue.

I have a fix that is passing all the tests for HasLabels. It adds Requirement to any existing Selector, rather than overwrite.
I am trying to do the same for MatchingLabels.
But to my surprise, there is a test It("Should produce an invalid selector when given invalid input",
So, the expected behavior, for MatchingLabels is that the ApplyToList can add Requirements that would be rejected if using
labels.NewRequirement(
In controller-runtime I see no other dependency on such behavior, aside from that unit test.
Would it be possible to modify that behavior? Are there other consumers that require it?

@jsilvela
Copy link
Author

I.e. I have this for MatchingLabels

// NOTE: the invalid labels will not be applied
	for label, value := range m {
		r, err := labels.NewRequirement(label, selection.Equals, []string{value})
		if err == nil {
			sel = sel.Add(*r)
		}
	}

I saw that unit test is your work @alvaroaleman , I see it was fixing a previous bug. I'm assuming my way above won't work, because it would not produce any selector given invalid input.
Could you help me understand the context?
Many thanks

@alvaroaleman
Copy link
Member

@jsilvela the original problem was that invalid input to MatchingLabels would result in a nil selector which in turn selects everything. That was a huge footgun and I've seen this lead to pretty severe bugs a couple of times. The fix was to just construct an invalid selector which will then get rejected by the server. So yeah, your above code that would just ignore errors is a no-go.

At the end of the day, multiple requirements in a labelselector are anded by joining them comma-separated. So how about we make the two ApplyToList funcs update listOpts.Raw.LabelSelector (prefixed by a coma if non-empty)?

I'd also rather have us construct a string label selector in HasLabels, the fact that we currently swallow errors there is a really bad bug. I guess the label key tends to be constant which makes it less likely for ppl to run into that, but its definitely something we should fix.

@jsilvela
Copy link
Author

Ah, I see. Great information, thank you. I will start working on that direction today if I can.

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 30, 2023
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels May 30, 2023
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

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.

@k8s-ci-robot k8s-ci-robot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 29, 2023
@sbueringer
Copy link
Member

For reference this has been fixed via #2363

/close

@sbueringer sbueringer reopened this Jul 1, 2023
@k8s-ci-robot
Copy link
Contributor

@sbueringer: Closing this issue.

In response to this:

For reference this has been fixed via #2363

/close

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants