k8s.io/apiserver@v0.31.1/pkg/storage/cacher/lister_watcher.go (about)

     1  /*
     2  Copyright 2023 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 cacher
    18  
    19  import (
    20  	"context"
    21  
    22  	"google.golang.org/grpc/metadata"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/fields"
    26  	"k8s.io/apimachinery/pkg/labels"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/apimachinery/pkg/watch"
    29  	"k8s.io/apiserver/pkg/storage"
    30  	"k8s.io/client-go/tools/cache"
    31  )
    32  
    33  // listerWatcher opaques storage.Interface to expose cache.ListerWatcher.
    34  type listerWatcher struct {
    35  	storage         storage.Interface
    36  	resourcePrefix  string
    37  	newListFunc     func() runtime.Object
    38  	contextMetadata metadata.MD
    39  }
    40  
    41  // NewListerWatcher returns a storage.Interface backed ListerWatcher.
    42  func NewListerWatcher(storage storage.Interface, resourcePrefix string, newListFunc func() runtime.Object, contextMetadata metadata.MD) cache.ListerWatcher {
    43  	return &listerWatcher{
    44  		storage:         storage,
    45  		resourcePrefix:  resourcePrefix,
    46  		newListFunc:     newListFunc,
    47  		contextMetadata: contextMetadata,
    48  	}
    49  }
    50  
    51  // Implements cache.ListerWatcher interface.
    52  func (lw *listerWatcher) List(options metav1.ListOptions) (runtime.Object, error) {
    53  	list := lw.newListFunc()
    54  	pred := storage.SelectionPredicate{
    55  		Label:    labels.Everything(),
    56  		Field:    fields.Everything(),
    57  		Limit:    options.Limit,
    58  		Continue: options.Continue,
    59  	}
    60  
    61  	storageOpts := storage.ListOptions{
    62  		ResourceVersionMatch: options.ResourceVersionMatch,
    63  		Predicate:            pred,
    64  		Recursive:            true,
    65  	}
    66  	ctx := context.Background()
    67  	if lw.contextMetadata != nil {
    68  		ctx = metadata.NewOutgoingContext(ctx, lw.contextMetadata)
    69  	}
    70  	if err := lw.storage.GetList(ctx, lw.resourcePrefix, storageOpts, list); err != nil {
    71  		return nil, err
    72  	}
    73  	return list, nil
    74  }
    75  
    76  // Implements cache.ListerWatcher interface.
    77  func (lw *listerWatcher) Watch(options metav1.ListOptions) (watch.Interface, error) {
    78  	opts := storage.ListOptions{
    79  		ResourceVersion: options.ResourceVersion,
    80  		Predicate:       storage.Everything,
    81  		Recursive:       true,
    82  		ProgressNotify:  true,
    83  	}
    84  	ctx := context.Background()
    85  	if lw.contextMetadata != nil {
    86  		ctx = metadata.NewOutgoingContext(ctx, lw.contextMetadata)
    87  	}
    88  	return lw.storage.Watch(ctx, lw.resourcePrefix, opts)
    89  }