helpers.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. "bytes"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "k8s.io/apimachinery/pkg/fields"
  20. "k8s.io/apimachinery/pkg/labels"
  21. "k8s.io/apimachinery/pkg/selection"
  22. "k8s.io/apimachinery/pkg/types"
  23. )
  24. // LabelSelectorAsSelector converts the LabelSelector api type into a struct that implements
  25. // labels.Selector
  26. // Note: This function should be kept in sync with the selector methods in pkg/labels/selector.go
  27. func LabelSelectorAsSelector(ps *LabelSelector) (labels.Selector, error) {
  28. if ps == nil {
  29. return labels.Nothing(), nil
  30. }
  31. if len(ps.MatchLabels)+len(ps.MatchExpressions) == 0 {
  32. return labels.Everything(), nil
  33. }
  34. requirements := make([]labels.Requirement, 0, len(ps.MatchLabels)+len(ps.MatchExpressions))
  35. for k, v := range ps.MatchLabels {
  36. r, err := labels.NewRequirement(k, selection.Equals, []string{v})
  37. if err != nil {
  38. return nil, err
  39. }
  40. requirements = append(requirements, *r)
  41. }
  42. for _, expr := range ps.MatchExpressions {
  43. var op selection.Operator
  44. switch expr.Operator {
  45. case LabelSelectorOpIn:
  46. op = selection.In
  47. case LabelSelectorOpNotIn:
  48. op = selection.NotIn
  49. case LabelSelectorOpExists:
  50. op = selection.Exists
  51. case LabelSelectorOpDoesNotExist:
  52. op = selection.DoesNotExist
  53. default:
  54. return nil, fmt.Errorf("%q is not a valid label selector operator", expr.Operator)
  55. }
  56. r, err := labels.NewRequirement(expr.Key, op, append([]string(nil), expr.Values...))
  57. if err != nil {
  58. return nil, err
  59. }
  60. requirements = append(requirements, *r)
  61. }
  62. selector := labels.NewSelector()
  63. selector = selector.Add(requirements...)
  64. return selector, nil
  65. }
  66. // LabelSelectorAsMap converts the LabelSelector api type into a map of strings, ie. the
  67. // original structure of a label selector. Operators that cannot be converted into plain
  68. // labels (Exists, DoesNotExist, NotIn, and In with more than one value) will result in
  69. // an error.
  70. func LabelSelectorAsMap(ps *LabelSelector) (map[string]string, error) {
  71. if ps == nil {
  72. return nil, nil
  73. }
  74. selector := map[string]string{}
  75. for k, v := range ps.MatchLabels {
  76. selector[k] = v
  77. }
  78. for _, expr := range ps.MatchExpressions {
  79. switch expr.Operator {
  80. case LabelSelectorOpIn:
  81. if len(expr.Values) != 1 {
  82. return selector, fmt.Errorf("operator %q without a single value cannot be converted into the old label selector format", expr.Operator)
  83. }
  84. // Should we do anything in case this will override a previous key-value pair?
  85. selector[expr.Key] = expr.Values[0]
  86. case LabelSelectorOpNotIn, LabelSelectorOpExists, LabelSelectorOpDoesNotExist:
  87. return selector, fmt.Errorf("operator %q cannot be converted into the old label selector format", expr.Operator)
  88. default:
  89. return selector, fmt.Errorf("%q is not a valid selector operator", expr.Operator)
  90. }
  91. }
  92. return selector, nil
  93. }
  94. // ParseToLabelSelector parses a string representing a selector into a LabelSelector object.
  95. // Note: This function should be kept in sync with the parser in pkg/labels/selector.go
  96. func ParseToLabelSelector(selector string) (*LabelSelector, error) {
  97. reqs, err := labels.ParseToRequirements(selector)
  98. if err != nil {
  99. return nil, fmt.Errorf("couldn't parse the selector string \"%s\": %v", selector, err)
  100. }
  101. labelSelector := &LabelSelector{
  102. MatchLabels: map[string]string{},
  103. MatchExpressions: []LabelSelectorRequirement{},
  104. }
  105. for _, req := range reqs {
  106. var op LabelSelectorOperator
  107. switch req.Operator() {
  108. case selection.Equals, selection.DoubleEquals:
  109. vals := req.Values()
  110. if vals.Len() != 1 {
  111. return nil, fmt.Errorf("equals operator must have exactly one value")
  112. }
  113. val, ok := vals.PopAny()
  114. if !ok {
  115. return nil, fmt.Errorf("equals operator has exactly one value but it cannot be retrieved")
  116. }
  117. labelSelector.MatchLabels[req.Key()] = val
  118. continue
  119. case selection.In:
  120. op = LabelSelectorOpIn
  121. case selection.NotIn:
  122. op = LabelSelectorOpNotIn
  123. case selection.Exists:
  124. op = LabelSelectorOpExists
  125. case selection.DoesNotExist:
  126. op = LabelSelectorOpDoesNotExist
  127. case selection.GreaterThan, selection.LessThan:
  128. // Adding a separate case for these operators to indicate that this is deliberate
  129. return nil, fmt.Errorf("%q isn't supported in label selectors", req.Operator())
  130. default:
  131. return nil, fmt.Errorf("%q is not a valid label selector operator", req.Operator())
  132. }
  133. labelSelector.MatchExpressions = append(labelSelector.MatchExpressions, LabelSelectorRequirement{
  134. Key: req.Key(),
  135. Operator: op,
  136. Values: req.Values().List(),
  137. })
  138. }
  139. return labelSelector, nil
  140. }
  141. // SetAsLabelSelector converts the labels.Set object into a LabelSelector api object.
  142. func SetAsLabelSelector(ls labels.Set) *LabelSelector {
  143. if ls == nil {
  144. return nil
  145. }
  146. selector := &LabelSelector{
  147. MatchLabels: make(map[string]string, len(ls)),
  148. }
  149. for label, value := range ls {
  150. selector.MatchLabels[label] = value
  151. }
  152. return selector
  153. }
  154. // FormatLabelSelector convert labelSelector into plain string
  155. func FormatLabelSelector(labelSelector *LabelSelector) string {
  156. selector, err := LabelSelectorAsSelector(labelSelector)
  157. if err != nil {
  158. return "<error>"
  159. }
  160. l := selector.String()
  161. if len(l) == 0 {
  162. l = "<none>"
  163. }
  164. return l
  165. }
  166. func ExtractGroupVersions(l *APIGroupList) []string {
  167. var groupVersions []string
  168. for _, g := range l.Groups {
  169. for _, gv := range g.Versions {
  170. groupVersions = append(groupVersions, gv.GroupVersion)
  171. }
  172. }
  173. return groupVersions
  174. }
  175. // HasAnnotation returns a bool if passed in annotation exists
  176. func HasAnnotation(obj ObjectMeta, ann string) bool {
  177. _, found := obj.Annotations[ann]
  178. return found
  179. }
  180. // SetMetaDataAnnotation sets the annotation and value
  181. func SetMetaDataAnnotation(obj *ObjectMeta, ann string, value string) {
  182. if obj.Annotations == nil {
  183. obj.Annotations = make(map[string]string)
  184. }
  185. obj.Annotations[ann] = value
  186. }
  187. // HasLabel returns a bool if passed in label exists
  188. func HasLabel(obj ObjectMeta, label string) bool {
  189. _, found := obj.Labels[label]
  190. return found
  191. }
  192. // SetMetaDataLabel sets the label and value
  193. func SetMetaDataLabel(obj *ObjectMeta, label string, value string) {
  194. if obj.Labels == nil {
  195. obj.Labels = make(map[string]string)
  196. }
  197. obj.Labels[label] = value
  198. }
  199. // SingleObject returns a ListOptions for watching a single object.
  200. func SingleObject(meta ObjectMeta) ListOptions {
  201. return ListOptions{
  202. FieldSelector: fields.OneTermEqualSelector("metadata.name", meta.Name).String(),
  203. ResourceVersion: meta.ResourceVersion,
  204. }
  205. }
  206. // NewDeleteOptions returns a DeleteOptions indicating the resource should
  207. // be deleted within the specified grace period. Use zero to indicate
  208. // immediate deletion. If you would prefer to use the default grace period,
  209. // use &metav1.DeleteOptions{} directly.
  210. func NewDeleteOptions(grace int64) *DeleteOptions {
  211. return &DeleteOptions{GracePeriodSeconds: &grace}
  212. }
  213. // NewPreconditionDeleteOptions returns a DeleteOptions with a UID precondition set.
  214. func NewPreconditionDeleteOptions(uid string) *DeleteOptions {
  215. u := types.UID(uid)
  216. p := Preconditions{UID: &u}
  217. return &DeleteOptions{Preconditions: &p}
  218. }
  219. // NewUIDPreconditions returns a Preconditions with UID set.
  220. func NewUIDPreconditions(uid string) *Preconditions {
  221. u := types.UID(uid)
  222. return &Preconditions{UID: &u}
  223. }
  224. // NewRVDeletionPrecondition returns a DeleteOptions with a ResourceVersion precondition set.
  225. func NewRVDeletionPrecondition(rv string) *DeleteOptions {
  226. p := Preconditions{ResourceVersion: &rv}
  227. return &DeleteOptions{Preconditions: &p}
  228. }
  229. // HasObjectMetaSystemFieldValues returns true if fields that are managed by the system on ObjectMeta have values.
  230. func HasObjectMetaSystemFieldValues(meta Object) bool {
  231. return !meta.GetCreationTimestamp().Time.IsZero() ||
  232. len(meta.GetUID()) != 0
  233. }
  234. // ResetObjectMetaForStatus forces the meta fields for a status update to match the meta fields
  235. // for a pre-existing object. This is opt-in for new objects with Status subresource.
  236. func ResetObjectMetaForStatus(meta, existingMeta Object) {
  237. meta.SetDeletionTimestamp(existingMeta.GetDeletionTimestamp())
  238. meta.SetGeneration(existingMeta.GetGeneration())
  239. meta.SetSelfLink(existingMeta.GetSelfLink())
  240. meta.SetLabels(existingMeta.GetLabels())
  241. meta.SetAnnotations(existingMeta.GetAnnotations())
  242. meta.SetFinalizers(existingMeta.GetFinalizers())
  243. meta.SetOwnerReferences(existingMeta.GetOwnerReferences())
  244. // managedFields must be preserved since it's been modified to
  245. // track changed fields in the status update.
  246. //meta.SetManagedFields(existingMeta.GetManagedFields())
  247. }
  248. // MarshalJSON implements json.Marshaler
  249. // MarshalJSON may get called on pointers or values, so implement MarshalJSON on value.
  250. // http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go
  251. func (f FieldsV1) MarshalJSON() ([]byte, error) {
  252. if f.Raw == nil {
  253. return []byte("null"), nil
  254. }
  255. return f.Raw, nil
  256. }
  257. // UnmarshalJSON implements json.Unmarshaler
  258. func (f *FieldsV1) UnmarshalJSON(b []byte) error {
  259. if f == nil {
  260. return errors.New("metav1.Fields: UnmarshalJSON on nil pointer")
  261. }
  262. if !bytes.Equal(b, []byte("null")) {
  263. f.Raw = append(f.Raw[0:0], b...)
  264. }
  265. return nil
  266. }
  267. var _ json.Marshaler = FieldsV1{}
  268. var _ json.Unmarshaler = &FieldsV1{}