|
| 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 | +func wrapHandler[request comparable](handler handler.TypedEventHandler[client.Object, request], cluster cluster.Cluster) handler.TypedEventHandler[client.Object, request] { |
| 33 | + return &wrappedEventHandler[request]{ |
| 34 | + handler: handler, |
| 35 | + cluster: cluster, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +type wrappedEventHandler[request comparable] struct { |
| 40 | + handler handler.TypedEventHandler[client.Object, request] |
| 41 | + cluster cluster.Cluster |
| 42 | +} |
| 43 | + |
| 44 | +func (w *wrappedEventHandler[request]) Create(ctx context.Context, e event.TypedCreateEvent[client.Object], q workqueue.TypedRateLimitingInterface[request]) { |
| 45 | + annotations := e.Object.GetAnnotations() |
| 46 | + if annotations == nil { |
| 47 | + annotations = make(map[string]string) |
| 48 | + } |
| 49 | + annotations["cluster-name"] = w.cluster.Name() |
| 50 | + e.Object.SetAnnotations(annotations) |
| 51 | + w.handler.Create(ctx, e, q) |
| 52 | +} |
| 53 | + |
| 54 | +func (w *wrappedEventHandler[request]) Update(ctx context.Context, e event.TypedUpdateEvent[client.Object], q workqueue.TypedRateLimitingInterface[request]) { |
| 55 | + annotations := e.ObjectNew.GetAnnotations() |
| 56 | + if annotations == nil { |
| 57 | + annotations = make(map[string]string) |
| 58 | + } |
| 59 | + annotations["cluster-name"] = w.cluster.Name() |
| 60 | + e.ObjectNew.SetAnnotations(annotations) |
| 61 | + w.handler.Update(ctx, e, q) |
| 62 | +} |
| 63 | + |
| 64 | +func (w *wrappedEventHandler[request]) Delete(ctx context.Context, e event.TypedDeleteEvent[client.Object], q workqueue.TypedRateLimitingInterface[request]) { |
| 65 | + annotations := e.Object.GetAnnotations() |
| 66 | + if annotations == nil { |
| 67 | + annotations = make(map[string]string) |
| 68 | + } |
| 69 | + annotations["cluster-name"] = w.cluster.Name() |
| 70 | + e.Object.SetAnnotations(annotations) |
| 71 | + w.handler.Delete(ctx, e, q) |
| 72 | +} |
| 73 | + |
| 74 | +func (w *wrappedEventHandler[request]) Generic(ctx context.Context, e event.TypedGenericEvent[client.Object], q workqueue.TypedRateLimitingInterface[request]) { |
| 75 | + annotations := e.Object.GetAnnotations() |
| 76 | + if annotations == nil { |
| 77 | + annotations = make(map[string]string) |
| 78 | + } |
| 79 | + annotations["cluster-name"] = w.cluster.Name() |
| 80 | + e.Object.SetAnnotations(annotations) |
| 81 | + w.handler.Generic(ctx, e, q) |
| 82 | +} |
| 83 | + |
| 84 | +var _ handler.TypedEventHandler[client.Object, clusterRequest] = &EnqueueClusterRequestForObject{} |
| 85 | + |
| 86 | +type EnqueueClusterRequestForObject = TypedEnqueueClusterRequestForObject[client.Object] |
| 87 | + |
| 88 | +type TypedEnqueueClusterRequestForObject[object client.Object] struct{} |
| 89 | + |
| 90 | +// Create implements EventHandler. |
| 91 | +func (e *TypedEnqueueClusterRequestForObject[T]) Create(ctx context.Context, evt event.TypedCreateEvent[T], q workqueue.TypedRateLimitingInterface[clusterRequest]) { |
| 92 | + if isNil(evt.Object) { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + clusterName := evt.Object.GetAnnotations()["cluster-name"] |
| 97 | + |
| 98 | + q.Add(clusterRequest{ |
| 99 | + ClusterName: clusterName, |
| 100 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 101 | + Name: evt.Object.GetName(), |
| 102 | + Namespace: evt.Object.GetNamespace(), |
| 103 | + }}}) |
| 104 | +} |
| 105 | + |
| 106 | +// Update implements EventHandler. |
| 107 | +func (e *TypedEnqueueClusterRequestForObject[T]) Update(ctx context.Context, evt event.TypedUpdateEvent[T], q workqueue.TypedRateLimitingInterface[clusterRequest]) { |
| 108 | + clusterName := evt.ObjectNew.GetAnnotations()["cluster-name"] |
| 109 | + |
| 110 | + switch { |
| 111 | + case !isNil(evt.ObjectNew): |
| 112 | + q.Add(clusterRequest{ |
| 113 | + ClusterName: clusterName, |
| 114 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 115 | + Name: evt.ObjectNew.GetName(), |
| 116 | + Namespace: evt.ObjectNew.GetNamespace(), |
| 117 | + }}}) |
| 118 | + case !isNil(evt.ObjectOld): |
| 119 | + q.Add(clusterRequest{ |
| 120 | + ClusterName: clusterName, |
| 121 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 122 | + Name: evt.ObjectOld.GetName(), |
| 123 | + Namespace: evt.ObjectOld.GetNamespace(), |
| 124 | + }}}) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// Delete implements EventHandler. |
| 129 | +func (e *TypedEnqueueClusterRequestForObject[T]) Delete(ctx context.Context, evt event.TypedDeleteEvent[T], q workqueue.TypedRateLimitingInterface[clusterRequest]) { |
| 130 | + if isNil(evt.Object) { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + clusterName := evt.Object.GetAnnotations()["cluster-name"] |
| 135 | + |
| 136 | + q.Add(clusterRequest{ |
| 137 | + ClusterName: clusterName, |
| 138 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 139 | + Name: evt.Object.GetName(), |
| 140 | + Namespace: evt.Object.GetNamespace(), |
| 141 | + }}}) |
| 142 | +} |
| 143 | + |
| 144 | +// Generic implements EventHandler. |
| 145 | +func (e *TypedEnqueueClusterRequestForObject[T]) Generic(ctx context.Context, evt event.TypedGenericEvent[T], q workqueue.TypedRateLimitingInterface[clusterRequest]) { |
| 146 | + if isNil(evt.Object) { |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + clusterName := evt.Object.GetAnnotations()["cluster-name"] |
| 151 | + |
| 152 | + q.Add(clusterRequest{ |
| 153 | + ClusterName: clusterName, |
| 154 | + Request: reconcile.Request{NamespacedName: types.NamespacedName{ |
| 155 | + Name: evt.Object.GetName(), |
| 156 | + Namespace: evt.Object.GetNamespace(), |
| 157 | + }}}) |
| 158 | +} |
| 159 | + |
| 160 | +func isNil(arg any) bool { |
| 161 | + if v := reflect.ValueOf(arg); !v.IsValid() || ((v.Kind() == reflect.Ptr || |
| 162 | + v.Kind() == reflect.Interface || |
| 163 | + v.Kind() == reflect.Slice || |
| 164 | + v.Kind() == reflect.Map || |
| 165 | + v.Kind() == reflect.Chan || |
| 166 | + v.Kind() == reflect.Func) && v.IsNil()) { |
| 167 | + return true |
| 168 | + } |
| 169 | + return false |
| 170 | +} |
0 commit comments