...

Source file src/sigs.k8s.io/controller-runtime/pkg/handler/enqueue_mapped.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  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    26  )
    27  
    28  // MapFunc is the signature required for enqueueing requests from a generic function.
    29  // This type is usually used with EnqueueRequestsFromMapFunc when registering an event handler.
    30  type MapFunc = TypedMapFunc[client.Object]
    31  
    32  // TypedMapFunc is the signature required for enqueueing requests from a generic function.
    33  // This type is usually used with EnqueueRequestsFromTypedMapFunc when registering an event handler.
    34  //
    35  // TypedMapFunc is experimental and subject to future change.
    36  type TypedMapFunc[T any] func(context.Context, T) []reconcile.Request
    37  
    38  // EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
    39  // of reconcile.Requests on each Event.  The reconcile.Requests may be for an arbitrary set of objects
    40  // defined by some user specified transformation of the source Event.  (e.g. trigger Reconciler for a set of objects
    41  // in response to a cluster resize event caused by adding or deleting a Node)
    42  //
    43  // EnqueueRequestsFromMapFunc is frequently used to fan-out updates from one object to one or more other
    44  // objects of a differing type.
    45  //
    46  // For UpdateEvents which contain both a new and old object, the transformation function is run on both
    47  // objects and both sets of Requests are enqueue.
    48  func EnqueueRequestsFromMapFunc(fn MapFunc) EventHandler {
    49  	return TypedEnqueueRequestsFromMapFunc(fn)
    50  }
    51  
    52  // TypedEnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
    53  // of reconcile.Requests on each Event.  The reconcile.Requests may be for an arbitrary set of objects
    54  // defined by some user specified transformation of the source Event.  (e.g. trigger Reconciler for a set of objects
    55  // in response to a cluster resize event caused by adding or deleting a Node)
    56  //
    57  // TypedEnqueueRequestsFromMapFunc is frequently used to fan-out updates from one object to one or more other
    58  // objects of a differing type.
    59  //
    60  // For TypedUpdateEvents which contain both a new and old object, the transformation function is run on both
    61  // objects and both sets of Requests are enqueue.
    62  //
    63  // TypedEnqueueRequestsFromMapFunc is experimental and subject to future change.
    64  func TypedEnqueueRequestsFromMapFunc[T any](fn TypedMapFunc[T]) TypedEventHandler[T] {
    65  	return &enqueueRequestsFromMapFunc[T]{
    66  		toRequests: fn,
    67  	}
    68  }
    69  
    70  var _ EventHandler = &enqueueRequestsFromMapFunc[client.Object]{}
    71  
    72  type enqueueRequestsFromMapFunc[T any] struct {
    73  	// Mapper transforms the argument into a slice of keys to be reconciled
    74  	toRequests TypedMapFunc[T]
    75  }
    76  
    77  // Create implements EventHandler.
    78  func (e *enqueueRequestsFromMapFunc[T]) Create(ctx context.Context, evt event.TypedCreateEvent[T], q workqueue.RateLimitingInterface) {
    79  	reqs := map[reconcile.Request]empty{}
    80  	e.mapAndEnqueue(ctx, q, evt.Object, reqs)
    81  }
    82  
    83  // Update implements EventHandler.
    84  func (e *enqueueRequestsFromMapFunc[T]) Update(ctx context.Context, evt event.TypedUpdateEvent[T], q workqueue.RateLimitingInterface) {
    85  	reqs := map[reconcile.Request]empty{}
    86  	e.mapAndEnqueue(ctx, q, evt.ObjectOld, reqs)
    87  	e.mapAndEnqueue(ctx, q, evt.ObjectNew, reqs)
    88  }
    89  
    90  // Delete implements EventHandler.
    91  func (e *enqueueRequestsFromMapFunc[T]) Delete(ctx context.Context, evt event.TypedDeleteEvent[T], q workqueue.RateLimitingInterface) {
    92  	reqs := map[reconcile.Request]empty{}
    93  	e.mapAndEnqueue(ctx, q, evt.Object, reqs)
    94  }
    95  
    96  // Generic implements EventHandler.
    97  func (e *enqueueRequestsFromMapFunc[T]) Generic(ctx context.Context, evt event.TypedGenericEvent[T], q workqueue.RateLimitingInterface) {
    98  	reqs := map[reconcile.Request]empty{}
    99  	e.mapAndEnqueue(ctx, q, evt.Object, reqs)
   100  }
   101  
   102  func (e *enqueueRequestsFromMapFunc[T]) mapAndEnqueue(ctx context.Context, q workqueue.RateLimitingInterface, object T, reqs map[reconcile.Request]empty) {
   103  	for _, req := range e.toRequests(ctx, object) {
   104  		_, ok := reqs[req]
   105  		if !ok {
   106  			q.Add(req)
   107  			reqs[req] = empty{}
   108  		}
   109  	}
   110  }
   111  

View as plain text