|
| 1 | +/* |
| 2 | +Copyright 2019 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 informerfactory |
| 18 | + |
| 19 | +import ( |
| 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. |
| 26 | +type InformerFactory interface { |
| 27 | + ForResource(resource schema.GroupVersionResource) (informers.GenericInformer, error) |
| 28 | + Start(stopCh <-chan struct{}) |
| 29 | +} |
| 30 | + |
| 31 | +type informerFactory struct { |
| 32 | + typedInformerFactory informers.SharedInformerFactory |
| 33 | + metadataInformerFactory metadatainformer.SharedInformerFactory |
| 34 | +} |
| 35 | + |
| 36 | +func (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 | + |
| 44 | +func (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 |
| 51 | +func NewInformerFactory(typedInformerFactory informers.SharedInformerFactory, metadataInformerFactory metadatainformer.SharedInformerFactory) InformerFactory { |
| 52 | + return &informerFactory{ |
| 53 | + typedInformerFactory: typedInformerFactory, |
| 54 | + metadataInformerFactory: metadataInformerFactory, |
| 55 | + } |
| 56 | +} |
0 commit comments