Documentation ¶
Overview ¶
Package controller contains a reusable abstraction for efficiently watching for changes in resources in a Kubernetes cluster.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TestControllerRun ¶
func TestControllerRun(r Resource) func()
TestControllerRun takes the given Resource and runs the Controller. The returned function should be called to stop the controller. The returned function will block until the controller stops.
Types ¶
type Backgrounder ¶
type Backgrounder interface {
Run(<-chan struct{})
}
Backgrounder should be implemented by a Resource that requires additional background processing. If a Resource implements this, then the Controller will automatically Run the Backgrounder for the duration of the controller.
The channel will be closed when the Controller is quitting. The Controller will block until the Backgrounder completes.
type Controller ¶
type Controller struct { Log hclog.Logger Resource Resource // contains filtered or unexported fields }
Controller is a generic cache.Controller implementation that watches Kubernetes for changes to specific set of resources and calls the configured callbacks as data changes.
func (*Controller) HasSynced ¶
func (c *Controller) HasSynced() bool
HasSynced implements cache.Controller.
func (*Controller) LastSyncResourceVersion ¶
func (c *Controller) LastSyncResourceVersion() string
LastSyncResourceVersion implements cache.Controller.
func (*Controller) Run ¶
func (c *Controller) Run(stopCh <-chan struct{})
Run starts the Controller and blocks until stopCh is closed.
Important: Callers must ensure that Run is only called once at a time.
type Event ¶
type Event struct { // Key is in the form of <namespace>/<name>, e.g. default/pod-abc123, // and corresponds to the resource modified. Key string // Obj holds the resource that was modified at the time of the event // occurring. If possible, the resource should be retrieved from the informer // cache, instead of using this field because the cache will be more up to // date at the time the event is processed. // In some cases, such as a delete event, the resource will no longer exist // in the cache and then it is useful to have the resource here. Obj interface{} }
Event is something that occurred to the resources we're watching.
type Resource ¶
type Resource interface { // Informer returns the SharedIndexInformer that the controller will // use to watch for changes. An Informer is the long-running task that // holds blocking queries to K8S and stores data in a local store. Informer() cache.SharedIndexInformer // Upsert is the callback called when processing the queue // of changes from the Informer. If an error is returned, the given item // will be retried. Upsert(key string, obj interface{}) error // Delete is called on object deletion. // obj is the last known state of the object before deletion. In some // cases, it may not be up to date with the latest state of the object. // If an error is returned, the given item will be retried. Delete(key string, obj interface{}) error }
Resource should be implemented by anything that should be watchable by Controller. The Resource needs to be aware of how to create the Informer that is responsible for making API calls as well as what to do on Upsert and Delete.
func NewResource ¶
func NewResource( informer cache.SharedIndexInformer, upsert ResourceUpsertFunc, delete ResourceDeleteFunc, ) Resource
NewResource returns a Resource implementation for the given informer, upsert handler, and delete handler.
type ResourceDeleteFunc ¶
type ResourceUpsertFunc ¶
ResourceUpsertFunc and ResourceDeleteFunc are the callback types for when a resource is inserted, updated, or deleted.