selector.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. Copyright 2015 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 fields
  14. import (
  15. "bytes"
  16. "fmt"
  17. "sort"
  18. "strings"
  19. "k8s.io/apimachinery/pkg/selection"
  20. )
  21. // Selector represents a field selector.
  22. type Selector interface {
  23. // Matches returns true if this selector matches the given set of fields.
  24. Matches(Fields) bool
  25. // Empty returns true if this selector does not restrict the selection space.
  26. Empty() bool
  27. // RequiresExactMatch allows a caller to introspect whether a given selector
  28. // requires a single specific field to be set, and if so returns the value it
  29. // requires.
  30. RequiresExactMatch(field string) (value string, found bool)
  31. // Transform returns a new copy of the selector after TransformFunc has been
  32. // applied to the entire selector, or an error if fn returns an error.
  33. Transform(fn TransformFunc) (Selector, error)
  34. // Requirements converts this interface to Requirements to expose
  35. // more detailed selection information.
  36. Requirements() Requirements
  37. // String returns a human readable string that represents this selector.
  38. String() string
  39. }
  40. // Everything returns a selector that matches all fields.
  41. func Everything() Selector {
  42. return andTerm{}
  43. }
  44. type hasTerm struct {
  45. field, value string
  46. }
  47. func (t *hasTerm) Matches(ls Fields) bool {
  48. return ls.Get(t.field) == t.value
  49. }
  50. func (t *hasTerm) Empty() bool {
  51. return false
  52. }
  53. func (t *hasTerm) RequiresExactMatch(field string) (value string, found bool) {
  54. if t.field == field {
  55. return t.value, true
  56. }
  57. return "", false
  58. }
  59. func (t *hasTerm) Transform(fn TransformFunc) (Selector, error) {
  60. field, value, err := fn(t.field, t.value)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &hasTerm{field, value}, nil
  65. }
  66. func (t *hasTerm) Requirements() Requirements {
  67. return []Requirement{{
  68. Field: t.field,
  69. Operator: selection.Equals,
  70. Value: t.value,
  71. }}
  72. }
  73. func (t *hasTerm) String() string {
  74. return fmt.Sprintf("%v=%v", t.field, EscapeValue(t.value))
  75. }
  76. type notHasTerm struct {
  77. field, value string
  78. }
  79. func (t *notHasTerm) Matches(ls Fields) bool {
  80. return ls.Get(t.field) != t.value
  81. }
  82. func (t *notHasTerm) Empty() bool {
  83. return false
  84. }
  85. func (t *notHasTerm) RequiresExactMatch(field string) (value string, found bool) {
  86. return "", false
  87. }
  88. func (t *notHasTerm) Transform(fn TransformFunc) (Selector, error) {
  89. field, value, err := fn(t.field, t.value)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return &notHasTerm{field, value}, nil
  94. }
  95. func (t *notHasTerm) Requirements() Requirements {
  96. return []Requirement{{
  97. Field: t.field,
  98. Operator: selection.NotEquals,
  99. Value: t.value,
  100. }}
  101. }
  102. func (t *notHasTerm) String() string {
  103. return fmt.Sprintf("%v!=%v", t.field, EscapeValue(t.value))
  104. }
  105. type andTerm []Selector
  106. func (t andTerm) Matches(ls Fields) bool {
  107. for _, q := range t {
  108. if !q.Matches(ls) {
  109. return false
  110. }
  111. }
  112. return true
  113. }
  114. func (t andTerm) Empty() bool {
  115. if t == nil {
  116. return true
  117. }
  118. if len([]Selector(t)) == 0 {
  119. return true
  120. }
  121. for i := range t {
  122. if !t[i].Empty() {
  123. return false
  124. }
  125. }
  126. return true
  127. }
  128. func (t andTerm) RequiresExactMatch(field string) (string, bool) {
  129. if t == nil || len([]Selector(t)) == 0 {
  130. return "", false
  131. }
  132. for i := range t {
  133. if value, found := t[i].RequiresExactMatch(field); found {
  134. return value, found
  135. }
  136. }
  137. return "", false
  138. }
  139. func (t andTerm) Transform(fn TransformFunc) (Selector, error) {
  140. next := make([]Selector, len([]Selector(t)))
  141. for i, s := range []Selector(t) {
  142. n, err := s.Transform(fn)
  143. if err != nil {
  144. return nil, err
  145. }
  146. next[i] = n
  147. }
  148. return andTerm(next), nil
  149. }
  150. func (t andTerm) Requirements() Requirements {
  151. reqs := make([]Requirement, 0, len(t))
  152. for _, s := range []Selector(t) {
  153. rs := s.Requirements()
  154. reqs = append(reqs, rs...)
  155. }
  156. return reqs
  157. }
  158. func (t andTerm) String() string {
  159. var terms []string
  160. for _, q := range t {
  161. terms = append(terms, q.String())
  162. }
  163. return strings.Join(terms, ",")
  164. }
  165. // SelectorFromSet returns a Selector which will match exactly the given Set. A
  166. // nil Set is considered equivalent to Everything().
  167. func SelectorFromSet(ls Set) Selector {
  168. if ls == nil {
  169. return Everything()
  170. }
  171. items := make([]Selector, 0, len(ls))
  172. for field, value := range ls {
  173. items = append(items, &hasTerm{field: field, value: value})
  174. }
  175. if len(items) == 1 {
  176. return items[0]
  177. }
  178. return andTerm(items)
  179. }
  180. // valueEscaper prefixes \,= characters with a backslash
  181. var valueEscaper = strings.NewReplacer(
  182. // escape \ characters
  183. `\`, `\\`,
  184. // then escape , and = characters to allow unambiguous parsing of the value in a fieldSelector
  185. `,`, `\,`,
  186. `=`, `\=`,
  187. )
  188. // Escapes an arbitrary literal string for use as a fieldSelector value
  189. func EscapeValue(s string) string {
  190. return valueEscaper.Replace(s)
  191. }
  192. // InvalidEscapeSequence indicates an error occurred unescaping a field selector
  193. type InvalidEscapeSequence struct {
  194. sequence string
  195. }
  196. func (i InvalidEscapeSequence) Error() string {
  197. return fmt.Sprintf("invalid field selector: invalid escape sequence: %s", i.sequence)
  198. }
  199. // UnescapedRune indicates an error occurred unescaping a field selector
  200. type UnescapedRune struct {
  201. r rune
  202. }
  203. func (i UnescapedRune) Error() string {
  204. return fmt.Sprintf("invalid field selector: unescaped character in value: %v", i.r)
  205. }
  206. // Unescapes a fieldSelector value and returns the original literal value.
  207. // May return the original string if it contains no escaped or special characters.
  208. func UnescapeValue(s string) (string, error) {
  209. // if there's no escaping or special characters, just return to avoid allocation
  210. if !strings.ContainsAny(s, `\,=`) {
  211. return s, nil
  212. }
  213. v := bytes.NewBuffer(make([]byte, 0, len(s)))
  214. inSlash := false
  215. for _, c := range s {
  216. if inSlash {
  217. switch c {
  218. case '\\', ',', '=':
  219. // omit the \ for recognized escape sequences
  220. v.WriteRune(c)
  221. default:
  222. // error on unrecognized escape sequences
  223. return "", InvalidEscapeSequence{sequence: string([]rune{'\\', c})}
  224. }
  225. inSlash = false
  226. continue
  227. }
  228. switch c {
  229. case '\\':
  230. inSlash = true
  231. case ',', '=':
  232. // unescaped , and = characters are not allowed in field selector values
  233. return "", UnescapedRune{r: c}
  234. default:
  235. v.WriteRune(c)
  236. }
  237. }
  238. // Ending with a single backslash is an invalid sequence
  239. if inSlash {
  240. return "", InvalidEscapeSequence{sequence: "\\"}
  241. }
  242. return v.String(), nil
  243. }
  244. // ParseSelectorOrDie takes a string representing a selector and returns an
  245. // object suitable for matching, or panic when an error occur.
  246. func ParseSelectorOrDie(s string) Selector {
  247. selector, err := ParseSelector(s)
  248. if err != nil {
  249. panic(err)
  250. }
  251. return selector
  252. }
  253. // ParseSelector takes a string representing a selector and returns an
  254. // object suitable for matching, or an error.
  255. func ParseSelector(selector string) (Selector, error) {
  256. return parseSelector(selector,
  257. func(lhs, rhs string) (newLhs, newRhs string, err error) {
  258. return lhs, rhs, nil
  259. })
  260. }
  261. // Parses the selector and runs them through the given TransformFunc.
  262. func ParseAndTransformSelector(selector string, fn TransformFunc) (Selector, error) {
  263. return parseSelector(selector, fn)
  264. }
  265. // Function to transform selectors.
  266. type TransformFunc func(field, value string) (newField, newValue string, err error)
  267. // splitTerms returns the comma-separated terms contained in the given fieldSelector.
  268. // Backslash-escaped commas are treated as data instead of delimiters, and are included in the returned terms, with the leading backslash preserved.
  269. func splitTerms(fieldSelector string) []string {
  270. if len(fieldSelector) == 0 {
  271. return nil
  272. }
  273. terms := make([]string, 0, 1)
  274. startIndex := 0
  275. inSlash := false
  276. for i, c := range fieldSelector {
  277. switch {
  278. case inSlash:
  279. inSlash = false
  280. case c == '\\':
  281. inSlash = true
  282. case c == ',':
  283. terms = append(terms, fieldSelector[startIndex:i])
  284. startIndex = i + 1
  285. }
  286. }
  287. terms = append(terms, fieldSelector[startIndex:])
  288. return terms
  289. }
  290. const (
  291. notEqualOperator = "!="
  292. doubleEqualOperator = "=="
  293. equalOperator = "="
  294. )
  295. // termOperators holds the recognized operators supported in fieldSelectors.
  296. // doubleEqualOperator and equal are equivalent, but doubleEqualOperator is checked first
  297. // to avoid leaving a leading = character on the rhs value.
  298. var termOperators = []string{notEqualOperator, doubleEqualOperator, equalOperator}
  299. // splitTerm returns the lhs, operator, and rhs parsed from the given term, along with an indicator of whether the parse was successful.
  300. // no escaping of special characters is supported in the lhs value, so the first occurance of a recognized operator is used as the split point.
  301. // the literal rhs is returned, and the caller is responsible for applying any desired unescaping.
  302. func splitTerm(term string) (lhs, op, rhs string, ok bool) {
  303. for i := range term {
  304. remaining := term[i:]
  305. for _, op := range termOperators {
  306. if strings.HasPrefix(remaining, op) {
  307. return term[0:i], op, term[i+len(op):], true
  308. }
  309. }
  310. }
  311. return "", "", "", false
  312. }
  313. func parseSelector(selector string, fn TransformFunc) (Selector, error) {
  314. parts := splitTerms(selector)
  315. sort.StringSlice(parts).Sort()
  316. var items []Selector
  317. for _, part := range parts {
  318. if part == "" {
  319. continue
  320. }
  321. lhs, op, rhs, ok := splitTerm(part)
  322. if !ok {
  323. return nil, fmt.Errorf("invalid selector: '%s'; can't understand '%s'", selector, part)
  324. }
  325. unescapedRHS, err := UnescapeValue(rhs)
  326. if err != nil {
  327. return nil, err
  328. }
  329. switch op {
  330. case notEqualOperator:
  331. items = append(items, &notHasTerm{field: lhs, value: unescapedRHS})
  332. case doubleEqualOperator:
  333. items = append(items, &hasTerm{field: lhs, value: unescapedRHS})
  334. case equalOperator:
  335. items = append(items, &hasTerm{field: lhs, value: unescapedRHS})
  336. default:
  337. return nil, fmt.Errorf("invalid selector: '%s'; can't understand '%s'", selector, part)
  338. }
  339. }
  340. if len(items) == 1 {
  341. return items[0].Transform(fn)
  342. }
  343. return andTerm(items).Transform(fn)
  344. }
  345. // OneTermEqualSelector returns an object that matches objects where one field/field equals one value.
  346. // Cannot return an error.
  347. func OneTermEqualSelector(k, v string) Selector {
  348. return &hasTerm{field: k, value: v}
  349. }
  350. // AndSelectors creates a selector that is the logical AND of all the given selectors
  351. func AndSelectors(selectors ...Selector) Selector {
  352. return andTerm(selectors)
  353. }