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

📖 Add a design for cache options configuration #2261

Merged
merged 1 commit into from Jun 12, 2023

Conversation

alvaroaleman
Copy link
Member

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 1, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman

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 approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 1, 2023
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
@FillZpp
Copy link
Contributor

FillZpp commented Apr 7, 2023

/lgtm
/hold
see if there is more discussion @vincepri @joelanford @sbueringer

@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 Apr 7, 2023
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 7, 2023
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
"": {},
},
},
DefaultNamespaces: map[string]*cache.CacheSetting{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
DefaultNamespaces: map[string]*cache.CacheSetting{
ByNamespace: map[string]*cache.CacheSetting{

Maybe? The DefaultNamespaces doesn't really give away what it's doing

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason I used DefaultNamespaces is to clarify that this will only be used if ByObject has no namespace settings

designs/cache_options.md Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 10, 2023
@alvaroaleman alvaroaleman force-pushed the cache-opts branch 3 times, most recently from b25ef88 to ff08978 Compare April 11, 2023 23:05
@alvaroaleman
Copy link
Member Author

/retest

designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Outdated Show resolved Hide resolved
designs/cache_options.md Show resolved Hide resolved
@alvaroaleman alvaroaleman force-pushed the cache-opts branch 2 times, most recently from 8f637fe to dc57410 Compare May 31, 2023 07:54
@sbueringer
Copy link
Member

Thank you very much!

/lgtm

/hold
in case you want to wait for more feedback

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 31, 2023
designs/cache_options.md Outdated Show resolved Hide resolved
// An empty map prevents this.
//
// This must be unset for cluster-scoped objects.
Namespaces map[string]*Config
Copy link
Member

Choose a reason for hiding this comment

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

How does this map to RBAC? Do I only need list, get, watch permissions in the namespaces specified in this map, or do I need those permissions for all namespaces? Does this option limit what is requested/ watched from the APIServer or does it just limit what is being cached?

Copy link
Member Author

Choose a reason for hiding this comment

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

What is cached is what is being watched, so both.

Copy link

@irbekrm irbekrm May 31, 2023

Choose a reason for hiding this comment

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

What is cached is what is being watched, so both.

At the moment the calls to the kube apiserver to LIST resources are different (this might be in client-go, not sure) depending on whether the whole cache is scoped to a namespace or whether the namespace selector is applied as a field selector.
Because of that (I think) a namespace-scoped cache allows us to also scope RBAC to that namespace whereas the selector does not.

We have a use case where we want to watch Secrets in namespace X and Configmaps in all namespaces. We create two caches for this because although we could use the existing cache options to apply namespace filter to Secrets, only namespaced cache allows us to scope RBAC down so that users only need to give our controller permissions to list Secrets in that one namespace.

For us, it would be quite useful if we were able to use this Namespaces option in such a way that the namespace filter is also reflected in the required RBAC for our controller

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it will be. Something like your use-case is what we want to support. Your config would look something like:

cache.Options{
ByObject: map[client.Object]*cache.ByObject{
  &corev1.Secrets{}: *cache.ByObject{
    Namespaces: map[string]*cache.Config{"the-namespace-for-secrets": nil},
  }
}

The above config would watch secrets in one namespace and everything else in all namespaces. That also applies to rbac.

Copy link

Choose a reason for hiding this comment

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

Thank you- for us this would be very valuable 👍🏼

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 31, 2023
Comment on lines +84 to +112
// DefaultNamespaces maps namespace names to cache settings. If set, it
// will be used for all objects that have a nil Namespaces setting.
//
// It is possible to have a specific Config for just some namespaces
// but cache all namespaces by using the `AllNamespaces` const as the map
// key. This wil then include all namespaces that do not have a more
// specific setting.
//
// The options in the Config that are nil will be defaulted from
// the respective Default* settings.
DefaultNamespaces map[string]*Config

// DefaultLabelSelector is the label selector that will be used as
// the default field label selector for everything that doesn't
// have one configured.
DefaultLabelSelector labels.Selector

// DefaultFieldSelector is the field selector that will be used as
// the default field selector for everything that doesn't have
// one configured.
DefaultFieldSelector fields.Selector

// DefaultUnsafeDisableDeepCopy is the default for UnsafeDisableDeepCopy
// for everything that doesn't specify this.
DefaultUnsafeDisableDeepCopy *bool

// DefaultTransform will be used as transform for all object types
// unless they have a more specific transform set in ByObject.
DefaultTransform toolscache.TransformFunc
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better or worse to drop the Default prefix? It seems redundant, given that the ByObject seems clear that it overrides these imo

Copy link
Member Author

Choose a reason for hiding this comment

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

I like it because it clarifies that this is the last layer of defaulting

type Options struct {
// ByObject specifies per-object cache settings. If unset for a given
// object, this will fall through to Default* settings.
ByObject map[client.Object]*ByObject
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ByObject map[client.Object]*ByObject
ByObject map[client.Object]ByObject

Maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not a super strong opinion here, the reason I chose the pointer type is in some contexts an empty thing has a meaning (for example an empty labelSelector is a match-everything selector) so I thought by not requireding ppl to do a map[string]ByObject{"namespace":{}} we can avoid a bit of confusion

Comment on lines +71 to +76
// Config will be used for cluster-scoped objects and to default
// Config in the Namespaces field.
//
// It gets defaulted from the cache'sDefaultLabelSelector, DefaultFieldSelector,
// DefaultUnsafeDisableDeepCopy and DefaultTransform.
Config *Config
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Config will be used for cluster-scoped objects and to default
// Config in the Namespaces field.
//
// It gets defaulted from the cache'sDefaultLabelSelector, DefaultFieldSelector,
// DefaultUnsafeDisableDeepCopy and DefaultTransform.
Config *Config
// Overrides will be used for cluster-scoped objects and to default
// Config in the Namespaces field.
//
// It gets defaulted from the cache'sDefaultLabelSelector, DefaultFieldSelector,
// DefaultUnsafeDisableDeepCopy and DefaultTransform.
Overrides *Overrides

Maybe? Could be a bit more readable, 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that might be confusing because with an override, ppl expect the value in there to be the final setting but:

  • Its not the top-most setting for namespaced resources
  • It gets defaulted itself

@sbueringer
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 2, 2023
@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 Jun 12, 2023
@k8s-ci-robot k8s-ci-robot merged commit be2becb into kubernetes-sigs:main Jun 12, 2023
9 checks passed
@alvaroaleman alvaroaleman deleted the cache-opts branch June 12, 2023 18:15
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.

None yet