Skip to content

Commit 0ff9ffa

Browse files
authored
Merge pull request #12 from akartsky/namespace_scope
Namespace scope service controllers
2 parents ef007e3 + 13280e0 commit 0ff9ffa

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

pkg/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
flagAWSEndpointURL = "aws-endpoint-url"
3434
flagLogLevel = "log-level"
3535
flagResourceTags = "resource-tags"
36+
flagWatchNamespace = "watch-namespace"
3637
)
3738

3839
// Config contains configuration otpions for ACK service controllers
@@ -46,6 +47,7 @@ type Config struct {
4647
EndpointURL string
4748
LogLevel string
4849
ResourceTags []string
50+
WatchNamespace string
4951
}
5052

5153
// BindFlags defines CLI/runtime configuration options
@@ -99,6 +101,12 @@ func (cfg *Config) BindFlags() {
99101
[]string{},
100102
"Configures the ACK service controller to always set key/value pairs tags on resources that it manages.",
101103
)
104+
flag.StringVar(
105+
&cfg.WatchNamespace , flagWatchNamespace,
106+
"",
107+
"Specific namespace the service controller will watch for object creation from CRD. "+
108+
" By default it will listen to all namespaces",
109+
)
102110
}
103111

104112
// SetupLogger initializes the logger used in the service controller

pkg/runtime/adoption_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (r *adoptionReconciler) BindControllerManager(mgr ctrlrt.Manager) error {
5858
return err
5959
}
6060
r.kc = mgr.GetClient()
61-
r.cache = ackrtcache.New(clientset, r.log)
61+
r.cache = ackrtcache.New(clientset, r.log, r.cfg.WatchNamespace)
6262
r.cache.Run()
6363
return ctrlrt.NewControllerManagedBy(
6464
mgr,

pkg/runtime/cache/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ type Caches struct {
5858

5959
// New creates a new Caches object from a kubernetes.Interface and
6060
// a logr.Logger
61-
func New(clientset kubernetes.Interface, log logr.Logger) Caches {
61+
func New(clientset kubernetes.Interface, log logr.Logger, watchNamespace string) Caches {
6262
return Caches{
6363
Accounts: NewAccountCache(clientset, log),
64-
Namespaces: NewNamespaceCache(clientset, log),
64+
Namespaces: NewNamespaceCache(clientset, log, watchNamespace),
6565
}
6666
}
6767

pkg/runtime/cache/namespace.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ type NamespaceCache struct {
5555
sync.RWMutex
5656

5757
log logr.Logger
58+
// Provide a namespace specifically to listen to.
59+
// Provide empty string to listen to all namespaces except kube-system and kube-public.
60+
watchNamespace string
61+
5862
// Namespace informer
5963
informer k8scache.SharedInformer
6064
// namespaceInfos maps namespaces names to their known namespaceInfo
@@ -63,7 +67,7 @@ type NamespaceCache struct {
6367

6468
// NewNamespaceCache makes a new NamespaceCache from a
6569
// kubernetes.Interface and a logr.Logger
66-
func NewNamespaceCache(clientset kubernetes.Interface, log logr.Logger) *NamespaceCache {
70+
func NewNamespaceCache(clientset kubernetes.Interface, log logr.Logger, watchNamespace string) *NamespaceCache {
6771
sharedInformer := informersv1.NewNamespaceInformer(
6872
clientset,
6973
informerResyncPeriod,
@@ -72,40 +76,44 @@ func NewNamespaceCache(clientset kubernetes.Interface, log logr.Logger) *Namespa
7276
return &NamespaceCache{
7377
informer: sharedInformer,
7478
log: log.WithName("cache.namespace"),
79+
watchNamespace: watchNamespace,
7580
namespaceInfos: make(map[string]*namespaceInfo),
7681
}
7782
}
7883

79-
// isIgnoredNamespace returns true if an object is of type corev1.Namespace
80-
// and it metadata name is one of 'ack-system', 'kube-system' or 'kube-public'
81-
func isIgnoredNamespace(raw interface{}) bool {
84+
// Check if the provided namespace should be listened to or not
85+
func isWatchNamespace(raw interface{}, watchNamespace string) bool {
8286
object, ok := raw.(*corev1.Namespace)
83-
return ok &&
84-
(object.ObjectMeta.Name == "ack-system" ||
85-
object.ObjectMeta.Name == "kube-system" ||
86-
object.ObjectMeta.Name == "kube-public")
87+
if !ok {
88+
return false
89+
}
90+
91+
if watchNamespace != "" {
92+
return watchNamespace == object.ObjectMeta.Name
93+
}
94+
return object.ObjectMeta.Name != "kube-system" && object.ObjectMeta.Name != "kube-public"
8795
}
8896

8997
// Run adds event handler functions to the SharedInformer and
9098
// runs the informer to begin processing items.
9199
func (c *NamespaceCache) Run(stopCh <-chan struct{}) {
92100
c.informer.AddEventHandler(k8scache.ResourceEventHandlerFuncs{
93101
AddFunc: func(obj interface{}) {
94-
if !isIgnoredNamespace(obj) {
102+
if isWatchNamespace(obj, c.watchNamespace) {
95103
ns := obj.(*corev1.Namespace)
96104
c.setNamespaceInfoFromK8sObject(ns)
97105
c.log.V(1).Info("created namespace", "name", ns.ObjectMeta.Name)
98106
}
99107
},
100108
UpdateFunc: func(orig, desired interface{}) {
101-
if !isIgnoredNamespace(desired) {
109+
if isWatchNamespace(desired, c.watchNamespace) {
102110
ns := desired.(*corev1.Namespace)
103111
c.setNamespaceInfoFromK8sObject(ns)
104112
c.log.V(1).Info("updated namespace", "name", ns.ObjectMeta.Name)
105113
}
106114
},
107115
DeleteFunc: func(obj interface{}) {
108-
if !isIgnoredNamespace(obj) {
116+
if isWatchNamespace(obj, c.watchNamespace) {
109117
ns := obj.(*corev1.Namespace)
110118
c.deleteNamespaceInfo(ns.ObjectMeta.Name)
111119
c.log.V(1).Info("deleted namespace", "name", ns.ObjectMeta.Name)

pkg/runtime/cache/namespace_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ 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(k8sClient, fakeLogger, "")
5353
stopCh := make(chan struct{})
5454

5555
namespaceCache.Run(stopCh)

pkg/runtime/reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (r *resourceReconciler) BindControllerManager(mgr ctrlrt.Manager) error {
7979
return err
8080
}
8181
r.kc = mgr.GetClient()
82-
r.cache = ackrtcache.New(clientset, r.log)
82+
r.cache = ackrtcache.New(clientset, r.log, r.cfg.WatchNamespace)
8383
r.cache.Run()
8484
rd := r.rmf.ResourceDescriptor()
8585
return ctrlrt.NewControllerManagedBy(

0 commit comments

Comments
 (0)