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

cache: store namespace names instead of metadata #62

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sadlerap
Copy link
Collaborator

@sadlerap sadlerap commented Feb 7, 2025

Instead of caching namespace metadata, we can instead only cache the namespaces' names and pull their definitions from the client cache. This saves some memory at the expense of a minor increase in response latency (benchmarks indicated on the order of a few extra milliseconds).

Instead of caching namespace metadata, we can instead only cache the
namespaces' names and pull their definitions from the client cache.
This saves some memory at the expense of a minor increase in response
latency (benchmarks indicated on the order of a few extra milliseconds).

Signed-off-by: Andy Sadler <[email protected]>
namespace := corev1.Namespace{}
err := s.namespaceLister.Get(ctx, types.NamespacedName{Name: ns}, &namespace)
if err != nil {
slog.Error("failed to retrieve namespace", "namespace", ns, "err", err)
Copy link
Member

Choose a reason for hiding this comment

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

this is using the default logger, we should use s.logger instead

Suggested change
slog.Error("failed to retrieve namespace", "namespace", ns, "err", err)
s.logger.Error("failed to retrieve namespace", "namespace", ns, "err", err)

@@ -179,3 +185,21 @@ func (s *SynchronizedAccessCache) Start(ctx context.Context) {
}()
})
}

func (s *SynchronizedAccessCache) List(ctx context.Context, subject rbacv1.Subject) []corev1.Namespace {
Copy link
Member

Choose a reason for hiding this comment

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

I think this logic should be moved to AccessCache

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't agree. I think the AccessCache should solely be concerned with storage. SynchronizedAccessCache, meanwhile, should be able to provide the additional features we need in our cache, relying on AccessCache to provide synchronization.

In other words: I don't see the benefit of pushing this down a layer.

Comment on lines +96 to +100
if _, exists := c[s]; !exists {
c[s] = sets.New(ns.Name)
} else {
c[s].Insert(ns.Name)
}
Copy link
Member

Choose a reason for hiding this comment

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

if we rely on sets here, we can remove the removeDuplicateSubjects function and its invocation at L92

c[s] = append(c[s], ns)
if _, exists := c[s]; !exists {
c[s] = sets.New(ns.Name)
} else {
Copy link
Member

Choose a reason for hiding this comment

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

do you think we can remove this else somehow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

AIUI no, not directly. I was running into issues with zero-initialized sets, and my understanding is that the constructor (New) needs to be invoked to have a valid data structure.

That said, I may be able to refactor this into two passes, one that makes the sets and one that populates them.


for _, ns := range namespaces {
namespace := corev1.Namespace{}
err := s.namespaceLister.Get(ctx, types.NamespacedName{Name: ns}, &namespace)
Copy link
Member

Choose a reason for hiding this comment

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

this way we are getting access from the AccessCache and namespaces from the ResourcesCache. This is coupling the List operation to the two caches.

It may happen that the namespaces in the ResourcesCache were updated before the access in the AccessCache and this may introduce some errors.
I'd suggest to cache the namespaces used during the synch operation in the AccessCache, so that we always provide consistent results.

Copy link
Member

@filariow filariow Feb 10, 2025

Choose a reason for hiding this comment

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

Also if we cache namespaces in the AccessCache, we can implement a slightly more performant version of this algorithm by sorting namespaces ahead-of-time (cache Synch/Restock) and using binary search just-in-time (List)

@sadlerap sadlerap marked this pull request as draft February 10, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants