-
Notifications
You must be signed in to change notification settings - Fork 37
/
client.go
113 lines (96 loc) · 2.89 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
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
// Copyright (c) 2021 Terminus, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"encoding/base64"
"fmt"
"net/http"
"github.com/erda-project/kubeprober/pkg/probe-master/tunnel-client/clusterdialer"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
)
type ManageConfig struct {
Type string `json:"type"`
Address string `json:"address"`
CaData string `json:"caData"`
CertData string `json:"certData"`
KeyData string `json:"keyData"`
Token string `json:"token"`
}
const (
ManageProxy = "proxy"
)
// GetRestConfig get rest.Config with manage config
func GetRestConfig(c *ManageConfig) (*rest.Config, error) {
// If not provide api-server address, use in-cluster rest config
if c.Address == "" {
return rest.InClusterConfig()
}
rc := &rest.Config{
Host: c.Address,
APIPath: "/apis",
QPS: 1000,
Burst: 100,
ContentConfig: rest.ContentConfig{
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
},
TLSClientConfig: rest.TLSClientConfig{},
UserAgent: rest.DefaultKubernetesUserAgent(),
RateLimiter: flowcontrol.NewTokenBucketRateLimiter(1000, 100),
}
// If ca data is empty, the certificate is not validated
if c.CaData == "" {
rc.TLSClientConfig.Insecure = true
} else {
caBytes, err := base64.StdEncoding.DecodeString(c.CaData)
if err != nil {
return nil, err
}
rc.TLSClientConfig.CAData = caBytes
}
// If token is not empty, use token firstly, else use certificate
if c.Token != "" {
rc.BearerToken = c.Token
} else {
if c.CertData == "" || c.KeyData == "" {
return nil, fmt.Errorf("must provide available cert data and key data")
}
certBytes, err := base64.StdEncoding.DecodeString(c.CertData)
if err != nil {
return nil, err
}
keyBytes, err := base64.StdEncoding.DecodeString(c.KeyData)
if err != nil {
return nil, err
}
rc.TLSClientConfig.CertData = certBytes
rc.TLSClientConfig.KeyData = keyBytes
}
return rc, nil
}
func GetDialerRestConfig(clusterName string, c *ManageConfig) (*rest.Config, error) {
rc, err := GetRestConfig(c)
if err != nil {
return nil, err
}
rc.TLSClientConfig.NextProtos = []string{"http/1.1"}
rc.UserAgent = rest.DefaultKubernetesUserAgent() + " cluster " + clusterName
rc.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
if ht, ok := rt.(*http.Transport); ok {
ht.DialContext = clusterdialer.DialContext(clusterName)
}
return rt
}
return rc, nil
}