Skip to content

Commit

Permalink
To add Informer for ServiceAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
sdminonne committed Oct 11, 2016
1 parent 606716e commit 4dcd791
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/client/cache/serviceaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cache

import (
kapi "k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/labels"
)

// StoreToServiceAccountLister gives a store List and Exists methods. The store must contain only ServiceAccounts.
type StoreToServiceAccountLister struct {
cache.Indexer
}

func (s *StoreToServiceAccountLister) ServiceAccounts(namespace string) storeServiceAccountsNamespacer {
return storeServiceAccountsNamespacer{s.Indexer, namespace}
}

// storeServiceAccountsNamespacer provides a way to get and list ServiceAccounts from a specific namespace.
type storeServiceAccountsNamespacer struct {
indexer cache.Indexer
namespace string
}

// Get the ServiceAccount matching the name from the cache.
func (s storeServiceAccountsNamespacer) Get(name string) (*kapi.ServiceAccount, error) {
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
if err != nil {
return nil, err
}
if !exists {
return nil, kapierrors.NewNotFound(kapi.Resource("ServiceAccount"), name)
}
return obj.(*kapi.ServiceAccount), nil
}

// List all the ServiceAccounts that match the provided selector using a namespace index.
// If the indexed list fails then we will fallback to listing from all namespaces and filter
// by the namespace we want.
func (s storeServiceAccountsNamespacer) List(selector labels.Selector) ([]*kapi.ServiceAccount, error) {
configs := []*kapi.ServiceAccount{}

if s.namespace == kapi.NamespaceAll {
for _, obj := range s.indexer.List() {
bc := obj.(*kapi.ServiceAccount)
if selector.Matches(labels.Set(bc.Labels)) {
configs = append(configs, bc)
}
}
return configs, nil
}

items, err := s.indexer.ByIndex(cache.NamespaceIndex, s.namespace)
if err != nil {
return nil, err
}
for _, obj := range items {
bc := obj.(*kapi.ServiceAccount)
if selector.Matches(labels.Set(bc.Labels)) {
configs = append(configs, bc)
}
}
return configs, nil
}
62 changes: 62 additions & 0 deletions pkg/controller/shared/serviceaccount_informers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package shared

import (
"reflect"

kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/controller/framework"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"

oscache "github.com/openshift/origin/pkg/client/cache"
)

type ServiceAccountInformer interface {
Informer() framework.SharedIndexInformer
Indexer() cache.Indexer
Lister() oscache.StoreToServiceAccountLister
}

type serviceAccountInformer struct {
*sharedInformerFactory
}

func (s *serviceAccountInformer) Informer() framework.SharedIndexInformer {
s.lock.Lock()
defer s.lock.Unlock()

informerObj := &kapi.ServiceAccount{}
informerType := reflect.TypeOf(informerObj)
informer, exists := s.informers[informerType]
if exists {
return informer
}

informer = framework.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options kapi.ListOptions) (runtime.Object, error) {
return s.kubeClient.ServiceAccounts(kapi.NamespaceAll).List(options)
},
WatchFunc: func(options kapi.ListOptions) (watch.Interface, error) {
return s.kubeClient.ServiceAccounts(kapi.NamespaceAll).Watch(options)
},
},
informerObj,
s.defaultResync,
cache.Indexers{},
)
s.informers[informerType] = informer

return informer
}

func (s *serviceAccountInformer) Indexer() cache.Indexer {
informer := s.Informer()
return informer.GetIndexer()
}

func (s *serviceAccountInformer) Lister() oscache.StoreToServiceAccountLister {
informer := s.Informer()
return oscache.StoreToServiceAccountLister{Indexer: informer.GetIndexer()}
}
5 changes: 5 additions & 0 deletions pkg/controller/shared/shared_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type InformerFactory interface {
ImageStreams() ImageStreamInformer
SecurityContextConstraints() SecurityContextConstraintsInformer
ClusterResourceQuotas() ClusterResourceQuotaInformer
ServiceAccounts() ServiceAccountInformer

KubernetesInformers() informers.SharedInformerFactory
}
Expand Down Expand Up @@ -175,6 +176,10 @@ func (f *sharedInformerFactory) KubernetesInformers() informers.SharedInformerFa
return kubernetesSharedInformer{f}
}

func (f *sharedInformerFactory) ServiceAccounts() ServiceAccountInformer {
return &serviceAccountInformer{sharedInformerFactory: f}
}

// kubernetesSharedInformer adapts this informer factory to the identical interface as kubernetes
type kubernetesSharedInformer struct {
f *sharedInformerFactory
Expand Down

0 comments on commit 4dcd791

Please sign in to comment.