-
Notifications
You must be signed in to change notification settings - Fork 508
/
client_config.go
118 lines (104 loc) · 3.17 KB
/
client_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"fmt"
"net"
"net/http"
"time"
utilnet "k8s.io/apimachinery/pkg/util/net"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/transport"
)
const (
contentType = "application/vnd.kubernetes.protobuf"
qps = 100
burst = 200
)
// PrepareConfig creates and initializes client config.
func PrepareConfig(path string) (config *restclient.Config, err error) {
config, err = GetConfig(path)
if err != nil {
return nil, err
}
if err = transportHack(config); err != nil {
return nil, fmt.Errorf("config initialization error: %v", err)
}
return config, nil
}
// GetConfig returns client config without additional initialization.
func GetConfig(path string) (config *restclient.Config, err error) {
if path != "" {
config, err = loadConfig(path)
} else {
config, err = restclient.InClusterConfig()
}
if err != nil {
return nil, err
}
initializeWithDefaults(config)
return
}
func restclientConfig(path string) (*clientcmdapi.Config, error) {
c, err := clientcmd.LoadFromFile(path)
if err != nil {
return nil, fmt.Errorf("error loading kubeconfig: %v", err)
}
return c, nil
}
func loadConfig(path string) (*restclient.Config, error) {
c, err := restclientConfig(path)
if err != nil {
return nil, err
}
return clientcmd.NewDefaultClientConfig(*c, &clientcmd.ConfigOverrides{}).ClientConfig()
}
func initializeWithDefaults(config *restclient.Config) {
config.ContentType = contentType
config.QPS = qps
config.Burst = burst
}
func transportHack(config *restclient.Config) error {
// For the purpose of this test, we want to force that clients
// do not share underlying transport (which is a default behavior
// in Kubernetes). Thus, we are explicitly creating transport for
// each client here.
transportConfig, err := config.TransportConfig()
if err != nil {
return err
}
tlsConfig, err := transport.TLSConfigFor(transportConfig)
if err != nil {
return err
}
config.Transport = utilnet.SetTransportDefaults(&http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: tlsConfig,
MaxIdleConnsPerHost: 100,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
})
config.WrapTransport = transportConfig.WrapTransport
config.Dial = transportConfig.Dial
// Overwrite TLS-related fields from config to avoid collision with
// Transport field.
config.TLSClientConfig = restclient.TLSClientConfig{}
config.AuthProvider = nil
config.ExecProvider = nil
return nil
}