github.com/docker/compose-on-kubernetes@v0.5.0/api/client/informers/generic.go (about)

     1  package informers
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/compose-on-kubernetes/api/compose/v1alpha3"
     7  	"github.com/docker/compose-on-kubernetes/api/compose/v1beta2"
     8  	"k8s.io/apimachinery/pkg/runtime/schema"
     9  	"k8s.io/client-go/tools/cache"
    10  )
    11  
    12  // GenericInformer is type of SharedIndexInformer which will locate and delegate to other
    13  // sharedInformers based on type
    14  type GenericInformer interface {
    15  	Informer() cache.SharedIndexInformer
    16  	Lister() cache.GenericLister
    17  }
    18  
    19  type genericInformer struct {
    20  	informer cache.SharedIndexInformer
    21  	resource schema.GroupResource
    22  }
    23  
    24  // Informer returns the SharedIndexInformer.
    25  func (f *genericInformer) Informer() cache.SharedIndexInformer {
    26  	return f.informer
    27  }
    28  
    29  // Lister returns the GenericLister.
    30  func (f *genericInformer) Lister() cache.GenericLister {
    31  	return cache.NewGenericLister(f.Informer().GetIndexer(), f.resource)
    32  }
    33  
    34  // ForResource gives generic access to a shared informer of the matching type
    35  // TODO extend this to unknown resources with a client pool
    36  func (f *sharedInformerFactory) ForResource(resource schema.GroupVersionResource) (GenericInformer, error) {
    37  	switch resource {
    38  	// Group=Compose, Version=V1beta1
    39  	case v1beta2.SchemeGroupVersion.WithResource("stacks"):
    40  		return &genericInformer{resource: resource.GroupResource(), informer: f.Compose().V1beta2().Stacks().Informer()}, nil
    41  	case v1alpha3.SchemeGroupVersion.WithResource("stacks"):
    42  		return &genericInformer{resource: resource.GroupResource(), informer: f.Compose().V1alpha3().Stacks().Informer()}, nil
    43  	}
    44  
    45  	return nil, fmt.Errorf("no informer found for %v", resource)
    46  }