/
driver.go
131 lines (110 loc) · 3.22 KB
/
driver.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
119
120
121
122
123
124
125
126
127
128
129
130
131
package k8s
import (
"fmt"
"time"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"github.com/seal-io/walrus/pkg/dao/model"
"github.com/seal-io/walrus/pkg/dao/types/crypto"
"github.com/seal-io/walrus/pkg/dao/types/property"
"github.com/seal-io/walrus/utils/strs"
"github.com/seal-io/walrus/utils/version"
)
// ConfigOption holds the modification to modify the return rest.Config.
type ConfigOption func(*rest.Config)
// WithoutTimeout disables the timeout.
func WithoutTimeout() ConfigOption {
return func(c *rest.Config) {
c.Timeout = 0
}
}
// WithTimeout sets the timeout.
func WithTimeout(t time.Duration) ConfigOption {
return func(c *rest.Config) {
c.Timeout = t
}
}
// WithRateLimit sets rate limitation.
func WithRateLimit(qps float32, burst int) ConfigOption {
return func(c *rest.Config) {
c.QPS = qps
c.Burst = burst
}
}
// GetConfig returns the rest.Config with the given model,
// by default, the rest.Config configures with 15s timeout/16 qps/64 burst,
// please modify the default configuration with ConfigOption as need.
func GetConfig(conn model.Connector, opts ...ConfigOption) (restConfig *rest.Config, err error) {
apiConfig, _, err := LoadApiConfig(conn)
if err != nil {
return nil, err
}
restConfig, err = clientcmd.
NewNonInteractiveClientConfig(*apiConfig, "", &clientcmd.ConfigOverrides{}, nil).
ClientConfig()
if err != nil {
err = fmt.Errorf("cannot construct rest config from api config: %w", err)
return
}
restConfig.Timeout = 15 * time.Second
restConfig.QPS = 16
restConfig.Burst = 64
restConfig.ContentType = runtime.ContentTypeProtobuf
restConfig.UserAgent = version.GetUserAgent()
for i := range opts {
if opts[i] == nil {
continue
}
opts[i](restConfig)
}
return
}
// LoadApiConfig returns the client api.Config with the given model.
func LoadApiConfig(conn model.Connector) (apiConfig *api.Config, raw string, err error) {
switch conn.ConfigVersion {
default:
return nil, "", fmt.Errorf("unknown config version: %v", conn.ConfigVersion)
case "v1":
// {
// "configVersion": "v1",
// "configData": {
// "kubeconfig": "..."
// }
// }.
raw, err = loadRawConfigV1(conn.ConfigData)
if err != nil {
return nil, "", fmt.Errorf("error load config from connector %s: %w", conn.Name, err)
}
apiConfig, err = loadApiConfigV1(raw)
if err != nil {
return nil, "", fmt.Errorf("error load version %s config: %w", conn.ConfigVersion, err)
}
}
return
}
func loadRawConfigV1(data crypto.Properties) (string, error) {
// {
// "kubeconfig": "..."
// }.
kubeconfigText, exist, err := property.GetString(data["kubeconfig"].Value)
if err != nil {
return "", fmt.Errorf(`failed to get "kubeconfig": %w`, err)
}
if !exist {
return "", fmt.Errorf(`not found "kubeconfig"`)
}
return kubeconfigText, nil
}
func loadApiConfigV1(kubeconfigText string) (*api.Config, error) {
config, err := clientcmd.Load(strs.ToBytes(&kubeconfigText))
if err != nil {
return nil, fmt.Errorf("error load api config: %w", err)
}
err = clientcmd.Validate(*config)
if err != nil {
return nil, fmt.Errorf("error validate api config: %w", err)
}
return config, nil
}