k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/framework/config/client_config.go (about) 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 config 18 19 import ( 20 "fmt" 21 "net" 22 "net/http" 23 "time" 24 25 utilnet "k8s.io/apimachinery/pkg/util/net" 26 restclient "k8s.io/client-go/rest" 27 "k8s.io/client-go/tools/clientcmd" 28 clientcmdapi "k8s.io/client-go/tools/clientcmd/api" 29 "k8s.io/client-go/transport" 30 ) 31 32 const ( 33 contentType = "application/vnd.kubernetes.protobuf" 34 qps = 100 35 burst = 200 36 ) 37 38 // PrepareConfig creates and initializes client config. 39 func PrepareConfig(path string) (config *restclient.Config, err error) { 40 config, err = GetConfig(path) 41 if err != nil { 42 return nil, err 43 } 44 if err = transportHack(config); err != nil { 45 return nil, fmt.Errorf("config initialization error: %v", err) 46 } 47 return config, nil 48 } 49 50 // GetConfig returns client config without additional initialization. 51 func GetConfig(path string) (config *restclient.Config, err error) { 52 if path != "" { 53 config, err = loadConfig(path) 54 } else { 55 config, err = restclient.InClusterConfig() 56 } 57 if err != nil { 58 return nil, err 59 } 60 initializeWithDefaults(config) 61 return 62 } 63 64 func restclientConfig(path string) (*clientcmdapi.Config, error) { 65 c, err := clientcmd.LoadFromFile(path) 66 if err != nil { 67 return nil, fmt.Errorf("error loading kubeconfig: %v", err) 68 } 69 return c, nil 70 } 71 72 func loadConfig(path string) (*restclient.Config, error) { 73 c, err := restclientConfig(path) 74 if err != nil { 75 return nil, err 76 } 77 return clientcmd.NewDefaultClientConfig(*c, &clientcmd.ConfigOverrides{}).ClientConfig() 78 } 79 80 func initializeWithDefaults(config *restclient.Config) { 81 config.ContentType = contentType 82 config.QPS = qps 83 config.Burst = burst 84 } 85 86 func transportHack(config *restclient.Config) error { 87 // For the purpose of this test, we want to force that clients 88 // do not share underlying transport (which is a default behavior 89 // in Kubernetes). Thus, we are explicitly creating transport for 90 // each client here. 91 transportConfig, err := config.TransportConfig() 92 if err != nil { 93 return err 94 } 95 tlsConfig, err := transport.TLSConfigFor(transportConfig) 96 if err != nil { 97 return err 98 } 99 config.Transport = utilnet.SetTransportDefaults(&http.Transport{ 100 Proxy: http.ProxyFromEnvironment, 101 TLSHandshakeTimeout: 10 * time.Second, 102 TLSClientConfig: tlsConfig, 103 MaxIdleConnsPerHost: 100, 104 DialContext: (&net.Dialer{ 105 Timeout: 30 * time.Second, 106 KeepAlive: 30 * time.Second, 107 }).DialContext, 108 }) 109 config.WrapTransport = transportConfig.WrapTransport 110 config.Dial = transportConfig.Dial 111 // Overwrite TLS-related fields from config to avoid collision with 112 // Transport field. 113 config.TLSClientConfig = restclient.TLSClientConfig{} 114 config.AuthProvider = nil 115 config.ExecProvider = nil 116 117 return nil 118 }