k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/object_store_dynamic.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  /*
    18  This file is copy of https://github.com/kubernetes/kubernetes/blob/master/test/utils/pod_store.go
    19  with slight changes regarding labelSelector and flagSelector applied.
    20  */
    21  
    22  package util
    23  
    24  import (
    25  	"context"
    26  	"encoding/json"
    27  	"fmt"
    28  	"time"
    29  
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	"k8s.io/apimachinery/pkg/labels"
    32  	"k8s.io/apimachinery/pkg/runtime"
    33  	"k8s.io/apimachinery/pkg/runtime/schema"
    34  	"k8s.io/client-go/dynamic"
    35  	"k8s.io/client-go/dynamic/dynamicinformer"
    36  	"k8s.io/client-go/tools/cache"
    37  )
    38  
    39  const (
    40  	defaultResyncInterval = 10 * time.Second
    41  )
    42  
    43  // DynamicObjectStore is a convenient wrapper around cache.GenericLister.
    44  type DynamicObjectStore struct {
    45  	cache.GenericLister
    46  	namespaces map[string]bool
    47  }
    48  
    49  // NewDynamicObjectStore creates DynamicObjectStore based on given object version resource and selector.
    50  func NewDynamicObjectStore(ctx context.Context, dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, namespaces map[string]bool) (*DynamicObjectStore, error) {
    51  	informerFactory := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, defaultResyncInterval)
    52  	lister := informerFactory.ForResource(gvr).Lister()
    53  	informerFactory.Start(ctx.Done())
    54  	informerFactory.WaitForCacheSync(ctx.Done())
    55  
    56  	return &DynamicObjectStore{
    57  		GenericLister: lister,
    58  		namespaces:    namespaces,
    59  	}, nil
    60  }
    61  
    62  // ListObjectSimplifications returns list of objects with conditions for each object that was returned by lister.
    63  func (s *DynamicObjectStore) ListObjectSimplifications() ([]ObjectSimplification, error) {
    64  	objects, err := s.GenericLister.List(labels.Everything())
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	result := make([]ObjectSimplification, 0, len(objects))
    70  	for _, o := range objects {
    71  		os, err := getObjectSimplification(o)
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  		if !s.namespaces[os.Metadata.Namespace] {
    76  			continue
    77  		}
    78  		result = append(result, os)
    79  	}
    80  	return result, nil
    81  }
    82  
    83  // ObjectSimplification represents the content of the object
    84  // that is needed to be handled by this measurement.
    85  type ObjectSimplification struct {
    86  	Metadata metav1.ObjectMeta    `json:"metadata"`
    87  	Status   StatusWithConditions `json:"status"`
    88  }
    89  
    90  // StatusWithConditions represents the content of the status field
    91  // that is required to be handled by this measurement.
    92  type StatusWithConditions struct {
    93  	Conditions []metav1.Condition `json:"conditions"`
    94  }
    95  
    96  func (o ObjectSimplification) String() string {
    97  	return fmt.Sprintf("%s/%s", o.Metadata.Namespace, o.Metadata.Name)
    98  }
    99  
   100  func getObjectSimplification(o runtime.Object) (ObjectSimplification, error) {
   101  	dataMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(o)
   102  	if err != nil {
   103  		return ObjectSimplification{}, err
   104  	}
   105  
   106  	jsonBytes, err := json.Marshal(dataMap)
   107  	if err != nil {
   108  		return ObjectSimplification{}, err
   109  	}
   110  
   111  	object := ObjectSimplification{}
   112  	err = json.Unmarshal(jsonBytes, &object)
   113  	return object, err
   114  }