selector.go 32 KB

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