// NodeSelectorRequirementsAsSelector converts the []NodeSelectorRequirement api type into a struct that implements // labels.Selector. func NodeSelectorRequirementsAsSelector(nsm []NodeSelectorRequirement) (labels.Selector, error) { if len(nsm) == 0 { return labels.Nothing(), nil } selector := labels.NewSelector() for _, expr := range nsm { var op selection.Operator switch expr.Operator { case NodeSelectorOpIn: op = selection.In case NodeSelectorOpNotIn: op = selection.NotIn case NodeSelectorOpExists: op = selection.Exists case NodeSelectorOpDoesNotExist: op = selection.DoesNotExist case NodeSelectorOpGt: op = selection.GreaterThan case NodeSelectorOpLt: op = selection.LessThan default: return nil, fmt.Errorf("%q is not a valid node selector operator", expr.Operator) } r, err := labels.NewRequirement(expr.Key, op, expr.Values) if err != nil { return nil, err } selector = selector.Add(*r) } return selector, nil }
// matchPodName returns selection predicate that matches any pod with name in the set. // Makes testing simpler. func matchPodName(names ...string) storage.SelectionPredicate { // Note: even if pod name is a field, we have to use labels, // because field selector doesn't support "IN" operator. l, err := labels.NewRequirement("name", selection.In, names) if err != nil { panic("Labels requirement must validate successfully") } return storage.SelectionPredicate{ Label: labels.Everything().Add(*l), Field: fields.Everything(), GetAttrs: getPodAttrs, } }
// 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, selection.Equals, []string{v}) if err != nil { return nil, err } selector = selector.Add(*r) } for _, expr := range ps.MatchExpressions { var op selection.Operator switch expr.Operator { case LabelSelectorOpIn: op = selection.In case LabelSelectorOpNotIn: op = selection.NotIn case LabelSelectorOpExists: op = selection.Exists case LabelSelectorOpDoesNotExist: op = selection.DoesNotExist default: return nil, fmt.Errorf("%q is not a valid pod selector operator", expr.Operator) } r, err := labels.NewRequirement(expr.Key, op, append([]string(nil), expr.Values...)) if err != nil { return nil, err } selector = selector.Add(*r) } return selector, nil }