k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/informer/informer.go (about)

     1  /*
     2  Copyright 2019 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 informer
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  	"k8s.io/client-go/dynamic"
    26  	"k8s.io/client-go/dynamic/dynamicinformer"
    27  	"k8s.io/client-go/tools/cache"
    28  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    29  )
    30  
    31  // NewInformer creates a new informer.
    32  func NewInformer(
    33  	lw cache.ListerWatcher,
    34  	handleObj func(interface{}, interface{}),
    35  ) cache.SharedInformer {
    36  	informer := cache.NewSharedInformer(lw, nil, 0)
    37  	addEventHandler(informer, handleObj)
    38  	return informer
    39  }
    40  
    41  // NewDynamicInformer creates a new dynamic informer
    42  // for given namespace, fieldSelector and labelSelector.
    43  func NewDynamicInformer(
    44  	c dynamic.Interface,
    45  	gvr schema.GroupVersionResource,
    46  	selector *util.ObjectSelector,
    47  	handleObj func(interface{}, interface{}),
    48  ) cache.SharedInformer {
    49  	optionsModifier := func(options *metav1.ListOptions) {
    50  		options.FieldSelector = selector.FieldSelector
    51  		options.LabelSelector = selector.LabelSelector
    52  	}
    53  	tweakListOptions := dynamicinformer.TweakListOptionsFunc(optionsModifier)
    54  	dInformerFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(c, 0, selector.Namespace, tweakListOptions)
    55  
    56  	informer := dInformerFactory.ForResource(gvr).Informer()
    57  	addEventHandler(informer, handleObj)
    58  	return informer
    59  }
    60  
    61  func addEventHandler(i cache.SharedInformer,
    62  	handleObj func(interface{}, interface{}),
    63  ) {
    64  	i.AddEventHandler(cache.ResourceEventHandlerFuncs{
    65  		AddFunc: func(obj interface{}) {
    66  			handleObj(nil, obj)
    67  		},
    68  		UpdateFunc: func(oldObj, newObj interface{}) {
    69  			handleObj(oldObj, newObj)
    70  		},
    71  		DeleteFunc: func(obj interface{}) {
    72  			if tombstone, ok := obj.(cache.DeletedFinalStateUnknown); ok {
    73  				handleObj(tombstone.Obj, nil)
    74  			} else {
    75  				handleObj(obj, nil)
    76  			}
    77  		},
    78  	})
    79  }
    80  
    81  // StartAndSync starts informer and waits for it to be synced.
    82  func StartAndSync(i cache.SharedInformer, stopCh <-chan struct{}, timeout time.Duration) error {
    83  	go i.Run(stopCh)
    84  	timeoutCh := make(chan struct{})
    85  	timeoutTimer := time.AfterFunc(timeout, func() {
    86  		close(timeoutCh)
    87  	})
    88  	defer timeoutTimer.Stop()
    89  	if !cache.WaitForCacheSync(timeoutCh, i.HasSynced) {
    90  		return fmt.Errorf("timed out waiting for caches to sync")
    91  	}
    92  	return nil
    93  }