knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/injection/informers.go (about) 1 /* 2 Copyright 2019 The Knative 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 //nolint:fatcontext 18 package injection 19 20 import ( 21 "context" 22 23 "k8s.io/client-go/rest" 24 25 "knative.dev/pkg/controller" 26 ) 27 28 // InformerInjector holds the type of a callback that attaches a particular 29 // informer type to a context. 30 type InformerInjector func(context.Context) (context.Context, controller.Informer) 31 32 // FilteredInformersInjector holds the type of a callback that attaches a set of particular 33 // filtered informers type to a context. 34 type FilteredInformersInjector func(context.Context) (context.Context, []controller.Informer) 35 36 func (i *impl) RegisterInformer(ii InformerInjector) { 37 i.m.Lock() 38 defer i.m.Unlock() 39 40 i.informers = append(i.informers, ii) 41 } 42 43 func (i *impl) RegisterFilteredInformers(fii FilteredInformersInjector) { 44 i.m.Lock() 45 defer i.m.Unlock() 46 47 i.filteredInformers = append(i.filteredInformers, fii) 48 } 49 50 func (i *impl) GetInformers() []InformerInjector { 51 i.m.RLock() 52 defer i.m.RUnlock() 53 54 // Copy the slice before returning. 55 return append(i.informers[:0:0], i.informers...) 56 } 57 58 func (i *impl) GetFilteredInformers() []FilteredInformersInjector { 59 i.m.RLock() 60 defer i.m.RUnlock() 61 62 // Copy the slice before returning. 63 return append(i.filteredInformers[:0:0], i.filteredInformers...) 64 } 65 66 func (i *impl) SetupInformers(ctx context.Context, cfg *rest.Config) (context.Context, []controller.Informer) { 67 // Based on the reconcilers we have linked, build up a set of clients and inject 68 // them onto the context. 69 for _, ci := range i.GetClients() { 70 ctx = ci(ctx, cfg) 71 } 72 73 // Based on the reconcilers we have linked, build up a set of informer factories 74 // and inject them onto the context. 75 for _, ifi := range i.GetInformerFactories() { 76 ctx = ifi(ctx) 77 } 78 79 // Based on the reconcilers we have linked, build up a set of duck informer factories 80 // and inject them onto the context. 81 for _, duck := range i.GetDucks() { 82 ctx = duck(ctx) 83 } 84 85 // Based on the reconcilers we have linked, build up a set of informers 86 // and inject them onto the context. 87 var inf controller.Informer 88 var filteredinfs []controller.Informer 89 informers := make([]controller.Informer, 0, len(i.GetInformers())) 90 for _, ii := range i.GetInformers() { 91 ctx, inf = ii(ctx) 92 informers = append(informers, inf) 93 } 94 for _, fii := range i.GetFilteredInformers() { 95 ctx, filteredinfs = fii(ctx) 96 informers = append(informers, filteredinfs...) 97 } 98 return ctx, informers 99 }