k8s.io/kubernetes@v1.29.3/test/utils/pod_store.go (about)

     1  /*
     2  Copyright 2016 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 utils
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"k8s.io/api/core/v1"
    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/util/wait"
    29  	"k8s.io/apimachinery/pkg/watch"
    30  	clientset "k8s.io/client-go/kubernetes"
    31  	"k8s.io/client-go/tools/cache"
    32  )
    33  
    34  // Convenient wrapper around cache.Store that returns list of v1.Pod instead of interface{}.
    35  type PodStore struct {
    36  	cache.Store
    37  	stopCh    chan struct{}
    38  	Reflector *cache.Reflector
    39  }
    40  
    41  func NewPodStore(c clientset.Interface, namespace string, label labels.Selector, field fields.Selector) (*PodStore, error) {
    42  	lw := &cache.ListWatch{
    43  		ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
    44  			options.LabelSelector = label.String()
    45  			options.FieldSelector = field.String()
    46  			obj, err := c.CoreV1().Pods(namespace).List(context.TODO(), options)
    47  			return runtime.Object(obj), err
    48  		},
    49  		WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
    50  			options.LabelSelector = label.String()
    51  			options.FieldSelector = field.String()
    52  			return c.CoreV1().Pods(namespace).Watch(context.TODO(), options)
    53  		},
    54  	}
    55  	store := cache.NewStore(cache.MetaNamespaceKeyFunc)
    56  	stopCh := make(chan struct{})
    57  	reflector := cache.NewReflector(lw, &v1.Pod{}, store, 0)
    58  	go reflector.Run(stopCh)
    59  	if err := wait.PollImmediate(50*time.Millisecond, 2*time.Minute, func() (bool, error) {
    60  		if len(reflector.LastSyncResourceVersion()) != 0 {
    61  			return true, nil
    62  		}
    63  		return false, nil
    64  	}); err != nil {
    65  		close(stopCh)
    66  		return nil, err
    67  	}
    68  	return &PodStore{Store: store, stopCh: stopCh, Reflector: reflector}, nil
    69  }
    70  
    71  func (s *PodStore) List() []*v1.Pod {
    72  	objects := s.Store.List()
    73  	pods := make([]*v1.Pod, 0)
    74  	for _, o := range objects {
    75  		pods = append(pods, o.(*v1.Pod))
    76  	}
    77  	return pods
    78  }
    79  
    80  func (s *PodStore) Stop() {
    81  	close(s.stopCh)
    82  }