informer_factory.go 1.9 KB

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