Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To add Informer for ServiceAccount #11330

Merged
merged 1 commit into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
serviceAccounts := []*kapi.ServiceAccount{}

if s.namespace == kapi.NamespaceAll {
for _, obj := range s.indexer.List() {
bc := obj.(*kapi.ServiceAccount)
if selector.Matches(labels.Set(bc.Labels)) {
serviceAccounts = append(serviceAccounts, bc)
}
}
return serviceAccounts, 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)) {
serviceAccounts = append(serviceAccounts, bc)
}
}
return serviceAccounts, 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{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
)
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()}
}
6 changes: 6 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,11 @@ func (f *sharedInformerFactory) KubernetesInformers() informers.SharedInformerFa
return kubernetesSharedInformer{f}
}

// TODO: it should use upstream informer as soon #34960 get merged
func (f *sharedInformerFactory) ServiceAccounts() ServiceAccountInformer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add here todo, with a link to you upstream PR, so that we know we should use the upstream version upon next rebase (to 1.5).

return &serviceAccountInformer{sharedInformerFactory: f}
}

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