Controller Watch Functions

    Link to reference documentation

    Controllers may watch Resources and trigger Reconcile calls with the key of theobject from the watch event.

    This example configures a controller to watch for Pod events, and call Reconcile withthe Pod key.

    1. err := c.Watch(
    2. &source.Kind{Type: &v1.Pod{}},
    3. if err != nil {
    4. return err

    Controllers may watch Resources of types they create and trigger Reconcile calls with the key ofthe Owner of the object.

    This example configures a Controller to watch for Pod events, and call Reconcile withthe Owner ReplicaSet key. This is done by looking up the object referred to by the Owner referencefrom the watch event object.

    • Define a function to lookup the Owner from the key
    • Call WatchControllerOf with the Owned object and the function to lookup the ownerIf Pod default/foo-pod was created by ReplicaSet default/foo-rs, and the Pod is(re)created, updated or deleted, then Reconcile will be called with namespace: default, name: foo-rs

    Note: This requires adding the following annotations to your Controller struct to ensure thecorrect RBAC rules are in place and informers have been started.

    1. // Annotation to generate RBAC roles to watch and update Pods
    2. // +kubebuilder:rbac:groups="",resources=pods,verbs=get;watch;list;create;update;delete

    Example: To respond to cluster scaling events (e.g. the deletion or addition of Nodes),a Controller would watch Nodes and map the watch events to keys of objects managed bythe controller.

    This simple example configures a Controller to watch for Pod events, and then reconciles objects withnames derived from the Pod's name.

    If Pod default/foo is created, updated or deleted, then Reconcile will be called fornamespace: default, name: foo-parent-1 and for namespace: default, name: foo-parent-2.

      Controllers may trigger Reconcile for events written to Channels. This is useful if the Controllerneeds to trigger a Reconcile in response to something other than a create / update / delete eventto a Kubernetes object. Note: in most situations this case is better handled by updating a Kubernetesobject with the external state that would trigger the Reconcile.

      1. events := make(chan event.GenericEvent)
      2. &source.Channel{Source: events},
      3. &handler.EnqueueRequestForObject{},
      4. )
      5. if err != nil {
      6. return err