diff --git a/pkg/cache/cache_test.go b/pkg/cache/cache_test.go index eed63a1f99..287651abc7 100644 --- a/pkg/cache/cache_test.go +++ b/pkg/cache/cache_test.go @@ -38,6 +38,7 @@ import ( const testNamespaceOne = "test-namespace-1" const testNamespaceTwo = "test-namespace-2" +const testNamespaceThree = "test-namespace-3" // TODO(community): Pull these helper functions into testenv. // Restart policy is included to allow indexing on that field. @@ -74,6 +75,9 @@ func deletePod(pod runtime.Object) { var _ = Describe("Informer Cache", func() { CacheTest(cache.New) }) +var _ = Describe("Multi-Namesapce Informer Cache", func() { + CacheTest(cache.MultiNamespacedCacheBuilder([]string{testNamespaceOne, testNamespaceTwo, "default"})) +}) // nolint: gocyclo func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (cache.Cache, error)) { @@ -84,6 +88,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca knownPod1 runtime.Object knownPod2 runtime.Object knownPod3 runtime.Object + knownPod4 runtime.Object ) BeforeEach(func() { @@ -95,6 +100,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca knownPod1 = createPod("test-pod-1", testNamespaceOne, kcorev1.RestartPolicyNever) knownPod2 = createPod("test-pod-2", testNamespaceTwo, kcorev1.RestartPolicyAlways) knownPod3 = createPod("test-pod-3", testNamespaceTwo, kcorev1.RestartPolicyOnFailure) + knownPod4 = createPod("test-pod-4", testNamespaceThree, kcorev1.RestartPolicyNever) podGVK := schema.GroupVersionKind{ Kind: "Pod", Version: "v1", @@ -102,6 +108,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca knownPod1.GetObjectKind().SetGroupVersionKind(podGVK) knownPod2.GetObjectKind().SetGroupVersionKind(podGVK) knownPod3.GetObjectKind().SetGroupVersionKind(podGVK) + knownPod4.GetObjectKind().SetGroupVersionKind(podGVK) By("creating the informer cache") var err error @@ -120,6 +127,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca deletePod(knownPod1) deletePod(knownPod2) deletePod(knownPod3) + deletePod(knownPod4) close(stop) }) diff --git a/pkg/cache/example_test.go b/pkg/cache/example_test.go deleted file mode 100644 index 3f4231dd7d..0000000000 --- a/pkg/cache/example_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package cache_test - -import ( - "os" - - "sigs.k8s.io/controller-runtime/pkg/cache" - logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/manager" - "sigs.k8s.io/controller-runtime/pkg/runtime/signals" -) - -var ( - log = logf.Log.WithName("manager-examples") -) - -// This example shows how to create and set a new multi namespaced cache. -func ExampleNewMultiNamespacedCacheBuilder() { - mgr, err := manager.New(cfg, manager.Options{ - NewCache: cache.NewMultiNamespacedCacheBuilder([]string{"namespace1", "namespace2"}), - }) - if err != nil { - log.Error(err, "unable to create manager") - os.Exit(1) - } - err = mgr.Start(signals.SetupSignalHandler()) - if err != nil { - log.Error(err, "unable start the manager") - os.Exit(1) - } -} diff --git a/pkg/cache/multi_namespace_cache.go b/pkg/cache/multi_namespace_cache.go index 32f552d79b..bf854effd6 100644 --- a/pkg/cache/multi_namespace_cache.go +++ b/pkg/cache/multi_namespace_cache.go @@ -32,17 +32,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/apiutil" ) -// multiNamespaceCache knows how to handle multiple namespaced caches -// You may want to 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 { - namespaceToCache map[string]Cache - Scheme *runtime.Scheme -} +// NewCacheFunc - Function for creating a new cache from the options and a rest config +type NewCacheFunc func(config *rest.Config, opts Options) (Cache, error) -// NewMultiNamespacedCacheBuilder - Builder function to create a new multi-namespaced cache. -func NewMultiNamespacedCacheBuilder(namespaces []string) func(config *rest.Config, opts Options) (Cache, error) { +// MultiNamespacedCacheBuilder - Builder function to create a new multi-namespaced cache. +func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc { return func(config *rest.Config, opts Options) (Cache, error) { opts, err := defaultOpts(config, opts) if err != nil { @@ -61,6 +55,15 @@ func NewMultiNamespacedCacheBuilder(namespaces []string) func(config *rest.Confi } } +// multiNamespaceCache knows how to handle multiple namespaced caches +// You may want to 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 { + namespaceToCache map[string]Cache + Scheme *runtime.Scheme +} + var _ Cache = &multiNamespaceCache{} // Methods for multiNamespaceCache to conform to the Informers interface diff --git a/pkg/cache/multi_namespace_cache_test.go b/pkg/cache/multi_namespace_cache_test.go deleted file mode 100644 index 5d63a03888..0000000000 --- a/pkg/cache/multi_namespace_cache_test.go +++ /dev/null @@ -1,32 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package cache_test - -import ( - . "github.com/onsi/ginkgo" - kcorev1 "k8s.io/api/core/v1" - - "sigs.k8s.io/controller-runtime/pkg/cache" -) - -const testNamespaceThree = "test-namespace-3" - -var _ = Describe("Multinamesapce Informer Cache", func() { - knownPod1 := createPod("test-pod-1", testNamespaceThree, kcorev1.RestartPolicyNever) - defer deletePod(knownPod1) - CacheTest(cache.NewMultiNamespacedCacheBuilder([]string{testNamespaceOne, testNamespaceTwo, "default"})) -}) diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 8d346df6c1..8a1ccfbd68 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -122,7 +122,7 @@ type Options struct { // NewCache is the function that will create the cache to be used // by the manager. If not set this will use the default new cache function. - NewCache NewCacheFunc + NewCache cache.NewCacheFunc // NewClient will create the client to be used by the manager. // If not set this will create the default DelegatingClient that will @@ -136,9 +136,6 @@ type Options struct { newMetricsListener func(addr string) (net.Listener, error) } -// NewCacheFunc allows a user to define how to create a cache -type NewCacheFunc func(config *rest.Config, opts cache.Options) (cache.Cache, error) - // NewClientFunc allows a user to define how to create a client type NewClientFunc func(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error)