...

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

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

     1  // Copyright 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package config
    15  
    16  import (
    17  	"path/filepath"
    18  	"regexp"
    19  	"strings"
    20  	"time"
    21  
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	"k8s.io/cli-runtime/pkg/genericclioptions"
    24  	"k8s.io/client-go/discovery"
    25  	diskcached "k8s.io/client-go/discovery/cached/disk"
    26  	"k8s.io/client-go/rest"
    27  	"k8s.io/client-go/restmapper"
    28  	"k8s.io/client-go/tools/clientcmd"
    29  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    30  	"k8s.io/client-go/util/homedir"
    31  )
    32  
    33  var defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
    34  
    35  // simpleRestClientGetter implements genericclioptions.RESTClientGetter
    36  type simpleRestClientGetter struct {
    37  	clientcmdapi.Config
    38  }
    39  
    40  // ToRESTConfig returns restconfig
    41  func (getter *simpleRestClientGetter) ToRESTConfig() (*rest.Config, error) {
    42  	return getter.ToRawKubeConfigLoader().ClientConfig()
    43  }
    44  
    45  // ToDiscoveryClient returns discovery client
    46  func (getter *simpleRestClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
    47  	config, err := getter.ToRESTConfig()
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	config.Burst = 100
    53  
    54  	httpCacheDir := defaultCacheDir
    55  	discoveryCacheDir := computeDiscoverCacheDir(filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery"), config.Host)
    56  
    57  	return diskcached.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(10*time.Minute))
    58  }
    59  
    60  // ToRESTMapper returns a restmapper
    61  func (getter *simpleRestClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
    62  	discoveryClient, err := getter.ToDiscoveryClient()
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
    68  	expander := restmapper.NewShortcutExpander(mapper, discoveryClient)
    69  	return expander, nil
    70  }
    71  
    72  // ToRawKubeConfigLoader return kubeconfig loader as-is
    73  func (getter *simpleRestClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
    74  	return clientcmd.NewDefaultClientConfig(getter.Config, &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults})
    75  }
    76  
    77  // overlyCautiousIllegalFileCharacters matches characters that *might* not be supported.  Windows is really restrictive, so this is really restrictive
    78  var overlyCautiousIllegalFileCharacters = regexp.MustCompile(`[^(\w/\.)]`)
    79  
    80  // computeDiscoverCacheDir takes the parentDir and the host and comes up with a "usually non-colliding" name.
    81  func computeDiscoverCacheDir(parentDir, host string) string {
    82  	// strip the optional scheme from host if its there:
    83  	schemelessHost := strings.Replace(strings.Replace(host, "https://", "", 1), "http://", "", 1)
    84  	// now do a simple collapse of non-AZ09 characters.  Collisions are possible but unlikely.  Even if we do collide the problem is short lived
    85  	safeHost := overlyCautiousIllegalFileCharacters.ReplaceAllString(schemelessHost, "_")
    86  	return filepath.Join(parentDir, safeHost)
    87  }
    88  
    89  // NewSimpleRESTClientGetter initializes a new genericclioptions.RESTClientGetter from clientcmdapi.Config.
    90  func NewSimpleRESTClientGetter(config clientcmdapi.Config) genericclioptions.RESTClientGetter {
    91  	return &simpleRestClientGetter{config}
    92  }
    93