// 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 } key := &api.ReplicationController{ObjectMeta: api.ObjectMeta{Namespace: pod.Namespace}} items, err := s.Indexer.Index(NamespaceIndex, key) if err != nil { return } for _, m := range items { rc = *m.(*api.ReplicationController) 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 controller for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) } return }
// 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) (api.PodList, error) { pods := api.PodList{} if s.namespace == api.NamespaceAll { for _, m := range s.indexer.List() { pod := m.(*api.Pod) if selector.Matches(labels.Set(pod.Labels)) { pods.Items = append(pods.Items, *pod) } } return pods, nil } key := &api.Pod{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() { pod := m.(*api.Pod) if s.namespace == pod.Namespace && selector.Matches(labels.Set(pod.Labels)) { pods.Items = append(pods.Items, *pod) } } return pods, nil } for _, m := range items { pod := m.(*api.Pod) if selector.Matches(labels.Set(pod.Labels)) { pods.Items = append(pods.Items, *pod) } } return pods, nil }
func (s storeReplicationControllersNamespacer) List(selector labels.Selector) ([]api.ReplicationController, error) { controllers := []api.ReplicationController{} if s.namespace == api.NamespaceAll { for _, m := range s.indexer.List() { rc := *(m.(*api.ReplicationController)) if selector.Matches(labels.Set(rc.Labels)) { controllers = append(controllers, rc) } } return controllers, nil } key := &api.ReplicationController{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() { rc := *(m.(*api.ReplicationController)) if s.namespace == rc.Namespace && selector.Matches(labels.Set(rc.Labels)) { controllers = append(controllers, rc) } } return controllers, nil } for _, m := range items { rc := *(m.(*api.ReplicationController)) if selector.Matches(labels.Set(rc.Labels)) { controllers = append(controllers, rc) } } return controllers, 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 *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 }
func (dsc *DaemonSetsController) nodeShouldRunDaemonPod(node *api.Node, ds *extensions.DaemonSet) bool { // Check if the node satisfies the daemon set's node selector. nodeSelector := labels.Set(ds.Spec.Template.Spec.NodeSelector).AsSelector() if !nodeSelector.Matches(labels.Set(node.Labels)) { return false } // If the daemon set specifies a node name, check that it matches with node.Name. if !(ds.Spec.Template.Spec.NodeName == "" || ds.Spec.Template.Spec.NodeName == node.Name) { return false } for _, c := range node.Status.Conditions { if c.Type == api.NodeOutOfDisk && c.Status == api.ConditionTrue { return false } } newPod := &api.Pod{Spec: ds.Spec.Template.Spec} newPod.Spec.NodeName = node.Name pods := []*api.Pod{newPod} for _, m := range dsc.podStore.Indexer.List() { pod := m.(*api.Pod) if pod.Spec.NodeName != node.Name { continue } if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed { continue } // ignore pods that belong to the daemonset when taking into account wheter // a daemonset should bind to a node. if pds := dsc.getPodDaemonSet(pod); pds != nil && ds.Name == pds.Name { continue } pods = append(pods, pod) } _, notFittingCPU, notFittingMemory, notFittingNvidiaGPU := predicates.CheckPodsExceedingFreeResources(pods, node.Status.Allocatable) if len(notFittingCPU)+len(notFittingMemory)+len(notFittingNvidiaGPU) != 0 { dsc.eventRecorder.Eventf(ds, api.EventTypeNormal, "FailedPlacement", "failed to place pod on %q: insufficent free resources", node.ObjectMeta.Name) return false } ports := sets.String{} for _, pod := range pods { if errs := validation.AccumulateUniqueHostPorts(pod.Spec.Containers, &ports, field.NewPath("spec", "containers")); len(errs) > 0 { dsc.eventRecorder.Eventf(ds, api.EventTypeNormal, "FailedPlacement", "failed to place pod on %q: host port conflict", node.ObjectMeta.Name) return false } } return true }
// getOverlappingControllers finds rcs that this controller overlaps, as well as rcs overlapping this controller. func getOverlappingControllers(c client.ReplicationControllerInterface, rc *api.ReplicationController) ([]api.ReplicationController, error) { rcs, err := c.List(api.ListOptions{}) if err != nil { return nil, fmt.Errorf("error getting replication controllers: %v", err) } var matchingRCs []api.ReplicationController rcLabels := labels.Set(rc.Spec.Selector) for _, controller := range rcs.Items { newRCLabels := labels.Set(controller.Spec.Selector) if labels.SelectorFromSet(newRCLabels).Matches(rcLabels) || labels.SelectorFromSet(rcLabels).Matches(newRCLabels) { matchingRCs = append(matchingRCs, controller) } } return matchingRCs, nil }
// readyPods returns the old and new ready counts for their pods. // If a pod is observed as being ready, it's considered ready even // if it later becomes notReady. func (r *RollingUpdater) readyPods(oldRc, newRc *api.ReplicationController) (int32, int32, error) { controllers := []*api.ReplicationController{oldRc, newRc} oldReady := int32(0) newReady := int32(0) for i := range controllers { controller := controllers[i] selector := labels.Set(controller.Spec.Selector).AsSelector() options := api.ListOptions{LabelSelector: selector} pods, err := r.c.Pods(controller.Namespace).List(options) if err != nil { return 0, 0, err } for _, pod := range pods.Items { if api.IsPodReady(&pod) { switch controller.Name { case oldRc.Name: oldReady++ case newRc.Name: newReady++ } } } } return oldReady, newReady, nil }
// SelectableFields returns a label set that can be used for filter selection func SelectableFields(obj *api.Secret) labels.Set { objectMetaFieldsSet := generic.ObjectMetaFieldsSet(obj.ObjectMeta, true) secretSpecificFieldsSet := fields.Set{ "type": string(obj.Type), } return labels.Set(generic.MergeFieldsSets(objectMetaFieldsSet, secretSpecificFieldsSet)) }
// GetPodPetSets returns a list of PetSets managing a pod. Returns an error only if no matching PetSets are found. func (s *StoreToPetSetLister) GetPodPetSets(pod *api.Pod) (psList []apps.PetSet, err error) { var selector labels.Selector var ps apps.PetSet if len(pod.Labels) == 0 { err = fmt.Errorf("no PetSets found for pod %v because it has no labels", pod.Name) return } for _, m := range s.Store.List() { ps = *m.(*apps.PetSet) if ps.Namespace != pod.Namespace { continue } selector, err = unversioned.LabelSelectorAsSelector(ps.Spec.Selector) if err != nil { err = fmt.Errorf("invalid selector: %v", err) return } // If a PetSet 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 PetSet for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) } return }
// GetPodJobs returns a list of jobs managing a pod. Returns an error only if no matching jobs are found. func (s *StoreToJobLister) GetPodJobs(pod *api.Pod) (jobs []batch.Job, err error) { var selector labels.Selector var job batch.Job if len(pod.Labels) == 0 { err = fmt.Errorf("no jobs found for pod %v because it has no labels", pod.Name) return } for _, m := range s.Store.List() { job = *m.(*batch.Job) if job.Namespace != pod.Namespace { continue } selector, _ = unversioned.LabelSelectorAsSelector(job.Spec.Selector) if !selector.Matches(labels.Set(pod.Labels)) { continue } jobs = append(jobs, job) } if len(jobs) == 0 { err = fmt.Errorf("could not find jobs for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) } return }
// 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 *api.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 = unversioned.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 }
// 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 }
func (t *Tester) testListMatchLabels(obj runtime.Object, assignFn AssignFunc) { ctx := t.TestContext() testLabels := map[string]string{"key": "value"} foo3 := copyOrDie(obj) t.setObjectMeta(foo3, "foo3") foo4 := copyOrDie(obj) foo4Meta := t.getObjectMetaOrFail(foo4) foo4Meta.Name = "foo4" foo4Meta.Namespace = api.NamespaceValue(ctx) foo4Meta.Labels = testLabels objs := ([]runtime.Object{foo3, foo4}) assignFn(objs) filtered := []runtime.Object{objs[1]} selector := labels.SelectorFromSet(labels.Set(testLabels)) options := &api.ListOptions{LabelSelector: selector} listObj, err := t.storage.(rest.Lister).List(ctx, options) if err != nil { t.Errorf("unexpected error: %v", err) } items, err := listToItems(listObj) if err != nil { t.Errorf("unexpected error: %v", err) } if len(items) != len(filtered) { t.Errorf("unexpected number of items: %v", len(items)) } if !api.Semantic.DeepEqual(filtered, items) { t.Errorf("expected: %#v, got: %#v", filtered, items) } }
// 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) { var d extensions.Deployment 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 for _, m := range s.Store.List() { d = *m.(*extensions.Deployment) if d.Namespace != rs.Namespace { continue } 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 }
func (r RealPodControl) createPods(nodeName, namespace string, template *api.PodTemplateSpec, object runtime.Object) error { pod, err := GetPodFromTemplate(template, object) if err != nil { return err } if len(nodeName) != 0 { pod.Spec.NodeName = nodeName } if labels.Set(pod.Labels).AsSelector().Empty() { return fmt.Errorf("unable to create pods, no labels") } if newPod, err := r.KubeClient.Core().Pods(namespace).Create(pod); err != nil { r.Recorder.Eventf(object, api.EventTypeWarning, "FailedCreate", "Error creating: %v", err) return fmt.Errorf("unable to create pods: %v", err) } else { accessor, err := meta.Accessor(object) if err != nil { glog.Errorf("parentObject does not have ObjectMeta, %v", err) return nil } glog.V(4).Infof("Controller %v created pod %v", accessor.GetName(), newPod.Name) r.Recorder.Eventf(object, api.EventTypeNormal, "SuccessfulCreate", "Created pod: %v", newPod.Name) } return nil }
func TestSelectableFieldLabelConversions(t *testing.T) { apitesting.TestSelectableFieldLabelConversionsOfKind(t, testapi.Extensions.GroupVersion().String(), "Job", labels.Set(JobToSelectableFields(&batch.Job{})), nil, ) }
// PersistentVolumeToSelectableFields returns a label set that represents the object func PersistentVolumeToSelectableFields(persistentvolume *api.PersistentVolume) labels.Set { objectMetaFieldsSet := generic.ObjectMetaFieldsSet(persistentvolume.ObjectMeta, false) specificFieldsSet := fields.Set{ // This is a bug, but we need to support it for backward compatibility. "name": persistentvolume.Name, } return labels.Set(generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)) }
func TestSelectableFieldLabelConversions(t *testing.T) { apitesting.TestSelectableFieldLabelConversionsOfKind(t, testapi.Autoscaling.GroupVersion().String(), "Autoscaler", labels.Set(AutoscalerToSelectableFields(&autoscaling.HorizontalPodAutoscaler{})), nil, ) }
func TestSelectableFieldLabelConversions(t *testing.T) { apitesting.TestSelectableFieldLabelConversionsOfKind(t, testapi.Extensions.GroupVersion().String(), "Deployment", labels.Set(DeploymentToSelectableFields(&extensions.Deployment{})), nil, ) }
func TestSelectableFieldLabelConversions(t *testing.T) { apitesting.TestSelectableFieldLabelConversionsOfKind(t, testapi.Default.GroupVersion().String(), "LimitRange", labels.Set(LimitRangeToSelectableFields(&api.LimitRange{})), nil, ) }
// NamespaceToSelectableFields returns a label set that represents the object func NamespaceToSelectableFields(namespace *api.Namespace) labels.Set { objectMetaFieldsSet := generic.ObjectMetaFieldsSet(namespace.ObjectMeta, false) specificFieldsSet := fields.Set{ "status.phase": string(namespace.Status.Phase), // This is a bug, but we need to support it for backward compatibility. "name": namespace.Name, } return labels.Set(generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)) }
// 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.(*extensions.ThirdPartyResourceData) if !ok { return false, fmt.Errorf("not a ThirdPartyResourceData") } fields := SelectableFields(sa) return label.Matches(labels.Set(sa.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 }) }
// 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.(*rbac.RoleBinding) if !ok { return false, fmt.Errorf("not a RoleBinding") } fields := SelectableFields(sa) return label.Matches(labels.Set(sa.Labels)) && field.Matches(fields), 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 }) }
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 TestSelectableFieldLabelConversions(t *testing.T) { _, fieldsSet, err := EndpointsAttributes(&api.Endpoints{}) if err != nil { t.Fatal(err) } apitesting.TestSelectableFieldLabelConversionsOfKind(t, testapi.Default.GroupVersion().String(), "Endpoints", labels.Set(fieldsSet), nil, ) }
func TestFiltering(t *testing.T) { server, etcdStorage := newEtcdTestStorage(t, testapi.Default.Codec(), etcdtest.PathPrefix()) defer server.Terminate(t) cacher := newTestCacher(etcdStorage) defer cacher.Stop() // Ensure that the cacher is initialized, before creating any pods, // so that we are sure that all events will be present in cacher. syncWatcher, err := cacher.Watch(context.TODO(), "pods/ns/foo", "0", storage.Everything) if err != nil { t.Fatalf("Unexpected error: %v", err) } syncWatcher.Stop() podFoo := makeTestPod("foo") podFoo.Labels = map[string]string{"filter": "foo"} podFooFiltered := makeTestPod("foo") podFooPrime := makeTestPod("foo") podFooPrime.Labels = map[string]string{"filter": "foo"} podFooPrime.Spec.NodeName = "fakeNode" fooCreated := updatePod(t, etcdStorage, podFoo, nil) fooFiltered := updatePod(t, etcdStorage, podFooFiltered, fooCreated) fooUnfiltered := updatePod(t, etcdStorage, podFoo, fooFiltered) _ = updatePod(t, etcdStorage, podFooPrime, fooUnfiltered) deleted := api.Pod{} if err := etcdStorage.Delete(context.TODO(), etcdtest.AddPrefix("pods/ns/foo"), &deleted, nil); err != nil { t.Errorf("Unexpected error: %v", err) } // Set up Watch for object "podFoo" with label filter set. selector := labels.SelectorFromSet(labels.Set{"filter": "foo"}) filter := func(obj runtime.Object) bool { metadata, err := meta.Accessor(obj) if err != nil { t.Errorf("Unexpected error: %v", err) return false } return selector.Matches(labels.Set(metadata.GetLabels())) } watcher, err := cacher.Watch(context.TODO(), "pods/ns/foo", fooCreated.ResourceVersion, filter) if err != nil { t.Fatalf("Unexpected error: %v", err) } defer watcher.Stop() verifyWatchEvent(t, watcher, watch.Deleted, podFooFiltered) verifyWatchEvent(t, watcher, watch.Added, podFoo) verifyWatchEvent(t, watcher, watch.Modified, podFooPrime) verifyWatchEvent(t, watcher, watch.Deleted, podFooPrime) }
// MatchIngress is the filter used by the generic etcd backend to ingress // watch events from etcd to clients of the apiserver only interested in specific // labels/fields. func MatchIngress(label labels.Selector, field fields.Selector) generic.Matcher { return &generic.SelectionPredicate{ Label: label, Field: field, GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) { ingress, ok := obj.(*extensions.Ingress) if !ok { return nil, nil, fmt.Errorf("Given object is not an Ingress.") } return labels.Set(ingress.ObjectMeta.Labels), IngressToSelectableFields(ingress), nil }, } }
// MatchDeployment is the filter used by the generic etcd backend to route // watch events from etcd to clients of the apiserver only interested in specific // labels/fields. func MatchDeployment(label labels.Selector, field fields.Selector) generic.Matcher { return &generic.SelectionPredicate{ Label: label, Field: field, GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) { deployment, ok := obj.(*extensions.Deployment) if !ok { return nil, nil, fmt.Errorf("given object is not a deployment.") } return labels.Set(deployment.ObjectMeta.Labels), DeploymentToSelectableFields(deployment), nil }, } }