selector.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package labels
  14. import (
  15. "fmt"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "k8s.io/apimachinery/pkg/selection"
  20. "k8s.io/apimachinery/pkg/util/sets"
  21. "k8s.io/apimachinery/pkg/util/validation"
  22. "k8s.io/apimachinery/pkg/util/validation/field"
  23. "k8s.io/klog/v2"
  24. stringslices "k8s.io/utils/strings/slices"
  25. )
  26. var (
  27. unaryOperators = []string{
  28. string(selection.Exists), string(selection.DoesNotExist),
  29. }
  30. binaryOperators = []string{
  31. string(selection.In), string(selection.NotIn),
  32. string(selection.Equals), string(selection.DoubleEquals), string(selection.NotEquals),
  33. string(selection.GreaterThan), string(selection.LessThan),
  34. }
  35. validRequirementOperators = append(binaryOperators, unaryOperators...)
  36. )
  37. // Requirements is AND of all requirements.
  38. type Requirements []Requirement
  39. // Selector represents a label selector.
  40. type Selector interface {
  41. // Matches returns true if this selector matches the given set of labels.
  42. Matches(Labels) bool
  43. // Empty returns true if this selector does not restrict the selection space.
  44. Empty() bool
  45. // String returns a human readable string that represents this selector.
  46. String() string
  47. // Add adds requirements to the Selector
  48. Add(r ...Requirement) Selector
  49. // Requirements converts this interface into Requirements to expose
  50. // more detailed selection information.
  51. // If there are querying parameters, it will return converted requirements and selectable=true.
  52. // If this selector doesn't want to select anything, it will return selectable=false.
  53. Requirements() (requirements Requirements, selectable bool)
  54. // Make a deep copy of the selector.
  55. DeepCopySelector() Selector
  56. // RequiresExactMatch allows a caller to introspect whether a given selector
  57. // requires a single specific label to be set, and if so returns the value it
  58. // requires.
  59. RequiresExactMatch(label string) (value string, found bool)
  60. }
  61. // Sharing this saves 1 alloc per use; this is safe because it's immutable.
  62. var sharedEverythingSelector Selector = internalSelector{}
  63. // Everything returns a selector that matches all labels.
  64. func Everything() Selector {
  65. return sharedEverythingSelector
  66. }
  67. type nothingSelector struct{}
  68. func (n nothingSelector) Matches(_ Labels) bool { return false }
  69. func (n nothingSelector) Empty() bool { return false }
  70. func (n nothingSelector) String() string { return "" }
  71. func (n nothingSelector) Add(_ ...Requirement) Selector { return n }
  72. func (n nothingSelector) Requirements() (Requirements, bool) { return nil, false }
  73. func (n nothingSelector) DeepCopySelector() Selector { return n }
  74. func (n nothingSelector) RequiresExactMatch(label string) (value string, found bool) {
  75. return "", false
  76. }
  77. // Sharing this saves 1 alloc per use; this is safe because it's immutable.
  78. var sharedNothingSelector Selector = nothingSelector{}
  79. // Nothing returns a selector that matches no labels
  80. func Nothing() Selector {
  81. return sharedNothingSelector
  82. }
  83. // NewSelector returns a nil selector
  84. func NewSelector() Selector {
  85. return internalSelector(nil)
  86. }
  87. type internalSelector []Requirement
  88. func (s internalSelector) DeepCopy() internalSelector {
  89. if s == nil {
  90. return nil
  91. }
  92. result := make([]Requirement, len(s))
  93. for i := range s {
  94. s[i].DeepCopyInto(&result[i])
  95. }
  96. return result
  97. }
  98. func (s internalSelector) DeepCopySelector() Selector {
  99. return s.DeepCopy()
  100. }
  101. // ByKey sorts requirements by key to obtain deterministic parser
  102. type ByKey []Requirement
  103. func (a ByKey) Len() int { return len(a) }
  104. func (a ByKey) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  105. func (a ByKey) Less(i, j int) bool { return a[i].key < a[j].key }
  106. // Requirement contains values, a key, and an operator that relates the key and values.
  107. // The zero value of Requirement is invalid.
  108. // Requirement implements both set based match and exact match
  109. // Requirement should be initialized via NewRequirement constructor for creating a valid Requirement.
  110. // +k8s:deepcopy-gen=true
  111. type Requirement struct {
  112. key string
  113. operator selection.Operator
  114. // In huge majority of cases we have at most one value here.
  115. // It is generally faster to operate on a single-element slice
  116. // than on a single-element map, so we have a slice here.
  117. strValues []string
  118. }
  119. // NewRequirement is the constructor for a Requirement.
  120. // If any of these rules is violated, an error is returned:
  121. // (1) The operator can only be In, NotIn, Equals, DoubleEquals, Gt, Lt, NotEquals, Exists, or DoesNotExist.
  122. // (2) If the operator is In or NotIn, the values set must be non-empty.
  123. // (3) If the operator is Equals, DoubleEquals, or NotEquals, the values set must contain one value.
  124. // (4) If the operator is Exists or DoesNotExist, the value set must be empty.
  125. // (5) If the operator is Gt or Lt, the values set must contain only one value, which will be interpreted as an integer.
  126. // (6) The key is invalid due to its length, or sequence
  127. //
  128. // of characters. See validateLabelKey for more details.
  129. //
  130. // The empty string is a valid value in the input values set.
  131. // Returned error, if not nil, is guaranteed to be an aggregated field.ErrorList
  132. func NewRequirement(key string, op selection.Operator, vals []string, opts ...field.PathOption) (*Requirement, error) {
  133. var allErrs field.ErrorList
  134. path := field.ToPath(opts...)
  135. if err := validateLabelKey(key, path.Child("key")); err != nil {
  136. allErrs = append(allErrs, err)
  137. }
  138. valuePath := path.Child("values")
  139. switch op {
  140. case selection.In, selection.NotIn:
  141. if len(vals) == 0 {
  142. allErrs = append(allErrs, field.Invalid(valuePath, vals, "for 'in', 'notin' operators, values set can't be empty"))
  143. }
  144. case selection.Equals, selection.DoubleEquals, selection.NotEquals:
  145. if len(vals) != 1 {
  146. allErrs = append(allErrs, field.Invalid(valuePath, vals, "exact-match compatibility requires one single value"))
  147. }
  148. case selection.Exists, selection.DoesNotExist:
  149. if len(vals) != 0 {
  150. allErrs = append(allErrs, field.Invalid(valuePath, vals, "values set must be empty for exists and does not exist"))
  151. }
  152. case selection.GreaterThan, selection.LessThan:
  153. if len(vals) != 1 {
  154. allErrs = append(allErrs, field.Invalid(valuePath, vals, "for 'Gt', 'Lt' operators, exactly one value is required"))
  155. }
  156. for i := range vals {
  157. if _, err := strconv.ParseInt(vals[i], 10, 64); err != nil {
  158. allErrs = append(allErrs, field.Invalid(valuePath.Index(i), vals[i], "for 'Gt', 'Lt' operators, the value must be an integer"))
  159. }
  160. }
  161. default:
  162. allErrs = append(allErrs, field.NotSupported(path.Child("operator"), op, validRequirementOperators))
  163. }
  164. for i := range vals {
  165. if err := validateLabelValue(key, vals[i], valuePath.Index(i)); err != nil {
  166. allErrs = append(allErrs, err)
  167. }
  168. }
  169. return &Requirement{key: key, operator: op, strValues: vals}, allErrs.ToAggregate()
  170. }
  171. func (r *Requirement) hasValue(value string) bool {
  172. for i := range r.strValues {
  173. if r.strValues[i] == value {
  174. return true
  175. }
  176. }
  177. return false
  178. }
  179. // Matches returns true if the Requirement matches the input Labels.
  180. // There is a match in the following cases:
  181. // (1) The operator is Exists and Labels has the Requirement's key.
  182. // (2) The operator is In, Labels has the Requirement's key and Labels'
  183. //
  184. // value for that key is in Requirement's value set.
  185. //
  186. // (3) The operator is NotIn, Labels has the Requirement's key and
  187. //
  188. // Labels' value for that key is not in Requirement's value set.
  189. //
  190. // (4) The operator is DoesNotExist or NotIn and Labels does not have the
  191. //
  192. // Requirement's key.
  193. //
  194. // (5) The operator is GreaterThanOperator or LessThanOperator, and Labels has
  195. //
  196. // the Requirement's key and the corresponding value satisfies mathematical inequality.
  197. func (r *Requirement) Matches(ls Labels) bool {
  198. switch r.operator {
  199. case selection.In, selection.Equals, selection.DoubleEquals:
  200. if !ls.Has(r.key) {
  201. return false
  202. }
  203. return r.hasValue(ls.Get(r.key))
  204. case selection.NotIn, selection.NotEquals:
  205. if !ls.Has(r.key) {
  206. return true
  207. }
  208. return !r.hasValue(ls.Get(r.key))
  209. case selection.Exists:
  210. return ls.Has(r.key)
  211. case selection.DoesNotExist:
  212. return !ls.Has(r.key)
  213. case selection.GreaterThan, selection.LessThan:
  214. if !ls.Has(r.key) {
  215. return false
  216. }
  217. lsValue, err := strconv.ParseInt(ls.Get(r.key), 10, 64)
  218. if err != nil {
  219. klog.V(10).Infof("ParseInt failed for value %+v in label %+v, %+v", ls.Get(r.key), ls, err)
  220. return false
  221. }
  222. // There should be only one strValue in r.strValues, and can be converted to an integer.
  223. if len(r.strValues) != 1 {
  224. klog.V(10).Infof("Invalid values count %+v of requirement %#v, for 'Gt', 'Lt' operators, exactly one value is required", len(r.strValues), r)
  225. return false
  226. }
  227. var rValue int64
  228. for i := range r.strValues {
  229. rValue, err = strconv.ParseInt(r.strValues[i], 10, 64)
  230. if err != nil {
  231. klog.V(10).Infof("ParseInt failed for value %+v in requirement %#v, for 'Gt', 'Lt' operators, the value must be an integer", r.strValues[i], r)
  232. return false
  233. }
  234. }
  235. return (r.operator == selection.GreaterThan && lsValue > rValue) || (r.operator == selection.LessThan && lsValue < rValue)
  236. default:
  237. return false
  238. }
  239. }
  240. // Key returns requirement key
  241. func (r *Requirement) Key() string {
  242. return r.key
  243. }
  244. // Operator returns requirement operator
  245. func (r *Requirement) Operator() selection.Operator {
  246. return r.operator
  247. }
  248. // Values returns requirement values
  249. func (r *Requirement) Values() sets.String {
  250. ret := sets.String{}
  251. for i := range r.strValues {
  252. ret.Insert(r.strValues[i])
  253. }
  254. return ret
  255. }
  256. // Equal checks the equality of requirement.
  257. func (r Requirement) Equal(x Requirement) bool {
  258. if r.key != x.key {
  259. return false
  260. }
  261. if r.operator != x.operator {
  262. return false
  263. }
  264. return stringslices.Equal(r.strValues, x.strValues)
  265. }
  266. // Empty returns true if the internalSelector doesn't restrict selection space
  267. func (s internalSelector) Empty() bool {
  268. if s == nil {
  269. return true
  270. }
  271. return len(s) == 0
  272. }
  273. // String returns a human-readable string that represents this
  274. // Requirement. If called on an invalid Requirement, an error is
  275. // returned. See NewRequirement for creating a valid Requirement.
  276. func (r *Requirement) String() string {
  277. var sb strings.Builder
  278. sb.Grow(
  279. // length of r.key
  280. len(r.key) +
  281. // length of 'r.operator' + 2 spaces for the worst case ('in' and 'notin')
  282. len(r.operator) + 2 +
  283. // length of 'r.strValues' slice times. Heuristically 5 chars per word
  284. +5*len(r.strValues))
  285. if r.operator == selection.DoesNotExist {
  286. sb.WriteString("!")
  287. }
  288. sb.WriteString(r.key)
  289. switch r.operator {
  290. case selection.Equals:
  291. sb.WriteString("=")
  292. case selection.DoubleEquals:
  293. sb.WriteString("==")
  294. case selection.NotEquals:
  295. sb.WriteString("!=")
  296. case selection.In:
  297. sb.WriteString(" in ")
  298. case selection.NotIn:
  299. sb.WriteString(" notin ")
  300. case selection.GreaterThan:
  301. sb.WriteString(">")
  302. case selection.LessThan:
  303. sb.WriteString("<")
  304. case selection.Exists, selection.DoesNotExist:
  305. return sb.String()
  306. }
  307. switch r.operator {
  308. case selection.In, selection.NotIn:
  309. sb.WriteString("(")
  310. }
  311. if len(r.strValues) == 1 {
  312. sb.WriteString(r.strValues[0])
  313. } else { // only > 1 since == 0 prohibited by NewRequirement
  314. // normalizes value order on output, without mutating the in-memory selector representation
  315. // also avoids normalization when it is not required, and ensures we do not mutate shared data
  316. sb.WriteString(strings.Join(safeSort(r.strValues), ","))
  317. }
  318. switch r.operator {
  319. case selection.In, selection.NotIn:
  320. sb.WriteString(")")
  321. }
  322. return sb.String()
  323. }
  324. // safeSort sorts input strings without modification
  325. func safeSort(in []string) []string {
  326. if sort.StringsAreSorted(in) {
  327. return in
  328. }
  329. out := make([]string, len(in))
  330. copy(out, in)
  331. sort.Strings(out)
  332. return out
  333. }
  334. // Add adds requirements to the selector. It copies the current selector returning a new one
  335. func (s internalSelector) Add(reqs ...Requirement) Selector {
  336. ret := make(internalSelector, 0, len(s)+len(reqs))
  337. ret = append(ret, s...)
  338. ret = append(ret, reqs...)
  339. sort.Sort(ByKey(ret))
  340. return ret
  341. }
  342. // Matches for a internalSelector returns true if all
  343. // its Requirements match the input Labels. If any
  344. // Requirement does not match, false is returned.
  345. func (s internalSelector) Matches(l Labels) bool {
  346. for ix := range s {
  347. if matches := s[ix].Matches(l); !matches {
  348. return false
  349. }
  350. }
  351. return true
  352. }
  353. func (s internalSelector) Requirements() (Requirements, bool) { return Requirements(s), true }
  354. // String returns a comma-separated string of all
  355. // the internalSelector Requirements' human-readable strings.
  356. func (s internalSelector) String() string {
  357. var reqs []string
  358. for ix := range s {
  359. reqs = append(reqs, s[ix].String())
  360. }
  361. return strings.Join(reqs, ",")
  362. }
  363. // RequiresExactMatch introspects whether a given selector requires a single specific field
  364. // to be set, and if so returns the value it requires.
  365. func (s internalSelector) RequiresExactMatch(label string) (value string, found bool) {
  366. for ix := range s {
  367. if s[ix].key == label {
  368. switch s[ix].operator {
  369. case selection.Equals, selection.DoubleEquals, selection.In:
  370. if len(s[ix].strValues) == 1 {
  371. return s[ix].strValues[0], true
  372. }
  373. }
  374. return "", false
  375. }
  376. }
  377. return "", false
  378. }
  379. // Token represents constant definition for lexer token
  380. type Token int
  381. const (
  382. // ErrorToken represents scan error
  383. ErrorToken Token = iota
  384. // EndOfStringToken represents end of string
  385. EndOfStringToken
  386. // ClosedParToken represents close parenthesis
  387. ClosedParToken
  388. // CommaToken represents the comma
  389. CommaToken
  390. // DoesNotExistToken represents logic not
  391. DoesNotExistToken
  392. // DoubleEqualsToken represents double equals
  393. DoubleEqualsToken
  394. // EqualsToken represents equal
  395. EqualsToken
  396. // GreaterThanToken represents greater than
  397. GreaterThanToken
  398. // IdentifierToken represents identifier, e.g. keys and values
  399. IdentifierToken
  400. // InToken represents in
  401. InToken
  402. // LessThanToken represents less than
  403. LessThanToken
  404. // NotEqualsToken represents not equal
  405. NotEqualsToken
  406. // NotInToken represents not in
  407. NotInToken
  408. // OpenParToken represents open parenthesis
  409. OpenParToken
  410. )
  411. // string2token contains the mapping between lexer Token and token literal
  412. // (except IdentifierToken, EndOfStringToken and ErrorToken since it makes no sense)
  413. var string2token = map[string]Token{
  414. ")": ClosedParToken,
  415. ",": CommaToken,
  416. "!": DoesNotExistToken,
  417. "==": DoubleEqualsToken,
  418. "=": EqualsToken,
  419. ">": GreaterThanToken,
  420. "in": InToken,
  421. "<": LessThanToken,
  422. "!=": NotEqualsToken,
  423. "notin": NotInToken,
  424. "(": OpenParToken,
  425. }
  426. // ScannedItem contains the Token and the literal produced by the lexer.
  427. type ScannedItem struct {
  428. tok Token
  429. literal string
  430. }
  431. // isWhitespace returns true if the rune is a space, tab, or newline.
  432. func isWhitespace(ch byte) bool {
  433. return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'
  434. }
  435. // isSpecialSymbol detects if the character ch can be an operator
  436. func isSpecialSymbol(ch byte) bool {
  437. switch ch {
  438. case '=', '!', '(', ')', ',', '>', '<':
  439. return true
  440. }
  441. return false
  442. }
  443. // Lexer represents the Lexer struct for label selector.
  444. // It contains necessary informationt to tokenize the input string
  445. type Lexer struct {
  446. // s stores the string to be tokenized
  447. s string
  448. // pos is the position currently tokenized
  449. pos int
  450. }
  451. // read returns the character currently lexed
  452. // increment the position and check the buffer overflow
  453. func (l *Lexer) read() (b byte) {
  454. b = 0
  455. if l.pos < len(l.s) {
  456. b = l.s[l.pos]
  457. l.pos++
  458. }
  459. return b
  460. }
  461. // unread 'undoes' the last read character
  462. func (l *Lexer) unread() {
  463. l.pos--
  464. }
  465. // scanIDOrKeyword scans string to recognize literal token (for example 'in') or an identifier.
  466. func (l *Lexer) scanIDOrKeyword() (tok Token, lit string) {
  467. var buffer []byte
  468. IdentifierLoop:
  469. for {
  470. switch ch := l.read(); {
  471. case ch == 0:
  472. break IdentifierLoop
  473. case isSpecialSymbol(ch) || isWhitespace(ch):
  474. l.unread()
  475. break IdentifierLoop
  476. default:
  477. buffer = append(buffer, ch)
  478. }
  479. }
  480. s := string(buffer)
  481. if val, ok := string2token[s]; ok { // is a literal token?
  482. return val, s
  483. }
  484. return IdentifierToken, s // otherwise is an identifier
  485. }
  486. // scanSpecialSymbol scans string starting with special symbol.
  487. // special symbol identify non literal operators. "!=", "==", "="
  488. func (l *Lexer) scanSpecialSymbol() (Token, string) {
  489. lastScannedItem := ScannedItem{}
  490. var buffer []byte
  491. SpecialSymbolLoop:
  492. for {
  493. switch ch := l.read(); {
  494. case ch == 0:
  495. break SpecialSymbolLoop
  496. case isSpecialSymbol(ch):
  497. buffer = append(buffer, ch)
  498. if token, ok := string2token[string(buffer)]; ok {
  499. lastScannedItem = ScannedItem{tok: token, literal: string(buffer)}
  500. } else if lastScannedItem.tok != 0 {
  501. l.unread()
  502. break SpecialSymbolLoop
  503. }
  504. default:
  505. l.unread()
  506. break SpecialSymbolLoop
  507. }
  508. }
  509. if lastScannedItem.tok == 0 {
  510. return ErrorToken, fmt.Sprintf("error expected: keyword found '%s'", buffer)
  511. }
  512. return lastScannedItem.tok, lastScannedItem.literal
  513. }
  514. // skipWhiteSpaces consumes all blank characters
  515. // returning the first non blank character
  516. func (l *Lexer) skipWhiteSpaces(ch byte) byte {
  517. for {
  518. if !isWhitespace(ch) {
  519. return ch
  520. }
  521. ch = l.read()
  522. }
  523. }
  524. // Lex returns a pair of Token and the literal
  525. // literal is meaningfull only for IdentifierToken token
  526. func (l *Lexer) Lex() (tok Token, lit string) {
  527. switch ch := l.skipWhiteSpaces(l.read()); {
  528. case ch == 0:
  529. return EndOfStringToken, ""
  530. case isSpecialSymbol(ch):
  531. l.unread()
  532. return l.scanSpecialSymbol()
  533. default:
  534. l.unread()
  535. return l.scanIDOrKeyword()
  536. }
  537. }
  538. // Parser data structure contains the label selector parser data structure
  539. type Parser struct {
  540. l *Lexer
  541. scannedItems []ScannedItem
  542. position int
  543. path *field.Path
  544. }
  545. // ParserContext represents context during parsing:
  546. // some literal for example 'in' and 'notin' can be
  547. // recognized as operator for example 'x in (a)' but
  548. // it can be recognized as value for example 'value in (in)'
  549. type ParserContext int
  550. const (
  551. // KeyAndOperator represents key and operator
  552. KeyAndOperator ParserContext = iota
  553. // Values represents values
  554. Values
  555. )
  556. // lookahead func returns the current token and string. No increment of current position
  557. func (p *Parser) lookahead(context ParserContext) (Token, string) {
  558. tok, lit := p.scannedItems[p.position].tok, p.scannedItems[p.position].literal
  559. if context == Values {
  560. switch tok {
  561. case InToken, NotInToken:
  562. tok = IdentifierToken
  563. }
  564. }
  565. return tok, lit
  566. }
  567. // consume returns current token and string. Increments the position
  568. func (p *Parser) consume(context ParserContext) (Token, string) {
  569. p.position++
  570. tok, lit := p.scannedItems[p.position-1].tok, p.scannedItems[p.position-1].literal
  571. if context == Values {
  572. switch tok {
  573. case InToken, NotInToken:
  574. tok = IdentifierToken
  575. }
  576. }
  577. return tok, lit
  578. }
  579. // scan runs through the input string and stores the ScannedItem in an array
  580. // Parser can now lookahead and consume the tokens
  581. func (p *Parser) scan() {
  582. for {
  583. token, literal := p.l.Lex()
  584. p.scannedItems = append(p.scannedItems, ScannedItem{token, literal})
  585. if token == EndOfStringToken {
  586. break
  587. }
  588. }
  589. }
  590. // parse runs the left recursive descending algorithm
  591. // on input string. It returns a list of Requirement objects.
  592. func (p *Parser) parse() (internalSelector, error) {
  593. p.scan() // init scannedItems
  594. var requirements internalSelector
  595. for {
  596. tok, lit := p.lookahead(Values)
  597. switch tok {
  598. case IdentifierToken, DoesNotExistToken:
  599. r, err := p.parseRequirement()
  600. if err != nil {
  601. return nil, fmt.Errorf("unable to parse requirement: %v", err)
  602. }
  603. requirements = append(requirements, *r)
  604. t, l := p.consume(Values)
  605. switch t {
  606. case EndOfStringToken:
  607. return requirements, nil
  608. case CommaToken:
  609. t2, l2 := p.lookahead(Values)
  610. if t2 != IdentifierToken && t2 != DoesNotExistToken {
  611. return nil, fmt.Errorf("found '%s', expected: identifier after ','", l2)
  612. }
  613. default:
  614. return nil, fmt.Errorf("found '%s', expected: ',' or 'end of string'", l)
  615. }
  616. case EndOfStringToken:
  617. return requirements, nil
  618. default:
  619. return nil, fmt.Errorf("found '%s', expected: !, identifier, or 'end of string'", lit)
  620. }
  621. }
  622. }
  623. func (p *Parser) parseRequirement() (*Requirement, error) {
  624. key, operator, err := p.parseKeyAndInferOperator()
  625. if err != nil {
  626. return nil, err
  627. }
  628. if operator == selection.Exists || operator == selection.DoesNotExist { // operator found lookahead set checked
  629. return NewRequirement(key, operator, []string{}, field.WithPath(p.path))
  630. }
  631. operator, err = p.parseOperator()
  632. if err != nil {
  633. return nil, err
  634. }
  635. var values sets.String
  636. switch operator {
  637. case selection.In, selection.NotIn:
  638. values, err = p.parseValues()
  639. case selection.Equals, selection.DoubleEquals, selection.NotEquals, selection.GreaterThan, selection.LessThan:
  640. values, err = p.parseExactValue()
  641. }
  642. if err != nil {
  643. return nil, err
  644. }
  645. return NewRequirement(key, operator, values.List(), field.WithPath(p.path))
  646. }
  647. // parseKeyAndInferOperator parses literals.
  648. // in case of no operator '!, in, notin, ==, =, !=' are found
  649. // the 'exists' operator is inferred
  650. func (p *Parser) parseKeyAndInferOperator() (string, selection.Operator, error) {
  651. var operator selection.Operator
  652. tok, literal := p.consume(Values)
  653. if tok == DoesNotExistToken {
  654. operator = selection.DoesNotExist
  655. tok, literal = p.consume(Values)
  656. }
  657. if tok != IdentifierToken {
  658. err := fmt.Errorf("found '%s', expected: identifier", literal)
  659. return "", "", err
  660. }
  661. if err := validateLabelKey(literal, p.path); err != nil {
  662. return "", "", err
  663. }
  664. if t, _ := p.lookahead(Values); t == EndOfStringToken || t == CommaToken {
  665. if operator != selection.DoesNotExist {
  666. operator = selection.Exists
  667. }
  668. }
  669. return literal, operator, nil
  670. }
  671. // parseOperator returns operator and eventually matchType
  672. // matchType can be exact
  673. func (p *Parser) parseOperator() (op selection.Operator, err error) {
  674. tok, lit := p.consume(KeyAndOperator)
  675. switch tok {
  676. // DoesNotExistToken shouldn't be here because it's a unary operator, not a binary operator
  677. case InToken:
  678. op = selection.In
  679. case EqualsToken:
  680. op = selection.Equals
  681. case DoubleEqualsToken:
  682. op = selection.DoubleEquals
  683. case GreaterThanToken:
  684. op = selection.GreaterThan
  685. case LessThanToken:
  686. op = selection.LessThan
  687. case NotInToken:
  688. op = selection.NotIn
  689. case NotEqualsToken:
  690. op = selection.NotEquals
  691. default:
  692. return "", fmt.Errorf("found '%s', expected: %v", lit, strings.Join(binaryOperators, ", "))
  693. }
  694. return op, nil
  695. }
  696. // parseValues parses the values for set based matching (x,y,z)
  697. func (p *Parser) parseValues() (sets.String, error) {
  698. tok, lit := p.consume(Values)
  699. if tok != OpenParToken {
  700. return nil, fmt.Errorf("found '%s' expected: '('", lit)
  701. }
  702. tok, lit = p.lookahead(Values)
  703. switch tok {
  704. case IdentifierToken, CommaToken:
  705. s, err := p.parseIdentifiersList() // handles general cases
  706. if err != nil {
  707. return s, err
  708. }
  709. if tok, _ = p.consume(Values); tok != ClosedParToken {
  710. return nil, fmt.Errorf("found '%s', expected: ')'", lit)
  711. }
  712. return s, nil
  713. case ClosedParToken: // handles "()"
  714. p.consume(Values)
  715. return sets.NewString(""), nil
  716. default:
  717. return nil, fmt.Errorf("found '%s', expected: ',', ')' or identifier", lit)
  718. }
  719. }
  720. // parseIdentifiersList parses a (possibly empty) list of
  721. // of comma separated (possibly empty) identifiers
  722. func (p *Parser) parseIdentifiersList() (sets.String, error) {
  723. s := sets.NewString()
  724. for {
  725. tok, lit := p.consume(Values)
  726. switch tok {
  727. case IdentifierToken:
  728. s.Insert(lit)
  729. tok2, lit2 := p.lookahead(Values)
  730. switch tok2 {
  731. case CommaToken:
  732. continue
  733. case ClosedParToken:
  734. return s, nil
  735. default:
  736. return nil, fmt.Errorf("found '%s', expected: ',' or ')'", lit2)
  737. }
  738. case CommaToken: // handled here since we can have "(,"
  739. if s.Len() == 0 {
  740. s.Insert("") // to handle (,
  741. }
  742. tok2, _ := p.lookahead(Values)
  743. if tok2 == ClosedParToken {
  744. s.Insert("") // to handle ,) Double "" removed by StringSet
  745. return s, nil
  746. }
  747. if tok2 == CommaToken {
  748. p.consume(Values)
  749. s.Insert("") // to handle ,, Double "" removed by StringSet
  750. }
  751. default: // it can be operator
  752. return s, fmt.Errorf("found '%s', expected: ',', or identifier", lit)
  753. }
  754. }
  755. }
  756. // parseExactValue parses the only value for exact match style
  757. func (p *Parser) parseExactValue() (sets.String, error) {
  758. s := sets.NewString()
  759. tok, _ := p.lookahead(Values)
  760. if tok == EndOfStringToken || tok == CommaToken {
  761. s.Insert("")
  762. return s, nil
  763. }
  764. tok, lit := p.consume(Values)
  765. if tok == IdentifierToken {
  766. s.Insert(lit)
  767. return s, nil
  768. }
  769. return nil, fmt.Errorf("found '%s', expected: identifier", lit)
  770. }
  771. // Parse takes a string representing a selector and returns a selector
  772. // object, or an error. This parsing function differs from ParseSelector
  773. // as they parse different selectors with different syntaxes.
  774. // The input will cause an error if it does not follow this form:
  775. //
  776. // <selector-syntax> ::= <requirement> | <requirement> "," <selector-syntax>
  777. // <requirement> ::= [!] KEY [ <set-based-restriction> | <exact-match-restriction> ]
  778. // <set-based-restriction> ::= "" | <inclusion-exclusion> <value-set>
  779. // <inclusion-exclusion> ::= <inclusion> | <exclusion>
  780. // <exclusion> ::= "notin"
  781. // <inclusion> ::= "in"
  782. // <value-set> ::= "(" <values> ")"
  783. // <values> ::= VALUE | VALUE "," <values>
  784. // <exact-match-restriction> ::= ["="|"=="|"!="] VALUE
  785. //
  786. // KEY is a sequence of one or more characters following [ DNS_SUBDOMAIN "/" ] DNS_LABEL. Max length is 63 characters.
  787. // VALUE is a sequence of zero or more characters "([A-Za-z0-9_-\.])". Max length is 63 characters.
  788. // Delimiter is white space: (' ', '\t')
  789. // Example of valid syntax:
  790. //
  791. // "x in (foo,,baz),y,z notin ()"
  792. //
  793. // Note:
  794. //
  795. // (1) Inclusion - " in " - denotes that the KEY exists and is equal to any of the
  796. // VALUEs in its requirement
  797. // (2) Exclusion - " notin " - denotes that the KEY is not equal to any
  798. // of the VALUEs in its requirement or does not exist
  799. // (3) The empty string is a valid VALUE
  800. // (4) A requirement with just a KEY - as in "y" above - denotes that
  801. // the KEY exists and can be any VALUE.
  802. // (5) A requirement with just !KEY requires that the KEY not exist.
  803. func Parse(selector string, opts ...field.PathOption) (Selector, error) {
  804. parsedSelector, err := parse(selector, field.ToPath(opts...))
  805. if err == nil {
  806. return parsedSelector, nil
  807. }
  808. return nil, err
  809. }
  810. // parse parses the string representation of the selector and returns the internalSelector struct.
  811. // The callers of this method can then decide how to return the internalSelector struct to their
  812. // callers. This function has two callers now, one returns a Selector interface and the other
  813. // returns a list of requirements.
  814. func parse(selector string, path *field.Path) (internalSelector, error) {
  815. p := &Parser{l: &Lexer{s: selector, pos: 0}, path: path}
  816. items, err := p.parse()
  817. if err != nil {
  818. return nil, err
  819. }
  820. sort.Sort(ByKey(items)) // sort to grant determistic parsing
  821. return internalSelector(items), err
  822. }
  823. func validateLabelKey(k string, path *field.Path) *field.Error {
  824. if errs := validation.IsQualifiedName(k); len(errs) != 0 {
  825. return field.Invalid(path, k, strings.Join(errs, "; "))
  826. }
  827. return nil
  828. }
  829. func validateLabelValue(k, v string, path *field.Path) *field.Error {
  830. if errs := validation.IsValidLabelValue(v); len(errs) != 0 {
  831. return field.Invalid(path.Key(k), v, strings.Join(errs, "; "))
  832. }
  833. return nil
  834. }
  835. // SelectorFromSet returns a Selector which will match exactly the given Set. A
  836. // nil and empty Sets are considered equivalent to Everything().
  837. // It does not perform any validation, which means the server will reject
  838. // the request if the Set contains invalid values.
  839. func SelectorFromSet(ls Set) Selector {
  840. return SelectorFromValidatedSet(ls)
  841. }
  842. // ValidatedSelectorFromSet returns a Selector which will match exactly the given Set. A
  843. // nil and empty Sets are considered equivalent to Everything().
  844. // The Set is validated client-side, which allows to catch errors early.
  845. func ValidatedSelectorFromSet(ls Set) (Selector, error) {
  846. if ls == nil || len(ls) == 0 {
  847. return internalSelector{}, nil
  848. }
  849. requirements := make([]Requirement, 0, len(ls))
  850. for label, value := range ls {
  851. r, err := NewRequirement(label, selection.Equals, []string{value})
  852. if err != nil {
  853. return nil, err
  854. }
  855. requirements = append(requirements, *r)
  856. }
  857. // sort to have deterministic string representation
  858. sort.Sort(ByKey(requirements))
  859. return internalSelector(requirements), nil
  860. }
  861. // SelectorFromValidatedSet returns a Selector which will match exactly the given Set.
  862. // A nil and empty Sets are considered equivalent to Everything().
  863. // It assumes that Set is already validated and doesn't do any validation.
  864. func SelectorFromValidatedSet(ls Set) Selector {
  865. if ls == nil || len(ls) == 0 {
  866. return internalSelector{}
  867. }
  868. requirements := make([]Requirement, 0, len(ls))
  869. for label, value := range ls {
  870. requirements = append(requirements, Requirement{key: label, operator: selection.Equals, strValues: []string{value}})
  871. }
  872. // sort to have deterministic string representation
  873. sort.Sort(ByKey(requirements))
  874. return internalSelector(requirements)
  875. }
  876. // ParseToRequirements takes a string representing a selector and returns a list of
  877. // requirements. This function is suitable for those callers that perform additional
  878. // processing on selector requirements.
  879. // See the documentation for Parse() function for more details.
  880. // TODO: Consider exporting the internalSelector type instead.
  881. func ParseToRequirements(selector string, opts ...field.PathOption) ([]Requirement, error) {
  882. return parse(selector, field.ToPath(opts...))
  883. }