selector

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByKey

type ByKey []Requirement

ByKey sorts requirements by key to obtain deterministic parser

func (ByKey) Len

func (a ByKey) Len() int

func (ByKey) Less

func (a ByKey) Less(i, j int) bool

func (ByKey) Swap

func (a ByKey) Swap(i, j int)

type Lexer

type Lexer interface {
	Lex() (tok Token, lit string)
}

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser data structure contains the label selector parser data structure

type ParserContext

type ParserContext int

ParserContext 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 represents key and operator
	KeyAndOperator ParserContext = iota
	// Values represents values
	Values
)

type Requirement

type Requirement struct {
	// contains filtered or unexported fields
}

Requirement 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 should be initialized via NewRequirement constructor for creating a valid Requirement. +k8s:deepcopy-gen=true

func NewRequirement

func NewRequirement(key Tuple, op selection.Operator, vals []Tuple, opts ...field.PathOption) (*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, Equals, DoubleEquals, Contains, NotContains, Gt, Lt, Gte, Lte, NotEquals, Exists, or DoesNotExist.
  2. If the operator is In or NotIn, the values set must be non-empty.
  3. If the operator is Equals, DoubleEquals, or NotEquals, the values set must contain one value.
  4. If the operator is Exists or DoesNotExist, the value set must be empty.
  5. If the operator is Gt, Lt, Gte, Lte, the values set must contain only one value.

The empty string is a valid value in the input values set. Returned error, if not nil, is guaranteed to be an aggregated field.ErrorList

func ParseToRequirements

func ParseToRequirements(selector string, opts ...field.PathOption) ([]Requirement, error)

ParseToRequirements takes a string representing a selector and returns a list of requirements. This function is suitable for those callers that perform additional processing on selector requirements. See the documentation for Parse() function for more details. TODO: Consider exporting the internalSelector type instead.

func (*Requirement) DeepCopy

func (in *Requirement) DeepCopy() *Requirement

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Requirement.

func (*Requirement) DeepCopyInto

func (in *Requirement) DeepCopyInto(out *Requirement)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

func (Requirement) Equal

func (r Requirement) Equal(x Requirement) bool

Equal checks the equality of requirement.

func (*Requirement) Key

func (r *Requirement) Key() Tuple

Key returns requirement key

func (*Requirement) Matches

func (r *Requirement) Matches(ls k8sLabels.Labels) bool

Matches returns true if the Requirement matches the input k8sLabels. 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 DoesNotExist or NotIn and Labels does not have the Requirement's key.
  5. The operator is GreaterThanOperator or LessThanOperator, and Labels has the Requirement's key and the corresponding value satisfies mathematical inequality.

func (*Requirement) Operator

func (r *Requirement) Operator() selection.Operator

Operator returns requirement operator

func (*Requirement) String

func (r *Requirement) String() string

String returns a human-readable string that represents this Requirement. If called on an invalid Requirement, an error is returned. See NewRequirement for creating a valid Requirement.

func (*Requirement) Values

func (r *Requirement) Values() []Tuple

Values returns requirement values

type Requirements

type Requirements []Requirement

Requirements is AND of all requirements.

func (Requirements) String

func (r Requirements) String() string

type ScannedItem

type ScannedItem struct {
	// contains filtered or unexported fields
}

ScannedItem contains the Token and the literal produced by the lexer.

type Selector

type Selector interface {
	// Matches returns true if this selector matches the given set of k8sLabels.
	Matches(k8sLabels.Labels) bool

	// Empty returns true if this selector does not restrict the selection space.
	Empty() bool

	// String returns a human readable string that represents this selector.
	String() string

	// Add adds requirements to the Selector
	Add(r ...Requirement) Selector

	// Requirements converts this interface into Requirements to expose
	// more detailed selection information.
	// If there are querying parameters, it will return converted requirements and selectable=true.
	// If this selector doesn't want to select anything, it will return selectable=false.
	Requirements() (requirements Requirements, selectable bool)

	// Make a deep copy of the selector.
	DeepCopySelector() Selector

	// 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)
}

Selector represents a label selector.

func Everything

func Everything() Selector

Everything returns a selector that matches all k8sLabels.

func NewSelector

func NewSelector() Selector

NewSelector returns a nil selector

func Nothing

func Nothing() Selector

Nothing returns a selector that matches no labels

func Parse

func Parse(selector string, opts ...field.PathOption) (Selector, error)

Parse takes a string representing a selector and returns a selector object, or an error. 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> | <partial-match-restriction> ]
<set-based-restriction>     ::= "" | <inclusion-exclusion> <value-set>
<inclusion-exclusion>       ::= <inclusion> | <exclusion>
<exclusion>                 ::= "notin"
<inclusion>                 ::= "in"
<value-set>                 ::= "(" <values> ")"
<values>                    ::= VALUE | VALUE "," <values>
<exact-match-restriction>   ::= ["="|"=="|"!="] VALUE
<partial-match-restriction> ::= ["contains"|"notcontains"] VALUE

Delimiter is white space: (' ', '\t') Example of valid syntax:

"x in (foo,,baz),y,z notin ()"

Note:

  1. Inclusion - " in " - denotes that the KEY exists and is equal to any of the VALUEs in its requirement
  2. Exclusion - " notin " - denotes that the KEY is not equal to any of the VALUEs in its requirement or does not exist
  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.
  5. A requirement with just !KEY requires that the KEY not exist.

func ParseWithLexer

func ParseWithLexer(selector string, l Lexer, opts ...field.PathOption) (Selector, error)

ParseWithLexer takes a selector string and a custom lexer to tokenize the input, and returns a selector object or an error. This function is similar to Parse but allows for a custom lexer implementation.

func SelectorFromSet

func SelectorFromSet(ls k8sLabels.Set) Selector

SelectorFromSet returns a Selector which will match exactly the given Set. A nil and empty Sets are considered equivalent to Everything(). It does not perform any validation, which means the server will reject the request if the Set contains invalid values.

func SelectorFromValidatedSet

func SelectorFromValidatedSet(ls k8sLabels.Set) Selector

SelectorFromValidatedSet returns a Selector which will match exactly the given Set. A nil and empty Sets are considered equivalent to Everything(). It assumes that Set is already validated and doesn't do any validation. Note: this method copies the Set; if the Set is immutable, consider wrapping it with ValidatedSetSelector instead, which does not copy.

func ValidatedSelectorFromSet

func ValidatedSelectorFromSet(ls k8sLabels.Set) (Selector, error)

ValidatedSelectorFromSet returns a Selector which will match exactly the given Set. A nil and empty Sets are considered equivalent to Everything(). The Set is validated client-side, which allows to catch errors early.

type Token

type Token int

Token represents constant definition for lexer token

const (
	// ErrorToken represents scan error
	ErrorToken Token = iota
	// EndOfStringToken represents end of string
	EndOfStringToken
	// ClosedParToken represents close parenthesis
	ClosedParToken
	// CommaToken represents the comma
	CommaToken
	// DoesNotExistToken represents logic not
	DoesNotExistToken
	// DoubleEqualsToken represents double equals
	DoubleEqualsToken
	// EqualsToken represents equal
	EqualsToken
	// GreaterThanToken represents greater than
	GreaterThanToken
	// GreaterThanOrEqualsToken represents greater than or equal
	GreaterThanOrEqualsToken
	// IdentifierToken represents identifier, e.g. keys and values
	IdentifierToken
	// ContainsToken represents contains
	ContainsToken
	// InToken represents in
	InToken
	// LessThanToken represents less than or equal
	LessThanToken
	// LessThanOrEqualsToken represents less than
	LessThanOrEqualsToken
	// NotEqualsToken represents not equal
	NotEqualsToken
	// NotContainsToken represents not contains
	NotContainsToken
	// NotInToken represents not in
	NotInToken
	// OpenParToken represents open parenthesis
	OpenParToken
)

type Tuple

type Tuple []string

func (Tuple) String

func (t Tuple) String() string

type ValidatedSetSelector

type ValidatedSetSelector k8sLabels.Set

ValidatedSetSelector wraps a Set, allowing it to implement the Selector interface. Unlike Set.AsSelectorPreValidated (which copies the input Set), this type simply wraps the underlying Set. As a result, it is substantially more efficient. A nil and empty Sets are considered equivalent to Everything().

Callers MUST ensure the underlying Set is not mutated, and that it is already validated. If these constraints are not met, Set.AsValidatedSelector should be preferred

None of the Selector methods mutate the underlying Set, but Add() and Requirements() convert to the less optimized version.

func (ValidatedSetSelector) Add

func (ValidatedSetSelector) DeepCopySelector

func (s ValidatedSetSelector) DeepCopySelector() Selector

func (ValidatedSetSelector) Empty

func (s ValidatedSetSelector) Empty() bool

func (ValidatedSetSelector) Matches

func (s ValidatedSetSelector) Matches(labels k8sLabels.Labels) bool

func (ValidatedSetSelector) Requirements

func (s ValidatedSetSelector) Requirements() (requirements Requirements, selectable bool)

func (ValidatedSetSelector) RequiresExactMatch

func (s ValidatedSetSelector) RequiresExactMatch(label string) (value string, found bool)

func (ValidatedSetSelector) String

func (s ValidatedSetSelector) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL