...

Source file src/sigs.k8s.io/controller-runtime/pkg/handler/eventhandler.go

Documentation: sigs.k8s.io/controller-runtime/pkg/handler

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package handler
    18  
    19  import (
    20  	"context"
    21  
    22  	"k8s.io/client-go/util/workqueue"
    23  	"sigs.k8s.io/controller-runtime/pkg/client"
    24  	"sigs.k8s.io/controller-runtime/pkg/event"
    25  )
    26  
    27  // EventHandler enqueues reconcile.Requests in response to events (e.g. Pod Create).  EventHandlers map an Event
    28  // for one object to trigger Reconciles for either the same object or different objects - e.g. if there is an
    29  // Event for object with type Foo (using source.Kind) then reconcile one or more object(s) with type Bar.
    30  //
    31  // Identical reconcile.Requests will be batched together through the queuing mechanism before reconcile is called.
    32  //
    33  // * Use EnqueueRequestForObject to reconcile the object the event is for
    34  // - do this for events for the type the Controller Reconciles. (e.g. Deployment for a Deployment Controller)
    35  //
    36  // * Use EnqueueRequestForOwner to reconcile the owner of the object the event is for
    37  // - do this for events for the types the Controller creates.  (e.g. ReplicaSets created by a Deployment Controller)
    38  //
    39  // * Use EnqueueRequestsFromMapFunc to transform an event for an object to a reconcile of an object
    40  // of a different type - do this for events for types the Controller may be interested in, but doesn't create.
    41  // (e.g. If Foo responds to cluster size events, map Node events to Foo objects.)
    42  //
    43  // Unless you are implementing your own EventHandler, you can ignore the functions on the EventHandler interface.
    44  // Most users shouldn't need to implement their own EventHandler.
    45  type EventHandler TypedEventHandler[client.Object]
    46  
    47  // TypedEventHandler enqueues reconcile.Requests in response to events (e.g. Pod Create). TypedEventHandlers map an Event
    48  // for one object to trigger Reconciles for either the same object or different objects - e.g. if there is an
    49  // Event for object with type Foo (using source.Kind) then reconcile one or more object(s) with type Bar.
    50  //
    51  // Identical reconcile.Requests will be batched together through the queuing mechanism before reconcile is called.
    52  //
    53  // * Use TypedEnqueueRequestForObject to reconcile the object the event is for
    54  // - do this for events for the type the Controller Reconciles. (e.g. Deployment for a Deployment Controller)
    55  //
    56  // * Use TypedEnqueueRequestForOwner to reconcile the owner of the object the event is for
    57  // - do this for events for the types the Controller creates.  (e.g. ReplicaSets created by a Deployment Controller)
    58  //
    59  // * Use TypedEnqueueRequestsFromMapFunc to transform an event for an object to a reconcile of an object
    60  // of a different type - do this for events for types the Controller may be interested in, but doesn't create.
    61  // (e.g. If Foo responds to cluster size events, map Node events to Foo objects.)
    62  //
    63  // Unless you are implementing your own TypedEventHandler, you can ignore the functions on the TypedEventHandler interface.
    64  // Most users shouldn't need to implement their own TypedEventHandler.
    65  //
    66  // TypedEventHandler is experimental and subject to future change.
    67  type TypedEventHandler[T any] interface {
    68  	// Create is called in response to a create event - e.g. Pod Creation.
    69  	Create(context.Context, event.TypedCreateEvent[T], workqueue.RateLimitingInterface)
    70  
    71  	// Update is called in response to an update event -  e.g. Pod Updated.
    72  	Update(context.Context, event.TypedUpdateEvent[T], workqueue.RateLimitingInterface)
    73  
    74  	// Delete is called in response to a delete event - e.g. Pod Deleted.
    75  	Delete(context.Context, event.TypedDeleteEvent[T], workqueue.RateLimitingInterface)
    76  
    77  	// Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or
    78  	// external trigger request - e.g. reconcile Autoscaling, or a Webhook.
    79  	Generic(context.Context, event.TypedGenericEvent[T], workqueue.RateLimitingInterface)
    80  }
    81  
    82  var _ EventHandler = Funcs{}
    83  
    84  // Funcs implements eventhandler.
    85  type Funcs = TypedFuncs[client.Object]
    86  
    87  // TypedFuncs implements eventhandler.
    88  //
    89  // TypedFuncs is experimental and subject to future change.
    90  type TypedFuncs[T any] struct {
    91  	// Create is called in response to an add event.  Defaults to no-op.
    92  	// RateLimitingInterface is used to enqueue reconcile.Requests.
    93  	CreateFunc func(context.Context, event.TypedCreateEvent[T], workqueue.RateLimitingInterface)
    94  
    95  	// Update is called in response to an update event.  Defaults to no-op.
    96  	// RateLimitingInterface is used to enqueue reconcile.Requests.
    97  	UpdateFunc func(context.Context, event.TypedUpdateEvent[T], workqueue.RateLimitingInterface)
    98  
    99  	// Delete is called in response to a delete event.  Defaults to no-op.
   100  	// RateLimitingInterface is used to enqueue reconcile.Requests.
   101  	DeleteFunc func(context.Context, event.TypedDeleteEvent[T], workqueue.RateLimitingInterface)
   102  
   103  	// GenericFunc is called in response to a generic event.  Defaults to no-op.
   104  	// RateLimitingInterface is used to enqueue reconcile.Requests.
   105  	GenericFunc func(context.Context, event.TypedGenericEvent[T], workqueue.RateLimitingInterface)
   106  }
   107  
   108  // Create implements EventHandler.
   109  func (h TypedFuncs[T]) Create(ctx context.Context, e event.TypedCreateEvent[T], q workqueue.RateLimitingInterface) {
   110  	if h.CreateFunc != nil {
   111  		h.CreateFunc(ctx, e, q)
   112  	}
   113  }
   114  
   115  // Delete implements EventHandler.
   116  func (h TypedFuncs[T]) Delete(ctx context.Context, e event.TypedDeleteEvent[T], q workqueue.RateLimitingInterface) {
   117  	if h.DeleteFunc != nil {
   118  		h.DeleteFunc(ctx, e, q)
   119  	}
   120  }
   121  
   122  // Update implements EventHandler.
   123  func (h TypedFuncs[T]) Update(ctx context.Context, e event.TypedUpdateEvent[T], q workqueue.RateLimitingInterface) {
   124  	if h.UpdateFunc != nil {
   125  		h.UpdateFunc(ctx, e, q)
   126  	}
   127  }
   128  
   129  // Generic implements EventHandler.
   130  func (h TypedFuncs[T]) Generic(ctx context.Context, e event.TypedGenericEvent[T], q workqueue.RateLimitingInterface) {
   131  	if h.GenericFunc != nil {
   132  		h.GenericFunc(ctx, e, q)
   133  	}
   134  }
   135  

View as plain text