/
global.go
381 lines (344 loc) · 9.74 KB
/
global.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2016 TiKV Project 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 command
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"net/http"
"net/url"
"os"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/spf13/cobra"
pd "github.com/tikv/pd/client/http"
"github.com/tikv/pd/pkg/utils/apiutil"
"go.etcd.io/etcd/pkg/transport"
)
const (
pdControlCallerID = "pd-ctl"
clusterPrefix = "pd/api/v1/cluster"
)
func initTLSConfig(caPath, certPath, keyPath string) (*tls.Config, error) {
tlsInfo := transport.TLSInfo{
CertFile: certPath,
KeyFile: keyPath,
TrustedCAFile: caPath,
}
tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
return nil, errors.WithStack(err)
}
return tlsConfig, nil
}
// PDCli is a pd HTTP client
var PDCli pd.Client
func requirePDClient(cmd *cobra.Command, _ []string) error {
var (
caPath string
err error
)
caPath, err = cmd.Flags().GetString("cacert")
if err == nil && len(caPath) != 0 {
var certPath, keyPath string
certPath, err = cmd.Flags().GetString("cert")
if err != nil {
return err
}
keyPath, err = cmd.Flags().GetString("key")
if err != nil {
return err
}
return initNewPDClientWithTLS(cmd, caPath, certPath, keyPath)
}
return initNewPDClient(cmd)
}
// shouldInitPDClient checks whether we should create a new PD client according to the cluster information.
func shouldInitPDClient(cmd *cobra.Command) (bool, error) {
// Get the cluster information the current command assigned to.
newClusterInfoJSON, err := doRequest(cmd, clusterPrefix, http.MethodGet, http.Header{})
if err != nil {
return false, err
}
newClusterInfo := &metapb.Cluster{}
err = json.Unmarshal([]byte(newClusterInfoJSON), newClusterInfo)
if err != nil {
return false, err
}
// If the PD client is nil and we get the cluster information successfully,
// we should initialize the PD client directly.
if PDCli == nil {
return true, nil
}
// Get current cluster information that the PD client connects to.
currentClusterInfo, err := PDCli.GetCluster(cmd.Context())
if err != nil {
return true, nil
}
// Compare the cluster ID to determine whether we should re-initialize the PD client.
return currentClusterInfo.GetId() == 0 || newClusterInfo.GetId() != currentClusterInfo.GetId(), nil
}
func initNewPDClient(cmd *cobra.Command, opts ...pd.ClientOption) error {
if should, err := shouldInitPDClient(cmd); !should || err != nil {
return err
}
if PDCli != nil {
PDCli.Close()
}
PDCli = pd.NewClient(pdControlCallerID, getEndpoints(cmd), opts...)
return nil
}
func initNewPDClientWithTLS(cmd *cobra.Command, caPath, certPath, keyPath string) error {
tlsConfig, err := initTLSConfig(caPath, certPath, keyPath)
if err != nil {
return err
}
initNewPDClient(cmd, pd.WithTLSConfig(tlsConfig))
return nil
}
// TODO: replace dialClient with the PD HTTP client completely.
var dialClient = &http.Client{
Transport: apiutil.NewCallerIDRoundTripper(http.DefaultTransport, pdControlCallerID),
}
// RequireHTTPSClient creates a HTTPS client if the related flags are set
func RequireHTTPSClient(cmd *cobra.Command, _ []string) error {
caPath, err := cmd.Flags().GetString("cacert")
if err == nil && len(caPath) != 0 {
certPath, err := cmd.Flags().GetString("cert")
if err != nil {
return err
}
keyPath, err := cmd.Flags().GetString("key")
if err != nil {
return err
}
err = initHTTPSClient(caPath, certPath, keyPath)
if err != nil {
cmd.Println(err)
return err
}
}
return nil
}
func initHTTPSClient(caPath, certPath, keyPath string) error {
tlsConfig, err := initTLSConfig(caPath, certPath, keyPath)
if err != nil {
return err
}
dialClient = &http.Client{
Transport: apiutil.NewCallerIDRoundTripper(
&http.Transport{TLSClientConfig: tlsConfig}, pdControlCallerID),
}
return nil
}
type bodyOption struct {
body io.Reader
}
// BodyOption sets the type and content of the body
type BodyOption func(*bodyOption)
// WithBody returns a BodyOption
func WithBody(body io.Reader) BodyOption {
return func(bo *bodyOption) {
bo.body = body
}
}
func doRequest(cmd *cobra.Command, prefix string, method string, customHeader http.Header,
opts ...BodyOption) (string, error) {
b := &bodyOption{}
for _, o := range opts {
o(b)
}
var resp string
endpoints := getEndpoints(cmd)
err := tryURLs(cmd, endpoints, func(endpoint string) error {
return do(endpoint, prefix, method, &resp, customHeader, b)
})
return resp, err
}
func doRequestSingleEndpoint(cmd *cobra.Command, endpoint, prefix, method string, customHeader http.Header,
opts ...BodyOption) (string, error) {
b := &bodyOption{}
for _, o := range opts {
o(b)
}
var resp string
err := requestURL(cmd, endpoint, func(endpoint string) error {
return do(endpoint, prefix, method, &resp, customHeader, b)
})
return resp, err
}
func dial(req *http.Request) (string, error) {
resp, err := dialClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var msg []byte
msg, err = io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return "", errors.Errorf("[%d] %s", resp.StatusCode, msg)
}
content, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if req.Header.Get(apiutil.XForbiddenForwardToMicroServiceHeader) == "true" {
if resp.Header.Get(apiutil.XForwardedToMicroServiceHeader) == "true" {
return string(content), errors.Errorf("the request is forwarded to micro service unexpectedly")
}
}
return string(content), nil
}
// DoFunc receives an endpoint which you can issue request to
type DoFunc func(endpoint string) error
// tryURLs issues requests to each URL and tries next one if there
// is an error
func tryURLs(cmd *cobra.Command, endpoints []string, f DoFunc) error {
var err error
for _, endpoint := range endpoints {
endpoint, err = checkURL(endpoint)
if err != nil {
cmd.Println(err.Error())
os.Exit(1)
}
err = f(endpoint)
if err != nil {
continue
}
break
}
if len(endpoints) > 1 && err != nil {
err = errors.Errorf("after trying all endpoints, no endpoint is available, the last error we met: %s", err)
}
return err
}
func requestURL(cmd *cobra.Command, endpoint string, f DoFunc) error {
endpoint, err := checkURL(endpoint)
if err != nil {
cmd.Println(err.Error())
os.Exit(1)
}
return f(endpoint)
}
func getEndpoints(cmd *cobra.Command) []string {
addrs, err := cmd.Flags().GetString("pd")
if err != nil {
cmd.Println("get pd address failed, should set flag with '-u'")
os.Exit(1)
}
return strings.Split(addrs, ",")
}
func requestJSON(cmd *cobra.Command, method, prefix string, input map[string]any) {
data, err := json.Marshal(input)
if err != nil {
cmd.Println(err)
return
}
endpoints := getEndpoints(cmd)
var msg []byte
err = tryURLs(cmd, endpoints, func(endpoint string) error {
var req *http.Request
var resp *http.Response
url := endpoint + "/" + prefix
switch method {
case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodGet:
req, err = http.NewRequest(method, url, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err = dialClient.Do(req)
default:
err := errors.Errorf("method %s not supported", method)
return err
}
if err != nil {
return err
}
defer resp.Body.Close()
msg, err = io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf("[%d] %s", resp.StatusCode, msg)
}
return nil
})
if err != nil {
cmd.Printf("Failed! %s\n", err)
return
}
cmd.Printf("Success! %s\n", strings.Trim(string(msg), "\""))
}
func postJSON(cmd *cobra.Command, prefix string, input map[string]any) {
requestJSON(cmd, http.MethodPost, prefix, input)
}
func patchJSON(cmd *cobra.Command, prefix string, input map[string]any) {
requestJSON(cmd, http.MethodPatch, prefix, input)
}
// do send a request to server. Default is Get.
func do(endpoint, prefix, method string, resp *string, customHeader http.Header, b *bodyOption) error {
var err error
url := endpoint + "/" + prefix
if method == "" {
method = http.MethodGet
}
var req *http.Request
req, err = http.NewRequest(method, url, b.body)
if err != nil {
return err
}
for key, values := range customHeader {
for _, v := range values {
req.Header.Add(key, v)
}
}
// the resp would be returned by the outer function
*resp, err = dial(req)
if err != nil {
return err
}
return nil
}
func checkURL(endpoint string) (string, error) {
if j := strings.Index(endpoint, "//"); j == -1 {
endpoint = "//" + endpoint
}
var u *url.URL
u, err := url.Parse(endpoint)
if err != nil {
return "", errors.Errorf("address format is wrong, should like 'http://127.0.0.1:2379' or '127.0.0.1:2379'")
}
// tolerate some schemes that will be used by users, the TiKV SDK
// use 'tikv' as the scheme, it is really confused if we do not
// support it by pd-ctl
if u.Scheme == "" || u.Scheme == "pd" || u.Scheme == "tikv" {
u.Scheme = "http"
}
return u.String(), nil
}
func jsonPrint(cmd *cobra.Command, val any) {
jsonBytes, err := json.MarshalIndent(val, "", " ")
if err != nil {
cmd.Printf("Failed to marshal the data to json: %s\n", err)
return
}
cmd.Println(string(jsonBytes))
}