...

Source file src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags_fake.go

Documentation: k8s.io/cli-runtime/pkg/genericclioptions

     1  /*
     2  Copyright 2018 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 genericclioptions
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	"k8s.io/client-go/discovery"
    24  	"k8s.io/client-go/rest"
    25  	"k8s.io/client-go/restmapper"
    26  	"k8s.io/client-go/tools/clientcmd"
    27  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    28  )
    29  
    30  // TestConfigFlags contains clientConfig struct
    31  // and interfaces that implements RESTClientGetter
    32  type TestConfigFlags struct {
    33  	clientConfig    clientcmd.ClientConfig
    34  	discoveryClient discovery.CachedDiscoveryInterface
    35  	restMapper      meta.RESTMapper
    36  }
    37  
    38  // ToRawKubeConfigLoader implements RESTClientGetter
    39  // Returns a clientconfig if it's set
    40  func (f *TestConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig {
    41  	if f.clientConfig == nil {
    42  		panic("attempt to obtain a test RawKubeConfigLoader with no clientConfig specified")
    43  	}
    44  	return f.clientConfig
    45  }
    46  
    47  // ToRESTConfig implements RESTClientGetter.
    48  // Returns a REST client configuration based on a provided path
    49  // to a .kubeconfig file, loading rules, and config flag overrides.
    50  // Expects the AddFlags method to have been called.
    51  func (f *TestConfigFlags) ToRESTConfig() (*rest.Config, error) {
    52  	return f.ToRawKubeConfigLoader().ClientConfig()
    53  }
    54  
    55  // ToDiscoveryClient implements RESTClientGetter.
    56  // Returns a CachedDiscoveryInterface
    57  func (f *TestConfigFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
    58  	return f.discoveryClient, nil
    59  }
    60  
    61  // ToRESTMapper implements RESTClientGetter.
    62  // Returns a mapper.
    63  func (f *TestConfigFlags) ToRESTMapper() (meta.RESTMapper, error) {
    64  	if f.restMapper != nil {
    65  		return f.restMapper, nil
    66  	}
    67  	if f.discoveryClient != nil {
    68  		mapper := restmapper.NewDeferredDiscoveryRESTMapper(f.discoveryClient)
    69  		expander := restmapper.NewShortcutExpander(mapper, f.discoveryClient, nil)
    70  		return expander, nil
    71  	}
    72  	return nil, fmt.Errorf("no restmapper")
    73  }
    74  
    75  // WithClientConfig sets the clientConfig flag
    76  func (f *TestConfigFlags) WithClientConfig(clientConfig clientcmd.ClientConfig) *TestConfigFlags {
    77  	f.clientConfig = clientConfig
    78  	return f
    79  }
    80  
    81  // WithRESTMapper sets the restMapper flag
    82  func (f *TestConfigFlags) WithRESTMapper(mapper meta.RESTMapper) *TestConfigFlags {
    83  	f.restMapper = mapper
    84  	return f
    85  }
    86  
    87  // WithDiscoveryClient sets the discoveryClient flag
    88  func (f *TestConfigFlags) WithDiscoveryClient(c discovery.CachedDiscoveryInterface) *TestConfigFlags {
    89  	f.discoveryClient = c
    90  	return f
    91  }
    92  
    93  // WithNamespace sets the clientConfig flag by modifying delagate and namespace
    94  func (f *TestConfigFlags) WithNamespace(ns string) *TestConfigFlags {
    95  	if f.clientConfig == nil {
    96  		panic("attempt to obtain a test RawKubeConfigLoader with no clientConfig specified")
    97  	}
    98  	f.clientConfig = &namespacedClientConfig{
    99  		delegate:  f.clientConfig,
   100  		namespace: ns,
   101  	}
   102  	return f
   103  }
   104  
   105  // NewTestConfigFlags builds a TestConfigFlags struct to test ConfigFlags
   106  func NewTestConfigFlags() *TestConfigFlags {
   107  	return &TestConfigFlags{}
   108  }
   109  
   110  type namespacedClientConfig struct {
   111  	delegate  clientcmd.ClientConfig
   112  	namespace string
   113  }
   114  
   115  func (c *namespacedClientConfig) Namespace() (string, bool, error) {
   116  	return c.namespace, len(c.namespace) > 0, nil
   117  }
   118  
   119  func (c *namespacedClientConfig) RawConfig() (clientcmdapi.Config, error) {
   120  	return c.delegate.RawConfig()
   121  }
   122  func (c *namespacedClientConfig) ClientConfig() (*rest.Config, error) {
   123  	return c.delegate.ClientConfig()
   124  }
   125  func (c *namespacedClientConfig) ConfigAccess() clientcmd.ConfigAccess {
   126  	return c.delegate.ConfigAccess()
   127  }
   128  

View as plain text