-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters