...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/config/restclientgetter.go

Documentation: github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/config

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package config
    17  
    18  import (
    19  	"path/filepath"
    20  	"regexp"
    21  	"strings"
    22  	"time"
    23  
    24  	"k8s.io/apimachinery/pkg/api/meta"
    25  	"k8s.io/cli-runtime/pkg/genericclioptions"
    26  	"k8s.io/client-go/discovery"
    27  	diskcached "k8s.io/client-go/discovery/cached/disk"
    28  	"k8s.io/client-go/rest"
    29  	"k8s.io/client-go/restmapper"
    30  	"k8s.io/client-go/tools/clientcmd"
    31  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    32  	"k8s.io/client-go/util/homedir"
    33  )
    34  
    35  var defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
    36  
    37  // simpleRestClientGetter implements genericclioptions.RESTClientGetter
    38  type simpleRestClientGetter struct {
    39  	clientcmdapi.Config
    40  }
    41  
    42  // ToRESTConfig returns restconfig
    43  func (getter *simpleRestClientGetter) ToRESTConfig() (*rest.Config, error) {
    44  	return getter.ToRawKubeConfigLoader().ClientConfig()
    45  }
    46  
    47  // ToDiscoveryClient returns discovery client
    48  func (getter *simpleRestClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
    49  	config, err := getter.ToRESTConfig()
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	config.Burst = 100
    55  
    56  	httpCacheDir := defaultCacheDir
    57  	discoveryCacheDir := computeDiscoverCacheDir(filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery"), config.Host)
    58  
    59  	return diskcached.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(10*time.Minute))
    60  }
    61  
    62  // ToRESTMapper returns a restmapper
    63  func (getter *simpleRestClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
    64  	discoveryClient, err := getter.ToDiscoveryClient()
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
    70  	expander := restmapper.NewShortcutExpander(mapper, discoveryClient)
    71  	return expander, nil
    72  }
    73  
    74  // ToRawKubeConfigLoader return kubeconfig loader as-is
    75  func (getter *simpleRestClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
    76  	return clientcmd.NewDefaultClientConfig(getter.Config, &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults})
    77  }
    78  
    79  // overlyCautiousIllegalFileCharacters matches characters that *might* not be supported.  Windows is really restrictive, so this is really restrictive
    80  var overlyCautiousIllegalFileCharacters = regexp.MustCompile(`[^(\w/\.)]`)
    81  
    82  // computeDiscoverCacheDir takes the parentDir and the host and comes up with a "usually non-colliding" name.
    83  func computeDiscoverCacheDir(parentDir, host string) string {
    84  	// strip the optional scheme from host if its there:
    85  	schemelessHost := strings.Replace(strings.Replace(host, "https://", "", 1), "http://", "", 1)
    86  	// now do a simple collapse of non-AZ09 characters.  Collisions are possible but unlikely.  Even if we do collide the problem is short lived
    87  	safeHost := overlyCautiousIllegalFileCharacters.ReplaceAllString(schemelessHost, "_")
    88  	return filepath.Join(parentDir, safeHost)
    89  }
    90  
    91  // NewSimpleRESTClientGetter initializes a new genericclioptions.RESTClientGetter from clientcmdapi.Config.
    92  func NewSimpleRESTClientGetter(config clientcmdapi.Config) genericclioptions.RESTClientGetter {
    93  	return &simpleRestClientGetter{config}
    94  }
    95