/
client.go
59 lines (52 loc) · 1.37 KB
/
client.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
package client
import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
// DefaultQPS is the default QPS value.
DefaultQPS = 50
// DefaultBurst is the default Burst value.
DefaultBurst = 100
)
// SetQPS sets the QPS and Burst.
func SetQPS(qps float32, burst int) func(c *rest.Config) {
return func(c *rest.Config) {
c.QPS = qps
c.Burst = burst
}
}
// BuildConfigFromFlags builds rest configs from a master url or a kube config filepath.
func BuildConfigFromFlags(masterURL, kubeConfigPath string, options ...func(c *rest.Config)) (*rest.Config, error) {
restConfig, err := clientcmd.BuildConfigFromFlags(masterURL, kubeConfigPath)
if err != nil {
return nil, err
}
for _, opt := range options {
opt(restConfig)
}
if restConfig.QPS == 0 {
restConfig.QPS = DefaultQPS
}
if restConfig.Burst == 0 {
restConfig.Burst = DefaultBurst
}
return restConfig, nil
}
// BuildConfigFromKubeConfig builds rest configs from kube config data.
func BuildConfigFromKubeConfig(kubeconfig []byte, options ...func(c *rest.Config)) (*rest.Config, error) {
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeconfig)
if err != nil {
return nil, err
}
for _, opt := range options {
opt(restConfig)
}
if restConfig.QPS == 0 {
restConfig.QPS = DefaultQPS
}
if restConfig.Burst == 0 {
restConfig.Burst = DefaultBurst
}
return restConfig, nil
}