mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-31 08:36:16 +00:00
Remove legacy listers and informers
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["listers_test.go"],
|
||||
library = ":go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/apis/extensions/v1beta1:go_default_library",
|
||||
"//vendor:k8s.io/apimachinery/pkg/api/errors",
|
||||
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
|
||||
"//vendor:k8s.io/apimachinery/pkg/labels",
|
||||
"//vendor:k8s.io/apimachinery/pkg/util/sets",
|
||||
"//vendor:k8s.io/client-go/tools/cache",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"listers.go",
|
||||
"listers_core.go",
|
||||
"listers_extensions.go",
|
||||
"listers_rbac.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/apis/apps/v1beta1:go_default_library",
|
||||
"//pkg/apis/certificates/v1beta1:go_default_library",
|
||||
"//pkg/apis/extensions:go_default_library",
|
||||
"//pkg/apis/extensions/v1beta1:go_default_library",
|
||||
"//pkg/apis/policy/v1beta1:go_default_library",
|
||||
"//pkg/apis/rbac:go_default_library",
|
||||
"//pkg/apis/storage:go_default_library",
|
||||
"//pkg/apis/storage/v1beta1:go_default_library",
|
||||
"//vendor:github.com/golang/glog",
|
||||
"//vendor:k8s.io/apimachinery/pkg/api/errors",
|
||||
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
|
||||
"//vendor:k8s.io/apimachinery/pkg/labels",
|
||||
"//vendor:k8s.io/client-go/tools/cache",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
@@ -1,346 +0,0 @@
|
||||
/*
|
||||
Copyright 2014 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package listers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
apps "k8s.io/kubernetes/pkg/apis/apps/v1beta1"
|
||||
certificates "k8s.io/kubernetes/pkg/apis/certificates/v1beta1"
|
||||
extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
|
||||
policy "k8s.io/kubernetes/pkg/apis/policy/v1beta1"
|
||||
storageinternal "k8s.io/kubernetes/pkg/apis/storage"
|
||||
storage "k8s.io/kubernetes/pkg/apis/storage/v1beta1"
|
||||
)
|
||||
|
||||
// TODO: generate these classes and methods for all resources of interest using
|
||||
// a script. Can use "go generate" once 1.4 is supported by all users.
|
||||
|
||||
// NodeConditionPredicate is a function that indicates whether the given node's conditions meet
|
||||
// some set of criteria defined by the function.
|
||||
type NodeConditionPredicate func(node *v1.Node) bool
|
||||
|
||||
// StoreToNodeLister makes a Store have the List method of the client.NodeInterface
|
||||
// The Store must contain (only) Nodes.
|
||||
type StoreToNodeLister struct {
|
||||
cache.Store
|
||||
}
|
||||
|
||||
func (s *StoreToNodeLister) List() (machines v1.NodeList, err error) {
|
||||
for _, m := range s.Store.List() {
|
||||
machines.Items = append(machines.Items, *(m.(*v1.Node)))
|
||||
}
|
||||
return machines, nil
|
||||
}
|
||||
|
||||
// NodeCondition returns a storeToNodeConditionLister
|
||||
func (s *StoreToNodeLister) NodeCondition(predicate NodeConditionPredicate) storeToNodeConditionLister {
|
||||
// TODO: Move this filtering server side. Currently our selectors don't facilitate searching through a list so we
|
||||
// have the reflector filter out the Unschedulable field and sift through node conditions in the lister.
|
||||
return storeToNodeConditionLister{s.Store, predicate}
|
||||
}
|
||||
|
||||
// storeToNodeConditionLister filters and returns nodes matching the given type and status from the store.
|
||||
type storeToNodeConditionLister struct {
|
||||
store cache.Store
|
||||
predicate NodeConditionPredicate
|
||||
}
|
||||
|
||||
// List returns a list of nodes that match the conditions defined by the predicate functions in the storeToNodeConditionLister.
|
||||
func (s storeToNodeConditionLister) List() (nodes []*v1.Node, err error) {
|
||||
for _, m := range s.store.List() {
|
||||
node := m.(*v1.Node)
|
||||
if s.predicate(node) {
|
||||
nodes = append(nodes, node)
|
||||
} else {
|
||||
glog.V(5).Infof("Node %s matches none of the conditions", node.Name)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// StoreToDaemonSetLister gives a store List and Exists methods. The store must contain only DaemonSets.
|
||||
type StoreToDaemonSetLister struct {
|
||||
cache.Store
|
||||
}
|
||||
|
||||
// Exists checks if the given daemon set exists in the store.
|
||||
func (s *StoreToDaemonSetLister) Exists(ds *extensions.DaemonSet) (bool, error) {
|
||||
_, exists, err := s.Store.Get(ds)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// List lists all daemon sets in the store.
|
||||
// TODO: converge on the interface in pkg/client
|
||||
func (s *StoreToDaemonSetLister) List() (dss extensions.DaemonSetList, err error) {
|
||||
for _, c := range s.Store.List() {
|
||||
dss.Items = append(dss.Items, *(c.(*extensions.DaemonSet)))
|
||||
}
|
||||
return dss, nil
|
||||
}
|
||||
|
||||
// GetPodDaemonSets returns a list of daemon sets managing a pod.
|
||||
// Returns an error if and only if no matching daemon sets are found.
|
||||
func (s *StoreToDaemonSetLister) GetPodDaemonSets(pod *v1.Pod) (daemonSets []extensions.DaemonSet, err error) {
|
||||
var selector labels.Selector
|
||||
var daemonSet extensions.DaemonSet
|
||||
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("no daemon sets found for pod %v because it has no labels", pod.Name)
|
||||
return
|
||||
}
|
||||
|
||||
for _, m := range s.Store.List() {
|
||||
daemonSet = *m.(*extensions.DaemonSet)
|
||||
if daemonSet.Namespace != pod.Namespace {
|
||||
continue
|
||||
}
|
||||
selector, err = metav1.LabelSelectorAsSelector(daemonSet.Spec.Selector)
|
||||
if err != nil {
|
||||
// this should not happen if the DaemonSet passed validation
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If a daemonSet with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
||||
continue
|
||||
}
|
||||
daemonSets = append(daemonSets, daemonSet)
|
||||
}
|
||||
if len(daemonSets) == 0 {
|
||||
err = fmt.Errorf("could not find daemon set for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// StoreToEndpointsLister makes a Store that lists endpoints.
|
||||
type StoreToEndpointsLister struct {
|
||||
cache.Store
|
||||
}
|
||||
|
||||
// List lists all endpoints in the store.
|
||||
func (s *StoreToEndpointsLister) List() (services v1.EndpointsList, err error) {
|
||||
for _, m := range s.Store.List() {
|
||||
services.Items = append(services.Items, *(m.(*v1.Endpoints)))
|
||||
}
|
||||
return services, nil
|
||||
}
|
||||
|
||||
// GetServiceEndpoints returns the endpoints of a service, matched on service name.
|
||||
func (s *StoreToEndpointsLister) GetServiceEndpoints(svc *v1.Service) (ep v1.Endpoints, err error) {
|
||||
for _, m := range s.Store.List() {
|
||||
ep = *m.(*v1.Endpoints)
|
||||
if svc.Name == ep.Name && svc.Namespace == ep.Namespace {
|
||||
return ep, nil
|
||||
}
|
||||
}
|
||||
err = fmt.Errorf("could not find endpoints for service: %v", svc.Name)
|
||||
return
|
||||
}
|
||||
|
||||
// Typed wrapper around a store of PersistentVolumes
|
||||
type StoreToPVFetcher struct {
|
||||
cache.Store
|
||||
}
|
||||
|
||||
// GetPersistentVolumeInfo returns cached data for the PersistentVolume 'id'.
|
||||
func (s *StoreToPVFetcher) GetPersistentVolumeInfo(id string) (*v1.PersistentVolume, error) {
|
||||
o, exists, err := s.Get(&v1.PersistentVolume{ObjectMeta: metav1.ObjectMeta{Name: id}})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error retrieving PersistentVolume '%v' from cache: %v", id, err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("PersistentVolume '%v' not found", id)
|
||||
}
|
||||
|
||||
return o.(*v1.PersistentVolume), nil
|
||||
}
|
||||
|
||||
// StoreToStatefulSetLister gives a store List and Exists methods. The store must contain only StatefulSets.
|
||||
type StoreToStatefulSetLister struct {
|
||||
cache.Store
|
||||
}
|
||||
|
||||
// Exists checks if the given StatefulSet exists in the store.
|
||||
func (s *StoreToStatefulSetLister) Exists(ps *apps.StatefulSet) (bool, error) {
|
||||
_, exists, err := s.Store.Get(ps)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// List lists all StatefulSets in the store.
|
||||
func (s *StoreToStatefulSetLister) List() (psList []apps.StatefulSet, err error) {
|
||||
for _, ps := range s.Store.List() {
|
||||
psList = append(psList, *(ps.(*apps.StatefulSet)))
|
||||
}
|
||||
return psList, nil
|
||||
}
|
||||
|
||||
type storeStatefulSetsNamespacer struct {
|
||||
store cache.Store
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s *StoreToStatefulSetLister) StatefulSets(namespace string) storeStatefulSetsNamespacer {
|
||||
return storeStatefulSetsNamespacer{s.Store, namespace}
|
||||
}
|
||||
|
||||
// GetPodStatefulSets returns a list of StatefulSets managing a pod. Returns an error only if no matching StatefulSets are found.
|
||||
func (s *StoreToStatefulSetLister) GetPodStatefulSets(pod *v1.Pod) (psList []apps.StatefulSet, err error) {
|
||||
var selector labels.Selector
|
||||
var ps apps.StatefulSet
|
||||
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("no StatefulSets found for pod %v because it has no labels", pod.Name)
|
||||
return
|
||||
}
|
||||
|
||||
for _, m := range s.Store.List() {
|
||||
ps = *m.(*apps.StatefulSet)
|
||||
if ps.Namespace != pod.Namespace {
|
||||
continue
|
||||
}
|
||||
selector, err = metav1.LabelSelectorAsSelector(ps.Spec.Selector)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("invalid selector: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// If a StatefulSet with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
||||
continue
|
||||
}
|
||||
psList = append(psList, ps)
|
||||
}
|
||||
if len(psList) == 0 {
|
||||
err = fmt.Errorf("could not find StatefulSet for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// StoreToCertificateRequestLister gives a store List and Exists methods. The store must contain only CertificateRequests.
|
||||
type StoreToCertificateRequestLister struct {
|
||||
cache.Store
|
||||
}
|
||||
|
||||
// Exists checks if the given csr exists in the store.
|
||||
func (s *StoreToCertificateRequestLister) Exists(csr *certificates.CertificateSigningRequest) (bool, error) {
|
||||
_, exists, err := s.Store.Get(csr)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// StoreToCertificateRequestLister lists all csrs in the store.
|
||||
func (s *StoreToCertificateRequestLister) List() (csrs certificates.CertificateSigningRequestList, err error) {
|
||||
for _, c := range s.Store.List() {
|
||||
csrs.Items = append(csrs.Items, *(c.(*certificates.CertificateSigningRequest)))
|
||||
}
|
||||
return csrs, nil
|
||||
}
|
||||
|
||||
type StoreToPodDisruptionBudgetLister struct {
|
||||
cache.Store
|
||||
}
|
||||
|
||||
// GetPodPodDisruptionBudgets returns a list of PodDisruptionBudgets matching a pod. Returns an error only if no matching PodDisruptionBudgets are found.
|
||||
func (s *StoreToPodDisruptionBudgetLister) GetPodPodDisruptionBudgets(pod *v1.Pod) (pdbList []policy.PodDisruptionBudget, err error) {
|
||||
var selector labels.Selector
|
||||
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("no PodDisruptionBudgets found for pod %v because it has no labels", pod.Name)
|
||||
return
|
||||
}
|
||||
|
||||
for _, m := range s.Store.List() {
|
||||
pdb, ok := m.(*policy.PodDisruptionBudget)
|
||||
if !ok {
|
||||
glog.Errorf("Unexpected: %v is not a PodDisruptionBudget", m)
|
||||
continue
|
||||
}
|
||||
if pdb.Namespace != pod.Namespace {
|
||||
continue
|
||||
}
|
||||
selector, err = metav1.LabelSelectorAsSelector(pdb.Spec.Selector)
|
||||
if err != nil {
|
||||
glog.Warningf("invalid selector: %v", err)
|
||||
// TODO(mml): add an event to the PDB
|
||||
continue
|
||||
}
|
||||
|
||||
// If a PDB with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
||||
continue
|
||||
}
|
||||
pdbList = append(pdbList, *pdb)
|
||||
}
|
||||
if len(pdbList) == 0 {
|
||||
err = fmt.Errorf("could not find PodDisruptionBudget for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// StorageClassLister knows how to list storage classes
|
||||
type StorageClassLister interface {
|
||||
List(selector labels.Selector) (ret []*storage.StorageClass, err error)
|
||||
Get(name string) (*storage.StorageClass, error)
|
||||
}
|
||||
|
||||
// storageClassLister implements StorageClassLister
|
||||
type storageClassLister struct {
|
||||
indexer cache.Indexer
|
||||
}
|
||||
|
||||
// NewStorageClassLister returns a new lister.
|
||||
func NewStorageClassLister(indexer cache.Indexer) StorageClassLister {
|
||||
return &storageClassLister{indexer: indexer}
|
||||
}
|
||||
|
||||
// List returns a list of storage classes
|
||||
func (s *storageClassLister) List(selector labels.Selector) (ret []*storage.StorageClass, err error) {
|
||||
err = cache.ListAll(s.indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*storage.StorageClass))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
// Get returns storage class with name 'name'.
|
||||
func (s *storageClassLister) Get(name string) (*storage.StorageClass, error) {
|
||||
key := &storage.StorageClass{ObjectMeta: metav1.ObjectMeta{Name: name}}
|
||||
obj, exists, err := s.indexer.Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(storageinternal.Resource("storageclass"), name)
|
||||
}
|
||||
return obj.(*storage.StorageClass), nil
|
||||
}
|
@@ -1,351 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package listers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
)
|
||||
|
||||
// TODO: generate these classes and methods for all resources of interest using
|
||||
// a script. Can use "go generate" once 1.4 is supported by all users.
|
||||
|
||||
// Lister makes an Index have the List method. The Stores must contain only the expected type
|
||||
// Example:
|
||||
// s := cache.NewStore()
|
||||
// lw := cache.ListWatch{Client: c, FieldSelector: sel, Resource: "pods"}
|
||||
// r := cache.NewReflector(lw, &api.Pod{}, s).Run()
|
||||
// l := StoreToPodLister{s}
|
||||
// l.List()
|
||||
|
||||
// StoreToPodLister helps list pods
|
||||
type StoreToPodLister struct {
|
||||
Indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *StoreToPodLister) List(selector labels.Selector) (ret []*v1.Pod, err error) {
|
||||
err = cache.ListAll(s.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.Pod))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *StoreToPodLister) Pods(namespace string) storePodsNamespacer {
|
||||
return storePodsNamespacer{Indexer: s.Indexer, namespace: namespace}
|
||||
}
|
||||
|
||||
type storePodsNamespacer struct {
|
||||
Indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storePodsNamespacer) List(selector labels.Selector) (ret []*v1.Pod, err error) {
|
||||
err = cache.ListAllByNamespace(s.Indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.Pod))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s storePodsNamespacer) Get(name string) (*v1.Pod, error) {
|
||||
obj, exists, err := s.Indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(api.Resource("pod"), name)
|
||||
}
|
||||
return obj.(*v1.Pod), nil
|
||||
}
|
||||
|
||||
// StoreToServiceLister helps list services
|
||||
type StoreToServiceLister struct {
|
||||
Indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *StoreToServiceLister) List(selector labels.Selector) (ret []*v1.Service, err error) {
|
||||
err = cache.ListAll(s.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.Service))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *StoreToServiceLister) Services(namespace string) storeServicesNamespacer {
|
||||
return storeServicesNamespacer{s.Indexer, namespace}
|
||||
}
|
||||
|
||||
type storeServicesNamespacer struct {
|
||||
indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storeServicesNamespacer) List(selector labels.Selector) (ret []*v1.Service, err error) {
|
||||
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.Service))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s storeServicesNamespacer) Get(name string) (*v1.Service, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(api.Resource("service"), name)
|
||||
}
|
||||
return obj.(*v1.Service), nil
|
||||
}
|
||||
|
||||
// TODO: Move this back to scheduler as a helper function that takes a Store,
|
||||
// rather than a method of StoreToServiceLister.
|
||||
func (s *StoreToServiceLister) GetPodServices(pod *v1.Pod) (services []*v1.Service, err error) {
|
||||
allServices, err := s.Services(pod.Namespace).List(labels.Everything())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range allServices {
|
||||
service := allServices[i]
|
||||
if service.Spec.Selector == nil {
|
||||
// services with nil selectors match nothing, not everything.
|
||||
continue
|
||||
}
|
||||
selector := labels.Set(service.Spec.Selector).AsSelectorPreValidated()
|
||||
if selector.Matches(labels.Set(pod.Labels)) {
|
||||
services = append(services, service)
|
||||
}
|
||||
}
|
||||
|
||||
return services, nil
|
||||
}
|
||||
|
||||
// StoreToReplicationControllerLister helps list rcs
|
||||
type StoreToReplicationControllerLister struct {
|
||||
Indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *StoreToReplicationControllerLister) List(selector labels.Selector) (ret []*v1.ReplicationController, err error) {
|
||||
err = cache.ListAll(s.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.ReplicationController))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *StoreToReplicationControllerLister) ReplicationControllers(namespace string) storeReplicationControllersNamespacer {
|
||||
return storeReplicationControllersNamespacer{s.Indexer, namespace}
|
||||
}
|
||||
|
||||
type storeReplicationControllersNamespacer struct {
|
||||
indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storeReplicationControllersNamespacer) List(selector labels.Selector) (ret []*v1.ReplicationController, err error) {
|
||||
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.ReplicationController))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s storeReplicationControllersNamespacer) Get(name string) (*v1.ReplicationController, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(api.Resource("replicationcontroller"), name)
|
||||
}
|
||||
return obj.(*v1.ReplicationController), nil
|
||||
}
|
||||
|
||||
// GetPodControllers returns a list of replication controllers managing a pod. Returns an error only if no matching controllers are found.
|
||||
func (s *StoreToReplicationControllerLister) GetPodControllers(pod *v1.Pod) (controllers []*v1.ReplicationController, err error) {
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("no controllers found for pod %v because it has no labels", pod.Name)
|
||||
return
|
||||
}
|
||||
|
||||
key := &v1.ReplicationController{ObjectMeta: metav1.ObjectMeta{Namespace: pod.Namespace}}
|
||||
items, err := s.Indexer.Index(cache.NamespaceIndex, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, m := range items {
|
||||
rc := m.(*v1.ReplicationController)
|
||||
selector := labels.Set(rc.Spec.Selector).AsSelectorPreValidated()
|
||||
|
||||
// If an rc with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
||||
continue
|
||||
}
|
||||
controllers = append(controllers, rc)
|
||||
}
|
||||
if len(controllers) == 0 {
|
||||
err = fmt.Errorf("could not find controller for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// StoreToServiceAccountLister helps list service accounts
|
||||
type StoreToServiceAccountLister struct {
|
||||
Indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *StoreToServiceAccountLister) List(selector labels.Selector) (ret []*v1.ServiceAccount, err error) {
|
||||
err = cache.ListAll(s.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.ServiceAccount))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *StoreToServiceAccountLister) ServiceAccounts(namespace string) storeServiceAccountsNamespacer {
|
||||
return storeServiceAccountsNamespacer{s.Indexer, namespace}
|
||||
}
|
||||
|
||||
type storeServiceAccountsNamespacer struct {
|
||||
indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storeServiceAccountsNamespacer) List(selector labels.Selector) (ret []*v1.ServiceAccount, err error) {
|
||||
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.ServiceAccount))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s storeServiceAccountsNamespacer) Get(name string) (*v1.ServiceAccount, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(api.Resource("serviceaccount"), name)
|
||||
}
|
||||
return obj.(*v1.ServiceAccount), nil
|
||||
}
|
||||
|
||||
// StoreToLimitRangeLister helps list limit ranges
|
||||
type StoreToLimitRangeLister struct {
|
||||
Indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *StoreToLimitRangeLister) List(selector labels.Selector) (ret []*v1.LimitRange, err error) {
|
||||
err = cache.ListAll(s.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.LimitRange))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
// StoreToPersistentVolumeClaimLister helps list pvcs
|
||||
type StoreToPersistentVolumeClaimLister struct {
|
||||
Indexer cache.Indexer
|
||||
}
|
||||
|
||||
// List returns all persistentvolumeclaims that match the specified selector
|
||||
func (s *StoreToPersistentVolumeClaimLister) List(selector labels.Selector) (ret []*v1.PersistentVolumeClaim, err error) {
|
||||
err = cache.ListAll(s.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.PersistentVolumeClaim))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *StoreToLimitRangeLister) LimitRanges(namespace string) storeLimitRangesNamespacer {
|
||||
return storeLimitRangesNamespacer{s.Indexer, namespace}
|
||||
}
|
||||
|
||||
type storeLimitRangesNamespacer struct {
|
||||
indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storeLimitRangesNamespacer) List(selector labels.Selector) (ret []*v1.LimitRange, err error) {
|
||||
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.LimitRange))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s storeLimitRangesNamespacer) Get(name string) (*v1.LimitRange, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(api.Resource("limitrange"), name)
|
||||
}
|
||||
return obj.(*v1.LimitRange), nil
|
||||
}
|
||||
|
||||
// PersistentVolumeClaims returns all claims in a specified namespace.
|
||||
func (s *StoreToPersistentVolumeClaimLister) PersistentVolumeClaims(namespace string) storePersistentVolumeClaimsNamespacer {
|
||||
return storePersistentVolumeClaimsNamespacer{Indexer: s.Indexer, namespace: namespace}
|
||||
}
|
||||
|
||||
type storePersistentVolumeClaimsNamespacer struct {
|
||||
Indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storePersistentVolumeClaimsNamespacer) List(selector labels.Selector) (ret []*v1.PersistentVolumeClaim, err error) {
|
||||
err = cache.ListAllByNamespace(s.Indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.PersistentVolumeClaim))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s storePersistentVolumeClaimsNamespacer) Get(name string) (*v1.PersistentVolumeClaim, error) {
|
||||
obj, exists, err := s.Indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(api.Resource("persistentvolumeclaims"), name)
|
||||
}
|
||||
return obj.(*v1.PersistentVolumeClaim), nil
|
||||
}
|
||||
|
||||
// IndexerToNamespaceLister gives an Indexer List method
|
||||
type IndexerToNamespaceLister struct {
|
||||
cache.Indexer
|
||||
}
|
||||
|
||||
// List returns a list of namespaces
|
||||
func (i *IndexerToNamespaceLister) List(selector labels.Selector) (ret []*v1.Namespace, err error) {
|
||||
err = cache.ListAll(i.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1.Namespace))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (i *IndexerToNamespaceLister) Get(name string) (*v1.Namespace, error) {
|
||||
obj, exists, err := i.Indexer.GetByKey(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(api.Resource("namespace"), name)
|
||||
}
|
||||
return obj.(*v1.Namespace), nil
|
||||
}
|
@@ -1,212 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package listers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
extensionsinternal "k8s.io/kubernetes/pkg/apis/extensions"
|
||||
extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
|
||||
)
|
||||
|
||||
// TODO: generate these classes and methods for all resources of interest using
|
||||
// a script. Can use "go generate" once 1.4 is supported by all users.
|
||||
|
||||
// Lister makes an Index have the List method. The Stores must contain only the expected type
|
||||
// Example:
|
||||
// s := cache.NewStore()
|
||||
// lw := cache.ListWatch{Client: c, FieldSelector: sel, Resource: "pods"}
|
||||
// r := cache.NewReflector(lw, &extensions.Deployment{}, s).Run()
|
||||
// l := StoreToDeploymentLister{s}
|
||||
// l.List()
|
||||
|
||||
// StoreToDeploymentLister helps list deployments
|
||||
type StoreToDeploymentLister struct {
|
||||
Indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *StoreToDeploymentLister) List(selector labels.Selector) (ret []*extensions.Deployment, err error) {
|
||||
err = cache.ListAll(s.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*extensions.Deployment))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *StoreToDeploymentLister) Deployments(namespace string) storeDeploymentsNamespacer {
|
||||
return storeDeploymentsNamespacer{Indexer: s.Indexer, namespace: namespace}
|
||||
}
|
||||
|
||||
type storeDeploymentsNamespacer struct {
|
||||
Indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storeDeploymentsNamespacer) List(selector labels.Selector) (ret []*extensions.Deployment, err error) {
|
||||
err = cache.ListAllByNamespace(s.Indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*extensions.Deployment))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s storeDeploymentsNamespacer) Get(name string) (*extensions.Deployment, error) {
|
||||
obj, exists, err := s.Indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(extensionsinternal.Resource("deployment"), name)
|
||||
}
|
||||
return obj.(*extensions.Deployment), nil
|
||||
}
|
||||
|
||||
// GetDeploymentsForReplicaSet returns a list of deployments managing a replica set. Returns an error only if no matching deployments are found.
|
||||
func (s *StoreToDeploymentLister) GetDeploymentsForReplicaSet(rs *extensions.ReplicaSet) (deployments []*extensions.Deployment, err error) {
|
||||
if len(rs.Labels) == 0 {
|
||||
err = fmt.Errorf("no deployments found for ReplicaSet %v because it has no labels", rs.Name)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: MODIFY THIS METHOD so that it checks for the podTemplateSpecHash label
|
||||
dList, err := s.Deployments(rs.Namespace).List(labels.Everything())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, d := range dList {
|
||||
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid label selector: %v", err)
|
||||
}
|
||||
// If a deployment with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if selector.Empty() || !selector.Matches(labels.Set(rs.Labels)) {
|
||||
continue
|
||||
}
|
||||
deployments = append(deployments, d)
|
||||
}
|
||||
if len(deployments) == 0 {
|
||||
err = fmt.Errorf("could not find deployments set for ReplicaSet %s in namespace %s with labels: %v", rs.Name, rs.Namespace, rs.Labels)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetDeploymentsForDeployments returns a list of deployments managing a pod. Returns an error only if no matching deployments are found.
|
||||
// TODO eliminate shallow copies
|
||||
func (s *StoreToDeploymentLister) GetDeploymentsForPod(pod *v1.Pod) (deployments []*extensions.Deployment, err error) {
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("no deployments found for Pod %v because it has no labels", pod.Name)
|
||||
return
|
||||
}
|
||||
|
||||
if len(pod.Labels[extensions.DefaultDeploymentUniqueLabelKey]) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
dList, err := s.Deployments(pod.Namespace).List(labels.Everything())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, d := range dList {
|
||||
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid label selector: %v", err)
|
||||
}
|
||||
// If a deployment with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
||||
continue
|
||||
}
|
||||
deployments = append(deployments, d)
|
||||
}
|
||||
if len(deployments) == 0 {
|
||||
err = fmt.Errorf("could not find deployments set for Pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// StoreToReplicaSetLister helps list replicasets
|
||||
type StoreToReplicaSetLister struct {
|
||||
Indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *StoreToReplicaSetLister) List(selector labels.Selector) (ret []*extensions.ReplicaSet, err error) {
|
||||
err = cache.ListAll(s.Indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*extensions.ReplicaSet))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *StoreToReplicaSetLister) ReplicaSets(namespace string) storeReplicaSetsNamespacer {
|
||||
return storeReplicaSetsNamespacer{Indexer: s.Indexer, namespace: namespace}
|
||||
}
|
||||
|
||||
type storeReplicaSetsNamespacer struct {
|
||||
Indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storeReplicaSetsNamespacer) List(selector labels.Selector) (ret []*extensions.ReplicaSet, err error) {
|
||||
err = cache.ListAllByNamespace(s.Indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*extensions.ReplicaSet))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s storeReplicaSetsNamespacer) Get(name string) (*extensions.ReplicaSet, error) {
|
||||
obj, exists, err := s.Indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(extensionsinternal.Resource("replicaset"), name)
|
||||
}
|
||||
return obj.(*extensions.ReplicaSet), nil
|
||||
}
|
||||
|
||||
// GetPodReplicaSets returns a list of ReplicaSets managing a pod. Returns an error only if no matching ReplicaSets are found.
|
||||
func (s *StoreToReplicaSetLister) GetPodReplicaSets(pod *v1.Pod) (rss []*extensions.ReplicaSet, err error) {
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("no ReplicaSets found for pod %v because it has no labels", pod.Name)
|
||||
return
|
||||
}
|
||||
|
||||
list, err := s.ReplicaSets(pod.Namespace).List(labels.Everything())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, rs := range list {
|
||||
if rs.Namespace != pod.Namespace {
|
||||
continue
|
||||
}
|
||||
selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid selector: %v", err)
|
||||
}
|
||||
|
||||
// If a ReplicaSet with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
||||
continue
|
||||
}
|
||||
rss = append(rss, rs)
|
||||
}
|
||||
if len(rss) == 0 {
|
||||
err = fmt.Errorf("could not find ReplicaSet for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
|
||||
}
|
||||
return
|
||||
}
|
@@ -1,235 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package listers
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
rbac "k8s.io/kubernetes/pkg/apis/rbac"
|
||||
)
|
||||
|
||||
// TODO: generate these classes and methods for all resources of interest using
|
||||
// a script. Can use "go generate" once 1.4 is supported by all users.
|
||||
|
||||
// Lister makes an Index have the List method. The Stores must contain only the expected type
|
||||
// Example:
|
||||
// s := cache.NewStore()
|
||||
// lw := cache.ListWatch{Client: c, FieldSelector: sel, Resource: "pods"}
|
||||
// r := cache.NewReflector(lw, &rbac.ClusterRole{}, s).Run()
|
||||
// l := clusterRoleLister{s}
|
||||
// l.List()
|
||||
|
||||
func NewClusterRoleLister(indexer cache.Indexer) ClusterRoleLister {
|
||||
return &clusterRoleLister{indexer: indexer}
|
||||
}
|
||||
func NewClusterRoleBindingLister(indexer cache.Indexer) ClusterRoleBindingLister {
|
||||
return &clusterRoleBindingLister{indexer: indexer}
|
||||
}
|
||||
func NewRoleLister(indexer cache.Indexer) RoleLister {
|
||||
return &roleLister{indexer: indexer}
|
||||
}
|
||||
func NewRoleBindingLister(indexer cache.Indexer) RoleBindingLister {
|
||||
return &roleBindingLister{indexer: indexer}
|
||||
}
|
||||
|
||||
// these interfaces are used by the rbac authorizer
|
||||
type authorizerClusterRoleGetter interface {
|
||||
GetClusterRole(name string) (*rbac.ClusterRole, error)
|
||||
}
|
||||
|
||||
type authorizerClusterRoleBindingLister interface {
|
||||
ListClusterRoleBindings() ([]*rbac.ClusterRoleBinding, error)
|
||||
}
|
||||
|
||||
type authorizerRoleGetter interface {
|
||||
GetRole(namespace, name string) (*rbac.Role, error)
|
||||
}
|
||||
|
||||
type authorizerRoleBindingLister interface {
|
||||
ListRoleBindings(namespace string) ([]*rbac.RoleBinding, error)
|
||||
}
|
||||
|
||||
type ClusterRoleLister interface {
|
||||
authorizerClusterRoleGetter
|
||||
List(selector labels.Selector) (ret []*rbac.ClusterRole, err error)
|
||||
Get(name string) (*rbac.ClusterRole, error)
|
||||
}
|
||||
|
||||
type clusterRoleLister struct {
|
||||
indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *clusterRoleLister) List(selector labels.Selector) (ret []*rbac.ClusterRole, err error) {
|
||||
err = cache.ListAll(s.indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*rbac.ClusterRole))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s clusterRoleLister) Get(name string) (*rbac.ClusterRole, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(rbac.Resource("clusterrole"), name)
|
||||
}
|
||||
return obj.(*rbac.ClusterRole), nil
|
||||
}
|
||||
|
||||
func (s clusterRoleLister) GetClusterRole(name string) (*rbac.ClusterRole, error) {
|
||||
return s.Get(name)
|
||||
}
|
||||
|
||||
type ClusterRoleBindingLister interface {
|
||||
authorizerClusterRoleBindingLister
|
||||
List(selector labels.Selector) (ret []*rbac.ClusterRoleBinding, err error)
|
||||
Get(name string) (*rbac.ClusterRoleBinding, error)
|
||||
}
|
||||
|
||||
type clusterRoleBindingLister struct {
|
||||
indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *clusterRoleBindingLister) List(selector labels.Selector) (ret []*rbac.ClusterRoleBinding, err error) {
|
||||
err = cache.ListAll(s.indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*rbac.ClusterRoleBinding))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s clusterRoleBindingLister) Get(name string) (*rbac.ClusterRoleBinding, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(rbac.Resource("clusterrolebinding"), name)
|
||||
}
|
||||
return obj.(*rbac.ClusterRoleBinding), nil
|
||||
}
|
||||
|
||||
func (s clusterRoleBindingLister) ListClusterRoleBindings() ([]*rbac.ClusterRoleBinding, error) {
|
||||
return s.List(labels.Everything())
|
||||
}
|
||||
|
||||
type RoleLister interface {
|
||||
authorizerRoleGetter
|
||||
List(selector labels.Selector) (ret []*rbac.Role, err error)
|
||||
Roles(namespace string) RoleNamespaceLister
|
||||
}
|
||||
|
||||
type RoleNamespaceLister interface {
|
||||
List(selector labels.Selector) (ret []*rbac.Role, err error)
|
||||
Get(name string) (*rbac.Role, error)
|
||||
}
|
||||
|
||||
type roleLister struct {
|
||||
indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *roleLister) List(selector labels.Selector) (ret []*rbac.Role, err error) {
|
||||
err = cache.ListAll(s.indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*rbac.Role))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *roleLister) Roles(namespace string) RoleNamespaceLister {
|
||||
return roleNamespaceLister{indexer: s.indexer, namespace: namespace}
|
||||
}
|
||||
|
||||
func (s roleLister) GetRole(namespace, name string) (*rbac.Role, error) {
|
||||
return s.Roles(namespace).Get(name)
|
||||
}
|
||||
|
||||
type roleNamespaceLister struct {
|
||||
indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s roleNamespaceLister) List(selector labels.Selector) (ret []*rbac.Role, err error) {
|
||||
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*rbac.Role))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s roleNamespaceLister) Get(name string) (*rbac.Role, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(rbac.Resource("role"), name)
|
||||
}
|
||||
return obj.(*rbac.Role), nil
|
||||
}
|
||||
|
||||
type RoleBindingLister interface {
|
||||
authorizerRoleBindingLister
|
||||
List(selector labels.Selector) (ret []*rbac.RoleBinding, err error)
|
||||
RoleBindings(namespace string) RoleBindingNamespaceLister
|
||||
}
|
||||
|
||||
type RoleBindingNamespaceLister interface {
|
||||
List(selector labels.Selector) (ret []*rbac.RoleBinding, err error)
|
||||
Get(name string) (*rbac.RoleBinding, error)
|
||||
}
|
||||
|
||||
type roleBindingLister struct {
|
||||
indexer cache.Indexer
|
||||
}
|
||||
|
||||
func (s *roleBindingLister) List(selector labels.Selector) (ret []*rbac.RoleBinding, err error) {
|
||||
err = cache.ListAll(s.indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*rbac.RoleBinding))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *roleBindingLister) RoleBindings(namespace string) RoleBindingNamespaceLister {
|
||||
return roleBindingNamespaceLister{indexer: s.indexer, namespace: namespace}
|
||||
}
|
||||
|
||||
func (s roleBindingLister) ListRoleBindings(namespace string) ([]*rbac.RoleBinding, error) {
|
||||
return s.RoleBindings(namespace).List(labels.Everything())
|
||||
}
|
||||
|
||||
type roleBindingNamespaceLister struct {
|
||||
indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s roleBindingNamespaceLister) List(selector labels.Selector) (ret []*rbac.RoleBinding, err error) {
|
||||
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*rbac.RoleBinding))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s roleBindingNamespaceLister) Get(name string) (*rbac.RoleBinding, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(rbac.Resource("rolebinding"), name)
|
||||
}
|
||||
return obj.(*rbac.RoleBinding), nil
|
||||
}
|
@@ -1,614 +0,0 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package listers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
|
||||
)
|
||||
|
||||
func TestStoreToNodeLister(t *testing.T) {
|
||||
store := cache.NewStore(cache.MetaNamespaceKeyFunc)
|
||||
ids := sets.NewString("foo", "bar", "baz")
|
||||
for id := range ids {
|
||||
store.Add(&v1.Node{ObjectMeta: metav1.ObjectMeta{Name: id}})
|
||||
}
|
||||
sml := StoreToNodeLister{store}
|
||||
|
||||
gotNodes, err := sml.List()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
got := make([]string, len(gotNodes.Items))
|
||||
for ix := range gotNodes.Items {
|
||||
got[ix] = gotNodes.Items[ix].Name
|
||||
}
|
||||
if !ids.HasAll(got...) || len(got) != len(ids) {
|
||||
t.Errorf("Expected %v, got %v", ids, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreToNodeConditionLister(t *testing.T) {
|
||||
store := cache.NewStore(cache.MetaNamespaceKeyFunc)
|
||||
nodes := []*v1.Node{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Status: v1.NodeStatus{
|
||||
Conditions: []v1.NodeCondition{
|
||||
{
|
||||
Type: v1.NodeReady,
|
||||
Status: v1.ConditionTrue,
|
||||
},
|
||||
{
|
||||
Type: v1.NodeOutOfDisk,
|
||||
Status: v1.ConditionFalse,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
Status: v1.NodeStatus{
|
||||
Conditions: []v1.NodeCondition{
|
||||
{
|
||||
Type: v1.NodeOutOfDisk,
|
||||
Status: v1.ConditionTrue,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "baz"},
|
||||
Status: v1.NodeStatus{
|
||||
Conditions: []v1.NodeCondition{
|
||||
{
|
||||
Type: v1.NodeReady,
|
||||
Status: v1.ConditionFalse,
|
||||
},
|
||||
{
|
||||
Type: v1.NodeOutOfDisk,
|
||||
Status: v1.ConditionUnknown,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, n := range nodes {
|
||||
store.Add(n)
|
||||
}
|
||||
|
||||
predicate := func(node *v1.Node) bool {
|
||||
for _, cond := range node.Status.Conditions {
|
||||
if cond.Type == v1.NodeOutOfDisk && cond.Status == v1.ConditionTrue {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
snl := StoreToNodeLister{store}
|
||||
sncl := snl.NodeCondition(predicate)
|
||||
|
||||
want := sets.NewString("foo", "baz")
|
||||
gotNodes, err := sncl.List()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
got := make([]string, len(gotNodes))
|
||||
for ix := range gotNodes {
|
||||
got[ix] = gotNodes[ix].Name
|
||||
}
|
||||
if !want.HasAll(got...) || len(got) != len(want) {
|
||||
t.Errorf("Expected %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreToReplicationControllerLister(t *testing.T) {
|
||||
testCases := []struct {
|
||||
description string
|
||||
inRCs []*v1.ReplicationController
|
||||
list func(StoreToReplicationControllerLister) ([]*v1.ReplicationController, error)
|
||||
outRCNames sets.String
|
||||
expectErr bool
|
||||
onlyIfIndexedByNamespace bool
|
||||
}{
|
||||
{
|
||||
description: "Verify we can search all namespaces",
|
||||
inRCs: []*v1.ReplicationController{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "bar"},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "hmm", Namespace: "hmm"},
|
||||
},
|
||||
},
|
||||
list: func(lister StoreToReplicationControllerLister) ([]*v1.ReplicationController, error) {
|
||||
return lister.ReplicationControllers(metav1.NamespaceAll).List(labels.Set{}.AsSelectorPreValidated())
|
||||
},
|
||||
outRCNames: sets.NewString("hmm", "foo"),
|
||||
},
|
||||
{
|
||||
description: "Verify we can search a specific namespace",
|
||||
inRCs: []*v1.ReplicationController{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "bar"},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "hmm", Namespace: "hmm"},
|
||||
},
|
||||
},
|
||||
list: func(lister StoreToReplicationControllerLister) ([]*v1.ReplicationController, error) {
|
||||
return lister.ReplicationControllers("hmm").List(labels.Set{}.AsSelectorPreValidated())
|
||||
},
|
||||
outRCNames: sets.NewString("hmm"),
|
||||
},
|
||||
{
|
||||
description: "Basic listing with all labels and no selectors",
|
||||
inRCs: []*v1.ReplicationController{
|
||||
{ObjectMeta: metav1.ObjectMeta{Name: "basic"}},
|
||||
},
|
||||
list: func(lister StoreToReplicationControllerLister) ([]*v1.ReplicationController, error) {
|
||||
return lister.List(labels.Everything())
|
||||
},
|
||||
outRCNames: sets.NewString("basic"),
|
||||
},
|
||||
{
|
||||
description: "No pod labels",
|
||||
inRCs: []*v1.ReplicationController{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "basic", Namespace: "ns"},
|
||||
Spec: v1.ReplicationControllerSpec{
|
||||
Selector: map[string]string{"foo": "baz"},
|
||||
},
|
||||
},
|
||||
},
|
||||
list: func(lister StoreToReplicationControllerLister) ([]*v1.ReplicationController, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "ns"},
|
||||
}
|
||||
return lister.GetPodControllers(pod)
|
||||
},
|
||||
outRCNames: sets.NewString(),
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
description: "No RC selectors",
|
||||
inRCs: []*v1.ReplicationController{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "basic", Namespace: "ns"},
|
||||
},
|
||||
},
|
||||
list: func(lister StoreToReplicationControllerLister) ([]*v1.ReplicationController, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod1",
|
||||
Namespace: "ns",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
}
|
||||
return lister.GetPodControllers(pod)
|
||||
},
|
||||
outRCNames: sets.NewString(),
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
description: "Matching labels to selectors and namespace",
|
||||
inRCs: []*v1.ReplicationController{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Spec: v1.ReplicationControllerSpec{
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "ns"},
|
||||
Spec: v1.ReplicationControllerSpec{
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
list: func(lister StoreToReplicationControllerLister) ([]*v1.ReplicationController, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod1",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
Namespace: "ns",
|
||||
},
|
||||
}
|
||||
return lister.GetPodControllers(pod)
|
||||
},
|
||||
outRCNames: sets.NewString("bar"),
|
||||
onlyIfIndexedByNamespace: true,
|
||||
},
|
||||
}
|
||||
for _, c := range testCases {
|
||||
for _, withIndex := range []bool{true, false} {
|
||||
if c.onlyIfIndexedByNamespace && !withIndex {
|
||||
continue
|
||||
}
|
||||
var store cache.Indexer
|
||||
if withIndex {
|
||||
store = cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
} else {
|
||||
store = cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})
|
||||
}
|
||||
|
||||
for _, r := range c.inRCs {
|
||||
store.Add(r)
|
||||
}
|
||||
|
||||
gotControllers, err := c.list(StoreToReplicationControllerLister{store})
|
||||
if err != nil && c.expectErr {
|
||||
continue
|
||||
} else if c.expectErr {
|
||||
t.Errorf("(%q, withIndex=%v) Expected error, got none", c.description, withIndex)
|
||||
continue
|
||||
} else if err != nil {
|
||||
t.Errorf("(%q, withIndex=%v) Unexpected error %#v", c.description, withIndex, err)
|
||||
continue
|
||||
}
|
||||
gotNames := make([]string, len(gotControllers))
|
||||
for ix := range gotControllers {
|
||||
gotNames[ix] = gotControllers[ix].Name
|
||||
}
|
||||
if !c.outRCNames.HasAll(gotNames...) || len(gotNames) != len(c.outRCNames) {
|
||||
t.Errorf("(%q, withIndex=%v) Unexpected got controllers %+v expected %+v", c.description, withIndex, gotNames, c.outRCNames)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreToReplicaSetLister(t *testing.T) {
|
||||
store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
lister := StoreToReplicaSetLister{store}
|
||||
testCases := []struct {
|
||||
inRSs []*extensions.ReplicaSet
|
||||
list func() ([]*extensions.ReplicaSet, error)
|
||||
outRSNames sets.String
|
||||
expectErr bool
|
||||
}{
|
||||
// Basic listing with all labels and no selectors
|
||||
{
|
||||
inRSs: []*extensions.ReplicaSet{
|
||||
{ObjectMeta: metav1.ObjectMeta{Name: "basic"}},
|
||||
},
|
||||
list: func() ([]*extensions.ReplicaSet, error) {
|
||||
return lister.List(labels.Everything())
|
||||
},
|
||||
outRSNames: sets.NewString("basic"),
|
||||
},
|
||||
// No pod labels
|
||||
{
|
||||
inRSs: []*extensions.ReplicaSet{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "basic", Namespace: "ns"},
|
||||
Spec: extensions.ReplicaSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "baz"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
list: func() ([]*extensions.ReplicaSet, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "ns"},
|
||||
}
|
||||
return lister.GetPodReplicaSets(pod)
|
||||
},
|
||||
outRSNames: sets.NewString(),
|
||||
expectErr: true,
|
||||
},
|
||||
// No ReplicaSet selectors
|
||||
{
|
||||
inRSs: []*extensions.ReplicaSet{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "basic", Namespace: "ns"},
|
||||
},
|
||||
},
|
||||
list: func() ([]*extensions.ReplicaSet, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod1",
|
||||
Namespace: "ns",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
}
|
||||
return lister.GetPodReplicaSets(pod)
|
||||
},
|
||||
outRSNames: sets.NewString(),
|
||||
expectErr: true,
|
||||
},
|
||||
// Matching labels to selectors and namespace
|
||||
{
|
||||
inRSs: []*extensions.ReplicaSet{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Spec: extensions.ReplicaSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "ns"},
|
||||
Spec: extensions.ReplicaSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
list: func() ([]*extensions.ReplicaSet, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod1",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
Namespace: "ns",
|
||||
},
|
||||
}
|
||||
return lister.GetPodReplicaSets(pod)
|
||||
},
|
||||
outRSNames: sets.NewString("bar"),
|
||||
},
|
||||
}
|
||||
for _, c := range testCases {
|
||||
for _, r := range c.inRSs {
|
||||
store.Add(r)
|
||||
}
|
||||
|
||||
gotRSs, err := c.list()
|
||||
if err != nil && c.expectErr {
|
||||
continue
|
||||
} else if c.expectErr {
|
||||
t.Error("Expected error, got none")
|
||||
continue
|
||||
} else if err != nil {
|
||||
t.Errorf("Unexpected error %#v", err)
|
||||
continue
|
||||
}
|
||||
gotNames := make([]string, len(gotRSs))
|
||||
for ix := range gotRSs {
|
||||
gotNames[ix] = gotRSs[ix].Name
|
||||
}
|
||||
if !c.outRSNames.HasAll(gotNames...) || len(gotNames) != len(c.outRSNames) {
|
||||
t.Errorf("Unexpected got ReplicaSets %+v expected %+v", gotNames, c.outRSNames)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreToDaemonSetLister(t *testing.T) {
|
||||
store := cache.NewStore(cache.MetaNamespaceKeyFunc)
|
||||
lister := StoreToDaemonSetLister{store}
|
||||
testCases := []struct {
|
||||
inDSs []*extensions.DaemonSet
|
||||
list func() ([]extensions.DaemonSet, error)
|
||||
outDaemonSetNames sets.String
|
||||
expectErr bool
|
||||
}{
|
||||
// Basic listing
|
||||
{
|
||||
inDSs: []*extensions.DaemonSet{
|
||||
{ObjectMeta: metav1.ObjectMeta{Name: "basic"}},
|
||||
},
|
||||
list: func() ([]extensions.DaemonSet, error) {
|
||||
list, err := lister.List()
|
||||
return list.Items, err
|
||||
},
|
||||
outDaemonSetNames: sets.NewString("basic"),
|
||||
},
|
||||
// Listing multiple daemon sets
|
||||
{
|
||||
inDSs: []*extensions.DaemonSet{
|
||||
{ObjectMeta: metav1.ObjectMeta{Name: "basic"}},
|
||||
{ObjectMeta: metav1.ObjectMeta{Name: "complex"}},
|
||||
{ObjectMeta: metav1.ObjectMeta{Name: "complex2"}},
|
||||
},
|
||||
list: func() ([]extensions.DaemonSet, error) {
|
||||
list, err := lister.List()
|
||||
return list.Items, err
|
||||
},
|
||||
outDaemonSetNames: sets.NewString("basic", "complex", "complex2"),
|
||||
},
|
||||
// No pod labels
|
||||
{
|
||||
inDSs: []*extensions.DaemonSet{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "basic", Namespace: "ns"},
|
||||
Spec: extensions.DaemonSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "baz"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
list: func() ([]extensions.DaemonSet, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "ns"},
|
||||
}
|
||||
return lister.GetPodDaemonSets(pod)
|
||||
},
|
||||
outDaemonSetNames: sets.NewString(),
|
||||
expectErr: true,
|
||||
},
|
||||
// No DS selectors
|
||||
{
|
||||
inDSs: []*extensions.DaemonSet{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "basic", Namespace: "ns"},
|
||||
},
|
||||
},
|
||||
list: func() ([]extensions.DaemonSet, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod1",
|
||||
Namespace: "ns",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
}
|
||||
return lister.GetPodDaemonSets(pod)
|
||||
},
|
||||
outDaemonSetNames: sets.NewString(),
|
||||
expectErr: true,
|
||||
},
|
||||
// Matching labels to selectors and namespace
|
||||
{
|
||||
inDSs: []*extensions.DaemonSet{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Spec: extensions.DaemonSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "ns"},
|
||||
Spec: extensions.DaemonSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
list: func() ([]extensions.DaemonSet, error) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod1",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
Namespace: "ns",
|
||||
},
|
||||
}
|
||||
return lister.GetPodDaemonSets(pod)
|
||||
},
|
||||
outDaemonSetNames: sets.NewString("bar"),
|
||||
},
|
||||
}
|
||||
for _, c := range testCases {
|
||||
for _, r := range c.inDSs {
|
||||
store.Add(r)
|
||||
}
|
||||
|
||||
daemonSets, err := c.list()
|
||||
if err != nil && c.expectErr {
|
||||
continue
|
||||
} else if c.expectErr {
|
||||
t.Error("Expected error, got none")
|
||||
continue
|
||||
} else if err != nil {
|
||||
t.Errorf("Unexpected error %#v", err)
|
||||
continue
|
||||
}
|
||||
daemonSetNames := make([]string, len(daemonSets))
|
||||
for ix := range daemonSets {
|
||||
daemonSetNames[ix] = daemonSets[ix].Name
|
||||
}
|
||||
if !c.outDaemonSetNames.HasAll(daemonSetNames...) || len(daemonSetNames) != len(c.outDaemonSetNames) {
|
||||
t.Errorf("Unexpected got controllers %+v expected %+v", daemonSetNames, c.outDaemonSetNames)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreToPodLister(t *testing.T) {
|
||||
// We test with and without a namespace index, because StoreToPodLister has
|
||||
// special logic to work on namespaces even when no namespace index is
|
||||
// present.
|
||||
stores := []cache.Indexer{
|
||||
cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}),
|
||||
cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}),
|
||||
}
|
||||
for _, store := range stores {
|
||||
ids := []string{"foo", "bar", "baz"}
|
||||
for _, id := range ids {
|
||||
store.Add(&v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "other",
|
||||
Name: id,
|
||||
Labels: map[string]string{"name": id},
|
||||
},
|
||||
})
|
||||
}
|
||||
store.Add(&v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "quux",
|
||||
Namespace: metav1.NamespaceDefault,
|
||||
Labels: map[string]string{"name": "quux"},
|
||||
},
|
||||
})
|
||||
spl := StoreToPodLister{store}
|
||||
|
||||
// Verify that we can always look up by Namespace.
|
||||
defaultPods, err := spl.Pods(metav1.NamespaceDefault).List(labels.Set{}.AsSelectorPreValidated())
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
} else if e, a := 1, len(defaultPods); e != a {
|
||||
t.Errorf("Expected %v, got %v", e, a)
|
||||
} else if e, a := "quux", defaultPods[0].Name; e != a {
|
||||
t.Errorf("Expected %v, got %v", e, a)
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
got, err := spl.List(labels.Set{"name": id}.AsSelectorPreValidated())
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
continue
|
||||
}
|
||||
if e, a := 1, len(got); e != a {
|
||||
t.Errorf("Expected %v, got %v", e, a)
|
||||
continue
|
||||
}
|
||||
if e, a := id, got[0].Name; e != a {
|
||||
t.Errorf("Expected %v, got %v", e, a)
|
||||
continue
|
||||
}
|
||||
|
||||
_, err = spl.Pods("other").Get(id)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := spl.Pods("").Get("qux"); !apierrors.IsNotFound(err) {
|
||||
t.Error("Unexpected pod exists")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreToServiceLister(t *testing.T) {
|
||||
store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
store.Add(&v1.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Selector: map[string]string{},
|
||||
},
|
||||
})
|
||||
store.Add(&v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "bar"}})
|
||||
ssl := StoreToServiceLister{store}
|
||||
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foopod",
|
||||
Labels: map[string]string{"role": "foo"},
|
||||
},
|
||||
}
|
||||
|
||||
services, err := ssl.GetPodServices(pod)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if len(services) != 1 {
|
||||
t.Fatalf("Expected 1 service, got %v", len(services))
|
||||
}
|
||||
if e, a := "foo", services[0].Name; e != a {
|
||||
t.Errorf("Expected service %q, got %q", e, a)
|
||||
}
|
||||
}
|
@@ -1,56 +0,0 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"batch.go",
|
||||
"core.go",
|
||||
"extensions.go",
|
||||
"factory.go",
|
||||
"generic.go",
|
||||
"rbac.go",
|
||||
"storage.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/apis/batch:go_default_library",
|
||||
"//pkg/apis/batch/v1:go_default_library",
|
||||
"//pkg/apis/extensions:go_default_library",
|
||||
"//pkg/apis/extensions/v1beta1:go_default_library",
|
||||
"//pkg/apis/rbac:go_default_library",
|
||||
"//pkg/apis/storage/v1beta1:go_default_library",
|
||||
"//pkg/client/clientset_generated/clientset:go_default_library",
|
||||
"//pkg/client/clientset_generated/internalclientset:go_default_library",
|
||||
"//pkg/client/legacylisters:go_default_library",
|
||||
"//pkg/client/listers/batch/v1:go_default_library",
|
||||
"//pkg/client/listers/core/internalversion:go_default_library",
|
||||
"//vendor:github.com/golang/glog",
|
||||
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
|
||||
"//vendor:k8s.io/apimachinery/pkg/watch",
|
||||
"//vendor:k8s.io/client-go/tools/cache",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
@@ -1,6 +0,0 @@
|
||||
reviewers:
|
||||
- derekwaynecarr
|
||||
- caesarxuchao
|
||||
- wojtek-t
|
||||
- lavalamp
|
||||
- deads2k
|
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package informers
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
batch "k8s.io/kubernetes/pkg/apis/batch/v1"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
||||
batchv1listers "k8s.io/kubernetes/pkg/client/listers/batch/v1"
|
||||
)
|
||||
|
||||
// JobInformer is type of SharedIndexInformer which watches and lists all jobs.
|
||||
// Interface provides constructor for informer and lister for jobs
|
||||
type JobInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() batchv1listers.JobLister
|
||||
}
|
||||
|
||||
type jobInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether jobInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// jobInformer and connects it to sharedInformerFactory
|
||||
func (f *jobInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&batch.Job{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewJobInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// NewJobInformer returns a SharedIndexInformer that lists and watches all jobs
|
||||
func NewJobInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Batch().Jobs(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Batch().Jobs(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&batch.Job{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// Lister returns lister for jobInformer
|
||||
func (f *jobInformer) Lister() batchv1listers.JobLister {
|
||||
informer := f.Informer()
|
||||
return batchv1listers.NewJobLister(informer.GetIndexer())
|
||||
}
|
@@ -1,576 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package informers
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
"k8s.io/kubernetes/pkg/client/legacylisters"
|
||||
coreinternallisters "k8s.io/kubernetes/pkg/client/listers/core/internalversion"
|
||||
)
|
||||
|
||||
// PodInformer is type of SharedIndexInformer which watches and lists all pods.
|
||||
// Interface provides constructor for informer and lister for pods
|
||||
type PodInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToPodLister
|
||||
}
|
||||
|
||||
type podInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether podInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// podInformer and connects it to sharedInformerFactory
|
||||
func (f *podInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&v1.Pod{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewPodInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for podInformer
|
||||
func (f *podInformer) Lister() *listers.StoreToPodLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToPodLister{Indexer: informer.GetIndexer()}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// NamespaceInformer is type of SharedIndexInformer which watches and lists all namespaces.
|
||||
// Interface provides constructor for informer and lister for namsespaces
|
||||
type NamespaceInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.IndexerToNamespaceLister
|
||||
}
|
||||
|
||||
type namespaceInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether namespaceInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// namespaceInformer and connects it to sharedInformerFactory
|
||||
func (f *namespaceInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&v1.Namespace{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewNamespaceInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for namespaceInformer
|
||||
func (f *namespaceInformer) Lister() *listers.IndexerToNamespaceLister {
|
||||
informer := f.Informer()
|
||||
return &listers.IndexerToNamespaceLister{Indexer: informer.GetIndexer()}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// InternalNamespaceInformer is type of SharedIndexInformer which watches and lists all namespaces.
|
||||
// Interface provides constructor for informer and lister for namsespaces
|
||||
type InternalNamespaceInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() coreinternallisters.NamespaceLister
|
||||
}
|
||||
|
||||
type internalNamespaceInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether internalNamespaceInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// internalNamespaceInformer and connects it to sharedInformerFactory
|
||||
func (f *internalNamespaceInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&api.Namespace{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewInternalNamespaceInformer(f.internalclient, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for internalNamespaceInformer
|
||||
func (f *internalNamespaceInformer) Lister() coreinternallisters.NamespaceLister {
|
||||
informer := f.Informer()
|
||||
return coreinternallisters.NewNamespaceLister(informer.GetIndexer())
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// NodeInformer is type of SharedIndexInformer which watches and lists all nodes.
|
||||
// Interface provides constructor for informer and lister for nodes
|
||||
type NodeInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToNodeLister
|
||||
}
|
||||
|
||||
type nodeInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether nodeInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// nodeInformer and connects it to sharedInformerFactory
|
||||
func (f *nodeInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&v1.Node{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewNodeInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for nodeInformer
|
||||
func (f *nodeInformer) Lister() *listers.StoreToNodeLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToNodeLister{Store: informer.GetStore()}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// PVCInformer is type of SharedIndexInformer which watches and lists all persistent volume claims.
|
||||
// Interface provides constructor for informer and lister for persistent volume claims
|
||||
type PVCInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToPersistentVolumeClaimLister
|
||||
}
|
||||
|
||||
type pvcInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether pvcInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// pvcInformer and connects it to sharedInformerFactory
|
||||
func (f *pvcInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&v1.PersistentVolumeClaim{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewPVCInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for pvcInformer
|
||||
func (f *pvcInformer) Lister() *listers.StoreToPersistentVolumeClaimLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToPersistentVolumeClaimLister{Indexer: informer.GetIndexer()}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// PVInformer is type of SharedIndexInformer which watches and lists all persistent volumes.
|
||||
// Interface provides constructor for informer and lister for persistent volumes
|
||||
type PVInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToPVFetcher
|
||||
}
|
||||
|
||||
type pvInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether pvInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// pvInformer and connects it to sharedInformerFactory
|
||||
func (f *pvInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&v1.PersistentVolume{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewPVInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for pvInformer
|
||||
func (f *pvInformer) Lister() *listers.StoreToPVFetcher {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToPVFetcher{Store: informer.GetStore()}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// LimitRangeInformer is type of SharedIndexInformer which watches and lists all limit ranges.
|
||||
// Interface provides constructor for informer and lister for limit ranges.
|
||||
type LimitRangeInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToLimitRangeLister
|
||||
}
|
||||
|
||||
type limitRangeInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether pvcInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// limitRangeInformer and connects it to sharedInformerFactory
|
||||
func (f *limitRangeInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&v1.LimitRange{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewLimitRangeInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for limitRangeInformer
|
||||
func (f *limitRangeInformer) Lister() *listers.StoreToLimitRangeLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToLimitRangeLister{Indexer: informer.GetIndexer()}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// InternalLimitRangeInformer is type of SharedIndexInformer which watches and lists all limit ranges.
|
||||
// Interface provides constructor for informer and lister for limit ranges.
|
||||
type InternalLimitRangeInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() coreinternallisters.LimitRangeLister
|
||||
}
|
||||
|
||||
type internalLimitRangeInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether pvcInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// internalLimitRangeInformer and connects it to sharedInformerFactory
|
||||
func (f *internalLimitRangeInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&api.LimitRange{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewInternalLimitRangeInformer(f.internalclient, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for internalLimitRangeInformer
|
||||
func (f *internalLimitRangeInformer) Lister() coreinternallisters.LimitRangeLister {
|
||||
informer := f.Informer()
|
||||
return coreinternallisters.NewLimitRangeLister(informer.GetIndexer())
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// ReplicationControllerInformer is type of SharedIndexInformer which watches and lists all replication controllers.
|
||||
// Interface provides constructor for informer and lister for replication controllers.
|
||||
type ReplicationControllerInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToReplicationControllerLister
|
||||
}
|
||||
|
||||
type replicationControllerInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether replicationControllerInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// replicationControllerInformer and connects it to sharedInformerFactory
|
||||
func (f *replicationControllerInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&v1.ReplicationController{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewReplicationControllerInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for replicationControllerInformer
|
||||
func (f *replicationControllerInformer) Lister() *listers.StoreToReplicationControllerLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToReplicationControllerLister{Indexer: informer.GetIndexer()}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
// NewPodInformer returns a SharedIndexInformer that lists and watches all pods
|
||||
func NewPodInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().Pods(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().Pods(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&v1.Pod{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// NewNodeInformer returns a SharedIndexInformer that lists and watches all nodes
|
||||
func NewNodeInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().Nodes().List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().Nodes().Watch(options)
|
||||
},
|
||||
},
|
||||
&v1.Node{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{})
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// NewPVCInformer returns a SharedIndexInformer that lists and watches all PVCs
|
||||
func NewPVCInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().PersistentVolumeClaims(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().PersistentVolumeClaims(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&v1.PersistentVolumeClaim{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// NewPVInformer returns a SharedIndexInformer that lists and watches all PVs
|
||||
func NewPVInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().PersistentVolumes().List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().PersistentVolumes().Watch(options)
|
||||
},
|
||||
},
|
||||
&v1.PersistentVolume{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{})
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// NewNamespaceInformer returns a SharedIndexInformer that lists and watches namespaces
|
||||
func NewNamespaceInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().Namespaces().List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().Namespaces().Watch(options)
|
||||
},
|
||||
},
|
||||
&v1.Namespace{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{})
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// NewInternalNamespaceInformer returns a SharedIndexInformer that lists and watches namespaces
|
||||
func NewInternalNamespaceInformer(client internalclientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().Namespaces().List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().Namespaces().Watch(options)
|
||||
},
|
||||
},
|
||||
&api.Namespace{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{})
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// NewLimitRangeInformer returns a SharedIndexInformer that lists and watches all LimitRanges
|
||||
func NewLimitRangeInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().LimitRanges(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().LimitRanges(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&v1.LimitRange{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// NewInternalLimitRangeInformer returns a SharedIndexInformer that lists and watches all LimitRanges
|
||||
func NewInternalLimitRangeInformer(internalclient internalclientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return internalclient.Core().LimitRanges(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return internalclient.Core().LimitRanges(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&api.LimitRange{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
// NewReplicationControllerInformer returns a SharedIndexInformer that lists and watches all replication controllers.
|
||||
func NewReplicationControllerInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().ReplicationControllers(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().ReplicationControllers(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&v1.ReplicationController{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
// ServiceAccountInformer is type of SharedIndexInformer which watches and lists all ServiceAccounts.
|
||||
// Interface provides constructor for informer and lister for ServiceAccounts
|
||||
type ServiceAccountInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToServiceAccountLister
|
||||
}
|
||||
|
||||
type serviceAccountInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
// Informer checks whether ServiceAccountInformer exists in sharedInformerFactory and if not, it creates new informer of type
|
||||
// ServiceAccountInformer and connects it to sharedInformerFactory
|
||||
func (f *serviceAccountInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&v1.ServiceAccount{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = NewServiceAccountInformer(f.client, f.defaultResync)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
// Lister returns lister for ServiceAccountInformer
|
||||
func (f *serviceAccountInformer) Lister() *listers.StoreToServiceAccountLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToServiceAccountLister{Indexer: informer.GetIndexer()}
|
||||
}
|
||||
|
||||
// NewServiceAccountInformer returns a SharedIndexInformer that lists and watches all ServiceAccounts
|
||||
func NewServiceAccountInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
|
||||
sharedIndexInformer := cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return client.Core().ServiceAccounts(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return client.Core().ServiceAccounts(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&v1.ServiceAccount{},
|
||||
resyncPeriod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
|
||||
return sharedIndexInformer
|
||||
}
|
@@ -1,155 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package informers
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
|
||||
"k8s.io/kubernetes/pkg/client/legacylisters"
|
||||
)
|
||||
|
||||
// DaemonSetInformer is type of SharedIndexInformer which watches and lists all pods.
|
||||
// Interface provides constructor for informer and lister for pods
|
||||
type DaemonSetInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToDaemonSetLister
|
||||
}
|
||||
|
||||
type daemonSetInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
func (f *daemonSetInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&extensions.DaemonSet{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return f.client.Extensions().DaemonSets(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return f.client.Extensions().DaemonSets(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&extensions.DaemonSet{},
|
||||
f.defaultResync,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
func (f *daemonSetInformer) Lister() *listers.StoreToDaemonSetLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToDaemonSetLister{Store: informer.GetIndexer()}
|
||||
}
|
||||
|
||||
// DeploymentInformer is a type of SharedIndexInformer which watches and lists all deployments.
|
||||
type DeploymentInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToDeploymentLister
|
||||
}
|
||||
|
||||
type deploymentInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
func (f *deploymentInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&extensions.Deployment{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return f.client.Extensions().Deployments(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return f.client.Extensions().Deployments(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&extensions.Deployment{},
|
||||
f.defaultResync,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
func (f *deploymentInformer) Lister() *listers.StoreToDeploymentLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToDeploymentLister{Indexer: informer.GetIndexer()}
|
||||
}
|
||||
|
||||
// ReplicaSetInformer is a type of SharedIndexInformer which watches and lists all replicasets.
|
||||
type ReplicaSetInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() *listers.StoreToReplicaSetLister
|
||||
}
|
||||
|
||||
type replicaSetInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
func (f *replicaSetInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&extensions.ReplicaSet{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return f.client.Extensions().ReplicaSets(metav1.NamespaceAll).List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return f.client.Extensions().ReplicaSets(metav1.NamespaceAll).Watch(options)
|
||||
},
|
||||
},
|
||||
&extensions.ReplicaSet{},
|
||||
f.defaultResync,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
func (f *replicaSetInformer) Lister() *listers.StoreToReplicaSetLister {
|
||||
informer := f.Informer()
|
||||
return &listers.StoreToReplicaSetLister{Indexer: informer.GetIndexer()}
|
||||
}
|
@@ -1,193 +0,0 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package informers
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
)
|
||||
|
||||
// SharedInformerFactory provides interface which holds unique informers for pods, nodes, namespaces, persistent volume
|
||||
// claims and persistent volumes
|
||||
type SharedInformerFactory interface {
|
||||
// Start starts informers that can start AFTER the API server and controllers have started
|
||||
Start(stopCh <-chan struct{})
|
||||
|
||||
ForResource(schema.GroupResource) (GenericInformer, error)
|
||||
|
||||
// when you update these, update generic.go/ForResource, same package
|
||||
|
||||
Pods() PodInformer
|
||||
LimitRanges() LimitRangeInformer
|
||||
InternalLimitRanges() InternalLimitRangeInformer
|
||||
Namespaces() NamespaceInformer
|
||||
InternalNamespaces() InternalNamespaceInformer
|
||||
Nodes() NodeInformer
|
||||
PersistentVolumeClaims() PVCInformer
|
||||
PersistentVolumes() PVInformer
|
||||
ServiceAccounts() ServiceAccountInformer
|
||||
|
||||
DaemonSets() DaemonSetInformer
|
||||
Deployments() DeploymentInformer
|
||||
ReplicaSets() ReplicaSetInformer
|
||||
ReplicationControllers() ReplicationControllerInformer
|
||||
|
||||
ClusterRoleBindings() ClusterRoleBindingInformer
|
||||
ClusterRoles() ClusterRoleInformer
|
||||
RoleBindings() RoleBindingInformer
|
||||
Roles() RoleInformer
|
||||
|
||||
StorageClasses() StorageClassInformer
|
||||
|
||||
Jobs() JobInformer
|
||||
}
|
||||
|
||||
type sharedInformerFactory struct {
|
||||
client clientset.Interface
|
||||
// for admission plugins etc.
|
||||
internalclient internalclientset.Interface
|
||||
lock sync.Mutex
|
||||
defaultResync time.Duration
|
||||
|
||||
informers map[reflect.Type]cache.SharedIndexInformer
|
||||
// startedInformers is used for tracking which informers have been started
|
||||
// this allows calling of Start method multiple times
|
||||
startedInformers map[reflect.Type]bool
|
||||
}
|
||||
|
||||
// NewSharedInformerFactory constructs a new instance of sharedInformerFactory
|
||||
func NewSharedInformerFactory(client clientset.Interface, internalclient internalclientset.Interface, defaultResync time.Duration) SharedInformerFactory {
|
||||
return &sharedInformerFactory{
|
||||
client: client,
|
||||
internalclient: internalclient,
|
||||
defaultResync: defaultResync,
|
||||
informers: make(map[reflect.Type]cache.SharedIndexInformer),
|
||||
startedInformers: make(map[reflect.Type]bool),
|
||||
}
|
||||
}
|
||||
|
||||
// Start initializes all requested informers.
|
||||
func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
glog.V(1).Infoln("Starting informer factory")
|
||||
|
||||
for informerType, informer := range f.informers {
|
||||
if !f.startedInformers[informerType] {
|
||||
glog.V(2).Infof("Starting informer for %v", informerType)
|
||||
go informer.Run(stopCh)
|
||||
f.startedInformers[informerType] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pods returns a SharedIndexInformer that lists and watches all pods
|
||||
func (f *sharedInformerFactory) Pods() PodInformer {
|
||||
return &podInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// Nodes returns a SharedIndexInformer that lists and watches all nodes
|
||||
func (f *sharedInformerFactory) Nodes() NodeInformer {
|
||||
return &nodeInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// Namespaces returns a SharedIndexInformer that lists and watches all namespaces
|
||||
func (f *sharedInformerFactory) Namespaces() NamespaceInformer {
|
||||
return &namespaceInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// InternalNamespaces returns a SharedIndexInformer that lists and watches all namespaces
|
||||
func (f *sharedInformerFactory) InternalNamespaces() InternalNamespaceInformer {
|
||||
return &internalNamespaceInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// PersistentVolumeClaims returns a SharedIndexInformer that lists and watches all persistent volume claims
|
||||
func (f *sharedInformerFactory) PersistentVolumeClaims() PVCInformer {
|
||||
return &pvcInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// PersistentVolumes returns a SharedIndexInformer that lists and watches all persistent volumes
|
||||
func (f *sharedInformerFactory) PersistentVolumes() PVInformer {
|
||||
return &pvInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// ServiceAccounts returns a SharedIndexInformer that lists and watches all service accounts.
|
||||
func (f *sharedInformerFactory) ServiceAccounts() ServiceAccountInformer {
|
||||
return &serviceAccountInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// DaemonSets returns a SharedIndexInformer that lists and watches all daemon sets.
|
||||
func (f *sharedInformerFactory) DaemonSets() DaemonSetInformer {
|
||||
return &daemonSetInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
func (f *sharedInformerFactory) Deployments() DeploymentInformer {
|
||||
return &deploymentInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
func (f *sharedInformerFactory) ReplicaSets() ReplicaSetInformer {
|
||||
return &replicaSetInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
func (f *sharedInformerFactory) ReplicationControllers() ReplicationControllerInformer {
|
||||
return &replicationControllerInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
func (f *sharedInformerFactory) ClusterRoles() ClusterRoleInformer {
|
||||
return &clusterRoleInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
func (f *sharedInformerFactory) ClusterRoleBindings() ClusterRoleBindingInformer {
|
||||
return &clusterRoleBindingInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
func (f *sharedInformerFactory) Roles() RoleInformer {
|
||||
return &roleInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
func (f *sharedInformerFactory) RoleBindings() RoleBindingInformer {
|
||||
return &roleBindingInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// LimitRanges returns a SharedIndexInformer that lists and watches all limit ranges.
|
||||
func (f *sharedInformerFactory) LimitRanges() LimitRangeInformer {
|
||||
return &limitRangeInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// InternalLimitRanges returns a SharedIndexInformer that lists and watches all limit ranges.
|
||||
func (f *sharedInformerFactory) InternalLimitRanges() InternalLimitRangeInformer {
|
||||
return &internalLimitRangeInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// StorageClasses returns a SharedIndexInformer that lists and watches all storage classes
|
||||
func (f *sharedInformerFactory) StorageClasses() StorageClassInformer {
|
||||
return &storageClassInformer{sharedInformerFactory: f}
|
||||
}
|
||||
|
||||
// Jobs returns a SharedIndexInformer that lists and watches all storage jobs
|
||||
func (f *sharedInformerFactory) Jobs() JobInformer {
|
||||
return &jobInformer{sharedInformerFactory: f}
|
||||
}
|
@@ -1,90 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package informers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/apis/batch"
|
||||
extensionsinternal "k8s.io/kubernetes/pkg/apis/extensions"
|
||||
rbacinternal "k8s.io/kubernetes/pkg/apis/rbac"
|
||||
)
|
||||
|
||||
// GenericInformer is type of SharedIndexInformer which will locate and delegate to other
|
||||
// sharedInformers based on type
|
||||
type GenericInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() cache.GenericLister
|
||||
}
|
||||
|
||||
// ForResource gives generic access to a shared informer of the matching type
|
||||
// TODO extend this to unknown resources with a client pool
|
||||
func (f *sharedInformerFactory) ForResource(resource schema.GroupResource) (GenericInformer, error) {
|
||||
switch resource {
|
||||
case api.Resource("pods"):
|
||||
return &genericInformer{resource: resource, informer: f.Pods().Informer()}, nil
|
||||
case api.Resource("limitranges"):
|
||||
return &genericInformer{resource: resource, informer: f.LimitRanges().Informer()}, nil
|
||||
case api.Resource("namespaces"):
|
||||
return &genericInformer{resource: resource, informer: f.Namespaces().Informer()}, nil
|
||||
case api.Resource("nodes"):
|
||||
return &genericInformer{resource: resource, informer: f.Nodes().Informer()}, nil
|
||||
case api.Resource("persistentvolumeclaims"):
|
||||
return &genericInformer{resource: resource, informer: f.PersistentVolumeClaims().Informer()}, nil
|
||||
case api.Resource("persistentvolumes"):
|
||||
return &genericInformer{resource: resource, informer: f.PersistentVolumes().Informer()}, nil
|
||||
case api.Resource("serviceaccounts"):
|
||||
return &genericInformer{resource: resource, informer: f.ServiceAccounts().Informer()}, nil
|
||||
|
||||
case extensionsinternal.Resource("daemonsets"):
|
||||
return &genericInformer{resource: resource, informer: f.DaemonSets().Informer()}, nil
|
||||
case extensionsinternal.Resource("deployments"):
|
||||
return &genericInformer{resource: resource, informer: f.Deployments().Informer()}, nil
|
||||
case extensionsinternal.Resource("replicasets"):
|
||||
return &genericInformer{resource: resource, informer: f.ReplicaSets().Informer()}, nil
|
||||
|
||||
case rbacinternal.Resource("clusterrolebindings"):
|
||||
return &genericInformer{resource: resource, informer: f.ClusterRoleBindings().Informer()}, nil
|
||||
case rbacinternal.Resource("clusterroles"):
|
||||
return &genericInformer{resource: resource, informer: f.ClusterRoles().Informer()}, nil
|
||||
case rbacinternal.Resource("rolebindings"):
|
||||
return &genericInformer{resource: resource, informer: f.RoleBindings().Informer()}, nil
|
||||
case rbacinternal.Resource("roles"):
|
||||
return &genericInformer{resource: resource, informer: f.Roles().Informer()}, nil
|
||||
|
||||
case batch.Resource("jobs"):
|
||||
return &genericInformer{resource: resource, informer: f.Jobs().Informer()}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("no informer found for %v", resource)
|
||||
}
|
||||
|
||||
type genericInformer struct {
|
||||
informer cache.SharedIndexInformer
|
||||
resource schema.GroupResource
|
||||
}
|
||||
|
||||
func (f *genericInformer) Informer() cache.SharedIndexInformer {
|
||||
return f.informer
|
||||
}
|
||||
|
||||
func (f *genericInformer) Lister() cache.GenericLister {
|
||||
return cache.NewGenericLister(f.Informer().GetIndexer(), f.resource)
|
||||
}
|
@@ -1,197 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package informers
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/apis/rbac"
|
||||
"k8s.io/kubernetes/pkg/client/legacylisters"
|
||||
)
|
||||
|
||||
type ClusterRoleInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() listers.ClusterRoleLister
|
||||
}
|
||||
|
||||
type clusterRoleInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
func (f *clusterRoleInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&rbac.ClusterRole{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return f.internalclient.Rbac().ClusterRoles().List(convertListOptionsOrDie(options))
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return f.internalclient.Rbac().ClusterRoles().Watch(convertListOptionsOrDie(options))
|
||||
},
|
||||
},
|
||||
&rbac.ClusterRole{},
|
||||
f.defaultResync,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
func (f *clusterRoleInformer) Lister() listers.ClusterRoleLister {
|
||||
return listers.NewClusterRoleLister(f.Informer().GetIndexer())
|
||||
}
|
||||
|
||||
type ClusterRoleBindingInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() listers.ClusterRoleBindingLister
|
||||
}
|
||||
|
||||
type clusterRoleBindingInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
func (f *clusterRoleBindingInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&rbac.ClusterRoleBinding{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return f.internalclient.Rbac().ClusterRoleBindings().List(convertListOptionsOrDie(options))
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return f.internalclient.Rbac().ClusterRoleBindings().Watch(convertListOptionsOrDie(options))
|
||||
},
|
||||
},
|
||||
&rbac.ClusterRoleBinding{},
|
||||
f.defaultResync,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
func (f *clusterRoleBindingInformer) Lister() listers.ClusterRoleBindingLister {
|
||||
return listers.NewClusterRoleBindingLister(f.Informer().GetIndexer())
|
||||
}
|
||||
|
||||
type RoleInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() listers.RoleLister
|
||||
}
|
||||
|
||||
type roleInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
func (f *roleInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&rbac.Role{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return f.internalclient.Rbac().Roles(metav1.NamespaceAll).List(convertListOptionsOrDie(options))
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return f.internalclient.Rbac().Roles(metav1.NamespaceAll).Watch(convertListOptionsOrDie(options))
|
||||
},
|
||||
},
|
||||
&rbac.Role{},
|
||||
f.defaultResync,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
func (f *roleInformer) Lister() listers.RoleLister {
|
||||
return listers.NewRoleLister(f.Informer().GetIndexer())
|
||||
}
|
||||
|
||||
type RoleBindingInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() listers.RoleBindingLister
|
||||
}
|
||||
|
||||
type roleBindingInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
func (f *roleBindingInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&rbac.RoleBinding{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return f.internalclient.Rbac().RoleBindings(metav1.NamespaceAll).List(convertListOptionsOrDie(options))
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return f.internalclient.Rbac().RoleBindings(metav1.NamespaceAll).Watch(convertListOptionsOrDie(options))
|
||||
},
|
||||
},
|
||||
&rbac.RoleBinding{},
|
||||
f.defaultResync,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
||||
)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
func (f *roleBindingInformer) Lister() listers.RoleBindingLister {
|
||||
return listers.NewRoleBindingLister(f.Informer().GetIndexer())
|
||||
}
|
||||
|
||||
func convertListOptionsOrDie(in metav1.ListOptions) metav1.ListOptions {
|
||||
out := metav1.ListOptions{}
|
||||
if err := api.Scheme.Convert(&in, &out, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return out
|
||||
}
|
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package informers
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
storage "k8s.io/kubernetes/pkg/apis/storage/v1beta1"
|
||||
"k8s.io/kubernetes/pkg/client/legacylisters"
|
||||
)
|
||||
|
||||
// StorageClassInformer is type of SharedIndexInformer which watches and lists all storage classes.
|
||||
// Interface provides constructor for informer and lister for storage classes
|
||||
type StorageClassInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() listers.StorageClassLister
|
||||
}
|
||||
|
||||
type storageClassInformer struct {
|
||||
*sharedInformerFactory
|
||||
}
|
||||
|
||||
func (f *storageClassInformer) Informer() cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
informerType := reflect.TypeOf(&storage.StorageClass{})
|
||||
informer, exists := f.informers[informerType]
|
||||
if exists {
|
||||
return informer
|
||||
}
|
||||
informer = cache.NewSharedIndexInformer(
|
||||
&cache.ListWatch{
|
||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||
return f.client.Storage().StorageClasses().List(options)
|
||||
},
|
||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||
return f.client.Storage().StorageClasses().Watch(options)
|
||||
},
|
||||
},
|
||||
&storage.StorageClass{},
|
||||
f.defaultResync,
|
||||
cache.Indexers{},
|
||||
)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
}
|
||||
|
||||
func (f *storageClassInformer) Lister() listers.StorageClassLister {
|
||||
informer := f.Informer()
|
||||
return listers.NewStorageClassLister(informer.GetIndexer())
|
||||
}
|
Reference in New Issue
Block a user