1414package iamroleselector
1515
1616import (
17+ "context"
1718 "fmt"
1819 "sync"
1920
@@ -24,29 +25,33 @@ import (
2425 "k8s.io/apimachinery/pkg/runtime/schema"
2526 "k8s.io/client-go/dynamic"
2627 "k8s.io/client-go/dynamic/dynamicinformer"
28+ "k8s.io/client-go/kubernetes"
2729 "k8s.io/client-go/tools/cache"
2830
2931 ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
32+ ackcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
3033)
3134
3235// Cache wraps the informer for IAMRoleSelector resources
3336type Cache struct {
3437 sync.RWMutex
35- log logr.Logger
36- informer cache.SharedIndexInformer
37- selectors map [string ]* ackv1alpha1.IAMRoleSelector // name -> selector
38+ namespaces * ackcache.NamespaceCache
39+ log logr.Logger
40+ informer cache.SharedIndexInformer
41+ selectors map [string ]* ackv1alpha1.IAMRoleSelector // name -> selector
3842}
3943
4044// NewCache creates a new IAMRoleSelector cache
4145func NewCache (log logr.Logger ) * Cache {
4246 return & Cache {
43- log : log .WithName ("cache.iam-role-selector" ),
44- selectors : make (map [string ]* ackv1alpha1.IAMRoleSelector ),
47+ log : log .WithName ("cache.iam-role-selector" ),
48+ selectors : make (map [string ]* ackv1alpha1.IAMRoleSelector ),
49+ namespaces : ackcache .NewNamespaceCache (log , nil , nil ),
4550 }
4651}
4752
4853// Run starts the cache and blocks until stopCh is closed
49- func (c * Cache ) Run (client dynamic.Interface , stopCh <- chan struct {}) {
54+ func (c * Cache ) Run (client dynamic.Interface , namespaceClient kubernetes. Interface , stopCh <- chan struct {}) {
5055 c .log .V (1 ).Info ("Starting IAMRoleSelector cache" )
5156
5257 // Create dynamic informer factory
@@ -74,6 +79,8 @@ func (c *Cache) Run(client dynamic.Interface, stopCh <-chan struct{}) {
7479 })
7580
7681 factory .Start (stopCh )
82+
83+ c .namespaces .Run (namespaceClient , stopCh )
7784}
7885
7986func (c * Cache ) handleAdd (obj interface {}) {
@@ -197,15 +204,15 @@ func (c *Cache) ListSelectors() []*ackv1alpha1.IAMRoleSelector {
197204
198205// Matches returns a list of IAMRoleSelectors that match the given resource. This function
199206// should only be called after the cache has been started and synced.
200- func (c * Cache ) Matches (resource runtime.Object ) ([]* ackv1alpha1.IAMRoleSelector , error ) {
207+ func (c * Cache ) Matches (ctx context. Context , resource runtime.Object ) ([]* ackv1alpha1.IAMRoleSelector , error ) {
201208 // Extract metadata from the resource
202209 metaObj , err := meta .Accessor (resource )
203210 if err != nil {
204211 return nil , fmt .Errorf ("failed to get metadata from resource: %w" , err )
205212 }
206213
207- namespace := metaObj .GetNamespace ()
208-
214+ namespaceName := metaObj .GetNamespace ()
215+ namespaceLabels := c . namespaces . GetLabels ( namespaceName )
209216 // Get GVK - should be set on ACK resources
210217 gvk := resource .GetObjectKind ().GroupVersionKind ()
211218 if gvk .Empty () {
@@ -215,5 +222,5 @@ func (c *Cache) Matches(resource runtime.Object) ([]*ackv1alpha1.IAMRoleSelector
215222
216223 // TODO: get namespace labels from a namespace lister/cache
217224 // For now, pass empty namespace labels
218- return c .GetMatchingSelectors (namespace , nil , gvk )
225+ return c .GetMatchingSelectors (namespaceName , namespaceLabels , gvk )
219226}
0 commit comments