github.com/redhat-appstudio/release-service@v0.0.0-20240507143925-083712697924/controllers/utils/handlers/enqueue_matched.go (about) 1 /* 2 Copyright 2023. 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 handlers 18 19 import ( 20 "context" 21 "strings" 22 23 "github.com/redhat-appstudio/release-service/api/v1alpha1" 24 "k8s.io/apimachinery/pkg/types" 25 "k8s.io/client-go/util/workqueue" 26 "sigs.k8s.io/controller-runtime/pkg/event" 27 crtHandler "sigs.k8s.io/controller-runtime/pkg/handler" 28 "sigs.k8s.io/controller-runtime/pkg/reconcile" 29 ) 30 31 // EnqueueRequestForMatchedResource enqueues Request containing the Name and Namespace of the resource(s) specified in the 32 // Status of the ReleasePlans and ReleasePlanAdmissions that are the source of the Event. The source of the event 33 // triggers reconciliation of the parent resource. 34 type EnqueueRequestForMatchedResource struct{} 35 36 var _ crtHandler.EventHandler = &EnqueueRequestForMatchedResource{} 37 38 // Create implements EventHandler. 39 func (e *EnqueueRequestForMatchedResource) Create(_ context.Context, _ event.CreateEvent, _ workqueue.RateLimitingInterface) { 40 // A freshly created resource won't have any resources in its status 41 } 42 43 // Update implements EventHandler. 44 func (e *EnqueueRequestForMatchedResource) Update(_ context.Context, updateEvent event.UpdateEvent, rateLimitingInterface workqueue.RateLimitingInterface) { 45 if releasePlan, ok := updateEvent.ObjectOld.(*v1alpha1.ReleasePlan); ok { 46 enqueueRequest(releasePlan.Status.ReleasePlanAdmission.Name, rateLimitingInterface) 47 } else if releasePlanAdmission, ok := updateEvent.ObjectOld.(*v1alpha1.ReleasePlanAdmission); ok { 48 for _, releasePlan := range releasePlanAdmission.Status.ReleasePlans { 49 enqueueRequest(releasePlan.Name, rateLimitingInterface) 50 } 51 } 52 53 if releasePlan, ok := updateEvent.ObjectNew.(*v1alpha1.ReleasePlan); ok { 54 enqueueRequest(releasePlan.Status.ReleasePlanAdmission.Name, rateLimitingInterface) 55 } else if releasePlanAdmission, ok := updateEvent.ObjectNew.(*v1alpha1.ReleasePlanAdmission); ok { 56 for _, releasePlan := range releasePlanAdmission.Status.ReleasePlans { 57 enqueueRequest(releasePlan.Name, rateLimitingInterface) 58 } 59 } 60 } 61 62 // Delete implements EventHandler. 63 func (e *EnqueueRequestForMatchedResource) Delete(_ context.Context, deleteEvent event.DeleteEvent, rateLimitingInterface workqueue.RateLimitingInterface) { 64 if releasePlan, ok := deleteEvent.Object.(*v1alpha1.ReleasePlan); ok { 65 enqueueRequest(releasePlan.Status.ReleasePlanAdmission.Name, rateLimitingInterface) 66 } else if releasePlanAdmission, ok := deleteEvent.Object.(*v1alpha1.ReleasePlanAdmission); ok { 67 for _, releasePlan := range releasePlanAdmission.Status.ReleasePlans { 68 enqueueRequest(releasePlan.Name, rateLimitingInterface) 69 } 70 } 71 } 72 73 // Generic implements EventHandler. 74 func (e *EnqueueRequestForMatchedResource) Generic(_ context.Context, genericEvent event.GenericEvent, rateLimitingInterface workqueue.RateLimitingInterface) { 75 if releasePlan, ok := genericEvent.Object.(*v1alpha1.ReleasePlan); ok { 76 enqueueRequest(releasePlan.Status.ReleasePlanAdmission.Name, rateLimitingInterface) 77 } else if releasePlanAdmission, ok := genericEvent.Object.(*v1alpha1.ReleasePlanAdmission); ok { 78 for _, releasePlan := range releasePlanAdmission.Status.ReleasePlans { 79 enqueueRequest(releasePlan.Name, rateLimitingInterface) 80 } 81 } 82 } 83 84 // enqueueRequest parses the provided string to extract the namespace and name into a 85 // types.NamespacedName and adds a request to the RateLimitingInterface with it. 86 func enqueueRequest(namespacedNameString string, rateLimitingInterface workqueue.RateLimitingInterface) { 87 values := strings.SplitN(namespacedNameString, "/", 2) 88 89 if len(values) < 2 { 90 return 91 } 92 93 namespacedName := types.NamespacedName{Namespace: values[0], Name: values[1]} 94 rateLimitingInterface.Add(reconcile.Request{NamespacedName: namespacedName}) 95 }