Skip to content

Commit c5631b6

Browse files
authored
Optionally run the Namespace and ConfigMap caches (#42)
Issue N/A Description of changes: - Run the caches only when `--watch-namespace` is not empty. - Remove the "watch namespace" filter from the `NamespaceCache` event handlers. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9a99433 commit c5631b6

File tree

6 files changed

+61
-77
lines changed

6 files changed

+61
-77
lines changed

pkg/runtime/cache/account.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,13 @@ const (
3434
// make the changes accordingly.
3535
type AccountCache struct {
3636
sync.RWMutex
37-
38-
log logr.Logger
39-
40-
// ConfigMap informer
41-
informer k8scache.SharedInformer
37+
log logr.Logger
4238
roleARNs map[string]string
4339
}
4440

45-
// NewAccountCache makes a new AccountCache from a client.Interface
46-
// and a logr.Logger
47-
func NewAccountCache(clientset kubernetes.Interface, log logr.Logger) *AccountCache {
48-
sharedInformer := informersv1.NewConfigMapInformer(
49-
clientset,
50-
currentNamespace,
51-
informerResyncPeriod,
52-
k8scache.Indexers{},
53-
)
41+
// NewAccountCache instanciate a new AccountCache.
42+
func NewAccountCache(log logr.Logger) *AccountCache {
5443
return &AccountCache{
55-
informer: sharedInformer,
5644
log: log.WithName("cache.account"),
5745
roleARNs: make(map[string]string),
5846
}
@@ -65,10 +53,15 @@ func resourceMatchACKRoleAccountsConfigMap(raw interface{}) bool {
6553
return ok && object.ObjectMeta.Name == ACKRoleAccountMap
6654
}
6755

68-
// Run adds the default event handler functions to the SharedInformer and
69-
// runs the informer to begin processing items.
70-
func (c *AccountCache) Run(stopCh <-chan struct{}) {
71-
c.informer.AddEventHandler(k8scache.ResourceEventHandlerFuncs{
56+
// Run instantiate a new SharedInformer for ConfigMaps and runs it to begin processing items.
57+
func (c *AccountCache) Run(clientSet kubernetes.Interface, stopCh <-chan struct{}) {
58+
informer := informersv1.NewConfigMapInformer(
59+
clientSet,
60+
currentNamespace,
61+
informerResyncPeriod,
62+
k8scache.Indexers{},
63+
)
64+
informer.AddEventHandler(k8scache.ResourceEventHandlerFuncs{
7265
AddFunc: func(obj interface{}) {
7366
if resourceMatchACKRoleAccountsConfigMap(obj) {
7467
cm := obj.(*corev1.ConfigMap)
@@ -95,7 +88,7 @@ func (c *AccountCache) Run(stopCh <-chan struct{}) {
9588
}
9689
},
9790
})
98-
go c.informer.Run(stopCh)
91+
go informer.Run(stopCh)
9992
}
10093

10194
// GetAccountRoleARN queries the AWS accountID associated Role ARN

pkg/runtime/cache/account_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ func TestAccountCache(t *testing.T) {
6161
fakeLogger := ctrlrtzap.New(ctrlrtzap.UseFlagOptions(&zapOptions))
6262

6363
// initlizing account cache
64-
accountCache := ackrtcache.NewAccountCache(k8sClient, fakeLogger)
64+
accountCache := ackrtcache.NewAccountCache(fakeLogger)
6565
stopCh := make(chan struct{})
66-
accountCache.Run(stopCh)
66+
accountCache.Run(k8sClient, stopCh)
6767

6868
// Test create events
69-
k8sClient.CoreV1().ConfigMaps(testNamespace).Create(
69+
_, err := k8sClient.CoreV1().ConfigMaps(testNamespace).Create(
7070
context.Background(),
7171
&corev1.ConfigMap{
7272
ObjectMeta: metav1.ObjectMeta{
@@ -76,6 +76,7 @@ func TestAccountCache(t *testing.T) {
7676
},
7777
metav1.CreateOptions{},
7878
)
79+
require.Nil(t, err)
7980

8081
time.Sleep(time.Second)
8182

pkg/runtime/cache/cache.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,22 @@ type Caches struct {
5656
Namespaces *NamespaceCache
5757
}
5858

59-
// New creates a new Caches object from a kubernetes.Interface and
60-
// a logr.Logger
61-
func New(clientset kubernetes.Interface, log logr.Logger, watchNamespace string) Caches {
59+
// New instantiate a new Caches object.
60+
func New(log logr.Logger) Caches {
6261
return Caches{
63-
Accounts: NewAccountCache(clientset, log),
64-
Namespaces: NewNamespaceCache(clientset, log, watchNamespace),
62+
Accounts: NewAccountCache(log),
63+
Namespaces: NewNamespaceCache(log),
6564
}
6665
}
6766

6867
// Run runs all the owned caches
69-
func (c Caches) Run() {
68+
func (c Caches) Run(clientSet kubernetes.Interface) {
7069
stopCh := make(chan struct{})
7170
if c.Accounts != nil {
72-
c.Accounts.Run(stopCh)
71+
c.Accounts.Run(clientSet, stopCh)
7372
}
7473
if c.Namespaces != nil {
75-
c.Namespaces.Run(stopCh)
74+
c.Namespaces.Run(clientSet, stopCh)
7675
}
7776
c.stopCh = stopCh
7877
}

pkg/runtime/cache/namespace.go

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,74 +63,60 @@ func (n *namespaceInfo) getEndpointURL() string {
6363
// annotations, and caching those related to the ACK controller.
6464
type NamespaceCache struct {
6565
sync.RWMutex
66-
6766
log logr.Logger
68-
// Provide a namespace specifically to listen to.
69-
// Provide empty string to listen to all namespaces except kube-system and kube-public.
70-
watchNamespace string
71-
72-
// Namespace informer
73-
informer k8scache.SharedInformer
7467
// namespaceInfos maps namespaces names to their known namespaceInfo
7568
namespaceInfos map[string]*namespaceInfo
7669
}
7770

78-
// NewNamespaceCache makes a new NamespaceCache from a
79-
// kubernetes.Interface and a logr.Logger
80-
func NewNamespaceCache(clientset kubernetes.Interface, log logr.Logger, watchNamespace string) *NamespaceCache {
81-
sharedInformer := informersv1.NewNamespaceInformer(
82-
clientset,
83-
informerResyncPeriod,
84-
k8scache.Indexers{},
85-
)
71+
// NewNamespaceCache instanciate a new NamespaceCache.
72+
func NewNamespaceCache(log logr.Logger) *NamespaceCache {
8673
return &NamespaceCache{
87-
informer: sharedInformer,
8874
log: log.WithName("cache.namespace"),
89-
watchNamespace: watchNamespace,
9075
namespaceInfos: make(map[string]*namespaceInfo),
9176
}
9277
}
9378

94-
// Check if the provided namespace should be listened to or not
95-
func isWatchNamespace(raw interface{}, watchNamespace string) bool {
79+
// isIgnoredNamespace returns true if an object is of type corev1.Namespace
80+
// and its metadata name is one of 'ack-system', 'kube-system' or 'kube-public'
81+
func isIgnoredNamespace(raw interface{}) bool {
9682
object, ok := raw.(*corev1.Namespace)
97-
if !ok {
98-
return false
99-
}
100-
101-
if watchNamespace != "" {
102-
return watchNamespace == object.ObjectMeta.Name
103-
}
104-
return object.ObjectMeta.Name != "kube-system" && object.ObjectMeta.Name != "kube-public"
83+
return ok &&
84+
(object.ObjectMeta.Name == "ack-system" ||
85+
object.ObjectMeta.Name == "kube-system" ||
86+
object.ObjectMeta.Name == "kube-public")
10587
}
10688

107-
// Run adds event handler functions to the SharedInformer and
108-
// runs the informer to begin processing items.
109-
func (c *NamespaceCache) Run(stopCh <-chan struct{}) {
110-
c.informer.AddEventHandler(k8scache.ResourceEventHandlerFuncs{
89+
// Run instantiate a new shared informer for namespaces and runs it to begin processing items.
90+
func (c *NamespaceCache) Run(clientSet kubernetes.Interface, stopCh <-chan struct{}) {
91+
informer := informersv1.NewNamespaceInformer(
92+
clientSet,
93+
informerResyncPeriod,
94+
k8scache.Indexers{},
95+
)
96+
informer.AddEventHandler(k8scache.ResourceEventHandlerFuncs{
11197
AddFunc: func(obj interface{}) {
112-
if isWatchNamespace(obj, c.watchNamespace) {
98+
if !isIgnoredNamespace(obj) {
11399
ns := obj.(*corev1.Namespace)
114100
c.setNamespaceInfoFromK8sObject(ns)
115101
c.log.V(1).Info("created namespace", "name", ns.ObjectMeta.Name)
116102
}
117103
},
118104
UpdateFunc: func(orig, desired interface{}) {
119-
if isWatchNamespace(desired, c.watchNamespace) {
105+
if !isIgnoredNamespace(desired) {
120106
ns := desired.(*corev1.Namespace)
121107
c.setNamespaceInfoFromK8sObject(ns)
122108
c.log.V(1).Info("updated namespace", "name", ns.ObjectMeta.Name)
123109
}
124110
},
125111
DeleteFunc: func(obj interface{}) {
126-
if isWatchNamespace(obj, c.watchNamespace) {
112+
if !isIgnoredNamespace(obj) {
127113
ns := obj.(*corev1.Namespace)
128114
c.deleteNamespaceInfo(ns.ObjectMeta.Name)
129115
c.log.V(1).Info("deleted namespace", "name", ns.ObjectMeta.Name)
130116
}
131117
},
132118
})
133-
go c.informer.Run(stopCh)
119+
go informer.Run(stopCh)
134120
}
135121

136122
// GetDefaultRegion returns the default region if it it exists

pkg/runtime/cache/namespace_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ func TestNamespaceCache(t *testing.T) {
4949
fakeLogger := ctrlrtzap.New(ctrlrtzap.UseFlagOptions(&zapOptions))
5050

5151
// initlizing account cache
52-
namespaceCache := ackrtcache.NewNamespaceCache(k8sClient, fakeLogger, "")
52+
namespaceCache := ackrtcache.NewNamespaceCache(fakeLogger)
5353
stopCh := make(chan struct{})
5454

55-
namespaceCache.Run(stopCh)
55+
namespaceCache.Run(k8sClient, stopCh)
5656

5757
// Test create events
58-
k8sClient.CoreV1().Namespaces().Create(
58+
_, err := k8sClient.CoreV1().Namespaces().Create(
5959
context.Background(),
6060
&corev1.Namespace{
6161
ObjectMeta: metav1.ObjectMeta{
@@ -69,6 +69,7 @@ func TestNamespaceCache(t *testing.T) {
6969
},
7070
metav1.CreateOptions{},
7171
)
72+
require.Nil(t, err)
7273

7374
time.Sleep(time.Second)
7475

@@ -85,7 +86,7 @@ func TestNamespaceCache(t *testing.T) {
8586
require.Equal(t, "https://amazon-service.region.amazonaws.com", endpointURL)
8687

8788
// Test update events
88-
k8sClient.CoreV1().Namespaces().Update(
89+
_, err = k8sClient.CoreV1().Namespaces().Update(
8990
context.Background(),
9091
&corev1.Namespace{
9192
ObjectMeta: metav1.ObjectMeta{
@@ -99,6 +100,7 @@ func TestNamespaceCache(t *testing.T) {
99100
},
100101
metav1.UpdateOptions{},
101102
)
103+
require.Nil(t, err)
102104

103105
time.Sleep(time.Second)
104106

@@ -115,11 +117,12 @@ func TestNamespaceCache(t *testing.T) {
115117
require.Equal(t, "https://amazon-other-service.region.amazonaws.com", endpointURL)
116118

117119
// Test delete events
118-
k8sClient.CoreV1().Namespaces().Delete(
120+
err = k8sClient.CoreV1().Namespaces().Delete(
119121
context.Background(),
120122
"production",
121123
metav1.DeleteOptions{},
122124
)
125+
require.Nil(t, err)
123126

124127
time.Sleep(time.Second)
125128

pkg/runtime/service_controller.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,15 @@ func (c *serviceController) BindControllerManager(mgr ctrlrt.Manager, cfg ackcfg
133133
c.metaLock.Lock()
134134
defer c.metaLock.Unlock()
135135

136-
clusterConfig := mgr.GetConfig()
137-
clientset, err := kubernetes.NewForConfig(clusterConfig)
138-
if err != nil {
139-
return err
136+
cache := ackrtcache.New(c.log)
137+
if cfg.WatchNamespace == "" {
138+
clusterConfig := mgr.GetConfig()
139+
clientSet, err := kubernetes.NewForConfig(clusterConfig)
140+
if err != nil {
141+
return err
142+
}
143+
cache.Run(clientSet)
140144
}
141-
cache := ackrtcache.New(clientset, c.log, cfg.WatchNamespace)
142-
cache.Run()
143145

144146
for _, rmf := range c.rmFactories {
145147
rec := NewReconciler(c, rmf, c.log, cfg, c.metrics, cache)

0 commit comments

Comments
 (0)