Documentation
¶
Overview ¶
Package labels implements a simple label system, parsing and matching selectors with sets of labels.
Index ¶
- type ByKey
- type LabelSelector
- type Labels
- type Lexer
- type Operator
- type Parser
- type ParserContext
- type Requirement
- type ScannedItem
- type Selector
- func Everything() Selector
- func OneTermEqualSelector(k, v string) Selector
- func OneTermEqualSelectorParse(k, v string) Selector
- func Parse(selector string) (Selector, error)
- func ParseAndTransformSelector(selector string, fn TransformFunc) (Selector, error)
- func ParseSelector(selector string) (Selector, error)
- func SelectorFromSet(ls Set) Selector
- func SelectorFromSetParse(ls Set) (Selector, error)
- type Set
- type Token
- type TransformFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ByKey ¶ added in v0.12.0
type ByKey []Requirement
Sort by obtain determisitic parser (minimic previous andTerm based stuff)
type LabelSelector ¶
type LabelSelector struct {
Requirements []Requirement
}
LabelSelector contains a list of Requirements. LabelSelector is set-based and is distinguished from exact match-based selectors composed of key=value matching conjunctions.
func (LabelSelector) Empty ¶ added in v0.12.0
func (lsel LabelSelector) Empty() bool
Return true if the LabelSelector doesn't restrict selection space
func (LabelSelector) Matches ¶
func (lsel LabelSelector) Matches(l Labels) bool
Matches for a LabelSelector returns true if all its Requirements match the input Labels. If any Requirement does not match, false is returned.
func (LabelSelector) RequiresExactMatch ¶ added in v0.12.0
func (lsel LabelSelector) RequiresExactMatch(label string) (value string, found bool)
RequiresExactMatch allows a caller to introspect whether a given selector requires a single specific label to be set, and if so returns the value it requires.
type Labels ¶
type Labels interface {
// Has returns whether the provided label exists.
Has(label string) (exists bool)
// Get returns the value for the provided label.
Get(label string) (value string)
}
Labels allows you to present labels independently from their storage.
type Lexer ¶ added in v0.12.0
type Lexer struct {
// contains filtered or unexported fields
}
Lexer represents the Lexer struct for label selector. It contains necessary informationt to tokenize the input string
type Operator ¶
type Operator string
Operator represents a key's relationship to a set of values in a Requirement.
const (
EqualsOperator Operator = "="
DoubleEqualsOperator Operator = "=="
InOperator Operator = "in"
NotEqualsOperator Operator = "!="
NotInOperator Operator = "notin"
ExistsOperator Operator = "exists"
)
type Parser ¶ added in v0.12.0
type Parser struct {
// contains filtered or unexported fields
}
Parser data structure contains the label selector parser data strucutre
type ParserContext ¶ added in v0.12.0
type ParserContext int
Parser context represents context during parsing: some literal for example 'in' and 'notin' can be recognized as operator for example 'x in (a)' but it can be recognized as value for example 'value in (in)'
const (
KeyAndOperator ParserContext = iota
Values
)
type Requirement ¶
type Requirement struct {
// contains filtered or unexported fields
}
Requirement is a selector that contains values, a key and an operator that relates the key and values. The zero value of Requirement is invalid. Requirement implements both set based match and exact match Requirement is initialized via NewRequirement constructor for creating a valid Requirement.
func NewRequirement ¶
func NewRequirement(key string, op Operator, vals util.StringSet) (*Requirement, error)
NewRequirement is the constructor for a Requirement. If any of these rules is violated, an error is returned: (1) The operator can only be In, NotIn or Exists. (2) If the operator is In or NotIn, the values set must
be non-empty.
(3) The key is invalid due to its length, or sequence
of characters. See validateLabelKey for more details.
The empty string is a valid value in the input values set.
func (*Requirement) Matches ¶
func (r *Requirement) Matches(ls Labels) bool
Matches returns true if the Requirement matches the input Labels. There is a match in the following cases: (1) The operator is Exists and Labels has the Requirement's key. (2) The operator is In, Labels has the Requirement's key and Labels'
value for that key is in Requirement's value set.
(3) The operator is NotIn, Labels has the Requirement's key and
Labels' value for that key is not in Requirement's value set.
(4) The operator is NotIn and Labels does not have the
Requirement's key.
func (*Requirement) RequiresExactMatch ¶ added in v0.12.0
func (r *Requirement) RequiresExactMatch(label string) (string, bool)
RequiresExactMatch allows a caller to introspect whether a given selector requires a single specific label to be set, and if so returns the value it requires.
type ScannedItem ¶ added in v0.12.0
type ScannedItem struct {
// contains filtered or unexported fields
}
The item produced by the lexer. It contains the Token and the literal.
type Selector ¶
type Selector interface {
// Matches returns true if this selector matches the given set of labels.
Matches(Labels) bool
// Empty returns true if this selector does not restrict the selection space.
Empty() bool
// RequiresExactMatch allows a caller to introspect whether a given selector
// requires a single specific label to be set, and if so returns the value it
// requires.
RequiresExactMatch(label string) (value string, found bool)
// String returns a human readable string that represents this selector.
String() string
}
Selector represents a label selector.
func OneTermEqualSelector ¶ added in v0.6.0
func OneTermEqualSelector(k, v string) Selector
OneTermEqualSelector returns an object that matches objects where one label/field equals one value. Cannot return an error.
func OneTermEqualSelectorParse ¶ added in v0.12.0
func OneTermEqualSelectorParse(k, v string) Selector
OneTermEqualSelectorParse: implement OneTermEqualSelector using of LabelSelector and Requirement TODO: remove the original OneTermSelector and rename OneTermEqualSelectorParse to OneTermEqualSelector Since OneTermEqualSelector cannot return an error. the Requirement based version ignore error. it's up to the caller being sure that k and v are not empty
func Parse ¶ added in v0.5.1
func Parse(selector string) (Selector, error)
Parse takes a string representing a selector and returns a selector object, or an error. This parsing function differs from ParseSelector as they parse different selectors with different syntaxes. The input will cause an error if it does not follow this form:
<selector-syntax> ::= <requirement> | <requirement> "," <selector-syntax> ] <requirement> ::= KEY [ <set-based-restriction> | <exact-match-restriction> <set-based-restriction> ::= "" | <inclusion-exclusion> <value-set> <inclusion-exclusion> ::= <inclusion> | <exclusion>
<exclusion> ::= "not" <inclusion>
<inclusion> ::= "in"
<value-set> ::= "(" <values> ")"
<values> ::= VALUE | VALUE "," <values>
<exact-match-restriction> ::= ["="|"=="|"!="] VALUE KEY is a sequence of one or more characters following [ DNS_SUBDOMAIN "/" ] DNS_LABEL VALUE is a sequence of zero or more characters "([A-Za-z0-9_-\.])". Max length is 64 character. Delimiter is white space: (' ', '\t') Example of valid syntax:
"x in (foo,,baz),y,z not in ()"
Note:
(1) Inclusion - " in " - denotes that the KEY is equal to any of the
VALUEs in its requirement
(2) Exclusion - " not in " - denotes that the KEY is not equal to any
of the VALUEs in its requirement
(3) The empty string is a valid VALUE
(4) A requirement with just a KEY - as in "y" above - denotes that
the KEY exists and can be any VALUE.
func ParseAndTransformSelector ¶ added in v0.12.0
func ParseAndTransformSelector(selector string, fn TransformFunc) (Selector, error)
Parses the selector and runs them through the given TransformFunc.
func ParseSelector ¶
func ParseSelector(selector string) (Selector, error)
ParseSelector takes a string representing a selector and returns an object suitable for matching, or an error.
func SelectorFromSet ¶
func SelectorFromSet(ls Set) Selector
SelectorFromSet returns a Selector which will match exactly the given Set. A nil Set is considered equivalent to Everything().
func SelectorFromSetParse ¶ added in v0.12.0
func SelectorFromSetParse(ls Set) (Selector, error)
SelectorFromSet returns a Selector which will match exactly the given Set. A nil Set is considered equivalent to Everything().
type Set ¶
type Set map[string]string
Set is a map of label:value. It implements Labels.
func (Set) AsSelector ¶
func (ls Set) AsSelector() Selector
AsSelector converts labels into a selectors.
func (Set) Get ¶
func (ls Set) Get(label string) string
Get returns the value in the map for the provided label.
type Token ¶ added in v0.12.0
type Token int
constants definition for lexer token
const (
ErrorToken Token = iota
EndOfStringToken
ClosedParToken
CommaToken
DoubleEqualsToken
EqualsToken
IdentifierToken // to represent keys and values
InToken
NotEqualsToken
NotInToken
OpenParToken
)
type TransformFunc ¶ added in v0.12.0
type TransformFunc func(label, value string) (newLabel, newValue string, err error)
Function to transform selectors.