Skip to content

Commit 5c59791

Browse files
author
Shawn Hurley
committed
Adding more docs
1 parent 2f6a0b3 commit 5c59791

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

pkg/cache/multi_namespace_cache.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
type NewCacheFunc func(config *rest.Config, opts Options) (Cache, error)
3737

3838
// MultiNamespacedCacheBuilder - Builder function to create a new multi-namespaced cache.
39+
// This will scope the cache to a list of namespaces. Listing for all Namespaces
40+
// will list for all the namespaces that this knows about.
3941
func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc {
4042
return func(config *rest.Config, opts Options) (Cache, error) {
4143
opts, err := defaultOpts(config, opts)
@@ -56,7 +58,7 @@ func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc {
5658
}
5759

5860
// multiNamespaceCache knows how to handle multiple namespaced caches
59-
// You may want to use this feature when scoping permissions for your
61+
// Use this feature when scoping permissions for your
6062
// operator to a list of namespaces instead of watching every namespace
6163
// in the cluster.
6264
type multiNamespaceCache struct {
@@ -142,6 +144,8 @@ func (c *multiNamespaceCache) List(ctx context.Context, list runtime.Object, opt
142144
}
143145
return cache.List(ctx, list, opts...)
144146
}
147+
148+
// Get all the objects in the namespaces we are watching.
145149
gvk, err := apiutil.GVKForObject(list, c.Scheme)
146150
if err != nil {
147151
return err
@@ -162,7 +166,6 @@ func (c *multiNamespaceCache) List(ctx context.Context, list runtime.Object, opt
162166
if err != nil {
163167
return err
164168
}
165-
fmt.Printf("%s", data)
166169
return json.Unmarshal(data, list)
167170
}
168171

pkg/manager/example_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package manager_test
1919
import (
2020
"os"
2121

22+
"sigs.k8s.io/controller-runtime/pkg/cache"
2223
"sigs.k8s.io/controller-runtime/pkg/client/config"
2324
logf "sigs.k8s.io/controller-runtime/pkg/log"
2425
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -47,8 +48,26 @@ func ExampleNew() {
4748
log.Info("created manager", "manager", mgr)
4849
}
4950

51+
// This example creates a new Manager that has a cache scoped to a list of namespaces.
52+
func ExampleNew_multinamespaceCache() {
53+
cfg, err := config.GetConfig()
54+
if err != nil {
55+
log.Error(err, "unable to get kubeconfig")
56+
os.Exit(1)
57+
}
58+
59+
mgr, err := manager.New(cfg, manager.Options{
60+
NewCache: cache.MultiNamespacedCacheBuilder([]string{"namespace1", "namespace2"}),
61+
})
62+
if err != nil {
63+
log.Error(err, "unable to set up manager")
64+
os.Exit(1)
65+
}
66+
log.Info("created manager", "manager", mgr)
67+
}
68+
5069
// This example adds a Runnable for the Manager to Start.
51-
func ExampleManager_Add() {
70+
func ExampleManager_add() {
5271
err := mgr.Add(manager.RunnableFunc(func(<-chan struct{}) error {
5372
// Do something
5473
return nil
@@ -60,7 +79,7 @@ func ExampleManager_Add() {
6079
}
6180

6281
// This example starts a Manager that has had Runnables added.
63-
func ExampleManager_Start() {
82+
func ExampleManager_start() {
6483
err := mgr.Start(signals.SetupSignalHandler())
6584
if err != nil {
6685
log.Error(err, "unable start the manager")

0 commit comments

Comments
 (0)