helpers.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright 2016 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 v1
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/fields"
  17. "k8s.io/apimachinery/pkg/labels"
  18. "k8s.io/apimachinery/pkg/selection"
  19. "k8s.io/apimachinery/pkg/types"
  20. )
  21. // LabelSelectorAsSelector converts the LabelSelector api type into a struct that implements
  22. // labels.Selector
  23. // Note: This function should be kept in sync with the selector methods in pkg/labels/selector.go
  24. func LabelSelectorAsSelector(ps *LabelSelector) (labels.Selector, error) {
  25. if ps == nil {
  26. return labels.Nothing(), nil
  27. }
  28. if len(ps.MatchLabels)+len(ps.MatchExpressions) == 0 {
  29. return labels.Everything(), nil
  30. }
  31. selector := labels.NewSelector()
  32. for k, v := range ps.MatchLabels {
  33. r, err := labels.NewRequirement(k, selection.Equals, []string{v})
  34. if err != nil {
  35. return nil, err
  36. }
  37. selector = selector.Add(*r)
  38. }
  39. for _, expr := range ps.MatchExpressions {
  40. var op selection.Operator
  41. switch expr.Operator {
  42. case LabelSelectorOpIn:
  43. op = selection.In
  44. case LabelSelectorOpNotIn:
  45. op = selection.NotIn
  46. case LabelSelectorOpExists:
  47. op = selection.Exists
  48. case LabelSelectorOpDoesNotExist:
  49. op = selection.DoesNotExist
  50. default:
  51. return nil, fmt.Errorf("%q is not a valid pod selector operator", expr.Operator)
  52. }
  53. r, err := labels.NewRequirement(expr.Key, op, append([]string(nil), expr.Values...))
  54. if err != nil {
  55. return nil, err
  56. }
  57. selector = selector.Add(*r)
  58. }
  59. return selector, nil
  60. }
  61. // LabelSelectorAsMap converts the LabelSelector api type into a map of strings, ie. the
  62. // original structure of a label selector. Operators that cannot be converted into plain
  63. // labels (Exists, DoesNotExist, NotIn, and In with more than one value) will result in
  64. // an error.
  65. func LabelSelectorAsMap(ps *LabelSelector) (map[string]string, error) {
  66. if ps == nil {
  67. return nil, nil
  68. }
  69. selector := map[string]string{}
  70. for k, v := range ps.MatchLabels {
  71. selector[k] = v
  72. }
  73. for _, expr := range ps.MatchExpressions {
  74. switch expr.Operator {
  75. case LabelSelectorOpIn:
  76. if len(expr.Values) != 1 {
  77. return selector, fmt.Errorf("operator %q without a single value cannot be converted into the old label selector format", expr.Operator)
  78. }
  79. // Should we do anything in case this will override a previous key-value pair?
  80. selector[expr.Key] = expr.Values[0]
  81. case LabelSelectorOpNotIn, LabelSelectorOpExists, LabelSelectorOpDoesNotExist:
  82. return selector, fmt.Errorf("operator %q cannot be converted into the old label selector format", expr.Operator)
  83. default:
  84. return selector, fmt.Errorf("%q is not a valid selector operator", expr.Operator)
  85. }
  86. }
  87. return selector, nil
  88. }
  89. // ParseToLabelSelector parses a string representing a selector into a LabelSelector object.
  90. // Note: This function should be kept in sync with the parser in pkg/labels/selector.go
  91. func ParseToLabelSelector(selector string) (*LabelSelector, error) {
  92. reqs, err := labels.ParseToRequirements(selector)
  93. if err != nil {
  94. return nil, fmt.Errorf("couldn't parse the selector string \"%s\": %v", selector, err)
  95. }
  96. labelSelector := &LabelSelector{
  97. MatchLabels: map[string]string{},
  98. MatchExpressions: []LabelSelectorRequirement{},
  99. }
  100. for _, req := range reqs {
  101. var op LabelSelectorOperator
  102. switch req.Operator() {
  103. case selection.Equals, selection.DoubleEquals:
  104. vals := req.Values()
  105. if vals.Len() != 1 {
  106. return nil, fmt.Errorf("equals operator must have exactly one value")
  107. }
  108. val, ok := vals.PopAny()
  109. if !ok {
  110. return nil, fmt.Errorf("equals operator has exactly one value but it cannot be retrieved")
  111. }
  112. labelSelector.MatchLabels[req.Key()] = val
  113. continue
  114. case selection.In:
  115. op = LabelSelectorOpIn
  116. case selection.NotIn:
  117. op = LabelSelectorOpNotIn
  118. case selection.Exists:
  119. op = LabelSelectorOpExists
  120. case selection.DoesNotExist:
  121. op = LabelSelectorOpDoesNotExist
  122. case selection.GreaterThan, selection.LessThan:
  123. // Adding a separate case for these operators to indicate that this is deliberate
  124. return nil, fmt.Errorf("%q isn't supported in label selectors", req.Operator())
  125. default:
  126. return nil, fmt.Errorf("%q is not a valid label selector operator", req.Operator())
  127. }
  128. labelSelector.MatchExpressions = append(labelSelector.MatchExpressions, LabelSelectorRequirement{
  129. Key: req.Key(),
  130. Operator: op,
  131. Values: req.Values().List(),
  132. })
  133. }
  134. return labelSelector, nil
  135. }
  136. // SetAsLabelSelector converts the labels.Set object into a LabelSelector api object.
  137. func SetAsLabelSelector(ls labels.Set) *LabelSelector {
  138. if ls == nil {
  139. return nil
  140. }
  141. selector := &LabelSelector{
  142. MatchLabels: make(map[string]string),
  143. }
  144. for label, value := range ls {
  145. selector.MatchLabels[label] = value
  146. }
  147. return selector
  148. }
  149. // FormatLabelSelector convert labelSelector into plain string
  150. func FormatLabelSelector(labelSelector *LabelSelector) string {
  151. selector, err := LabelSelectorAsSelector(labelSelector)
  152. if err != nil {
  153. return "<error>"
  154. }
  155. l := selector.String()
  156. if len(l) == 0 {
  157. l = "<none>"
  158. }
  159. return l
  160. }
  161. func ExtractGroupVersions(l *APIGroupList) []string {
  162. var groupVersions []string
  163. for _, g := range l.Groups {
  164. for _, gv := range g.Versions {
  165. groupVersions = append(groupVersions, gv.GroupVersion)
  166. }
  167. }
  168. return groupVersions
  169. }
  170. // HasAnnotation returns a bool if passed in annotation exists
  171. func HasAnnotation(obj ObjectMeta, ann string) bool {
  172. _, found := obj.Annotations[ann]
  173. return found
  174. }
  175. // SetMetaDataAnnotation sets the annotation and value
  176. func SetMetaDataAnnotation(obj *ObjectMeta, ann string, value string) {
  177. if obj.Annotations == nil {
  178. obj.Annotations = make(map[string]string)
  179. }
  180. obj.Annotations[ann] = value
  181. }
  182. // SingleObject returns a ListOptions for watching a single object.
  183. func SingleObject(meta ObjectMeta) ListOptions {
  184. return ListOptions{
  185. FieldSelector: fields.OneTermEqualSelector("metadata.name", meta.Name).String(),
  186. ResourceVersion: meta.ResourceVersion,
  187. }
  188. }
  189. // NewDeleteOptions returns a DeleteOptions indicating the resource should
  190. // be deleted within the specified grace period. Use zero to indicate
  191. // immediate deletion. If you would prefer to use the default grace period,
  192. // use &metav1.DeleteOptions{} directly.
  193. func NewDeleteOptions(grace int64) *DeleteOptions {
  194. return &DeleteOptions{GracePeriodSeconds: &grace}
  195. }
  196. // NewPreconditionDeleteOptions returns a DeleteOptions with a UID precondition set.
  197. func NewPreconditionDeleteOptions(uid string) *DeleteOptions {
  198. u := types.UID(uid)
  199. p := Preconditions{UID: &u}
  200. return &DeleteOptions{Preconditions: &p}
  201. }
  202. // NewUIDPreconditions returns a Preconditions with UID set.
  203. func NewUIDPreconditions(uid string) *Preconditions {
  204. u := types.UID(uid)
  205. return &Preconditions{UID: &u}
  206. }
  207. // HasObjectMetaSystemFieldValues returns true if fields that are managed by the system on ObjectMeta have values.
  208. func HasObjectMetaSystemFieldValues(meta Object) bool {
  209. return !meta.GetCreationTimestamp().Time.IsZero() ||
  210. len(meta.GetUID()) != 0
  211. }