2020-10-21 05:49:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-06-01 17:01:19 +00:00
|
|
|
"context"
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
"k8s.io/client-go/util/workqueue"
|
2021-06-25 05:02:01 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2020-10-21 05:49:41 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/event"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
)
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// MapFunc is the signature required for enqueueing requests from a generic function.
|
|
|
|
// This type is usually used with EnqueueRequestsFromMapFunc when registering an event handler.
|
2024-05-13 20:57:03 +00:00
|
|
|
type MapFunc = TypedMapFunc[client.Object]
|
|
|
|
|
|
|
|
// TypedMapFunc is the signature required for enqueueing requests from a generic function.
|
|
|
|
// This type is usually used with EnqueueRequestsFromTypedMapFunc when registering an event handler.
|
|
|
|
//
|
|
|
|
// TypedMapFunc is experimental and subject to future change.
|
|
|
|
type TypedMapFunc[T any] func(context.Context, T) []reconcile.Request
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
|
|
|
|
// of reconcile.Requests on each Event. The reconcile.Requests may be for an arbitrary set of objects
|
|
|
|
// defined by some user specified transformation of the source Event. (e.g. trigger Reconciler for a set of objects
|
|
|
|
// in response to a cluster resize event caused by adding or deleting a Node)
|
|
|
|
//
|
|
|
|
// EnqueueRequestsFromMapFunc is frequently used to fan-out updates from one object to one or more other
|
|
|
|
// objects of a differing type.
|
|
|
|
//
|
|
|
|
// For UpdateEvents which contain both a new and old object, the transformation function is run on both
|
|
|
|
// objects and both sets of Requests are enqueue.
|
2021-06-25 05:02:01 +00:00
|
|
|
func EnqueueRequestsFromMapFunc(fn MapFunc) EventHandler {
|
2024-05-13 20:57:03 +00:00
|
|
|
return TypedEnqueueRequestsFromMapFunc(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TypedEnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
|
|
|
|
// of reconcile.Requests on each Event. The reconcile.Requests may be for an arbitrary set of objects
|
|
|
|
// defined by some user specified transformation of the source Event. (e.g. trigger Reconciler for a set of objects
|
|
|
|
// in response to a cluster resize event caused by adding or deleting a Node)
|
|
|
|
//
|
|
|
|
// TypedEnqueueRequestsFromMapFunc is frequently used to fan-out updates from one object to one or more other
|
|
|
|
// objects of a differing type.
|
|
|
|
//
|
|
|
|
// For TypedUpdateEvents which contain both a new and old object, the transformation function is run on both
|
|
|
|
// objects and both sets of Requests are enqueue.
|
|
|
|
//
|
|
|
|
// TypedEnqueueRequestsFromMapFunc is experimental and subject to future change.
|
|
|
|
func TypedEnqueueRequestsFromMapFunc[T any](fn TypedMapFunc[T]) TypedEventHandler[T] {
|
|
|
|
return &enqueueRequestsFromMapFunc[T]{
|
2021-06-25 05:02:01 +00:00
|
|
|
toRequests: fn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
var _ EventHandler = &enqueueRequestsFromMapFunc[client.Object]{}
|
2021-06-25 05:02:01 +00:00
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
type enqueueRequestsFromMapFunc[T any] struct {
|
2020-10-21 05:49:41 +00:00
|
|
|
// Mapper transforms the argument into a slice of keys to be reconciled
|
2024-05-13 20:57:03 +00:00
|
|
|
toRequests TypedMapFunc[T]
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Create implements EventHandler.
|
2024-05-13 20:57:03 +00:00
|
|
|
func (e *enqueueRequestsFromMapFunc[T]) Create(ctx context.Context, evt event.TypedCreateEvent[T], q workqueue.RateLimitingInterface) {
|
2021-06-25 05:02:01 +00:00
|
|
|
reqs := map[reconcile.Request]empty{}
|
2023-06-01 17:01:19 +00:00
|
|
|
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Update implements EventHandler.
|
2024-05-13 20:57:03 +00:00
|
|
|
func (e *enqueueRequestsFromMapFunc[T]) Update(ctx context.Context, evt event.TypedUpdateEvent[T], q workqueue.RateLimitingInterface) {
|
2021-06-25 05:02:01 +00:00
|
|
|
reqs := map[reconcile.Request]empty{}
|
2023-06-01 17:01:19 +00:00
|
|
|
e.mapAndEnqueue(ctx, q, evt.ObjectOld, reqs)
|
|
|
|
e.mapAndEnqueue(ctx, q, evt.ObjectNew, reqs)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Delete implements EventHandler.
|
2024-05-13 20:57:03 +00:00
|
|
|
func (e *enqueueRequestsFromMapFunc[T]) Delete(ctx context.Context, evt event.TypedDeleteEvent[T], q workqueue.RateLimitingInterface) {
|
2021-06-25 05:02:01 +00:00
|
|
|
reqs := map[reconcile.Request]empty{}
|
2023-06-01 17:01:19 +00:00
|
|
|
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Generic implements EventHandler.
|
2024-05-13 20:57:03 +00:00
|
|
|
func (e *enqueueRequestsFromMapFunc[T]) Generic(ctx context.Context, evt event.TypedGenericEvent[T], q workqueue.RateLimitingInterface) {
|
2021-06-25 05:02:01 +00:00
|
|
|
reqs := map[reconcile.Request]empty{}
|
2023-06-01 17:01:19 +00:00
|
|
|
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
func (e *enqueueRequestsFromMapFunc[T]) mapAndEnqueue(ctx context.Context, q workqueue.RateLimitingInterface, object T, reqs map[reconcile.Request]empty) {
|
2023-06-01 17:01:19 +00:00
|
|
|
for _, req := range e.toRequests(ctx, object) {
|
2021-06-25 05:02:01 +00:00
|
|
|
_, ok := reqs[req]
|
|
|
|
if !ok {
|
|
|
|
q.Add(req)
|
|
|
|
reqs[req] = empty{}
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|