1/* 2Copyright 2019 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package informerfactory 18 19import ( 20 "k8s.io/apimachinery/pkg/runtime/schema" 21 "k8s.io/client-go/informers" 22 "k8s.io/client-go/metadata/metadatainformer" 23) 24 25// InformerFactory creates informers for each group version resource. 26type InformerFactory interface { 27 ForResource(resource schema.GroupVersionResource) (informers.GenericInformer, error) 28 Start(stopCh <-chan struct{}) 29} 30 31type informerFactory struct { 32 typedInformerFactory informers.SharedInformerFactory 33 metadataInformerFactory metadatainformer.SharedInformerFactory 34} 35 36func (i *informerFactory) ForResource(resource schema.GroupVersionResource) (informers.GenericInformer, error) { 37 informer, err := i.typedInformerFactory.ForResource(resource) 38 if err != nil { 39 return i.metadataInformerFactory.ForResource(resource), nil 40 } 41 return informer, nil 42} 43 44func (i *informerFactory) Start(stopCh <-chan struct{}) { 45 i.typedInformerFactory.Start(stopCh) 46 i.metadataInformerFactory.Start(stopCh) 47} 48 49// NewInformerFactory creates a new InformerFactory which works with both typed 50// resources and metadata-only resources 51func NewInformerFactory(typedInformerFactory informers.SharedInformerFactory, metadataInformerFactory metadatainformer.SharedInformerFactory) InformerFactory { 52 return &informerFactory{ 53 typedInformerFactory: typedInformerFactory, 54 metadataInformerFactory: metadataInformerFactory, 55 } 56} 57