From ad8c798db31be5668730ea7b73a17a32f46ff556 Mon Sep 17 00:00:00 2001 From: Fabian Ruff Date: Wed, 9 Dec 2020 20:43:59 +0100 Subject: [PATCH] Add --namespace flag to support forwarding a single service (#34) --- cmd/localizer/localizer.go | 6 +++++- internal/kevents/cache.go | 11 ++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/localizer/localizer.go b/cmd/localizer/localizer.go index 0ede940..1d4b3d9 100644 --- a/cmd/localizer/localizer.go +++ b/cmd/localizer/localizer.go @@ -93,6 +93,10 @@ func main() { //nolint:funlen,gocyclo Usage: "Set the IP address CIDR, must include the /", Value: "127.0.0.1/8", }, + &cli.StringFlag{ + Name: "namespace", + Usage: "Restrict forwarding to the given namespace. (default: all namespaces)", + }, }, Commands: []*cli.Command{ NewListCommand(log), @@ -129,7 +133,7 @@ func main() { //nolint:funlen,gocyclo return err } log.Infof("using apiserver %s", config.Host) - kevents.ConfigureGlobalCache(k) + kevents.ConfigureGlobalCache(k, c.String("namespace")) return nil }, diff --git a/internal/kevents/cache.go b/internal/kevents/cache.go index 2cf7acc..12ff709 100644 --- a/internal/kevents/cache.go +++ b/internal/kevents/cache.go @@ -22,8 +22,9 @@ type EventHandler func(Event) type Cache struct { k kubernetes.Interface - stores map[string]cache.Store - subs map[string][]EventHandler + stores map[string]cache.Store + subs map[string][]EventHandler + namespace string } type Event struct { @@ -44,8 +45,8 @@ var ( ) // ConfigureGlobalCache sets up package wide global cache -func ConfigureGlobalCache(k kubernetes.Interface) { - GlobalCache = &Cache{k, make(map[string]cache.Store), make(map[string][]EventHandler)} +func ConfigureGlobalCache(k kubernetes.Interface, namespace string) { + GlobalCache = &Cache{k, make(map[string]cache.Store), make(map[string][]EventHandler), namespace} } func WaitForSync(ctx context.Context, cont cache.Controller) error { @@ -84,7 +85,7 @@ func (c *Cache) TrackObject(resourceName string, obj runtime.Object) cache.Contr key := c.getObjectType(obj) objStore, objInformer := cache.NewInformer( - cache.NewListWatchFromClient(c.k.CoreV1().RESTClient(), resourceName, corev1.NamespaceAll, fields.Everything()), + cache.NewListWatchFromClient(c.k.CoreV1().RESTClient(), resourceName, c.namespace, fields.Everything()), &corev1.Endpoints{}, time.Second*60, cache.ResourceEventHandlerFuncs{