// 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 labels.Operator switch expr.Operator { case NodeSelectorOpIn: op = labels.InOperator case NodeSelectorOpNotIn: op = labels.NotInOperator case NodeSelectorOpExists: op = labels.ExistsOperator case NodeSelectorOpDoesNotExist: op = labels.DoesNotExistOperator case NodeSelectorOpGt: op = labels.GreaterThanOperator case NodeSelectorOpLt: op = labels.LessThanOperator default: return nil, fmt.Errorf("%q is not a valid node 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 }
// 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 }