...

Source file src/k8s.io/client-go/tools/cache/listwatch.go

Documentation: k8s.io/client-go/tools/cache

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cache
    18  
    19  import (
    20  	"context"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/fields"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/watch"
    26  	restclient "k8s.io/client-go/rest"
    27  )
    28  
    29  // Lister is any object that knows how to perform an initial list.
    30  type Lister interface {
    31  	// List should return a list type object; the Items field will be extracted, and the
    32  	// ResourceVersion field will be used to start the watch in the right place.
    33  	List(options metav1.ListOptions) (runtime.Object, error)
    34  }
    35  
    36  // Watcher is any object that knows how to start a watch on a resource.
    37  type Watcher interface {
    38  	// Watch should begin a watch at the specified version.
    39  	Watch(options metav1.ListOptions) (watch.Interface, error)
    40  }
    41  
    42  // ListerWatcher is any object that knows how to perform an initial list and start a watch on a resource.
    43  type ListerWatcher interface {
    44  	Lister
    45  	Watcher
    46  }
    47  
    48  // ListFunc knows how to list resources
    49  type ListFunc func(options metav1.ListOptions) (runtime.Object, error)
    50  
    51  // WatchFunc knows how to watch resources
    52  type WatchFunc func(options metav1.ListOptions) (watch.Interface, error)
    53  
    54  // ListWatch knows how to list and watch a set of apiserver resources.  It satisfies the ListerWatcher interface.
    55  // It is a convenience function for users of NewReflector, etc.
    56  // ListFunc and WatchFunc must not be nil
    57  type ListWatch struct {
    58  	ListFunc  ListFunc
    59  	WatchFunc WatchFunc
    60  	// DisableChunking requests no chunking for this list watcher.
    61  	DisableChunking bool
    62  }
    63  
    64  // Getter interface knows how to access Get method from RESTClient.
    65  type Getter interface {
    66  	Get() *restclient.Request
    67  }
    68  
    69  // NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
    70  func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
    71  	optionsModifier := func(options *metav1.ListOptions) {
    72  		options.FieldSelector = fieldSelector.String()
    73  	}
    74  	return NewFilteredListWatchFromClient(c, resource, namespace, optionsModifier)
    75  }
    76  
    77  // NewFilteredListWatchFromClient creates a new ListWatch from the specified client, resource, namespace, and option modifier.
    78  // Option modifier is a function takes a ListOptions and modifies the consumed ListOptions. Provide customized modifier function
    79  // to apply modification to ListOptions with a field selector, a label selector, or any other desired options.
    80  func NewFilteredListWatchFromClient(c Getter, resource string, namespace string, optionsModifier func(options *metav1.ListOptions)) *ListWatch {
    81  	listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
    82  		optionsModifier(&options)
    83  		return c.Get().
    84  			Namespace(namespace).
    85  			Resource(resource).
    86  			VersionedParams(&options, metav1.ParameterCodec).
    87  			Do(context.TODO()).
    88  			Get()
    89  	}
    90  	watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
    91  		options.Watch = true
    92  		optionsModifier(&options)
    93  		return c.Get().
    94  			Namespace(namespace).
    95  			Resource(resource).
    96  			VersionedParams(&options, metav1.ParameterCodec).
    97  			Watch(context.TODO())
    98  	}
    99  	return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
   100  }
   101  
   102  // List a set of apiserver resources
   103  func (lw *ListWatch) List(options metav1.ListOptions) (runtime.Object, error) {
   104  	// ListWatch is used in Reflector, which already supports pagination.
   105  	// Don't paginate here to avoid duplication.
   106  	return lw.ListFunc(options)
   107  }
   108  
   109  // Watch a set of apiserver resources
   110  func (lw *ListWatch) Watch(options metav1.ListOptions) (watch.Interface, error) {
   111  	return lw.WatchFunc(options)
   112  }
   113  

View as plain text