manager package - sigs.k8s.io/controller-runtime/pkg/manager - Go Packages (original) (raw)
Package manager is required to create Controllers and provides shared dependencies such as clients, caches, schemes, etc. Controllers must be started by calling Manager.Start.
This section is empty.
This section is empty.
This section is empty.
BaseContextFunc is a function used to provide a base Context to Runnables managed by a Manager.
type LeaderElectionRunnable interface {
NeedLeaderElection() [bool](/builtin#bool)
}
LeaderElectionRunnable knows if a Runnable needs to be run in the leader election mode.
Manager initializes shared dependencies such as Caches and Clients, and provides them to Runnables. A Manager is required to create Controllers.
This example adds a Runnable for the Manager to Start.
package main
import ( "context" "os"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
var ( mgr manager.Manager
log = logf.Log.WithName("manager-examples")
)
func main() { err := mgr.Add(manager.RunnableFunc(func(context.Context) error { // Do something return nil })) if err != nil { log.Error(err, "unable add a runnable to the manager") os.Exit(1) } }
Output:
This example starts a Manager that has had Runnables added.
package main
import ( "os"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
var ( mgr manager.Manager // NB: don't call SetLogger in init(), or else you'll mess up logging in the main suite. log = logf.Log.WithName("manager-examples") )
func main() { if err := mgr.Start(signals.SetupSignalHandler()); err != nil { log.Error(err, "unable start the manager") os.Exit(1) } }
Output:
New returns a new Manager for creating Controllers. Note that if ContentType in the given config is not set, "application/vnd.kubernetes.protobuf" will be used for all built-in resources of Kubernetes, and "application/json" is for other types including all CRD resources.
This example creates a new Manager that can be used with controller.New to create Controllers.
package main
import ( "os"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
var
// NB: don't call SetLogger in init(), or else you'll mess up logging in the main suite. log = logf.Log.WithName("manager-examples")
func main() { cfg, err := config.GetConfig() if err != nil { log.Error(err, "unable to get kubeconfig") os.Exit(1) }
mgr, err := manager.New(cfg, manager.Options{})
if err != nil {
log.Error(err, "unable to set up manager")
os.Exit(1)
}
log.Info("created manager", "manager", mgr)
}
Output:
This example creates a new Manager that has a cache scoped to a list of namespaces.
package main
import ( "os"
"k8s.io/client-go/rest"
"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"
)
var
// NB: don't call SetLogger in init(), or else you'll mess up logging in the main suite. log = logf.Log.WithName("manager-examples")
func main() { 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: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
opts.DefaultNamespaces = map[string]cache.Config{
"namespace1": {},
"namespace2": {},
}
return cache.New(config, opts)
}},
)
if err != nil {
log.Error(err, "unable to set up manager")
os.Exit(1)
}
log.Info("created manager", "manager", mgr)
}
Output:
Options are the arguments for creating a new Manager.
Runnable allows a component to be started. It's very important that Start blocks until it's done running.
RunnableFunc implements Runnable using a function. It's very important that the given function block until it's done running.
Start implements Runnable.
Server is a general purpose HTTP server Runnable for a manager. It is used to serve some internal handlers for health probes and profiling, but it can also be used to run custom servers.
func (s *Server) NeedLeaderElection() bool
NeedLeaderElection returns true if the server should only be started when the manager is the leader.
Start starts the server. It will block until the server is stopped or an error occurs.