mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-03 08:04:45 +00:00
published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub) copied from https://github.com/kubernetes/kubernetes.git, branch master, last commit is e56cfc5322138aa23e6418ee30a6ab54c7c6fe8c
This commit is contained in:
236
tools/cache/listers.go
vendored
236
tools/cache/listers.go
vendored
@@ -20,16 +20,15 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/client-go/1.5/pkg/api"
|
||||
"k8s.io/client-go/1.5/pkg/api/errors"
|
||||
"k8s.io/client-go/1.5/pkg/api/meta"
|
||||
"k8s.io/client-go/1.5/pkg/api/unversioned"
|
||||
"k8s.io/client-go/1.5/pkg/apis/apps"
|
||||
"k8s.io/client-go/1.5/pkg/apis/batch"
|
||||
"k8s.io/client-go/1.5/pkg/apis/certificates"
|
||||
"k8s.io/client-go/1.5/pkg/apis/extensions"
|
||||
"k8s.io/client-go/1.5/pkg/apis/policy"
|
||||
"k8s.io/client-go/1.5/pkg/labels"
|
||||
"k8s.io/client-go/pkg/api"
|
||||
"k8s.io/client-go/pkg/api/meta"
|
||||
"k8s.io/client-go/pkg/api/unversioned"
|
||||
"k8s.io/client-go/pkg/apis/apps"
|
||||
"k8s.io/client-go/pkg/apis/batch"
|
||||
"k8s.io/client-go/pkg/apis/certificates"
|
||||
"k8s.io/client-go/pkg/apis/extensions"
|
||||
"k8s.io/client-go/pkg/apis/policy"
|
||||
"k8s.io/client-go/pkg/labels"
|
||||
)
|
||||
|
||||
// AppendFunc is used to add a matching item to whatever list the caller is using
|
||||
@@ -62,7 +61,7 @@ func ListAllByNamespace(indexer Indexer, namespace string, selector labels.Selec
|
||||
return nil
|
||||
}
|
||||
|
||||
items, err := indexer.Index(NamespaceIndex, api.ObjectMeta{Namespace: namespace})
|
||||
items, err := indexer.Index(NamespaceIndex, &api.ObjectMeta{Namespace: namespace})
|
||||
if err != nil {
|
||||
// Ignore error; do slow search without index.
|
||||
glog.Warningf("can not retrieve list of objects using index : %v", err)
|
||||
@@ -137,221 +136,6 @@ func (s storeToNodeConditionLister) List() (nodes []*api.Node, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// StoreToDeploymentLister gives a store List and Exists methods. The store must contain only Deployments.
|
||||
type StoreToDeploymentLister struct {
|
||||
Indexer
|
||||
}
|
||||
|
||||
// Exists checks if the given deployment exists in the store.
|
||||
func (s *StoreToDeploymentLister) Exists(deployment *extensions.Deployment) (bool, error) {
|
||||
_, exists, err := s.Indexer.Get(deployment)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// StoreToDeploymentLister lists all deployments in the store.
|
||||
// TODO: converge on the interface in pkg/client
|
||||
func (s *StoreToDeploymentLister) List() (deployments []extensions.Deployment, err error) {
|
||||
for _, c := range s.Indexer.List() {
|
||||
deployments = append(deployments, *(c.(*extensions.Deployment)))
|
||||
}
|
||||
return deployments, 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 := unversioned.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
|
||||
}
|
||||
|
||||
type storeToDeploymentNamespacer struct {
|
||||
indexer Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
// storeToDeploymentNamespacer lists deployments under its namespace in the store.
|
||||
func (s storeToDeploymentNamespacer) List(selector labels.Selector) (deployments []extensions.Deployment, err error) {
|
||||
if s.namespace == api.NamespaceAll {
|
||||
for _, m := range s.indexer.List() {
|
||||
d := *(m.(*extensions.Deployment))
|
||||
if selector.Matches(labels.Set(d.Labels)) {
|
||||
deployments = append(deployments, d)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
key := &extensions.Deployment{ObjectMeta: api.ObjectMeta{Namespace: s.namespace}}
|
||||
items, err := s.indexer.Index(NamespaceIndex, key)
|
||||
if err != nil {
|
||||
// Ignore error; do slow search without index.
|
||||
glog.Warningf("can not retrieve list of objects using index : %v", err)
|
||||
for _, m := range s.indexer.List() {
|
||||
d := *(m.(*extensions.Deployment))
|
||||
if s.namespace == d.Namespace && selector.Matches(labels.Set(d.Labels)) {
|
||||
deployments = append(deployments, d)
|
||||
}
|
||||
}
|
||||
return deployments, nil
|
||||
}
|
||||
for _, m := range items {
|
||||
d := *(m.(*extensions.Deployment))
|
||||
if selector.Matches(labels.Set(d.Labels)) {
|
||||
deployments = append(deployments, d)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *StoreToDeploymentLister) Deployments(namespace string) storeToDeploymentNamespacer {
|
||||
return storeToDeploymentNamespacer{s.Indexer, namespace}
|
||||
}
|
||||
|
||||
// GetDeploymentsForPods returns a list of deployments managing a pod. Returns an error only if no matching deployments are found.
|
||||
func (s *StoreToDeploymentLister) GetDeploymentsForPod(pod *api.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 := unversioned.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 gives a store List and Exists methods. The store must contain only ReplicaSets.
|
||||
type StoreToReplicaSetLister struct {
|
||||
Store
|
||||
}
|
||||
|
||||
// Exists checks if the given ReplicaSet exists in the store.
|
||||
func (s *StoreToReplicaSetLister) Exists(rs *extensions.ReplicaSet) (bool, error) {
|
||||
_, exists, err := s.Store.Get(rs)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// List lists all ReplicaSets in the store.
|
||||
// TODO: converge on the interface in pkg/client
|
||||
func (s *StoreToReplicaSetLister) List() (rss []extensions.ReplicaSet, err error) {
|
||||
for _, rs := range s.Store.List() {
|
||||
rss = append(rss, *(rs.(*extensions.ReplicaSet)))
|
||||
}
|
||||
return rss, nil
|
||||
}
|
||||
|
||||
type storeReplicaSetsNamespacer struct {
|
||||
store Store
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (s storeReplicaSetsNamespacer) List(selector labels.Selector) (rss []extensions.ReplicaSet, err error) {
|
||||
for _, c := range s.store.List() {
|
||||
rs := *(c.(*extensions.ReplicaSet))
|
||||
if s.namespace == api.NamespaceAll || s.namespace == rs.Namespace {
|
||||
if selector.Matches(labels.Set(rs.Labels)) {
|
||||
rss = append(rss, rs)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s storeReplicaSetsNamespacer) Get(name string) (*extensions.ReplicaSet, error) {
|
||||
obj, exists, err := s.store.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(extensions.Resource("replicaset"), name)
|
||||
}
|
||||
return obj.(*extensions.ReplicaSet), nil
|
||||
}
|
||||
|
||||
func (s *StoreToReplicaSetLister) ReplicaSets(namespace string) storeReplicaSetsNamespacer {
|
||||
return storeReplicaSetsNamespacer{s.Store, namespace}
|
||||
}
|
||||
|
||||
// GetPodReplicaSets returns a list of ReplicaSets managing a pod. Returns an error only if no matching ReplicaSets are found.
|
||||
func (s *StoreToReplicaSetLister) GetPodReplicaSets(pod *api.Pod) (rss []extensions.ReplicaSet, err error) {
|
||||
var selector labels.Selector
|
||||
var rs extensions.ReplicaSet
|
||||
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("no ReplicaSets found for pod %v because it has no labels", pod.Name)
|
||||
return
|
||||
}
|
||||
|
||||
for _, m := range s.Store.List() {
|
||||
rs = *m.(*extensions.ReplicaSet)
|
||||
if rs.Namespace != pod.Namespace {
|
||||
continue
|
||||
}
|
||||
selector, err = unversioned.LabelSelectorAsSelector(rs.Spec.Selector)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("invalid selector: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// StoreToDaemonSetLister gives a store List and Exists methods. The store must contain only DaemonSets.
|
||||
type StoreToDaemonSetLister struct {
|
||||
Store
|
||||
|
Reference in New Issue
Block a user