func (gcc *PodGCController) gc() { terminatedPods, _ := gcc.podStore.List(labels.Everything()) terminatedPodCount := len(terminatedPods) sort.Sort(byCreationTimestamp(terminatedPods)) deleteCount := terminatedPodCount - gcc.threshold if deleteCount > terminatedPodCount { deleteCount = terminatedPodCount } if deleteCount > 0 { glog.Infof("garbage collecting %v pods", deleteCount) } var wait sync.WaitGroup for i := 0; i < deleteCount; i++ { wait.Add(1) go func(namespace string, name string) { defer wait.Done() if err := gcc.deletePod(namespace, name); err != nil { // ignore not founds defer utilruntime.HandleError(err) } }(terminatedPods[i].Namespace, terminatedPods[i].Name) } wait.Wait() }
func (c *FakeHorizontalPodAutoscalers) List(opts api.ListOptions) (*autoscaling.HorizontalPodAutoscalerList, error) { obj, err := c.Fake.Invokes(NewListAction("horizontalpodautoscalers", c.Namespace, opts), &autoscaling.HorizontalPodAutoscalerList{}) if obj == nil { return nil, err } label := opts.LabelSelector if label == nil { label = labels.Everything() } list := &autoscaling.HorizontalPodAutoscalerList{} for _, a := range obj.(*autoscaling.HorizontalPodAutoscalerList).Items { if label.Matches(labels.Set(a.Labels)) { list.Items = append(list.Items, a) } } return list, err }
func (c *FakePods) List(opts api.ListOptions) (*api.PodList, error) { obj, err := c.Fake.Invokes(NewListAction("pods", c.Namespace, opts), &api.PodList{}) if obj == nil { return nil, err } label := opts.LabelSelector if label == nil { label = labels.Everything() } list := &api.PodList{} for _, pod := range obj.(*api.PodList).Items { if label.Matches(labels.Set(pod.Labels)) { list.Items = append(list.Items, pod) } } return list, err }
func (c *FakePodSecurityPolicies) List(opts api.ListOptions) (result *extensions.PodSecurityPolicyList, err error) { obj, err := c.Fake. Invokes(core.NewRootListAction(podsecuritypoliciesResource, opts), &extensions.PodSecurityPolicyList{}) if obj == nil { return nil, err } label := opts.LabelSelector if label == nil { label = labels.Everything() } list := &extensions.PodSecurityPolicyList{} for _, item := range obj.(*extensions.PodSecurityPolicyList).Items { if label.Matches(labels.Set(item.Labels)) { list.Items = append(list.Items, item) } } return list, err }
// cleanupOrphanedPods deletes pods that are bound to nodes that don't // exist. func (nc *NodeController) cleanupOrphanedPods() { pods, err := nc.podStore.List(labels.Everything()) if err != nil { utilruntime.HandleError(err) return } for _, pod := range pods { if pod.Spec.NodeName == "" { continue } if _, exists, _ := nc.nodeStore.Store.GetByKey(pod.Spec.NodeName); exists { continue } if err := nc.forcefullyDeletePod(pod); err != nil { utilruntime.HandleError(err) } } }
func (c *FakeComponentStatuses) List(opts api.ListOptions) (result *api.ComponentStatusList, err error) { obj, err := c.Fake. Invokes(core.NewRootListAction(componentstatusesResource, opts), &api.ComponentStatusList{}) if obj == nil { return nil, err } label := opts.LabelSelector if label == nil { label = labels.Everything() } list := &api.ComponentStatusList{} for _, item := range obj.(*api.ComponentStatusList).Items { if label.Matches(labels.Set(item.Labels)) { list.Items = append(list.Items, item) } } return list, err }
func (c *FakeThirdPartyResources) List(opts api.ListOptions) (result *v1beta1.ThirdPartyResourceList, err error) { obj, err := c.Fake. Invokes(core.NewRootListAction(thirdpartyresourcesResource, opts), &v1beta1.ThirdPartyResourceList{}) if obj == nil { return nil, err } label := opts.LabelSelector if label == nil { label = labels.Everything() } list := &v1beta1.ThirdPartyResourceList{} for _, item := range obj.(*v1beta1.ThirdPartyResourceList).Items { if label.Matches(labels.Set(item.Labels)) { list.Items = append(list.Items, item) } } return list, err }
func (c *FakeServiceAccounts) List(opts api.ListOptions) (result *v1.ServiceAccountList, err error) { obj, err := c.Fake. Invokes(core.NewListAction(serviceaccountsResource, c.ns, opts), &v1.ServiceAccountList{}) if obj == nil { return nil, err } label := opts.LabelSelector if label == nil { label = labels.Everything() } list := &v1.ServiceAccountList{} for _, item := range obj.(*v1.ServiceAccountList).Items { if label.Matches(labels.Set(item.Labels)) { list.Items = append(list.Items, item) } } return list, err }
func (c *FakeHorizontalPodAutoscalers) List(opts api.ListOptions) (result *v1beta1.HorizontalPodAutoscalerList, err error) { obj, err := c.Fake. Invokes(core.NewListAction(horizontalpodautoscalersResource, c.ns, opts), &v1beta1.HorizontalPodAutoscalerList{}) if obj == nil { return nil, err } label := opts.LabelSelector if label == nil { label = labels.Everything() } list := &v1beta1.HorizontalPodAutoscalerList{} for _, item := range obj.(*v1beta1.HorizontalPodAutoscalerList).Items { if label.Matches(labels.Set(item.Labels)) { list.Items = append(list.Items, item) } } return list, err }
func TestMatchNode(t *testing.T) { testFieldMap := map[bool][]fields.Set{ true: { {"metadata.name": "foo"}, }, false: { {"foo": "bar"}, }, } for expectedResult, fieldSet := range testFieldMap { for _, field := range fieldSet { m := MatchNode(labels.Everything(), field.AsSelector()) _, matchesSingle := m.MatchesSingle() if e, a := expectedResult, matchesSingle; e != a { t.Errorf("%+v: expected %v, got %v", fieldSet, e, a) } } } }
func (c *FakePersistentVolumeClaims) List(opts api.ListOptions) (result *api.PersistentVolumeClaimList, err error) { obj, err := c.Fake. Invokes(core.NewListAction(persistentvolumeclaimsResource, c.ns, opts), &api.PersistentVolumeClaimList{}) if obj == nil { return nil, err } label := opts.LabelSelector if label == nil { label = labels.Everything() } list := &api.PersistentVolumeClaimList{} for _, item := range obj.(*api.PersistentVolumeClaimList).Items { if label.Matches(labels.Set(item.Labels)) { list.Items = append(list.Items, item) } } return list, err }
// ResourceTypeOrNameArgs indicates that the builder should accept arguments // of the form `(<type1>[,<type2>,...]|<type> <name1>[,<name2>,...])`. When one argument is // received, the types provided will be retrieved from the server (and be comma delimited). // When two or more arguments are received, they must be a single type and resource name(s). // The allowEmptySelector permits to select all the resources (via Everything func). func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string) *Builder { args = normalizeMultipleResourcesArgs(args) if ok, err := hasCombinedTypeArgs(args); ok { if err != nil { b.errs = append(b.errs, err) return b } for _, s := range args { tuple, ok, err := splitResourceTypeName(s) if err != nil { b.errs = append(b.errs, err) return b } if ok { b.resourceTuples = append(b.resourceTuples, tuple) } } return b } if len(args) > 0 { // Try replacing aliases only in types args[0] = b.replaceAliases(args[0]) } switch { case len(args) > 2: b.names = append(b.names, args[1:]...) b.ResourceTypes(SplitResourceArgument(args[0])...) case len(args) == 2: b.names = append(b.names, args[1]) b.ResourceTypes(SplitResourceArgument(args[0])...) case len(args) == 1: b.ResourceTypes(SplitResourceArgument(args[0])...) if b.selector == nil && allowEmptySelector { b.selector = labels.Everything() } case len(args) == 0: default: b.errs = append(b.errs, fmt.Errorf("when passing arguments, must be resource or resource and name")) } return b }
// LabelSelectorAsSelector converts the LabelSelector api type into a struct that implements // labels.Selector // Note: This function should be kept in sync with the selector methods in pkg/labels/selector.go func LabelSelectorAsSelector(ps *LabelSelector) (labels.Selector, error) { if ps == nil { return labels.Nothing(), nil } if len(ps.MatchLabels)+len(ps.MatchExpressions) == 0 { return labels.Everything(), nil } selector := labels.NewSelector() for k, v := range ps.MatchLabels { r, err := labels.NewRequirement(k, labels.EqualsOperator, sets.NewString(v)) if err != nil { return nil, err } selector = selector.Add(*r) } for _, expr := range ps.MatchExpressions { var op labels.Operator switch expr.Operator { case LabelSelectorOpIn: op = labels.InOperator case LabelSelectorOpNotIn: op = labels.NotInOperator case LabelSelectorOpExists: op = labels.ExistsOperator case LabelSelectorOpDoesNotExist: op = labels.DoesNotExistOperator default: return nil, fmt.Errorf("%q is not a valid pod selector operator", expr.Operator) } r, err := labels.NewRequirement(expr.Key, op, sets.NewString(expr.Values...)) if err != nil { return nil, err } selector = selector.Add(*r) } return selector, nil }
func TestWatchParamParsing(t *testing.T) { simpleStorage := &SimpleRESTStorage{} handler := handle(map[string]rest.Storage{ "simples": simpleStorage, "simpleroots": simpleStorage, }) server := httptest.NewServer(handler) defer server.Close() dest, _ := url.Parse(server.URL) rootPath := "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simples" namespacedPath := "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/namespaces/other/simpleroots" table := []struct { path string rawQuery string resourceVersion string labelSelector string fieldSelector string namespace string }{ { path: rootPath, rawQuery: "resourceVersion=1234", resourceVersion: "1234", labelSelector: "", fieldSelector: "", namespace: api.NamespaceAll, }, { path: rootPath, rawQuery: "resourceVersion=314159&fieldSelector=Host%3D&labelSelector=name%3Dfoo", resourceVersion: "314159", labelSelector: "name=foo", fieldSelector: "Host=", namespace: api.NamespaceAll, }, { path: rootPath, rawQuery: "fieldSelector=id%3dfoo&resourceVersion=1492", resourceVersion: "1492", labelSelector: "", fieldSelector: "id=foo", namespace: api.NamespaceAll, }, { path: rootPath, rawQuery: "", resourceVersion: "", labelSelector: "", fieldSelector: "", namespace: api.NamespaceAll, }, { path: namespacedPath, rawQuery: "resourceVersion=1234", resourceVersion: "1234", labelSelector: "", fieldSelector: "", namespace: "other", }, { path: namespacedPath, rawQuery: "resourceVersion=314159&fieldSelector=Host%3D&labelSelector=name%3Dfoo", resourceVersion: "314159", labelSelector: "name=foo", fieldSelector: "Host=", namespace: "other", }, { path: namespacedPath, rawQuery: "fieldSelector=id%3dfoo&resourceVersion=1492", resourceVersion: "1492", labelSelector: "", fieldSelector: "id=foo", namespace: "other", }, { path: namespacedPath, rawQuery: "", resourceVersion: "", labelSelector: "", fieldSelector: "", namespace: "other", }, } for _, item := range table { simpleStorage.requestedLabelSelector = labels.Everything() simpleStorage.requestedFieldSelector = fields.Everything() simpleStorage.requestedResourceVersion = "5" // Prove this is set in all cases simpleStorage.requestedResourceNamespace = "" dest.Path = item.path dest.RawQuery = item.rawQuery resp, err := http.Get(dest.String()) if err != nil { t.Errorf("%v: unexpected error: %v", item.rawQuery, err) continue } resp.Body.Close() if e, a := item.namespace, simpleStorage.requestedResourceNamespace; e != a { t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) } if e, a := item.resourceVersion, simpleStorage.requestedResourceVersion; e != a { t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) } if e, a := item.labelSelector, simpleStorage.requestedLabelSelector.String(); e != a { t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) } if e, a := item.fieldSelector, simpleStorage.requestedFieldSelector.String(); e != a { t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) } } }
func (b *Builder) visitorResult() *Result { if len(b.errs) > 0 { return &Result{err: utilerrors.NewAggregate(b.errs)} } if b.selectAll { b.selector = labels.Everything() } // visit selectors if b.selector != nil { if len(b.names) != 0 { return &Result{err: fmt.Errorf("name cannot be provided when a selector is specified")} } if len(b.resourceTuples) != 0 { return &Result{err: fmt.Errorf("selectors and the all flag cannot be used when passing resource/name arguments")} } if len(b.resources) == 0 { return &Result{err: fmt.Errorf("at least one resource must be specified to use a selector")} } // empty selector has different error message for paths being provided if len(b.paths) != 0 { if b.selector.Empty() { return &Result{err: fmt.Errorf("when paths, URLs, or stdin is provided as input, you may not specify a resource by arguments as well")} } else { return &Result{err: fmt.Errorf("a selector may not be specified when path, URL, or stdin is provided as input")} } } mappings, err := b.resourceMappings() if err != nil { return &Result{err: err} } visitors := []Visitor{} for _, mapping := range mappings { client, err := b.mapper.ClientForMapping(mapping) if err != nil { return &Result{err: err} } selectorNamespace := b.namespace if mapping.Scope.Name() != meta.RESTScopeNameNamespace { selectorNamespace = "" } visitors = append(visitors, NewSelector(client, mapping, selectorNamespace, b.selector, b.export)) } if b.continueOnError { return &Result{visitor: EagerVisitorList(visitors), sources: visitors} } return &Result{visitor: VisitorList(visitors), sources: visitors} } // visit items specified by resource and name if len(b.resourceTuples) != 0 { // if b.singular is false, this could be by default, so double-check length // of resourceTuples to determine if in fact it is singular or not isSingular := b.singular if !isSingular { isSingular = len(b.resourceTuples) == 1 } if len(b.paths) != 0 { return &Result{singular: isSingular, err: fmt.Errorf("when paths, URLs, or stdin is provided as input, you may not specify a resource by arguments as well")} } if len(b.resources) != 0 { return &Result{singular: isSingular, err: fmt.Errorf("you may not specify individual resources and bulk resources in the same call")} } // retrieve one client for each resource mappings, err := b.resourceTupleMappings() if err != nil { return &Result{singular: isSingular, err: err} } clients := make(map[string]RESTClient) for _, mapping := range mappings { s := fmt.Sprintf("%s/%s", mapping.GroupVersionKind.GroupVersion().String(), mapping.Resource) if _, ok := clients[s]; ok { continue } client, err := b.mapper.ClientForMapping(mapping) if err != nil { return &Result{err: err} } clients[s] = client } items := []Visitor{} for _, tuple := range b.resourceTuples { mapping, ok := mappings[tuple.Resource] if !ok { return &Result{singular: isSingular, err: fmt.Errorf("resource %q is not recognized: %v", tuple.Resource, mappings)} } s := fmt.Sprintf("%s/%s", mapping.GroupVersionKind.GroupVersion().String(), mapping.Resource) client, ok := clients[s] if !ok { return &Result{singular: isSingular, err: fmt.Errorf("could not find a client for resource %q", tuple.Resource)} } selectorNamespace := b.namespace if mapping.Scope.Name() != meta.RESTScopeNameNamespace { selectorNamespace = "" } else { if len(b.namespace) == 0 { return &Result{singular: isSingular, err: fmt.Errorf("namespace may not be empty when retrieving a resource by name")} } } info := NewInfo(client, mapping, selectorNamespace, tuple.Name, b.export) items = append(items, info) } var visitors Visitor if b.continueOnError { visitors = EagerVisitorList(items) } else { visitors = VisitorList(items) } return &Result{singular: isSingular, visitor: visitors, sources: items} } // visit items specified by name if len(b.names) != 0 { isSingular := len(b.names) == 1 if len(b.paths) != 0 { return &Result{singular: isSingular, err: fmt.Errorf("when paths, URLs, or stdin is provided as input, you may not specify a resource by arguments as well")} } if len(b.resources) == 0 { return &Result{singular: isSingular, err: fmt.Errorf("you must provide a resource and a resource name together")} } if len(b.resources) > 1 { return &Result{singular: isSingular, err: fmt.Errorf("you must specify only one resource")} } mappings, err := b.resourceMappings() if err != nil { return &Result{singular: isSingular, err: err} } mapping := mappings[0] client, err := b.mapper.ClientForMapping(mapping) if err != nil { return &Result{err: err} } selectorNamespace := b.namespace if mapping.Scope.Name() != meta.RESTScopeNameNamespace { selectorNamespace = "" } else { if len(b.namespace) == 0 { return &Result{singular: isSingular, err: fmt.Errorf("namespace may not be empty when retrieving a resource by name")} } } visitors := []Visitor{} for _, name := range b.names { info := NewInfo(client, mapping, selectorNamespace, name, b.export) visitors = append(visitors, info) } return &Result{singular: isSingular, visitor: VisitorList(visitors), sources: visitors} } // visit items specified by paths if len(b.paths) != 0 { singular := !b.dir && !b.stream && len(b.paths) == 1 if len(b.resources) != 0 { return &Result{singular: singular, err: fmt.Errorf("when paths, URLs, or stdin is provided as input, you may not specify resource arguments as well")} } var visitors Visitor if b.continueOnError { visitors = EagerVisitorList(b.paths) } else { visitors = VisitorList(b.paths) } // only items from disk can be refetched if b.latest { // must flatten lists prior to fetching if b.flatten { visitors = NewFlattenListVisitor(visitors, b.mapper) } // must set namespace prior to fetching if b.defaultNamespace { visitors = NewDecoratedVisitor(visitors, SetNamespace(b.namespace)) } visitors = NewDecoratedVisitor(visitors, RetrieveLatest) } return &Result{singular: singular, visitor: visitors, sources: b.paths} } if len(b.resources) != 0 { return &Result{err: fmt.Errorf("resource(s) were provided, but no name, label selector, or --all flag specified")} } return &Result{err: fmt.Errorf("you must provide one or more resources by argument or filename (%s)", strings.Join(InputExtensions, "|"))} }