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"
|
2024-05-13 20:57:03 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2020-10-21 05:49:41 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/event"
|
2024-08-19 08:02:11 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
2020-10-21 05:49:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EventHandler enqueues reconcile.Requests in response to events (e.g. Pod Create). EventHandlers map an Event
|
|
|
|
// for one object to trigger Reconciles for either the same object or different objects - e.g. if there is an
|
2024-05-13 20:57:03 +00:00
|
|
|
// Event for object with type Foo (using source.Kind) then reconcile one or more object(s) with type Bar.
|
2020-10-21 05:49:41 +00:00
|
|
|
//
|
|
|
|
// Identical reconcile.Requests will be batched together through the queuing mechanism before reconcile is called.
|
|
|
|
//
|
|
|
|
// * Use EnqueueRequestForObject to reconcile the object the event is for
|
|
|
|
// - do this for events for the type the Controller Reconciles. (e.g. Deployment for a Deployment Controller)
|
|
|
|
//
|
|
|
|
// * Use EnqueueRequestForOwner to reconcile the owner of the object the event is for
|
|
|
|
// - do this for events for the types the Controller creates. (e.g. ReplicaSets created by a Deployment Controller)
|
|
|
|
//
|
|
|
|
// * Use EnqueueRequestsFromMapFunc to transform an event for an object to a reconcile of an object
|
|
|
|
// of a different type - do this for events for types the Controller may be interested in, but doesn't create.
|
|
|
|
// (e.g. If Foo responds to cluster size events, map Node events to Foo objects.)
|
|
|
|
//
|
|
|
|
// Unless you are implementing your own EventHandler, you can ignore the functions on the EventHandler interface.
|
|
|
|
// Most users shouldn't need to implement their own EventHandler.
|
2024-08-19 08:02:11 +00:00
|
|
|
type EventHandler = TypedEventHandler[client.Object, reconcile.Request]
|
2024-05-13 20:57:03 +00:00
|
|
|
|
|
|
|
// TypedEventHandler enqueues reconcile.Requests in response to events (e.g. Pod Create). TypedEventHandlers map an Event
|
|
|
|
// for one object to trigger Reconciles for either the same object or different objects - e.g. if there is an
|
|
|
|
// Event for object with type Foo (using source.Kind) then reconcile one or more object(s) with type Bar.
|
|
|
|
//
|
|
|
|
// Identical reconcile.Requests will be batched together through the queuing mechanism before reconcile is called.
|
|
|
|
//
|
|
|
|
// * Use TypedEnqueueRequestForObject to reconcile the object the event is for
|
|
|
|
// - do this for events for the type the Controller Reconciles. (e.g. Deployment for a Deployment Controller)
|
|
|
|
//
|
|
|
|
// * Use TypedEnqueueRequestForOwner to reconcile the owner of the object the event is for
|
|
|
|
// - do this for events for the types the Controller creates. (e.g. ReplicaSets created by a Deployment Controller)
|
|
|
|
//
|
|
|
|
// * Use TypedEnqueueRequestsFromMapFunc to transform an event for an object to a reconcile of an object
|
|
|
|
// of a different type - do this for events for types the Controller may be interested in, but doesn't create.
|
|
|
|
// (e.g. If Foo responds to cluster size events, map Node events to Foo objects.)
|
|
|
|
//
|
|
|
|
// Unless you are implementing your own TypedEventHandler, you can ignore the functions on the TypedEventHandler interface.
|
|
|
|
// Most users shouldn't need to implement their own TypedEventHandler.
|
|
|
|
//
|
|
|
|
// TypedEventHandler is experimental and subject to future change.
|
2024-08-19 08:02:11 +00:00
|
|
|
type TypedEventHandler[object any, request comparable] interface {
|
2024-02-01 13:08:54 +00:00
|
|
|
// Create is called in response to a create event - e.g. Pod Creation.
|
2024-08-19 08:02:11 +00:00
|
|
|
Create(context.Context, event.TypedCreateEvent[object], workqueue.TypedRateLimitingInterface[request])
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// Update is called in response to an update event - e.g. Pod Updated.
|
2024-08-19 08:02:11 +00:00
|
|
|
Update(context.Context, event.TypedUpdateEvent[object], workqueue.TypedRateLimitingInterface[request])
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// Delete is called in response to a delete event - e.g. Pod Deleted.
|
2024-08-19 08:02:11 +00:00
|
|
|
Delete(context.Context, event.TypedDeleteEvent[object], workqueue.TypedRateLimitingInterface[request])
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// 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.
|
2024-08-19 08:02:11 +00:00
|
|
|
Generic(context.Context, event.TypedGenericEvent[object], workqueue.TypedRateLimitingInterface[request])
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ EventHandler = Funcs{}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
// Funcs implements eventhandler.
|
2024-08-19 08:02:11 +00:00
|
|
|
type Funcs = TypedFuncs[client.Object, reconcile.Request]
|
2024-05-13 20:57:03 +00:00
|
|
|
|
|
|
|
// TypedFuncs implements eventhandler.
|
|
|
|
//
|
|
|
|
// TypedFuncs is experimental and subject to future change.
|
2024-08-19 08:02:11 +00:00
|
|
|
type TypedFuncs[object any, request comparable] struct {
|
2020-10-21 05:49:41 +00:00
|
|
|
// Create is called in response to an add event. Defaults to no-op.
|
|
|
|
// RateLimitingInterface is used to enqueue reconcile.Requests.
|
2024-08-19 08:02:11 +00:00
|
|
|
CreateFunc func(context.Context, event.TypedCreateEvent[object], workqueue.TypedRateLimitingInterface[request])
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// Update is called in response to an update event. Defaults to no-op.
|
|
|
|
// RateLimitingInterface is used to enqueue reconcile.Requests.
|
2024-08-19 08:02:11 +00:00
|
|
|
UpdateFunc func(context.Context, event.TypedUpdateEvent[object], workqueue.TypedRateLimitingInterface[request])
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// Delete is called in response to a delete event. Defaults to no-op.
|
|
|
|
// RateLimitingInterface is used to enqueue reconcile.Requests.
|
2024-08-19 08:02:11 +00:00
|
|
|
DeleteFunc func(context.Context, event.TypedDeleteEvent[object], workqueue.TypedRateLimitingInterface[request])
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// GenericFunc is called in response to a generic event. Defaults to no-op.
|
|
|
|
// RateLimitingInterface is used to enqueue reconcile.Requests.
|
2024-08-19 08:02:11 +00:00
|
|
|
GenericFunc func(context.Context, event.TypedGenericEvent[object], workqueue.TypedRateLimitingInterface[request])
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Create implements EventHandler.
|
2024-08-19 08:02:11 +00:00
|
|
|
func (h TypedFuncs[object, request]) Create(ctx context.Context, e event.TypedCreateEvent[object], q workqueue.TypedRateLimitingInterface[request]) {
|
2020-10-21 05:49:41 +00:00
|
|
|
if h.CreateFunc != nil {
|
2023-06-01 17:01:19 +00:00
|
|
|
h.CreateFunc(ctx, e, q)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Delete implements EventHandler.
|
2024-08-19 08:02:11 +00:00
|
|
|
func (h TypedFuncs[object, request]) Delete(ctx context.Context, e event.TypedDeleteEvent[object], q workqueue.TypedRateLimitingInterface[request]) {
|
2020-10-21 05:49:41 +00:00
|
|
|
if h.DeleteFunc != nil {
|
2023-06-01 17:01:19 +00:00
|
|
|
h.DeleteFunc(ctx, e, q)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Update implements EventHandler.
|
2024-08-19 08:02:11 +00:00
|
|
|
func (h TypedFuncs[object, request]) Update(ctx context.Context, e event.TypedUpdateEvent[object], q workqueue.TypedRateLimitingInterface[request]) {
|
2020-10-21 05:49:41 +00:00
|
|
|
if h.UpdateFunc != nil {
|
2023-06-01 17:01:19 +00:00
|
|
|
h.UpdateFunc(ctx, e, q)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Generic implements EventHandler.
|
2024-08-19 08:02:11 +00:00
|
|
|
func (h TypedFuncs[object, request]) Generic(ctx context.Context, e event.TypedGenericEvent[object], q workqueue.TypedRateLimitingInterface[request]) {
|
2020-10-21 05:49:41 +00:00
|
|
|
if h.GenericFunc != nil {
|
2023-06-01 17:01:19 +00:00
|
|
|
h.GenericFunc(ctx, e, q)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|