Adding a func to labels.Selector to add requirement

This commit is contained in:
Salvatore Dario Minonne
2015-03-25 16:17:22 +01:00
parent 35b2c5ce9d
commit 6ad5c07350
2 changed files with 49 additions and 0 deletions

View File

@@ -36,6 +36,9 @@ 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
}
// Everything returns a selector that matches all labels.
@@ -189,6 +192,18 @@ 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
for _, item := range lsel {
reqs = append(reqs, item)
}
if r, err := NewRequirement(key, operator, util.NewStringSet(values...)); err == nil {
reqs = append(reqs, *r)
}
return LabelSelector(reqs)
}
// Matches for a LabelSelector returns true if all
// its Requirements match the input Labels. If any
// Requirement does not match, false is returned.