|
| 1 | +/* |
| 2 | +Copyright 2024 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "reflect" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/types" |
| 24 | + "k8s.io/client-go/util/workqueue" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/cluster" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 30 | +) |
| 31 | + |
| 32 | +var _ handler.TypedDeepCopyableEventHandler[client.Object, clusterRequest] = &EnqueueClusterRequestForObject{} |
| 33 | + |
| 34 | +type EnqueueClusterRequestForObject = TypedEnqueueClusterRequestForObject[client.Object] |
| 35 | + |
| 36 | +type TypedEnqueueClusterRequestForObject[object client.Object] struct { |
| 37 | + clusterName string |
| 38 | +} |
| 39 | + |
| 40 | +// Create implements EventHandler. |
| 41 | +func (e *TypedEnqueueClusterRequestForObject[T]) Create(ctx context.Context, evt event.TypedCreateEvent[T], q workqueue.TypedRateLimitingInterface[clusterRequest]) { |
| 42 | + if isNil(evt.Object) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + q.Add(clusterRequest{ |
| 47 | + ClusterName: e.clusterName, |
| 48 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 49 | + Name: evt.Object.GetName(), |
| 50 | + Namespace: evt.Object.GetNamespace(), |
| 51 | + }}}) |
| 52 | +} |
| 53 | + |
| 54 | +// Update implements EventHandler. |
| 55 | +func (e *TypedEnqueueClusterRequestForObject[T]) Update(ctx context.Context, evt event.TypedUpdateEvent[T], q workqueue.TypedRateLimitingInterface[clusterRequest]) { |
| 56 | + |
| 57 | + switch { |
| 58 | + case !isNil(evt.ObjectNew): |
| 59 | + q.Add(clusterRequest{ |
| 60 | + ClusterName: e.clusterName, |
| 61 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 62 | + Name: evt.ObjectNew.GetName(), |
| 63 | + Namespace: evt.ObjectNew.GetNamespace(), |
| 64 | + }}}) |
| 65 | + case !isNil(evt.ObjectOld): |
| 66 | + q.Add(clusterRequest{ |
| 67 | + ClusterName: e.clusterName, |
| 68 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 69 | + Name: evt.ObjectOld.GetName(), |
| 70 | + Namespace: evt.ObjectOld.GetNamespace(), |
| 71 | + }}}) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// Delete implements EventHandler. |
| 76 | +func (e *TypedEnqueueClusterRequestForObject[T]) Delete(ctx context.Context, evt event.TypedDeleteEvent[T], q workqueue.TypedRateLimitingInterface[clusterRequest]) { |
| 77 | + if isNil(evt.Object) { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + q.Add(clusterRequest{ |
| 82 | + ClusterName: e.clusterName, |
| 83 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 84 | + Name: evt.Object.GetName(), |
| 85 | + Namespace: evt.Object.GetNamespace(), |
| 86 | + }}}) |
| 87 | +} |
| 88 | + |
| 89 | +// Generic implements EventHandler. |
| 90 | +func (e *TypedEnqueueClusterRequestForObject[T]) Generic(ctx context.Context, evt event.TypedGenericEvent[T], q workqueue.TypedRateLimitingInterface[clusterRequest]) { |
| 91 | + if isNil(evt.Object) { |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + q.Add(clusterRequest{ |
| 96 | + ClusterName: e.clusterName, |
| 97 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 98 | + Name: evt.Object.GetName(), |
| 99 | + Namespace: evt.Object.GetNamespace(), |
| 100 | + }}}) |
| 101 | +} |
| 102 | + |
| 103 | +func (e *TypedEnqueueClusterRequestForObject[T]) DeepCopyFor(c cluster.Cluster) handler.TypedDeepCopyableEventHandler[T, clusterRequest] { |
| 104 | + if e == nil { |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + out := new(TypedEnqueueClusterRequestForObject[T]) |
| 109 | + *out = *e |
| 110 | + out.clusterName = c.Name() |
| 111 | + |
| 112 | + return out |
| 113 | +} |
| 114 | + |
| 115 | +func isNil(arg any) bool { |
| 116 | + if v := reflect.ValueOf(arg); !v.IsValid() || ((v.Kind() == reflect.Ptr || |
| 117 | + v.Kind() == reflect.Interface || |
| 118 | + v.Kind() == reflect.Slice || |
| 119 | + v.Kind() == reflect.Map || |
| 120 | + v.Kind() == reflect.Chan || |
| 121 | + v.Kind() == reflect.Func) && v.IsNil()) { |
| 122 | + return true |
| 123 | + } |
| 124 | + return false |
| 125 | +} |
0 commit comments