// 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 controllers managing a pod. Returns an error only if no matching controllers are found. func (s *StoreToControllerLister) 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 }
// List returns []*api.Pod matching a query. func (f FakePodLister) List(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 }
// MatchPodTemplate returns a generic matcher for a given label and field selector. func MatchPodTemplate(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { podObj, ok := obj.(*api.PodTemplate) if !ok { return false, fmt.Errorf("not a pod template") } return label.Matches(labels.Set(podObj.Labels)), nil }) }
// MatchPersistentVolume returns a generic matcher for a given label and field selector. func MatchPersistentVolumes(label labels.Selector, field fields.Selector) generic.Matcher { return generic.MatcherFunc(func(obj runtime.Object) (bool, error) { persistentvolumeObj, ok := obj.(*api.PersistentVolume) if !ok { return false, fmt.Errorf("not a persistentvolume") } fields := PersistentVolumeToSelectableFields(persistentvolumeObj) return label.Matches(labels.Set(persistentvolumeObj.Labels)) && field.Matches(fields), nil }) }
// 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 }) }
// 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 }) }
// 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.Secret) if !ok { return false, fmt.Errorf("not a secret") } fields := SelectableFields(sa) return label.Matches(labels.Set(sa.Labels)) && field.Matches(fields), 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 == nil { return r } if s.Empty() { return r } return r.setParam(api.LabelSelectorQueryParam(r.apiVersion), s.String()) }
// 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 }
// 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(ctx api.Context, label labels.Selector, field fields.Selector) (runtime.Object, error) { list, err := rs.registry.ListServices(ctx) if err != nil { return nil, err } var filtered []api.Service for _, service := range list.Items { if label.Matches(labels.Set(service.Labels)) { filtered = append(filtered, service) } } list.Items = filtered return list, err }
// GetPodServices gets the services that have the selector that match the labels on the given pod func (f FakeServiceLister) GetPodServices(pod *api.Pod) (services []api.Service, err error) { var selector labels.Selector for _, service := range f { // consider only services that are in the same namespace as the pod if service.Namespace != pod.Namespace { 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 }
// WatchServices begins watching for new, changed, or deleted service configurations. func (r *Registry) WatchServices(ctx api.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) { version, err := tools.ParseWatchResourceVersion(resourceVersion, "service") if err != nil { return nil, err } if !label.Empty() { return nil, fmt.Errorf("label selectors are not supported on services") } if value, found := field.RequiresExactMatch("name"); found { key, err := makeServiceKey(ctx, value) if err != nil { return nil, err } // TODO: use generic.SelectionPredicate return r.Watch(key, version, tools.Everything) } if field.Empty() { return r.WatchList(makeServiceListKey(ctx), version, tools.Everything) } return nil, fmt.Errorf("only the 'name' and default (everything) field selectors are supported") }
// CheckServiceAffinity ensures that only the minions that match the specified labels are considered for scheduling. // The set of labels to be considered are provided to the struct (ServiceAffinity). // The pod is checked for the labels and any missing labels are then checked in the minion // that hosts the service pods (peers) for the given pod. // // We add an implicit selector requiring some particular value V for label L to a pod, if: // - L is listed in the ServiceAffinity object that is passed into the function // - the pod does not have any NodeSelector for L // - some other pod from the same service is already scheduled onto a minion that has value V for label L func (s *ServiceAffinity) CheckServiceAffinity(pod *api.Pod, existingPods []*api.Pod, node string) (bool, error) { var affinitySelector labels.Selector // check if the pod being scheduled has the affinity labels specified in its NodeSelector affinityLabels := map[string]string{} nodeSelector := labels.Set(pod.Spec.NodeSelector) labelsExist := true for _, l := range s.labels { if nodeSelector.Has(l) { affinityLabels[l] = nodeSelector.Get(l) } else { // the current pod does not specify all the labels, look in the existing service pods labelsExist = false } } // skip looking at other pods in the service if the current pod defines all the required affinity labels if !labelsExist { services, err := s.serviceLister.GetPodServices(pod) if err == nil { // just use the first service and get the other pods within the service // TODO: a separate predicate can be created that tries to handle all services for the pod selector := labels.SelectorFromSet(services[0].Spec.Selector) servicePods, err := s.podLister.List(selector) if err != nil { return false, err } // consider only the pods that belong to the same namespace nsServicePods := []*api.Pod{} for _, nsPod := range servicePods { if nsPod.Namespace == pod.Namespace { nsServicePods = append(nsServicePods, nsPod) } } if len(nsServicePods) > 0 { // consider any service pod and fetch the minion its hosted on otherMinion, err := s.nodeInfo.GetNodeInfo(nsServicePods[0].Spec.NodeName) if err != nil { return false, err } for _, l := range s.labels { // If the pod being scheduled has the label value specified, do not override it if _, exists := affinityLabels[l]; exists { continue } if labels.Set(otherMinion.Labels).Has(l) { affinityLabels[l] = labels.Set(otherMinion.Labels).Get(l) } } } } } // if there are no existing pods in the service, consider all minions if len(affinityLabels) == 0 { affinitySelector = labels.Everything() } else { affinitySelector = labels.Set(affinityLabels).AsSelector() } minion, err := s.nodeInfo.GetNodeInfo(node) if err != nil { return false, err } // check if the minion matches the selector return affinitySelector.Matches(labels.Set(minion.Labels)), nil }
func (r *PodRegistry) ListPods(ctx api.Context, selector labels.Selector) (*api.PodList, error) { return r.ListPodsPredicate(ctx, func(pod *api.Pod) bool { return selector.Matches(labels.Set(pod.Labels)) }) }