// WatchControllers begins watching for new, changed, or deleted controllers. func (r *Registry) WatchControllers(ctx api.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) { if !field.Empty() { return nil, fmt.Errorf("field selectors are not supported on replication controllers") } version, err := tools.ParseWatchResourceVersion(resourceVersion, "replicationControllers") if err != nil { return nil, err } key := makeControllerListKey(ctx) return r.WatchList(key, version, func(obj runtime.Object) bool { controller, ok := obj.(*api.ReplicationController) if !ok { // Must be an error: return true to propagate to upper level. return true } match := label.Matches(labels.Set(controller.Labels)) if match { pods, err := r.pods.ListPods(ctx, labels.Set(controller.Spec.Selector).AsSelector()) if err != nil { glog.Warningf("Error listing pods: %v", err) // No object that's useable so drop it on the floor return false } if pods == nil { glog.Warningf("Pods list is nil. This should never happen...") // No object that's useable so drop it on the floor return false } controller.Status.Replicas = len(pods.Items) } return match }) }
// 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 *api.Pod) (controllers []api.ReplicationController, err error) { var selector labels.Selector var rc api.ReplicationController if len(pod.Labels) == 0 { err = fmt.Errorf("No controllers found for pod %v because it has no labels", pod.Name) return } for _, m := range s.Store.List() { rc = *m.(*api.ReplicationController) if rc.Namespace != pod.Namespace { continue } labelSet := labels.Set(rc.Spec.Selector) selector = labels.Set(rc.Spec.Selector).AsSelector() // If an rc with a nil or empty selector creeps in, it should match nothing, not everything. if labelSet.AsSelector().Empty() || !selector.Matches(labels.Set(pod.Labels)) { continue } controllers = append(controllers, rc) } if len(controllers) == 0 { err = fmt.Errorf("Could not find controllers for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) } return }
// 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 *api.Pod) (services []api.Service, err error) { var selector labels.Selector var service api.Service for _, m := range s.Store.List() { service = *m.(*api.Service) // consider only services that are in the same namespace as the pod if service.Namespace != pod.Namespace { continue } if service.Spec.Selector == nil { // services with nil selectors match nothing, not everything. continue } selector = labels.Set(service.Spec.Selector).AsSelector() if selector.Matches(labels.Set(pod.Labels)) { services = append(services, service) } } if len(services) == 0 { err = fmt.Errorf("Could not find service for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) } return }
// ListPods returns []api.Pod matching a query. func (f FakePodLister) ListPods(s labels.Selector) (selected []api.Pod, err error) { for _, pod := range f { if s.Matches(labels.Set(pod.Labels)) { selected = append(selected, pod) } } return selected, nil }
// WatchControllers begins watching for new, changed, or deleted controllers. func (registry *EtcdRegistry) WatchControllers(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { if !field.Empty() { return nil, fmt.Errorf("no field selector implemented for controllers") } return registry.helper.WatchList("/registry/controllers", resourceVersion, func(obj interface{}) bool { return label.Matches(labels.Set(obj.(*api.ReplicationController).Labels)) }) }
// Watch begins watching for new, changed, or deleted ImageRepositories. func (s *ImageRepositoryStorage) Watch(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { return s.registry.WatchImageRepositories(resourceVersion, func(repo *api.ImageRepository) bool { fields := labels.Set{ "ID": repo.ID, "DockerImageRepository": repo.DockerImageRepository, } return label.Matches(labels.Set(repo.Labels)) && field.Matches(fields) }) }
// SelectorParam adds the given selector as a query parameter with the name paramName. func (r *Request) SelectorParam(paramName string, s labels.Selector) *Request { if r.err != nil { return r } if s.Empty() { return r } return r.setParam(paramName, s.String()) }
// MatchTemplate returns a generic matcher for a given label and field selector. func MatchTemplate(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { o, ok := obj.(*api.Template) if !ok { return false, fmt.Errorf("not a pod") } return label.Matches(labels.Set(o.Labels)), nil }) }
// TODO Get rid of the selector because that is confusing because the user might not realize that there has already been // some selection at the caching stage. Also, consistency will facilitate code generation. However, the pkg/client // is inconsistent too. func (s *StoreToPodLister) List(selector labels.Selector) (pods []api.Pod, err error) { for _, m := range s.Store.List() { pod := m.(*api.Pod) if selector.Matches(labels.Set(pod.Labels)) { pods = append(pods, *pod) } } return pods, nil }
func (registry *MemoryRegistry) ListPods(selector labels.Selector) ([]api.Pod, error) { result := []api.Pod{} for _, value := range registry.podData { if selector.Matches(labels.Set(value.Labels)) { result = append(result, value) } } return result, nil }
// LabelsSelectorParam adds the given selector as a query parameter func (r *Request) LabelsSelectorParam(s labels.Selector) *Request { if r.err != nil { return r } if s.Empty() { return r } return r.setParam(api.LabelSelectorQueryParam(r.apiVersion), s.String()) }
// MatchNamespace returns a generic matcher for a given label and field selector. func MatchNamespace(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { namespaceObj, ok := obj.(*api.Namespace) if !ok { return false, fmt.Errorf("not a namespace") } fields := NamespaceToSelectableFields(namespaceObj) return label.Matches(labels.Set(namespaceObj.Labels)) && field.Matches(fields), nil }) }
// Matchtoken returns a generic matcher for a given label and field selector. func Matcher(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { clientObj, ok := obj.(*api.OAuthClientAuthorization) if !ok { return false, fmt.Errorf("not a client authorization") } fields := SelectableFields(clientObj) return label.Matches(labels.Set(clientObj.Labels)) && field.Matches(fields), nil }) }
// MatchPod returns a generic matcher for a given label and field selector. func MatchPod(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { podObj, ok := obj.(*api.Pod) if !ok { return false, fmt.Errorf("not a pod") } fields := PodToSelectableFields(podObj) return label.Matches(labels.Set(podObj.Labels)) && field.Matches(fields), nil }) }
// Matcher returns a generic matcher for a given label and field selector. func Matcher(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { scc, ok := obj.(*api.SecurityContextConstraints) if !ok { return false, fmt.Errorf("not a securitycontextconstraint") } fields := SelectableFields(scc) return label.Matches(labels.Set(scc.Labels)) && field.Matches(fields), nil }) }
// Matchtoken returns a generic matcher for a given label and field selector. func Matcher(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { tokenObj, ok := obj.(*api.OAuthAccessToken) if !ok { return false, fmt.Errorf("not a token") } fields := SelectableFields(tokenObj) return label.Matches(labels.Set(tokenObj.Labels)) && field.Matches(fields), nil }) }
// Matcher returns a generic matcher for a given label and field selector. func Matcher(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { sa, ok := obj.(*api.ServiceAccount) if !ok { return false, fmt.Errorf("not a serviceaccount") } fields := SelectableFields(sa) return label.Matches(labels.Set(sa.Labels)) && field.Matches(fields), nil }) }
// MatchPersistentVolumeClaim returns a generic matcher for a given label and field selector. func MatchPersistentVolumeClaim(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { persistentvolumeclaimObj, ok := obj.(*api.PersistentVolumeClaim) if !ok { return false, fmt.Errorf("not a persistentvolumeclaim") } fields := PersistentVolumeClaimToSelectableFields(persistentvolumeclaimObj) return label.Matches(labels.Set(persistentvolumeclaimObj.Labels)) && field.Matches(fields), nil }) }
// MatchUser returns a generic matcher for a given label and field selector. func MatchUser(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { userObj, ok := obj.(*api.User) if !ok { return false, fmt.Errorf("not a user") } fields := UserToSelectableFields(userObj) return label.Matches(labels.Set(userObj.Labels)) && field.Matches(fields), nil }) }
// MatchResourceQuota returns a generic matcher for a given label and field selector. func MatchResourceQuota(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { resourcequotaObj, ok := obj.(*api.ResourceQuota) if !ok { return false, fmt.Errorf("not a resourcequota") } fields := ResourceQuotaToSelectableFields(resourcequotaObj) return label.Matches(labels.Set(resourcequotaObj.Labels)) && field.Matches(fields), nil }) }
// Watch begins watching for new, changed, or deleted pods. func (rs *REST) Watch(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { return rs.registry.WatchPods(resourceVersion, func(pod *api.Pod) bool { fields := labels.Set{ "ID": pod.ID, "DesiredState.Status": string(pod.DesiredState.Status), "DesiredState.Host": pod.DesiredState.Host, } return label.Matches(labels.Set(pod.Labels)) && field.Matches(fields) }) }
// MatchGroup returns a generic matcher for a given label and field selector. func MatchGroup(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { groupObj, ok := obj.(*api.Group) if !ok { return false, fmt.Errorf("not a group") } fields := GroupToSelectableFields(groupObj) return label.Matches(labels.Set(groupObj.Labels)) && field.Matches(fields), nil }) }
// MatchImageStream returns a generic matcher for a given label and field selector. func MatchImageStream(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { ir, ok := obj.(*api.ImageStream) if !ok { return false, fmt.Errorf("not an ImageStream") } fields := ImageStreamToSelectableFields(ir) return label.Matches(labels.Set(ir.Labels)) && field.Matches(fields), nil }) }
// MatchIdentity returns a generic matcher for a given label and field selector. func MatchIdentity(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { identityObj, ok := obj.(*api.Identity) if !ok { return false, fmt.Errorf("not an identity") } fields := IdentityToSelectableFields(identityObj) return label.Matches(labels.Set(identityObj.Labels)) && field.Matches(fields), nil }) }
func (registry *MockPodRegistry) ListPods(selector labels.Selector) ([]api.Pod, error) { if registry.err != nil { return registry.pods, registry.err } var filtered []api.Pod for _, pod := range registry.pods { if selector.Matches(labels.Set(pod.Labels)) { filtered = append(filtered, pod) } } return filtered, nil }
// List obtains a list of ReplicationControllers that match selector. func (storage *ControllerRegistryStorage) List(selector labels.Selector) (interface{}, error) { result := api.ReplicationControllerList{} controllers, err := storage.registry.ListControllers() if err == nil { for _, controller := range controllers { if selector.Matches(labels.Set(controller.Labels)) { result.Items = append(result.Items, controller) } } } return result, err }
// Please note that selector is filtering among the pods that have gotten into // the store; there may have been some filtering that already happened before // that. func (s storePodsNamespacer) List(selector labels.Selector) (pods api.PodList, err error) { list := api.PodList{} for _, m := range s.store.List() { pod := m.(*api.Pod) if s.namespace == api.NamespaceAll || s.namespace == pod.Namespace { if selector.Matches(labels.Set(pod.Labels)) { list.Items = append(list.Items, *pod) } } } return list, nil }
// Watch returns ReplicationController events via a watch.Interface. // It implements apiserver.ResourceWatcher. func (rs *RegistryStorage) Watch(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { if !field.Empty() { return nil, fmt.Errorf("no field selector implemented for controllers") } incoming, err := rs.registry.WatchControllers(resourceVersion) if err != nil { return nil, err } return watch.Filter(incoming, func(e watch.Event) (watch.Event, bool) { repController := e.Object.(*api.ReplicationController) return e, label.Matches(labels.Set(repController.Labels)) }), nil }
// Please note that selector is filtering among the pods that have gotten into // the store; there may have been some filtering that already happened before // that. // // TODO: converge on the interface in pkg/client. func (s *StoreToPodLister) List(selector labels.Selector) (pods []*api.Pod, err error) { // TODO: it'd be great to just call // s.Pods(api.NamespaceAll).List(selector), however then we'd have to // remake the list.Items as a []*api.Pod. So leave this separate for // now. for _, m := range s.Store.List() { pod := m.(*api.Pod) if selector.Matches(labels.Set(pod.Labels)) { pods = append(pods, pod) } } return pods, nil }
func (rs *REST) List(selector labels.Selector) (runtime.Object, error) { list, err := rs.registry.ListServices() if err != nil { return nil, err } var filtered []api.Service for _, service := range list.Items { if selector.Matches(labels.Set(service.Labels)) { filtered = append(filtered, service) } } list.Items = filtered return list, err }