...

Source file src/k8s.io/kubernetes/test/utils/ktesting/clientcontext.go

Documentation: k8s.io/kubernetes/test/utils/ktesting

     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 ktesting
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/onsi/gomega"
    23  	apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
    24  	"k8s.io/client-go/discovery/cached/memory"
    25  	"k8s.io/client-go/dynamic"
    26  	clientset "k8s.io/client-go/kubernetes"
    27  	"k8s.io/client-go/rest"
    28  	"k8s.io/client-go/restmapper"
    29  	"k8s.io/klog/v2"
    30  )
    31  
    32  // WithRESTConfig initializes all client-go clients with new clients
    33  // created for the config. The current test name gets included in the UserAgent.
    34  func WithRESTConfig(tCtx TContext, cfg *rest.Config) TContext {
    35  	cfg = rest.CopyConfig(cfg)
    36  	cfg.UserAgent = fmt.Sprintf("%s -- %s", rest.DefaultKubernetesUserAgent(), tCtx.Name())
    37  
    38  	cCtx := clientContext{
    39  		TContext:      tCtx,
    40  		restConfig:    cfg,
    41  		client:        clientset.NewForConfigOrDie(cfg),
    42  		dynamic:       dynamic.NewForConfigOrDie(cfg),
    43  		apiextensions: apiextensions.NewForConfigOrDie(cfg),
    44  	}
    45  
    46  	cachedDiscovery := memory.NewMemCacheClient(cCtx.client.Discovery())
    47  	cCtx.restMapper = restmapper.NewDeferredDiscoveryRESTMapper(cachedDiscovery)
    48  
    49  	return &cCtx
    50  }
    51  
    52  // WithClients uses an existing config and clients.
    53  func WithClients(tCtx TContext, cfg *rest.Config, mapper *restmapper.DeferredDiscoveryRESTMapper, client clientset.Interface, dynamic dynamic.Interface, apiextensions apiextensions.Interface) TContext {
    54  	return clientContext{
    55  		TContext:      tCtx,
    56  		restConfig:    cfg,
    57  		restMapper:    mapper,
    58  		client:        client,
    59  		dynamic:       dynamic,
    60  		apiextensions: apiextensions,
    61  	}
    62  }
    63  
    64  type clientContext struct {
    65  	TContext
    66  
    67  	restConfig    *rest.Config
    68  	restMapper    *restmapper.DeferredDiscoveryRESTMapper
    69  	client        clientset.Interface
    70  	dynamic       dynamic.Interface
    71  	apiextensions apiextensions.Interface
    72  }
    73  
    74  func (cCtx clientContext) CleanupCtx(cb func(TContext)) {
    75  	cCtx.Helper()
    76  	cleanupCtx(cCtx, cb)
    77  }
    78  
    79  func (cCtx clientContext) Expect(actual interface{}, extra ...interface{}) gomega.Assertion {
    80  	cCtx.Helper()
    81  	return expect(cCtx, actual, extra...)
    82  }
    83  
    84  func (cCtx clientContext) ExpectNoError(err error, explain ...interface{}) {
    85  	cCtx.Helper()
    86  	expectNoError(cCtx, err, explain...)
    87  }
    88  
    89  func (cCtx clientContext) Logger() klog.Logger {
    90  	return klog.FromContext(cCtx)
    91  }
    92  
    93  func (cCtx clientContext) RESTConfig() *rest.Config {
    94  	if cCtx.restConfig == nil {
    95  		return nil
    96  	}
    97  	return rest.CopyConfig(cCtx.restConfig)
    98  }
    99  
   100  func (cCtx clientContext) RESTMapper() *restmapper.DeferredDiscoveryRESTMapper {
   101  	return cCtx.restMapper
   102  }
   103  
   104  func (cCtx clientContext) Client() clientset.Interface {
   105  	return cCtx.client
   106  }
   107  
   108  func (cCtx clientContext) Dynamic() dynamic.Interface {
   109  	return cCtx.dynamic
   110  }
   111  
   112  func (cCtx clientContext) APIExtensions() apiextensions.Interface {
   113  	return cCtx.apiextensions
   114  }
   115  

View as plain text