github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/cache/cache.go (about) 1 /* 2 Copyright 2022. 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 cache 18 19 import ( 20 "context" 21 22 applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1" 23 "github.com/redhat-appstudio/release-service/api/v1alpha1" 24 ctrl "sigs.k8s.io/controller-runtime" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 ) 27 28 // SetupComponentCache adds a new index field to be able to search Components by application. 29 func SetupComponentCache(mgr ctrl.Manager) error { 30 componentIndexFunc := func(obj client.Object) []string { 31 return []string{obj.(*applicationapiv1alpha1.Component).Spec.Application} 32 } 33 34 return mgr.GetCache().IndexField(context.Background(), &applicationapiv1alpha1.Component{}, 35 "spec.application", componentIndexFunc) 36 } 37 38 // SetupReleasePlanCache adds a new index field to be able to search ReleasePlans by target. 39 func SetupReleasePlanCache(mgr ctrl.Manager) error { 40 releasePlanIndexFunc := func(obj client.Object) []string { 41 return []string{obj.(*v1alpha1.ReleasePlan).Spec.Target} 42 } 43 44 return mgr.GetCache().IndexField(context.Background(), &v1alpha1.ReleasePlan{}, 45 "spec.target", releasePlanIndexFunc) 46 } 47 48 // SetupReleasePlanAdmissionCache adds a new index field to be able to search ReleasePlanAdmissions by origin. 49 func SetupReleasePlanAdmissionCache(mgr ctrl.Manager) error { 50 releasePlanAdmissionIndexFunc := func(obj client.Object) []string { 51 return []string{obj.(*v1alpha1.ReleasePlanAdmission).Spec.Origin} 52 } 53 54 return mgr.GetCache().IndexField(context.Background(), &v1alpha1.ReleasePlanAdmission{}, 55 "spec.origin", releasePlanAdmissionIndexFunc) 56 }