mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
Merge pull request #18157 from sdminonne/LabelSelector_internalSelector
renaming labels.LabelSelector to labels.internalSelector
This commit is contained in:
@@ -37,21 +37,21 @@ type Selector interface {
|
||||
// String returns a human readable string that represents this selector.
|
||||
String() string
|
||||
|
||||
// Add add a specific requirement for the selector
|
||||
Add(key string, operator Operator, values []string) Selector
|
||||
// Add adds requirements to the Selector
|
||||
Add(r ...Requirement) Selector
|
||||
}
|
||||
|
||||
// Everything returns a selector that matches all labels.
|
||||
func Everything() Selector {
|
||||
return LabelSelector{}
|
||||
return internalSelector{}
|
||||
}
|
||||
|
||||
type nothingSelector struct{}
|
||||
|
||||
func (n nothingSelector) Matches(_ Labels) bool { return false }
|
||||
func (n nothingSelector) Empty() bool { return false }
|
||||
func (n nothingSelector) String() string { return "<null>" }
|
||||
func (n nothingSelector) Add(_ string, _ Operator, _ []string) Selector { return n }
|
||||
func (n nothingSelector) Matches(_ Labels) bool { return false }
|
||||
func (n nothingSelector) Empty() bool { return false }
|
||||
func (n nothingSelector) String() string { return "<null>" }
|
||||
func (n nothingSelector) Add(_ ...Requirement) Selector { return n }
|
||||
|
||||
// Nothing returns a selector that matches no labels
|
||||
func Nothing() Selector {
|
||||
@@ -72,10 +72,13 @@ const (
|
||||
ExistsOperator Operator = "exists"
|
||||
)
|
||||
|
||||
//LabelSelector is a list of Requirements.
|
||||
type LabelSelector []Requirement
|
||||
func NewSelector() Selector {
|
||||
return internalSelector(nil)
|
||||
}
|
||||
|
||||
// Sort by obtain determisitic parser
|
||||
type internalSelector []Requirement
|
||||
|
||||
// Sort by key to obtain determisitic parser
|
||||
type ByKey []Requirement
|
||||
|
||||
func (a ByKey) Len() int { return len(a) }
|
||||
@@ -178,8 +181,8 @@ func (r *Requirement) Values() sets.String {
|
||||
return ret
|
||||
}
|
||||
|
||||
// Return true if the LabelSelector doesn't restrict selection space
|
||||
func (lsel LabelSelector) Empty() bool {
|
||||
// Return true if the internalSelector doesn't restrict selection space
|
||||
func (lsel internalSelector) Empty() bool {
|
||||
if lsel == nil {
|
||||
return true
|
||||
}
|
||||
@@ -228,22 +231,23 @@ func (r *Requirement) String() string {
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// Add adds a requirement to the selector. It copies the current selector returning a new one
|
||||
func (lsel LabelSelector) Add(key string, operator Operator, values []string) Selector {
|
||||
var reqs []Requirement
|
||||
// Add adds requirements to the selector. It copies the current selector returning a new one
|
||||
func (lsel internalSelector) Add(reqs ...Requirement) Selector {
|
||||
var sel internalSelector
|
||||
for _, item := range lsel {
|
||||
reqs = append(reqs, item)
|
||||
sel = append(sel, item)
|
||||
}
|
||||
if r, err := NewRequirement(key, operator, sets.NewString(values...)); err == nil {
|
||||
reqs = append(reqs, *r)
|
||||
for _, r := range reqs {
|
||||
sel = append(sel, r)
|
||||
}
|
||||
return LabelSelector(reqs)
|
||||
sort.Sort(ByKey(sel))
|
||||
return sel
|
||||
}
|
||||
|
||||
// Matches for a LabelSelector returns true if all
|
||||
// Matches for a internalSelector returns true if all
|
||||
// its Requirements match the input Labels. If any
|
||||
// Requirement does not match, false is returned.
|
||||
func (lsel LabelSelector) Matches(l Labels) bool {
|
||||
func (lsel internalSelector) Matches(l Labels) bool {
|
||||
for _, req := range lsel {
|
||||
if matches := req.Matches(l); !matches {
|
||||
return false
|
||||
@@ -253,8 +257,8 @@ func (lsel LabelSelector) Matches(l Labels) bool {
|
||||
}
|
||||
|
||||
// String returns a comma-separated string of all
|
||||
// the LabelSelector Requirements' human-readable strings.
|
||||
func (lsel LabelSelector) String() string {
|
||||
// the internalSelector Requirements' human-readable strings.
|
||||
func (lsel internalSelector) String() string {
|
||||
var reqs []string
|
||||
for _, req := range lsel {
|
||||
reqs = append(reqs, req.String())
|
||||
@@ -473,10 +477,10 @@ func (p *Parser) scan() {
|
||||
|
||||
// parse runs the left recursive descending algorithm
|
||||
// on input string. It returns a list of Requirement objects.
|
||||
func (p *Parser) parse() ([]Requirement, error) {
|
||||
func (p *Parser) parse() (internalSelector, error) {
|
||||
p.scan() // init scannedItems
|
||||
|
||||
var requirements []Requirement
|
||||
var requirements internalSelector
|
||||
for {
|
||||
tok, lit := p.lookahead(Values)
|
||||
switch tok {
|
||||
@@ -692,7 +696,7 @@ func Parse(selector string) (Selector, error) {
|
||||
items, error := p.parse()
|
||||
if error == nil {
|
||||
sort.Sort(ByKey(items)) // sort to grant determistic parsing
|
||||
return LabelSelector(items), error
|
||||
return internalSelector(items), error
|
||||
}
|
||||
return nil, error
|
||||
}
|
||||
@@ -718,18 +722,18 @@ func validateLabelValue(v string) error {
|
||||
// nil and empty Sets are considered equivalent to Everything().
|
||||
func SelectorFromSet(ls Set) Selector {
|
||||
if ls == nil {
|
||||
return LabelSelector{}
|
||||
return internalSelector{}
|
||||
}
|
||||
var requirements []Requirement
|
||||
var requirements internalSelector
|
||||
for label, value := range ls {
|
||||
if r, err := NewRequirement(label, EqualsOperator, sets.NewString(value)); err != nil {
|
||||
//TODO: double check errors when input comes from serialization?
|
||||
return LabelSelector{}
|
||||
return internalSelector{}
|
||||
} else {
|
||||
requirements = append(requirements, *r)
|
||||
}
|
||||
}
|
||||
// sort to have deterministic string representation
|
||||
sort.Sort(ByKey(requirements))
|
||||
return LabelSelector(requirements)
|
||||
return internalSelector(requirements)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user