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

✨ Allow configuring a default cache selector #1710

Merged
merged 1 commit into from
Dec 1, 2021

Conversation

alvaroaleman
Copy link
Member

@alvaroaleman alvaroaleman commented Nov 1, 2021

It is already possible to configure cache selectors per gvk, but it is
not possible to default this selector for all types. This change adds
that.

Fixes #1708

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 1, 2021
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 1, 2021
@alvaroaleman alvaroaleman force-pushed the default-selector branch 7 times, most recently from b1f91f5 to 0c219e7 Compare November 1, 2021 16:21
@alvaroaleman
Copy link
Member Author

/assign @joelanford @vincepri

Comment on lines 98 to 108
// SelectorsByObjectDefaultKey can be used in SelectorsByObject to configure a default
// selector for all kinds that do not have a specific selector set up.
type SelectorsByObjectDefaultKey struct{ metav1.Object }

// GetObjectKind implements runtime.Object.
func (s *SelectorsByObjectDefaultKey) GetObjectKind() schema.ObjectKind {
return schema.EmptyObjectKind
}

// DeepCopyObject implements runtime.Object.
func (s *SelectorsByObjectDefaultKey) DeepCopyObject() runtime.Object { return s }
Copy link
Member

Choose a reason for hiding this comment

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

Rather than encoding the default selector into the SelectorsByObject option which requires this new exported SelectorsByObjectDefaultKey type, is it possible to add a DefaultSelector field to the options struct and then handle the setup internally?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a great idea, updated the PR, PTAL

Copy link
Contributor

@FillZpp FillZpp left a comment

Choose a reason for hiding this comment

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

In fact, now we already have ObjectAll and GroupVersionKindAll.

In type DisableDeepCopyByObject map[client.Object]bool, If developers use ObjectAll as its key, it will be the default value. So how about reuse ObjectAll in SelectorsByObject to avoid defining a new option?

@joelanford
Copy link
Member

@FillZpp Brings up a good point that there's already a mechanism for this with ObjectAll.

I'm torn though. On one hand, I think a separate Default* field is much better from a user discoverability standpoint. On the other, there is already precedent for storing the default value in the map with a special key.

Interested on folks thoughts about introducing Default* fields for all of these *ByObject maps?

@alvaroaleman
Copy link
Member Author

In type DisableDeepCopyByObject map[client.Object]bool, If developers use ObjectAll as its key, it will be the default value. So how about reuse ObjectAll in SelectorsByObject to avoid defining a new option?

Right, that is where I got the idea from, I thought I remembered we did that but then couldn't find it :)

So tbh I am thorn as well, the UX of having a top-level field is much better than a magic key but does that warrant making things differently? Also I feel like the selector option warrants it because it is likely going to be used more than the somewhat exotic DisableDeepCopyByObject but I might be biased in that opinion as I need one but not the other.

To not get stuck in this question I would suggest to only add a magic key and not a top-level option for now. If we later find that ppl want to use this but have trouble finding the magic key we can still add a top-level option without breaking compatibility (by copying its value into the magic key entry). WDYT @joelanford ?

@alvaroaleman
Copy link
Member Author

@joelanford could you have another look, please?

pkg/cache/cache.go Outdated Show resolved Hide resolved
Comment on lines 93 to 95
// Use SelectorsByObjectDefaultKey as key to specify a default selector that
// will be used for all types that do not have a selector configured.
Copy link
Member

Choose a reason for hiding this comment

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

Is this comment still valid?

Copy link
Member Author

Choose a reason for hiding this comment

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

err after the back-and-forth regarding magic key vs config option I decided to use the magic key, as it is easy to add a config option later but not easy to remove it again: #1710 (comment)

Apparently I forgot to update the code though 😬 did that now.

I ended up not re-using the ObjectAll because it is a default that doesn't apply to everything, just to everything that doesn't have a setting so the name might cause confusion.

Copy link
Contributor

Choose a reason for hiding this comment

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

I ended up not re-using the ObjectAll because it is a default that doesn't apply to everything, just to everything that doesn't have a setting so the name might cause confusion.

Emm.. I'm confused that the default and all here seem to be the same semantics? They all mean the default configuration for all kinds of object, except the objects that have their specific values.

@@ -203,6 +209,7 @@ func convertToSelectorsByGVK(selectorsByObject SelectorsByObject, scheme *runtim
}
selectorsByGVK[gvk] = internal.Selector(selector)
}
selectorsByGVK[schema.GroupVersionKind{}] = internal.Selector(defaultSelector)
Copy link
Member

Choose a reason for hiding this comment

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

nit not blocking
const defalutSelectorKey = schema.GroupVersionKind{}

Copy link
Member Author

Choose a reason for hiding this comment

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

Needs to be a var but good idea, updated the code.

@alvaroaleman alvaroaleman force-pushed the default-selector branch 2 times, most recently from 30ce6b2 to 0b22232 Compare November 15, 2021 21:36
@alvaroaleman
Copy link
Member Author

/retest
The failure seems unrelated

@alvaroaleman
Copy link
Member Author

/retest

@alvaroaleman
Copy link
Member Author

/retest

@alvaroaleman
Copy link
Member Author

@joelanford @vincepri could you have another look, please?

Comment on lines 147 to 149
SelectorsByObject: cache.SelectorsByObject{
&corev1.ServiceAccount{}: {Field: fields.OneTermEqualSelector("metadata.namespace", testNamespaceOne)},
&cache.SelectorsByObjectDefaultKey{}: cache.ObjectSelector{Field: fields.OneTermEqualSelector("metadata.namespace", testNamespaceTwo)},
},
Copy link
Member

Choose a reason for hiding this comment

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

Looking at how this is being used, if I understood what we're trying to do here correctly, we can summarize it as:

  • For objects of type ServiceAccount, always cache only the objects in namespace 1
  • For any other object, cache objects in namespace 2

If that's the case, I'm not super sure if I like this APIs, it's confusing that the fallback is also a "selector by object", which seems like a wildcard, although it could be confusing to users.

Have we thought instead of adding another option, like DefaultSelector, or similar?

Copy link
Member Author

Choose a reason for hiding this comment

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

@vincepri yes, see the discussion here: #1710 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to not add and deprecate any magic key in favor of a different field, it makes a nicer UX and easier understanding of the codebase. Without this test/example, it wouldn't have been clear what the PR was trying to achieve and how.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, reverted it back to add a top-level DefaultSelector field, ptal.

It is already possible to configure cache selectors per gvk, but it is
not possible to default this selector for all types. This change adds
that.
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 1, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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,vincepri]

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 7e7bf8c into kubernetes-sigs:master Dec 1, 2021
@k8s-ci-robot k8s-ci-robot added this to the v0.10.x milestone Dec 1, 2021
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.

Cache selectors should allow setting a default
6 participants