-
Notifications
You must be signed in to change notification settings - Fork 682
/
cluster.go
416 lines (374 loc) · 11.2 KB
/
cluster.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package daemon
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/datawire/ambassador/internal/pkg/edgectl"
"github.com/datawire/ambassador/pkg/api/edgectl/rpc"
"github.com/datawire/ambassador/pkg/k8s"
"github.com/datawire/ambassador/pkg/metriton"
"github.com/datawire/ambassador/pkg/supervisor"
)
var simpleTransport = &http.Transport{
// #nosec G402
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: nil,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 1 * time.Second,
DualStack: true,
}).DialContext,
DisableKeepAlives: true,
}
var hClient = &http.Client{
Transport: simpleTransport,
Timeout: 15 * time.Second,
}
// connect the daemon to a cluster
func (d *daemon) connect(p *supervisor.Process, cr *rpc.ConnectRequest) *rpc.ConnectResponse {
reporter := &metriton.Reporter{
Application: "edgectl",
Version: edgectl.Version,
GetInstallID: func(_ *metriton.Reporter) (string, error) { return cr.InstallID, nil },
BaseMetadata: map[string]interface{}{"mode": "daemon"},
}
if _, err := reporter.Report(p.Context(), map[string]interface{}{"action": "connect"}); err != nil {
p.Logf("report failed: %+v", err)
}
// Sanity checks
r := &rpc.ConnectResponse{}
if d.cluster != nil {
r.Error = rpc.ConnectResponse_AlreadyConnected
return r
}
if d.bridge != nil {
r.Error = rpc.ConnectResponse_Disconnecting
return r
}
if d.network == nil {
r.Error = rpc.ConnectResponse_Paused
return r
}
nwOk := d.network.IsOkay()
if !nwOk {
if cr.WaitForNetwork > 0 {
// wait the given number of seconds, checking every fifth of a second.
for i := 5 * cr.WaitForNetwork; !nwOk && i > 0; i-- {
time.Sleep(200 * time.Millisecond)
nwOk = d.network.IsOkay()
}
}
if !nwOk {
r.Error = rpc.ConnectResponse_EstablishingOverrides
return r
}
}
p.Logf("Connecting to traffic manager in namespace %s...", cr.ManagerNS)
cluster, err := TrackKCluster(p, runAsUserFromRPC(cr.User), cr.Context, cr.Namespace, cr.Args)
if err != nil {
r.Error = rpc.ConnectResponse_ClusterFailed
r.ErrorText = err.Error()
return r
}
d.cluster = cluster
previewHost, err := getClusterPreviewHostname(p, cluster)
if err != nil {
p.Logf("get preview URL hostname: %+v", err)
previewHost = ""
}
rai := runAsUserFromRPC(cr.User)
bridge, err := CheckedRetryingCommand(
p,
"bridge",
[]string{edgectl.GetExe(), "teleproxy", "bridge", cluster.context, cluster.namespace},
rai,
checkBridge,
15*time.Second,
)
if err != nil {
d.cluster.Close()
d.cluster = nil
r.Error = rpc.ConnectResponse_BridgeFailed
r.ErrorText = err.Error()
return r
}
d.bridge = bridge
d.cluster.SetBridgeCheck(d.bridge.IsOkay)
p.Logf("Connected to context %s (%s)", d.cluster.Context(), d.cluster.Server())
r.ClusterContext = d.cluster.Context()
r.ClusterServer = d.cluster.Server()
tmgr, err := NewTrafficManager(p, d.cluster, cr.ManagerNS, cr.InstallID, cr.IsCI)
if err != nil {
p.Logf("Unable to connect to TrafficManager: %s", err)
r.Error = rpc.ConnectResponse_TrafficManagerFailed
r.ErrorText = err.Error()
return r
}
tmgr.previewHost = previewHost
d.trafficMgr = tmgr
return r
}
// disconnect from the connected cluster
func (d *daemon) disconnect(p *supervisor.Process) *rpc.DisconnectResponse {
// Sanity checks
r := &rpc.DisconnectResponse{}
if d.cluster == nil {
r.Error = rpc.DisconnectResponse_NotConnected
return r
}
_ = d.ClearIntercepts(p)
if d.bridge != nil {
d.cluster.SetBridgeCheck(nil) // Stop depending on this bridge
_ = d.bridge.Close()
d.bridge = nil
}
if d.trafficMgr != nil {
_ = d.trafficMgr.Close()
d.trafficMgr = nil
}
err := d.cluster.Close()
d.cluster = nil
if err != nil {
r.Error = rpc.DisconnectResponse_DisconnectFailed
r.ErrorText = err.Error()
}
return r
}
// getClusterPreviewHostname returns the hostname of the first Host resource it
// finds that has Preview URLs enabled with a supported URL type.
func getClusterPreviewHostname(p *supervisor.Process, cluster *KCluster) (hostname string, err error) {
p.Log("Looking for a Host with Preview URLs enabled")
// kubectl get hosts, in all namespaces or in this namespace
var outBytes []byte
outBytes, err = func() ([]byte, error) {
clusterCmd := cluster.GetKubectlCmdNoNamespace(p, "get", "host", "-o", "yaml", "--all-namespaces")
if outBytes, err := clusterCmd.CombinedOutput(); err == nil {
return outBytes, nil
}
nsCmd := cluster.GetKubectlCmd(p, "get", "host", "-o", "yaml")
if outBytes, err := nsCmd.CombinedOutput(); err == nil {
return outBytes, nil
} else {
return nil, err
}
}()
if err != nil {
return
}
// Parse the output
hostLists, kerr := k8s.ParseResources("get hosts", string(outBytes))
if kerr != nil {
err = kerr
return
}
if len(hostLists) != 1 {
err = errors.Errorf("weird result with length %d", len(hostLists))
return
}
// Grab the "items" slice, as the result should be a list of Host resources
hostItems := k8s.Map(hostLists[0]).GetMaps("items")
p.Logf("Found %d Host resources", len(hostItems))
// Loop over Hosts looking for a Preview URL hostname
for _, hostItem := range hostItems {
host := k8s.Resource(hostItem)
logEntry := fmt.Sprintf("- Host %s / %s: %%s", host.Namespace(), host.Name())
previewUrlSpec := host.Spec().GetMap("previewUrl")
if len(previewUrlSpec) == 0 {
p.Logf(logEntry, "no preview URL config")
continue
}
if enabled, ok := previewUrlSpec["enabled"].(bool); !ok || !enabled {
p.Logf(logEntry, "preview URL not enabled")
continue
}
// missing type, default is "Path" --> success
// type is present, set to "Path" --> success
// otherwise --> failure
if pType, ok := previewUrlSpec["type"].(string); ok && pType != "Path" {
p.Logf(logEntry+": %#v", "unsupported preview URL type", previewUrlSpec["type"])
continue
}
if hostname = host.Spec().GetString("hostname"); hostname == "" {
p.Logf(logEntry, "empty hostname???")
continue
}
p.Logf(logEntry+": %q", "SUCCESS! Hostname is", hostname)
return
}
p.Logf("No appropriate Host resource found.")
return
}
// checkBridge checks the status of teleproxy bridge by doing the equivalent of
// curl http://traffic-proxy.svc:8022.
// Note there is no namespace specified, as we are checking for bridge status in the
// current namespace.
func checkBridge(_ *supervisor.Process) error {
address := "traffic-proxy.svc:8022"
conn, err := net.DialTimeout("tcp", address, 15*time.Second)
if err != nil {
return errors.Wrap(err, "tcp connect")
}
if conn != nil {
defer conn.Close()
msg, _, err := bufio.NewReader(conn).ReadLine()
if err != nil {
return errors.Wrap(err, "tcp read")
}
if !strings.Contains(string(msg), "SSH") {
return fmt.Errorf("expected SSH prompt, got: %v", string(msg))
}
} else {
return fmt.Errorf("fail to establish tcp connection to %v", address)
}
return nil
}
// TrafficManager is a handle to access the Traffic Manager in a
// cluster.
type TrafficManager struct {
crc Resource
apiPort int
sshPort int
namespace string
interceptables []string
totalClusCepts int
snapshotSent bool
installID string // edgectl's install ID
connectCI bool // whether --ci was passed to connect
apiErr error // holds the latest traffic-manager API error
licenseInfo string // license information from traffic-manager
previewHost string // hostname to use for preview URLs, if enabled
}
// NewTrafficManager returns a TrafficManager resource for the given
// cluster if it has a Traffic Manager service.
func NewTrafficManager(p *supervisor.Process, cluster *KCluster, managerNs string, installID string, isCI bool) (*TrafficManager, error) {
cmd := cluster.GetKubectlCmd(p, "get", "-n", managerNs, "svc/telepresence-proxy", "deploy/telepresence-proxy")
err := cmd.Run()
if err != nil {
return nil, errors.Wrap(err, "kubectl get svc/deploy telepresency-proxy")
}
apiPort, err := GetFreePort()
if err != nil {
return nil, errors.Wrap(err, "get free port for API")
}
sshPort, err := GetFreePort()
if err != nil {
return nil, errors.Wrap(err, "get free port for ssh")
}
kpfArgStr := fmt.Sprintf("port-forward -n %s svc/telepresence-proxy %d:8022 %d:8081", managerNs, sshPort, apiPort)
kpfArgs := cluster.GetKubectlArgs(strings.Fields(kpfArgStr)...)
tm := &TrafficManager{
apiPort: apiPort,
sshPort: sshPort,
namespace: managerNs,
installID: installID,
connectCI: isCI,
}
pf, err := CheckedRetryingCommand(p, "traffic-kpf", kpfArgs, cluster.RAI(), tm.check, 15*time.Second)
if err != nil {
return nil, err
}
tm.crc = pf
return tm, nil
}
func (tm *TrafficManager) check(p *supervisor.Process) error {
body, code, err := tm.request("GET", "state", []byte{})
if err != nil {
return err
}
if code != http.StatusOK {
tm.apiErr = fmt.Errorf("%v: %v", code, body)
return tm.apiErr
}
tm.apiErr = nil
var state map[string]interface{}
if err := json.Unmarshal([]byte(body), &state); err != nil {
p.Logf("check: bad JSON from tm: %v", err)
p.Logf("check: JSON data is: %q", body)
return err
}
if licenseInfo, ok := state["LicenseInfo"]; ok {
tm.licenseInfo = licenseInfo.(string)
}
deployments, ok := state["Deployments"].(map[string]interface{})
if !ok {
p.Log("check: failed to get deployment info")
p.Logf("check: JSON data is: %q", body)
}
tm.interceptables = make([]string, len(deployments))
tm.totalClusCepts = 0
idx := 0
for deployment := range deployments {
tm.interceptables[idx] = deployment
idx++
info, ok := deployments[deployment].(map[string]interface{})
if !ok {
continue
}
cepts, ok := info["Intercepts"].([]interface{})
if ok {
tm.totalClusCepts += len(cepts)
}
}
if !tm.snapshotSent {
p.Log("trying to send snapshot")
tm.snapshotSent = true // don't try again, even if this fails
body, code, err := tm.request("GET", "snapshot", []byte{})
if err != nil || code != 200 {
p.Logf("snapshot request failed: %v", err)
return nil
}
resp, err := hClient.Post("http://teleproxy/api/tables/", "application/json", strings.NewReader(body))
if err != nil {
p.Logf("snapshot post failed: %v", err)
return nil
}
_, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
p.Log("snapshot sent!")
}
return nil
}
func (tm *TrafficManager) request(method, path string, data []byte) (result string, code int, err error) {
url := fmt.Sprintf("http://127.0.0.1:%d/%s", tm.apiPort, path)
req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
if err != nil {
return
}
req.Header.Set("edgectl-install-id", tm.installID)
req.Header.Set("edgectl-connect-ci", strconv.FormatBool(tm.connectCI))
resp, err := hClient.Do(req)
if err != nil {
err = errors.Wrap(err, "get")
return
}
defer resp.Body.Close()
code = resp.StatusCode
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
err = errors.Wrap(err, "read body")
return
}
result = string(body)
return
}
// Name implements Resource
func (tm *TrafficManager) Name() string {
return "trafficMgr"
}
// IsOkay implements Resource
func (tm *TrafficManager) IsOkay() bool {
return tm.crc.IsOkay()
}
// Close implements Resource
func (tm *TrafficManager) Close() error {
return tm.crc.Close()
}