@@ -63,74 +63,60 @@ func (n *namespaceInfo) getEndpointURL() string {
6363// annotations, and caching those related to the ACK controller.
6464type 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
0 commit comments