mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-14 02:10:21 +00:00
b1a4590967
There is no release for sigs.k8s.io/controller-runtime that supports Kubernetes v1.27. The main branch has all the required modifications, so we can use that for the time being. Signed-off-by: Niels de Vos <ndevos@ibm.com>
89 lines
3.3 KiB
Go
89 lines
3.3 KiB
Go
/*
|
|
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 (
|
|
"context"
|
|
|
|
"k8s.io/client-go/util/workqueue"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/event"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
)
|
|
|
|
// MapFunc is the signature required for enqueueing requests from a generic function.
|
|
// This type is usually used with EnqueueRequestsFromMapFunc when registering an event handler.
|
|
type MapFunc func(context.Context, client.Object) []reconcile.Request
|
|
|
|
// 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.
|
|
func EnqueueRequestsFromMapFunc(fn MapFunc) EventHandler {
|
|
return &enqueueRequestsFromMapFunc{
|
|
toRequests: fn,
|
|
}
|
|
}
|
|
|
|
var _ EventHandler = &enqueueRequestsFromMapFunc{}
|
|
|
|
type enqueueRequestsFromMapFunc struct {
|
|
// Mapper transforms the argument into a slice of keys to be reconciled
|
|
toRequests MapFunc
|
|
}
|
|
|
|
// Create implements EventHandler.
|
|
func (e *enqueueRequestsFromMapFunc) Create(ctx context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
|
|
reqs := map[reconcile.Request]empty{}
|
|
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
|
|
}
|
|
|
|
// Update implements EventHandler.
|
|
func (e *enqueueRequestsFromMapFunc) Update(ctx context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
|
|
reqs := map[reconcile.Request]empty{}
|
|
e.mapAndEnqueue(ctx, q, evt.ObjectOld, reqs)
|
|
e.mapAndEnqueue(ctx, q, evt.ObjectNew, reqs)
|
|
}
|
|
|
|
// Delete implements EventHandler.
|
|
func (e *enqueueRequestsFromMapFunc) Delete(ctx context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
|
|
reqs := map[reconcile.Request]empty{}
|
|
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
|
|
}
|
|
|
|
// Generic implements EventHandler.
|
|
func (e *enqueueRequestsFromMapFunc) Generic(ctx context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) {
|
|
reqs := map[reconcile.Request]empty{}
|
|
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
|
|
}
|
|
|
|
func (e *enqueueRequestsFromMapFunc) mapAndEnqueue(ctx context.Context, q workqueue.RateLimitingInterface, object client.Object, reqs map[reconcile.Request]empty) {
|
|
for _, req := range e.toRequests(ctx, object) {
|
|
_, ok := reqs[req]
|
|
if !ok {
|
|
q.Add(req)
|
|
reqs[req] = empty{}
|
|
}
|
|
}
|
|
}
|