diff --git a/pkg/cache/multi_namespace_cache.go b/pkg/cache/multi_namespace_cache.go index bf854effd6..4f59299487 100644 --- a/pkg/cache/multi_namespace_cache.go +++ b/pkg/cache/multi_namespace_cache.go @@ -36,6 +36,8 @@ import ( type NewCacheFunc func(config *rest.Config, opts Options) (Cache, error) // MultiNamespacedCacheBuilder - Builder function to create a new multi-namespaced cache. +// This will scope the cache to a list of namespaces. Listing for all Namespaces +// will list for all the namespaces that this knows about. func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc { return func(config *rest.Config, opts Options) (Cache, error) { opts, err := defaultOpts(config, opts) @@ -56,7 +58,7 @@ func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc { } // multiNamespaceCache knows how to handle multiple namespaced caches -// You may want to use this feature when scoping permissions for your +// Use this feature when scoping permissions for your // operator to a list of namespaces instead of watching every namespace // in the cluster. type multiNamespaceCache struct { @@ -142,6 +144,8 @@ func (c *multiNamespaceCache) List(ctx context.Context, list runtime.Object, opt } return cache.List(ctx, list, opts...) } + + // Get all the objects in the namespaces we are watching. gvk, err := apiutil.GVKForObject(list, c.Scheme) if err != nil { return err @@ -162,7 +166,6 @@ func (c *multiNamespaceCache) List(ctx context.Context, list runtime.Object, opt if err != nil { return err } - fmt.Printf("%s", data) return json.Unmarshal(data, list) } diff --git a/pkg/manager/example_test.go b/pkg/manager/example_test.go index 26d9e611f9..21ee4e7852 100644 --- a/pkg/manager/example_test.go +++ b/pkg/manager/example_test.go @@ -19,6 +19,7 @@ package manager_test import ( "os" + "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client/config" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" @@ -47,8 +48,26 @@ func ExampleNew() { log.Info("created manager", "manager", mgr) } +// This example creates a new Manager that has a cache scoped to a list of namespaces. +func ExampleNew_multinamespaceCache() { + cfg, err := config.GetConfig() + if err != nil { + log.Error(err, "unable to get kubeconfig") + os.Exit(1) + } + + mgr, err := manager.New(cfg, manager.Options{ + NewCache: cache.MultiNamespacedCacheBuilder([]string{"namespace1", "namespace2"}), + }) + if err != nil { + log.Error(err, "unable to set up manager") + os.Exit(1) + } + log.Info("created manager", "manager", mgr) +} + // This example adds a Runnable for the Manager to Start. -func ExampleManager_Add() { +func ExampleManager_add() { err := mgr.Add(manager.RunnableFunc(func(<-chan struct{}) error { // Do something return nil @@ -60,7 +79,7 @@ func ExampleManager_Add() { } // This example starts a Manager that has had Runnables added. -func ExampleManager_Start() { +func ExampleManager_start() { err := mgr.Start(signals.SetupSignalHandler()) if err != nil { log.Error(err, "unable start the manager")