Skip to content

Commit

Permalink
Adding more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Hurley committed Jan 23, 2019
1 parent 2f6a0b3 commit 5c59791
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 5 additions & 2 deletions pkg/cache/multi_namespace_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
23 changes: 21 additions & 2 deletions pkg/manager/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit 5c59791

Please sign in to comment.