listwatch.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 cache
  14. import (
  15. "context"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/fields"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/watch"
  20. restclient "k8s.io/client-go/rest"
  21. )
  22. // Lister is any object that knows how to perform an initial list.
  23. type Lister interface {
  24. // List should return a list type object; the Items field will be extracted, and the
  25. // ResourceVersion field will be used to start the watch in the right place.
  26. List(options metav1.ListOptions) (runtime.Object, error)
  27. }
  28. // Watcher is any object that knows how to start a watch on a resource.
  29. type Watcher interface {
  30. // Watch should begin a watch at the specified version.
  31. Watch(options metav1.ListOptions) (watch.Interface, error)
  32. }
  33. // ListerWatcher is any object that knows how to perform an initial list and start a watch on a resource.
  34. type ListerWatcher interface {
  35. Lister
  36. Watcher
  37. }
  38. // ListFunc knows how to list resources
  39. type ListFunc func(options metav1.ListOptions) (runtime.Object, error)
  40. // WatchFunc knows how to watch resources
  41. type WatchFunc func(options metav1.ListOptions) (watch.Interface, error)
  42. // ListWatch knows how to list and watch a set of apiserver resources. It satisfies the ListerWatcher interface.
  43. // It is a convenience function for users of NewReflector, etc.
  44. // ListFunc and WatchFunc must not be nil
  45. type ListWatch struct {
  46. ListFunc ListFunc
  47. WatchFunc WatchFunc
  48. // DisableChunking requests no chunking for this list watcher.
  49. DisableChunking bool
  50. }
  51. // Getter interface knows how to access Get method from RESTClient.
  52. type Getter interface {
  53. Get() *restclient.Request
  54. }
  55. // NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
  56. func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
  57. optionsModifier := func(options *metav1.ListOptions) {
  58. options.FieldSelector = fieldSelector.String()
  59. }
  60. return NewFilteredListWatchFromClient(c, resource, namespace, optionsModifier)
  61. }
  62. // NewFilteredListWatchFromClient creates a new ListWatch from the specified client, resource, namespace, and option modifier.
  63. // Option modifier is a function takes a ListOptions and modifies the consumed ListOptions. Provide customized modifier function
  64. // to apply modification to ListOptions with a field selector, a label selector, or any other desired options.
  65. func NewFilteredListWatchFromClient(c Getter, resource string, namespace string, optionsModifier func(options *metav1.ListOptions)) *ListWatch {
  66. listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
  67. optionsModifier(&options)
  68. return c.Get().
  69. Namespace(namespace).
  70. Resource(resource).
  71. VersionedParams(&options, metav1.ParameterCodec).
  72. Do(context.TODO()).
  73. Get()
  74. }
  75. watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
  76. options.Watch = true
  77. optionsModifier(&options)
  78. return c.Get().
  79. Namespace(namespace).
  80. Resource(resource).
  81. VersionedParams(&options, metav1.ParameterCodec).
  82. Watch(context.TODO())
  83. }
  84. return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
  85. }
  86. // List a set of apiserver resources
  87. func (lw *ListWatch) List(options metav1.ListOptions) (runtime.Object, error) {
  88. // ListWatch is used in Reflector, which already supports pagination.
  89. // Don't paginate here to avoid duplication.
  90. return lw.ListFunc(options)
  91. }
  92. // Watch a set of apiserver resources
  93. func (lw *ListWatch) Watch(options metav1.ListOptions) (watch.Interface, error) {
  94. return lw.WatchFunc(options)
  95. }