/
agent.go
124 lines (107 loc) · 2.8 KB
/
agent.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
package agent
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
clientv3 "go.etcd.io/etcd/client/v3"
)
func MemberList(gcfg GlobalConfig, eps []string, options ...clientv3.OpOption) (*clientv3.MemberListResponse, error) {
cfgSpec := clientConfigWithoutEndpoints(gcfg)
var err error
if len(eps) == 0 {
eps, err = endpointsFromCmd(gcfg)
if err != nil {
return nil, err
}
}
cfgSpec.Endpoints = eps
c, err := createClient(cfgSpec)
if err != nil {
return nil, err
}
ctx, cancel := commandCtx(gcfg.CommandTimeout)
defer func() {
c.Close()
cancel()
}()
members, err := c.MemberList(ctx, options...)
if err != nil {
return nil, err
}
return members, nil
}
func EndpointStatus(gcfg GlobalConfig, ep string) (*clientv3.StatusResponse, error) {
cfgSpec := clientConfigWithoutEndpoints(gcfg)
cfgSpec.Endpoints = []string{ep}
c, err := createClient(cfgSpec)
if err != nil {
return nil, fmt.Errorf("failed to createClient: %w", err)
}
ctx, cancel := commandCtx(gcfg.CommandTimeout)
defer func() {
c.Close()
cancel()
}()
return c.Status(ctx, ep)
}
func Read(gcfg GlobalConfig, eps []string, key string, options ...clientv3.OpOption) (*clientv3.GetResponse, error) {
cfgSpec := clientConfigWithoutEndpoints(gcfg)
cfgSpec.Endpoints = eps
c, err := createClient(cfgSpec)
if err != nil {
return nil, fmt.Errorf("failed to createClient: %w", err)
}
ctx, cancel := commandCtx(gcfg.CommandTimeout)
defer func() {
c.Close()
cancel()
}()
return c.Get(ctx, key, options...)
}
func Metrics(gcfg GlobalConfig, ep string) ([]string, error) {
if !strings.HasPrefix(ep, "http://") && !strings.HasPrefix(ep, "https://") {
ep = "http://" + ep
}
urlPath, err := url.JoinPath(ep, "metrics")
if err != nil {
return nil, fmt.Errorf("failed to join metrics url path: %w", err)
}
client := &http.Client{}
if strings.HasPrefix(urlPath, "https://") {
// load client certificate
cert, err := tls.LoadX509KeyPair(gcfg.CertFile, gcfg.KeyFile)
if err != nil {
return nil, fmt.Errorf("failed to load certificate: %w", err)
}
// load CA
caCert, err := os.ReadFile(gcfg.CaFile)
if err != nil {
return nil, fmt.Errorf("failed to load CA: %w", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
InsecureSkipVerify: gcfg.InsecureSkipVerify,
},
}
client.Transport = tr
}
resp, err := client.Get(urlPath)
if err != nil {
return nil, fmt.Errorf("http get failed: %w", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read metrics response: %w", err)
}
return strings.Split(string(data), "\n"), nil
}