package handler

参考链接:
import "sigs.k8s.io/controller-runtime/pkg/handler"

该包处理程序定义了使协调入队的EventHandlers.Requests响应从Watching Kubernetes API中观察到的Create,Update,Deletion事件. 用户应向Controller.Watch提供source.Source和handler.EventHandler,以便生成和入列reconcile.Request工作项.

通常,对于大多数用例而言,以下预制的事件处理程序应已足够:

EventHandlers

1)EnqueueRequestForObject

使一个reconcile.Request入队,该请求包含Event中对象的名称和命名空间. 这将导致协调作为事件源的对象(例如,创建/删除/更新的对象).

2)EnqueueRequestForOwner

将包含事件中对象所有者的名称和名称空间的reconcile.Request入队. 这将导致协调作为事件源的对象的所有者(例如,创建对象的所有者对象).

3)EnqueueRequestsFromMapFunc

用户提供的转换函数产生的请求针对Event中的对象运行. 这将导致协调对象的任意集合(通过源对象的转换定义).

Index 

Examples 


一:type EnqueueRequestForObject struct{}

EnqueueRequestForObject使包含事件的源的对象的名称和名称空间的请求入队. (例如,创建/删除/更新的对象的名称和命名空间). 几乎所有具有关联资源(例如CRD)的控制器都使用handler.EnqueueRequestForObject来协调关联的资源.

本示例监视Pod,并使用事件中Pod的名称和命名空间使请求入队(即,由Create,Update,Delete引起的更改).

Code:

// controller is a controller.controller
err := c.Watch(
    &source.Kind{Type: &corev1.Pod{}},
    &handler.EnqueueRequestForObject{},
)
if err != nil {
    // handle it
}

1.创建实现EventHandler

func (*EnqueueRequestForObject) Create  Uses

2.删除工具EventHandler

func (*EnqueueRequestForObject) Delete  Uses

3.泛型实现EventHandler

func (*EnqueueRequestForObject) Generic  Uses

4.更新实现EventHandler

func (*EnqueueRequestForObject) Update  Uses

二:type EnqueueRequestForOwner struct {}

type EnqueueRequestForOwner struct {
    // OwnerType is the type of the Owner object to look for in OwnerReferences.  Only Group and Kind are compared.
    OwnerType runtime.Object

    // IsController if set will only look at the first OwnerReference with Controller: true.
    IsController bool
    // contains filtered or unexported fields
}

EnqueueRequestForOwner使对对象所有者的请求入队. 例如,创建作为事件源的对象的对象.

如果ReplicaSet创建Pod,则用户可以使用以下方法调和ReplicaSet以响应Pod事件:

-一个源.找到具有Pod类型的源.

-一个handler.EnqueueRequestForOwner EventHandler,其OwnerType为ReplicaSet且IsController设置为true.

本示例监视ReplicaSets并使其入队,该请求包含负责创建ReplicaSet的拥有(直接)部署的名称和命名空间.

Code:

// controller is a controller.controller
err := c.Watch(
    &source.Kind{Type: &appsv1.ReplicaSet{}},
    &handler.EnqueueRequestForOwner{
        OwnerType:    &appsv1.Deployment{},
        IsController: true,
    },
)
if err != nil {
    // handle it
}

 1.创建实现EventHandler

func (*EnqueueRequestForOwner) Create  Uses

2.删除工具EventHandler

func (*EnqueueRequestForOwner) Delete  Uses

3.泛型实现EventHandler

func (*EnqueueRequestForOwner) Generic  Uses

4.控制器调用InjectMapper来提供管理器使用的其余映射器.

func (*EnqueueRequestForOwner) InjectMapper  Uses

func (e *EnqueueRequestForOwner) InjectMapper(m meta.RESTMapper) error

5.控制器调用InjectScheme来为EnqueueRequestForOwner提供单例方案.

func (*EnqueueRequestForOwner) InjectScheme  Uses

func (e *EnqueueRequestForOwner) InjectScheme(s *runtime.Scheme) error

6.更新实现EventHandler

func (*EnqueueRequestForOwner) Update  Uses

 

 

type EventHandler  Uses

type EventHandler interface {
    // Create is called in response to an create event - e.g. Pod Creation.
    Create(event.CreateEvent, workqueue.RateLimitingInterface)

    // Update is called in response to an update event -  e.g. Pod Updated.
    Update(event.UpdateEvent, workqueue.RateLimitingInterface)

    // Delete is called in response to a delete event - e.g. Pod Deleted.
    Delete(event.DeleteEvent, workqueue.RateLimitingInterface)

    // Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or
    // external trigger request - e.g. reconcile Autoscaling, or a Webhook.
    Generic(event.GenericEvent, workqueue.RateLimitingInterface)
}

EventHandler使协调队列.响应事件的请求(例如Pod Create). EventHandlers为一个对象映射一个事件,以触发同一对象或不同对象的对帐-例如,如果存在Foo类型的事件(使用source.KindSource),则协调一个或多个Bar类型的对象.

完全一致.在调用协调之前,请求将通过排队机制进行批处理.

*使用EnqueueRequestForObject调和事件用于的对象-对控制器调和类型的事件执行此操作. (例如,部署控制器的部署)

*使用EnqueueRequestForOwner协调事件所针对的对象的所有者-对Controller创建的类型的事件执行此操作. (例如,由部署控制器创建的副本集)

*使用EnqueueRequestsFromMapFunc将对象的事件转换为其他类型的对象的协调-对Controller可能感兴趣但不创建的类型的事件执行此操作. (例如,如果Foo响应集群大小事件,则将Node事件映射到Foo对象.)

除非您实现自己的EventHandler,否则可以忽略EventHandler接口上的函数. 大多数用户不需要实现自己的EventHandler.

func EnqueueRequestsFromMapFunc  Uses

func EnqueueRequestsFromMapFunc(mapFN func(MapObject) []reconcile.Request) EventHandler

EnqueueRequestsFromMapFunc通过运行转换函数使请求入队,该转换函数在每个Event上输出reconcile.Requests的集合. reconcile.Requests可以用于由源Event的某些用户指定的转换定义的任意对象集. (例如,响应一组由于添加或删除节点而导致的集群调整大小事件,触发一组对象的协调器)

EnqueueRequestsFromMapFunc通常用于将更新从一个对象散发到不同类型的一个或多个其他对象.

对于同时包含新对象和旧对象的UpdateEvent,转换函数将在两个对象上运行,并且两组请求均入队.

 

本示例使用用户定义的映射功能监视Deployment并使请求入队,该请求包含不同对象(类型:MyKind)的名称和命名空间.

Code:

// controller is a controller.controller
err := c.Watch(
    &source.Kind{Type: &appsv1.Deployment{}},
    handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
        return []reconcile.Request{
            {NamespacedName: types.NamespacedName{
                Name:      a.Object.GetName() + "-1",
                Namespace: a.Object.GetNamespace(),
            }},
            {NamespacedName: types.NamespacedName{
                Name:      a.Object.GetName() + "-2",
                Namespace: a.Object.GetNamespace(),
            }},
        }
    }),
)
if err != nil {
    // handle it
}

 

type Funcs  Uses

type Funcs struct {
    // Create is called in response to an add event.  Defaults to no-op.
    // RateLimitingInterface is used to enqueue reconcile.Requests.
    CreateFunc func(event.CreateEvent, workqueue.RateLimitingInterface)

    // Update is called in response to an update event.  Defaults to no-op.
    // RateLimitingInterface is used to enqueue reconcile.Requests.
    UpdateFunc func(event.UpdateEvent, workqueue.RateLimitingInterface)

    // Delete is called in response to a delete event.  Defaults to no-op.
    // RateLimitingInterface is used to enqueue reconcile.Requests.
    DeleteFunc func(event.DeleteEvent, workqueue.RateLimitingInterface)

    // GenericFunc is called in response to a generic event.  Defaults to no-op.
    // RateLimitingInterface is used to enqueue reconcile.Requests.
    GenericFunc func(event.GenericEvent, workqueue.RateLimitingInterface)
}

Funcs实现EventHandler.

 

本示例实现handler.EnqueueRequestForObject.

Code:

// controller is a controller.controller
err := c.Watch(
    &source.Kind{Type: &corev1.Pod{}},
    handler.Funcs{
        CreateFunc: func(e event.CreateEvent, q workqueue.RateLimitingInterface) {
            q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
                Name:      e.Object.GetName(),
                Namespace: e.Object.GetNamespace(),
            }})
        },
        UpdateFunc: func(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
            q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
                Name:      e.ObjectNew.GetName(),
                Namespace: e.ObjectNew.GetNamespace(),
            }})
        },
        DeleteFunc: func(e event.DeleteEvent, q workqueue.RateLimitingInterface) {
            q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
                Name:      e.Object.GetName(),
                Namespace: e.Object.GetNamespace(),
            }})
        },
        GenericFunc: func(e event.GenericEvent, q workqueue.RateLimitingInterface) {
            q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
                Name:      e.Object.GetName(),
                Namespace: e.Object.GetNamespace(),
            }})
        },
    },
)
if err != nil {
    // handle it
}

 

func (Funcs) Create  Uses

创建实现EventHandler

func (Funcs) Delete  Uses

Delete implements EventHandler

func (Funcs) Generic  Uses

泛型实现EventHandler

func (Funcs) Update  Uses

更新实现EventHandler

type MapObject  Uses

type MapObject struct {
    Object controllerutil.Object
}

MapObject包含来自事件的信息,该事件将被转换为Request.

 

demo:

// Define a mapping from the object in the event to one or more
// objects to Reconcile
mapFn := handler.ToRequestsFunc(
    func(a handler.MapObject) []reconcile.Request {
        return []reconcile.Request{
            {NamespacedName: types.NamespacedName{
                Name:      a.Meta.GetName() + "-1",
                Namespace: a.Meta.GetNamespace(),
            }},
            {NamespacedName: types.NamespacedName{
                Name:      a.Meta.GetName() + "-2",
                Namespace: a.Meta.GetNamespace(),
            }},
        }
    })
// 'UpdateFunc' and 'CreateFunc' used to judge if a event about the object is
// what we want. If that is true, the event will be processed by the reconciler.
p := predicate.Funcs{
    UpdateFunc: func(e event.UpdateEvent) bool {
        // The object doesn't contain label "foo", so the event will be
        // ignored.
        if _, ok := e.MetaOld.GetLabels()["foo"]; !ok {
            return false
        }
        return e.ObjectOld != e.ObjectNew
    },
    CreateFunc: func(e event.CreateEvent) bool {
        if _, ok := e.Meta.GetLabels()["foo"]; !ok {
            return false
        }
        return true
    },
}
// Watch Deployments and trigger Reconciles for objects
// mapped from the Deployment in the event
err := c.Watch(
    &source.Kind{Type: &appsv1.Deployment{}},
    &handler.EnqueueRequestsFromMapFunc{
        ToRequests: mapFn,
    },
    // Comment it if default predicate fun is used.
    p)
if err != nil {
    return err
}

 

 

posted @ 2020-09-11 14:04  salami_china  阅读(853)  评论(0编辑  收藏  举报