sigs.k8s.io/controller-runtime@v0.18.2/pkg/handler/enqueue.go (about) 1 /* 2 Copyright 2018 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 handler 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/event" 27 logf "sigs.k8s.io/controller-runtime/pkg/internal/log" 28 "sigs.k8s.io/controller-runtime/pkg/reconcile" 29 ) 30 31 var enqueueLog = logf.RuntimeLog.WithName("eventhandler").WithName("EnqueueRequestForObject") 32 33 type empty struct{} 34 35 var _ EventHandler = &EnqueueRequestForObject{} 36 37 // EnqueueRequestForObject enqueues a Request containing the Name and Namespace of the object that is the source of the Event. 38 // (e.g. the created / deleted / updated objects Name and Namespace). handler.EnqueueRequestForObject is used by almost all 39 // Controllers that have associated Resources (e.g. CRDs) to reconcile the associated Resource. 40 type EnqueueRequestForObject = TypedEnqueueRequestForObject[client.Object] 41 42 // TypedEnqueueRequestForObject enqueues a Request containing the Name and Namespace of the object that is the source of the Event. 43 // (e.g. the created / deleted / updated objects Name and Namespace). handler.TypedEnqueueRequestForObject is used by almost all 44 // Controllers that have associated Resources (e.g. CRDs) to reconcile the associated Resource. 45 // 46 // TypedEnqueueRequestForObject is experimental and subject to future change. 47 type TypedEnqueueRequestForObject[T client.Object] struct{} 48 49 // Create implements EventHandler. 50 func (e *TypedEnqueueRequestForObject[T]) Create(ctx context.Context, evt event.TypedCreateEvent[T], q workqueue.RateLimitingInterface) { 51 if isNil(evt.Object) { 52 enqueueLog.Error(nil, "CreateEvent received with no metadata", "event", evt) 53 return 54 } 55 q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ 56 Name: evt.Object.GetName(), 57 Namespace: evt.Object.GetNamespace(), 58 }}) 59 } 60 61 // Update implements EventHandler. 62 func (e *TypedEnqueueRequestForObject[T]) Update(ctx context.Context, evt event.TypedUpdateEvent[T], q workqueue.RateLimitingInterface) { 63 switch { 64 case !isNil(evt.ObjectNew): 65 q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ 66 Name: evt.ObjectNew.GetName(), 67 Namespace: evt.ObjectNew.GetNamespace(), 68 }}) 69 case !isNil(evt.ObjectOld): 70 q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ 71 Name: evt.ObjectOld.GetName(), 72 Namespace: evt.ObjectOld.GetNamespace(), 73 }}) 74 default: 75 enqueueLog.Error(nil, "UpdateEvent received with no metadata", "event", evt) 76 } 77 } 78 79 // Delete implements EventHandler. 80 func (e *TypedEnqueueRequestForObject[T]) Delete(ctx context.Context, evt event.TypedDeleteEvent[T], q workqueue.RateLimitingInterface) { 81 if isNil(evt.Object) { 82 enqueueLog.Error(nil, "DeleteEvent received with no metadata", "event", evt) 83 return 84 } 85 q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ 86 Name: evt.Object.GetName(), 87 Namespace: evt.Object.GetNamespace(), 88 }}) 89 } 90 91 // Generic implements EventHandler. 92 func (e *TypedEnqueueRequestForObject[T]) Generic(ctx context.Context, evt event.TypedGenericEvent[T], q workqueue.RateLimitingInterface) { 93 if isNil(evt.Object) { 94 enqueueLog.Error(nil, "GenericEvent received with no metadata", "event", evt) 95 return 96 } 97 q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ 98 Name: evt.Object.GetName(), 99 Namespace: evt.Object.GetNamespace(), 100 }}) 101 } 102 103 func isNil(arg any) bool { 104 if v := reflect.ValueOf(arg); !v.IsValid() || ((v.Kind() == reflect.Ptr || 105 v.Kind() == reflect.Interface || 106 v.Kind() == reflect.Slice || 107 v.Kind() == reflect.Map || 108 v.Kind() == reflect.Chan || 109 v.Kind() == reflect.Func) && v.IsNil()) { 110 return true 111 } 112 return false 113 }