Documentation ¶
Overview ¶
Package controllerutil implements a ControllerManager type that is used to manage and run several Kubernetes controllers as part of a single process.
Controller are registered with the ControllerManager via the Register method and the group of controllers are run using the Run method on the ControllerManager.
Example (CustomResourceDefinition) ¶
package main import ( "context" "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clientset "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/record" "github.com/ianlewis/controllerutil" "github.com/ianlewis/controllerutil/controller" "github.com/ianlewis/controllerutil/logging" // CRD type definitions examplev1 "github.com/ianlewis/controllerutil/example/pkg/apis/example.com/v1" // Generated CRD client fooclientset "github.com/ianlewis/controllerutil/example/pkg/client/clientset/versioned" // Generated CRD informer fooinformers "github.com/ianlewis/controllerutil/example/pkg/client/informers/externalversions/example/v1" ) // FooController is a controller that watches Foo custom resource instances. type FooController struct { client clientset.Interface fooClient fooclientset.Interface fooInformer cache.SharedIndexInformer recorder record.EventRecorder l *logging.Logger } func (c *FooController) Run(ctx context.Context) error { // run control loop return nil } // NewFooController creates a new controller instance func NewFooController( client clientset.Interface, fooClient fooclientset.Interface, fooInformer cache.SharedIndexInformer, recorder record.EventRecorder, l *logging.Logger, ) *FooController { c := &FooController{ client: client, fooClient: fooClient, fooInformer: fooInformer, recorder: recorder, l: l, } // Attach event handlers // fooInformer.AddEventHandler(...) return c } func main() { // Initialize an in-cluster Kubernetes client config, _ := rest.InClusterConfig() client, _ := clientset.NewForConfig(config) // Create the client for the custom resource definition (CRD) "Foo" fooclient, err := fooclientset.NewForConfig(config) if err != nil { // handle error } // Create a new ControllerManager instance m := controllerutil.NewControllerManager("foo", client) // Register the foo controller. m.Register("foo", func(ctx *controller.Context) controller.Interface { return NewFooController( // ctx.Client is the same client passed to controllerutil.New ctx.Client, // fooclient is the CRD client instance fooclient, // ctx.SharedInformers manages lifecycle of all shared informers // InformerFor registers the informer for the given type if it hasn't been registered already. ctx.SharedInformers.InformerFor( &examplev1.Foo{}, func() cache.SharedIndexInformer { return fooinformers.NewFooInformer( fooclient, metav1.NamespaceAll, 12*time.Hour, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, ) }, ), // ctx.Recorder is used for recording Kubernetes events. ctx.Recorder, // ctx.Logger is a convenient wrapper used for logging. ctx.Logger, ) }) if err := m.Run(context.Background()); err != nil { // handle error } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ControllerManager ¶
type ControllerManager struct {
// contains filtered or unexported fields
}
ControllerManager manages a set of controllers registered to it. It manages their lifecycle and the lifecycle of informers that controllers use.
Example ¶
package main import ( "context" clientset "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "github.com/ianlewis/controllerutil" ) func main() { config, _ := rest.InClusterConfig() client, _ := clientset.NewForConfig(config) m := controllerutil.NewControllerManager("hoge", client) err := m.Run(context.Background()) if err != nil { // handle error } }
Output:
func NewControllerManager ¶
func NewControllerManager(name string, client clientset.Interface) *ControllerManager
New creates a new controller manager.
func (*ControllerManager) Register ¶
func (m *ControllerManager) Register(name string, c controller.Constructor)
Register registers a controller created by the given constructor by the given name. The given name should be unique to the controller and is used in Kubernetes event recorders and logging.
Example ¶
package main import ( "context" clientset "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" "github.com/ianlewis/controllerutil" "github.com/ianlewis/controllerutil/controller" "github.com/ianlewis/controllerutil/logging" ) type HogeController struct { client clientset.Interface recorder record.EventRecorder l *logging.Logger } func (c *HogeController) Run(ctx context.Context) error { return nil } func main() { config, _ := rest.InClusterConfig() client, _ := clientset.NewForConfig(config) m := controllerutil.NewControllerManager("hoge", client) // Register the hoge controller. ctx is a controller context. m.Register("hoge", func(ctx *controller.Context) controller.Interface { return &HogeController{ // ctx.Client is the same client passed to controllerutil.New client: ctx.Client, // ctx.Recorder is used for recording Kubernetes events. recorder: ctx.Recorder, // ctx.InfoLogger is a convenient wrapper used for logging. l: ctx.Logger, } }) }
Output:
func (*ControllerManager) Run ¶
func (m *ControllerManager) Run(ctx context.Context) error
Run starts all controllers and informers registered to the ControllerManager instance. ControllerManager assumes all registered controllers are essential to proper functioning. ControllerManager cancels all controllers and returns the first error returned by the controllers in the event of a failure. ControllerManager will not attempt to restart controllers and will simply return. As a best practice the calling process should log the returned error message and exit. It is assumed that the controller manager will be run by a supervisor process like supervisord or the Kubernetes kubelet and will be restarted gracefully if fatal errors occur.
The ControllerManager starts shared informers and waits for their caches to be synced before starting controllers. Controllers do not need to wait for informers to sync.
Directories ¶
Path | Synopsis |
---|---|
Package controller provides interfaces that are used to define and construct controller instances.
|
Package controller provides interfaces that are used to define and construct controller instances. |
pkg/apis/example.com/v1
Package v1 is the v1 version of the API.
|
Package v1 is the v1 version of the API. |
pkg/client/clientset/versioned/typed/example/v1
This package has the automatically generated typed clients.
|
This package has the automatically generated typed clients. |
pkg/client/clientset/versioned/typed/example/v1/fake
Package fake has the automatically generated clients.
|
Package fake has the automatically generated clients. |
Package logging provides standard log.Logger wrappers around the github.com/golang/glog package.
|
Package logging provides standard log.Logger wrappers around the github.com/golang/glog package. |