-
Notifications
You must be signed in to change notification settings - Fork 117
/
envoyadmin.go
182 lines (151 loc) · 4.3 KB
/
envoyadmin.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
package envoyadmin
// <!-- START clutchdoc -->
// description: Executes remote queries against the Envoy Proxy admin interface.
// <!-- END clutchdoc -->
import (
"context"
"fmt"
"io"
"net/http"
"github.com/golang/protobuf/ptypes/any"
"github.com/uber-go/tally/v4"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
envoyadminv1 "github.com/lyft/clutch/backend/api/config/service/envoyadmin/v1"
envoytriagev1 "github.com/lyft/clutch/backend/api/envoytriage/v1"
"github.com/lyft/clutch/backend/service"
)
const Name = "clutch.service.envoyadmin"
func New(cfg *any.Any, logger *zap.Logger, scope tally.Scope) (service.Service, error) {
return NewWithHTTPClient(cfg, logger, scope, &http.Client{})
}
func NewWithHTTPClient(cfg *any.Any, logger *zap.Logger, scope tally.Scope, httpClient *http.Client) (service.Service, error) {
config := &envoyadminv1.Config{}
if err := cfg.UnmarshalTo(config); err != nil {
return nil, err
}
return &client{
defaultPort: config.DefaultRemotePort,
httpClient: httpClient,
}, nil
}
type Client interface {
// Get performs read-only operations concurrently and returns the results. If any of the operations fail,
// an error is returned.
Get(ctx context.Context, operation *envoytriagev1.ReadOperation) (*envoytriagev1.Result, error)
}
type client struct {
defaultPort uint32
httpClient *http.Client
}
func makeRequest(ctx context.Context, cl *http.Client, baseURL, path string) ([]byte, error) {
url := fmt.Sprintf("%s%s", baseURL, path)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
resp, err := cl.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("received non-200 status '%d %s'", resp.StatusCode, resp.Status)
}
return io.ReadAll(resp.Body)
}
func (c *client) Get(ctx context.Context, operation *envoytriagev1.ReadOperation) (*envoytriagev1.Result, error) {
defer c.httpClient.CloseIdleConnections()
// Use default port if one was not provided and construct address.
port := operation.Address.Port
if port == 0 {
port = c.defaultPort
}
addr := fmt.Sprintf("%s:%d", operation.Address.Host, port)
baseURL := fmt.Sprintf("http://%s", addr)
// Make an empty result.
result := &envoytriagev1.Result{
Address: &envoytriagev1.Address{Host: operation.Address.Host, Port: port},
Output: &envoytriagev1.Result_Output{},
}
// Make concurrent requests using an error group.
g, ctx := errgroup.WithContext(ctx)
if operation.Include.Clusters {
g.Go(func() error {
resp, err := makeRequest(ctx, c.httpClient, baseURL, "/clusters?format=json")
if err != nil {
return err
}
v, err := clustersFromResponse(resp)
result.Output.Clusters = v
return err
})
}
if operation.Include.ConfigDump {
g.Go(func() error {
resp, err := makeRequest(ctx, c.httpClient, baseURL, "/config_dump")
if err != nil {
return err
}
v, err := configDumpFromResponse(resp)
result.Output.ConfigDump = v
return err
})
}
if operation.Include.Listeners {
g.Go(func() error {
resp, err := makeRequest(ctx, c.httpClient, baseURL, "/listeners?format=json")
if err != nil {
return err
}
v, err := listenersFromResponse(resp)
result.Output.Listeners = v
return err
})
}
if operation.Include.Runtime {
g.Go(func() error {
resp, err := makeRequest(ctx, c.httpClient, baseURL, "/runtime")
if err != nil {
return err
}
v, err := runtimeFromResponse(resp)
result.Output.Runtime = v
return err
})
}
if operation.Include.Stats {
g.Go(func() error {
resp, err := makeRequest(ctx, c.httpClient, baseURL, "/stats")
if err != nil {
return err
}
v, err := statsFromResponse(resp)
result.Output.Stats = v
return err
})
}
// Always fetch server info so we can populate node metadata.
g.Go(func() error {
resp, err := makeRequest(ctx, c.httpClient, baseURL, "/server_info")
if err != nil {
return err
}
// Include server info.
if operation.Include.ServerInfo {
v, err := serverInfoFromResponse(resp)
if err != nil {
return err
}
result.Output.ServerInfo = v
}
// Include node metadata.
v, err := nodeMetadataFromResponse(resp)
result.NodeMetadata = v
return err
})
if err := g.Wait(); err != nil {
return nil, err
}
return result, nil
}