-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) { | ||
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 | ||
} |
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{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()} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).