-
Notifications
You must be signed in to change notification settings - Fork 684
/
client.go
515 lines (481 loc) · 15.9 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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package client
import (
"context"
"fmt"
"os"
"os/user"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"github.com/datawire/ambassador/internal/pkg/edgectl"
"github.com/datawire/ambassador/pkg/api/edgectl/rpc"
)
// IsServerRunning reports whether or not the daemon server is running.
func IsServerRunning() bool {
_, _, err := daemonVersion()
return err == nil
}
var daemonIsNotRunning = errors.New("Daemon is not running (see \"edgectl help daemon\")")
// Version requests version info from the daemon and prints both client and daemon version.
func Version(cmd *cobra.Command, _ []string) error {
av, dv, err := daemonVersion()
if err == nil {
fmt.Fprintf(cmd.OutOrStdout(), "Client %s\nDaemon v%s (api v%d)\n", edgectl.DisplayVersion(), dv, av)
return nil
}
fmt.Fprintf(cmd.OutOrStdout(), "Client %s\n", edgectl.DisplayVersion())
if err != daemonIsNotRunning {
// Socket exists but connection failed anyway.
err = fmt.Errorf("Unable to connect to daemon: %s", err)
}
return err
}
// A ConnectInfo contains all information needed to connect to a cluster.
type ConnectInfo struct {
rpc.ConnectRequest
LegacyMode bool
}
// Connect asks the daemon to connect to a cluster
func (x *ConnectInfo) Connect(cmd *cobra.Command, args []string) error {
if !x.LegacyMode {
return errors.New("This command is deprecated. Please go to https://a8r.io/go to use Telepresence 2! or use edgectl connect --legacy to continue using Service Preview.")
}
u, err := getRunAsInfo()
if err != nil {
return err
}
x.User = u
x.Args = args
x.InstallID = NewScout("unused").Reporter.InstallID()
// When set, wait that number of seconds for network before returning ConnectResponse_EstablishingOverrides
x.WaitForNetwork = 0
// TODO: Progress reporting during connect. Either divide into several calls and report completion
// of each, or use a stream. Can be made as part of ticket #1334.
var r *rpc.ConnectResponse
fmt.Fprintf(cmd.OutOrStdout(), "Connecting to traffic manager in namespace %s...\n", x.ManagerNS)
err = withDaemon(func(c rpc.DaemonClient) error {
r, err = c.Connect(context.Background(), &x.ConnectRequest)
return err
})
if err != nil {
return err
}
stderr := cmd.OutOrStderr()
switch r.Error {
case rpc.ConnectResponse_Ok:
fmt.Fprintf(cmd.OutOrStdout(), "Connected to context %s (%s)\n", r.ClusterContext, r.ClusterServer)
return nil
case rpc.ConnectResponse_AlreadyConnected:
fmt.Fprintln(cmd.OutOrStdout(), "Already connected")
return nil
case rpc.ConnectResponse_Disconnecting:
fmt.Fprintln(stderr, "Not ready: Trying to disconnect")
case rpc.ConnectResponse_Paused:
fmt.Fprintln(stderr, "Not ready: Network overrides are paused (use \"edgectl resume\")")
case rpc.ConnectResponse_EstablishingOverrides:
fmt.Fprintln(stderr, "Not ready: Establishing network overrides")
case rpc.ConnectResponse_ClusterFailed, rpc.ConnectResponse_BridgeFailed:
fmt.Fprintln(stderr, r.ErrorText)
case rpc.ConnectResponse_TrafficManagerFailed:
stdout := cmd.OutOrStdout()
fmt.Fprintf(stdout, "Connected to context %s (%s)\n", r.ClusterContext, r.ClusterServer)
fmt.Fprintln(stdout)
fmt.Fprintln(stdout, "Unable to connect to the traffic manager in your cluster.")
fmt.Fprintln(stdout, "The intercept feature will not be available.")
fmt.Fprintln(stdout, "Error was:", r.ErrorText)
return nil
}
os.Exit(1)
return nil
}
// Disconnect asks the daemon to disconnect from the connected cluster
func Disconnect(cmd *cobra.Command, _ []string) error {
return withDaemon(func(d rpc.DaemonClient) error {
r, err := d.Disconnect(context.Background(), &rpc.Empty{})
if err != nil {
return err
}
switch r.Error {
case rpc.DisconnectResponse_Ok:
return nil
case rpc.DisconnectResponse_NotConnected:
fmt.Fprintln(cmd.OutOrStderr(), "Not connected (use 'edgectl connect' to connect to your cluster)")
case rpc.DisconnectResponse_DisconnectFailed:
fmt.Fprintln(cmd.OutOrStderr(), r.ErrorText)
}
os.Exit(1)
return nil
})
}
// Status will retrieve connectivity status from the daemon and print it on stdout.
func Status(cmd *cobra.Command, _ []string) error {
return withDaemon(func(d rpc.DaemonClient) error {
out := cmd.OutOrStdout()
s, err := d.Status(context.Background(), &rpc.Empty{})
if err != nil {
return err
}
switch s.Error {
case rpc.StatusResponse_Ok:
cl := s.Cluster
if cl.Connected {
fmt.Fprintln(out, "Connected")
} else {
fmt.Fprintln(out, "Attempting to reconnect...")
}
fmt.Fprintf(out, " Context: %s (%s)\n", cl.Context, cl.Server)
if s.Bridge {
fmt.Fprintln(out, " Proxy: ON (networking to the cluster is enabled)")
} else {
fmt.Fprintln(out, " Proxy: OFF (attempting to connect...)")
}
ic := s.Intercepts
if ic == nil {
fmt.Fprintln(out, " Intercepts: Unavailable: no traffic manager")
break
}
if ic.Connected {
fmt.Fprintf(out, " Interceptable: %d deployments\n", ic.InterceptableCount)
fmt.Fprintf(out, " Intercepts: %d total, %d local\n", ic.ClusterIntercepts, ic.LocalIntercepts)
if ic.LicenseInfo != "" {
fmt.Fprintln(out, ic.LicenseInfo)
}
break
}
if s.ErrorText != "" {
fmt.Fprintf(out, " Intercepts: %s\n", s.ErrorText)
} else {
fmt.Fprintln(out, " Intercepts: (connecting to traffic manager...)")
}
case rpc.StatusResponse_Paused:
fmt.Fprintln(out, "Network overrides are paused")
case rpc.StatusResponse_NoNetwork:
fmt.Fprintln(out, "Network overrides NOT established")
case rpc.StatusResponse_Disconnected:
fmt.Fprintln(out, "Not connected (use 'edgectl connect' to connect to your cluster)")
}
return nil
})
}
// Pause requests that the network overrides are turned off
func Pause(cmd *cobra.Command, _ []string) error {
var r *rpc.PauseResponse
var err error
err = withDaemon(func(d rpc.DaemonClient) error {
r, err = d.Pause(context.Background(), &rpc.Empty{})
return err
})
if err != nil {
return err
}
stderr := cmd.OutOrStderr()
switch r.Error {
case rpc.PauseResponse_Ok:
stdout := cmd.OutOrStdout()
fmt.Fprintln(stdout, "Network overrides paused.")
fmt.Fprintln(stdout, `Use "edgectl resume" to reestablish network overrides.`)
return nil
case rpc.PauseResponse_AlreadyPaused:
fmt.Fprintln(stderr, "Network overrides are already paused")
case rpc.PauseResponse_ConnectedToCluster:
fmt.Fprintln(stderr, "Edge Control is connected to a cluster.")
fmt.Fprintln(stderr, "See \"edgectl status\" for details.")
fmt.Fprintln(stderr, "Please disconnect before pausing.")
default:
fmt.Fprintf(stderr, "Unexpected error while pausing: %v\n", r.ErrorText)
}
os.Exit(1)
return nil
}
// Resume requests that the network overrides are turned back on (after using Pause)
func Resume(cmd *cobra.Command, _ []string) error {
var r *rpc.ResumeResponse
var err error
err = withDaemon(func(d rpc.DaemonClient) error {
r, err = d.Resume(context.Background(), &rpc.Empty{})
return err
})
if err != nil {
return err
}
var msg string
switch r.Error {
case rpc.ResumeResponse_Ok:
fmt.Fprintln(cmd.OutOrStdout(), "Network overrides reestablished.")
return nil
case rpc.ResumeResponse_NotPaused:
msg = "Network overrides are established (not paused)"
case rpc.ResumeResponse_ReEstablishing:
msg = "Network overrides are being reestablished..."
default:
msg = fmt.Sprintf("Unexpected error establishing network overrides: %v", err)
}
fmt.Fprintln(cmd.OutOrStderr(), msg)
os.Exit(1)
return nil
}
// Quit sends the quit message to the daemon and waits for it to exit.
func Quit(cmd *cobra.Command, _ []string) error {
return withDaemon(func(d rpc.DaemonClient) error {
fmt.Fprintln(cmd.OutOrStdout(), "Edge Control Daemon quitting...")
_, _ = d.Quit(context.Background(), &rpc.Empty{})
return nil
})
}
// An InterceptInfo contains all information needed to add a deployment intercept.
type InterceptInfo struct {
rpc.InterceptRequest
}
// AddIntercept tells the daemon to add a deployment intercept.
func (x *InterceptInfo) AddIntercept(cmd *cobra.Command, args []string) error {
x.Deployment = args[0]
if x.Name == "" {
x.Name = fmt.Sprintf("cept-%d", time.Now().Unix())
}
// if intercept.Namespace == "" {
// intercept.Namespace = "default"
// }
if x.Prefix == "" {
x.Prefix = "/"
}
var host, portStr string
hp := strings.SplitN(x.TargetHost, ":", 2)
if len(hp) < 2 {
portStr = hp[0]
} else {
host = strings.TrimSpace(hp[0])
portStr = hp[1]
}
if len(host) == 0 {
host = "127.0.0.1"
}
port, err := strconv.Atoi(portStr)
stderr := cmd.OutOrStderr()
if err != nil {
fmt.Fprintf(stderr, "Failed to parse %q as HOST:PORT: %v\n", x.TargetHost, err)
os.Exit(1)
}
x.TargetHost = host
x.TargetPort = int32(port)
// If the user specifies --preview on the command line, then use its
// value (--preview is the same as --preview=true, or it could be
// --preview=false). But if the user does not specify --preview on
// the command line, compute its value from the presence or absence
// of --match, since they are mutually exclusive.
userSetPreviewFlag := cmd.Flags().Changed("preview")
userSetMatchFlag := len(x.Patterns) > 0
switch {
case userSetPreviewFlag && x.Preview:
// User specified --preview (or --preview=true) at the command line
if userSetMatchFlag {
fmt.Fprintln(stderr, "Error: Cannot use --match and --preview at the same time")
os.Exit(1)
}
// ok: --preview=true and no --match
case userSetPreviewFlag && !x.Preview:
// User specified --preview=false at the command line
if !userSetMatchFlag {
fmt.Fprintln(stderr, "Error: Must specify --match when using --preview=false")
os.Exit(1)
}
// ok: --preview=false and at least one --match
default:
// User did not specify --preview at the command line
if userSetMatchFlag {
// ok: at least one --match
x.Preview = false
} else {
// ok: neither --match nor --preview, fall back to preview
x.Preview = true
}
}
if x.Preview {
x.Patterns = make(map[string]string)
x.Patterns["x-service-preview"] = NewScout("unused").Reporter.InstallID()
}
var r *rpc.InterceptResponse
err = withDaemon(func(c rpc.DaemonClient) error {
var err error
r, err = c.AddIntercept(context.Background(), &x.InterceptRequest)
return err
})
if err != nil {
return err
}
if r.Error != rpc.InterceptError_InterceptOk {
fmt.Fprintln(cmd.OutOrStderr(), interceptMessage(r.Error, r.Text))
os.Exit(1)
}
fmt.Fprintf(cmd.OutOrStdout(), "Using deployment %s in namespace %s\n", x.Deployment, r.Text)
if r.PreviewURL != "" {
fmt.Fprintln(cmd.OutOrStdout(), "Share a preview of your changes with anyone by visiting\n ", r.PreviewURL)
}
return nil
}
// AvailableIntercepts requests a list of deployments available for intercept from the daemon
func AvailableIntercepts(cmd *cobra.Command, _ []string) error {
var r *rpc.AvailableInterceptsResponse
err := withDaemon(func(c rpc.DaemonClient) error {
var err error
r, err = c.AvailableIntercepts(context.Background(), &rpc.Empty{})
return err
})
if err != nil {
return err
}
if r.Error != rpc.InterceptError_InterceptOk {
fmt.Fprintln(cmd.OutOrStderr(), interceptMessage(r.Error, r.Text))
os.Exit(1)
}
stdout := cmd.OutOrStdout()
if len(r.Intercepts) == 0 {
fmt.Fprintln(stdout, "No interceptable deployments")
return nil
}
fmt.Fprintf(stdout, "Found %d interceptable deployment(s):\n", len(r.Intercepts))
for idx, cept := range r.Intercepts {
fmt.Fprintf(stdout, "%4d. %s in namespace %s\n", idx+1, cept.Deployment, cept.Namespace)
}
return nil
}
// ListIntercepts requests a list current intercepts from the daemon
func ListIntercepts(cmd *cobra.Command, _ []string) error {
var r *rpc.ListInterceptsResponse
err := withDaemon(func(c rpc.DaemonClient) error {
var err error
r, err = c.ListIntercepts(context.Background(), &rpc.Empty{})
return err
})
if err != nil {
return err
}
if r.Error != rpc.InterceptError_InterceptOk {
fmt.Fprintln(cmd.OutOrStderr(), interceptMessage(r.Error, r.Text))
os.Exit(1)
}
stdout := cmd.OutOrStdout()
if len(r.Intercepts) == 0 {
fmt.Fprintln(stdout, "No intercepts")
return nil
}
var previewURL string
for idx, cept := range r.Intercepts {
fmt.Fprintf(stdout, "%4d. %s\n", idx+1, cept.Name)
if cept.PreviewURL != "" {
previewURL = cept.PreviewURL
fmt.Fprintln(stdout, " (preview URL available)")
}
fmt.Fprintf(stdout, " Intercepting requests to %s when\n", cept.Deployment)
for k, v := range cept.Patterns {
fmt.Fprintf(stdout, " - %s: %s\n", k, v)
}
fmt.Fprintf(stdout, " and redirecting them to %s:%d\n", cept.TargetHost, cept.TargetPort)
}
if previewURL != "" {
fmt.Fprintln(stdout, "Share a preview of your changes with anyone by visiting\n ", previewURL)
}
return nil
}
// RemoveIntercept tells the daemon to deactivate and remove an existent intercept
func RemoveIntercept(cmd *cobra.Command, args []string) error {
name := strings.TrimSpace(args[0])
var r *rpc.InterceptResponse
err := withDaemon(func(c rpc.DaemonClient) error {
var err error
r, err = c.RemoveIntercept(context.Background(), &rpc.RemoveInterceptRequest{Name: name})
return err
})
if err != nil {
return err
}
if r.Error != rpc.InterceptError_InterceptOk {
fmt.Fprintln(cmd.OutOrStderr(), interceptMessage(r.Error, r.Text))
os.Exit(1)
}
return nil
}
func daemonVersion() (apiVersion int, version string, err error) {
err = withDaemon(func(d rpc.DaemonClient) error {
vi, err := d.Version(context.Background(), &rpc.Empty{})
if err == nil {
apiVersion = int(vi.APIVersion)
version = vi.Version
}
return err
})
return
}
// withDaemon establishes a connection, calls the function with the gRPC client, and ensures
// that the connection is closed.
func withDaemon(f func(rpc.DaemonClient) error) error {
_, err := os.Stat(edgectl.DaemonSocketName)
if err != nil {
if os.IsNotExist(err) {
err = daemonIsNotRunning
}
return err
}
conn, err := grpc.Dial("unix:"+edgectl.DaemonSocketName,
grpc.WithInsecure(), grpc.WithNoProxy())
if err == nil {
defer conn.Close()
return f(rpc.NewDaemonClient(conn))
}
return err
}
func getRunAsInfo() (*rpc.ConnectRequest_UserInfo, error) {
usr, err := user.Current()
if err != nil {
return nil, errors.Wrap(err, "user.Current()")
}
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "os.Getwd()")
}
rai := &rpc.ConnectRequest_UserInfo{
Name: usr.Username,
Cwd: cwd,
Env: os.Environ(),
}
return rai, nil
}
func interceptMessage(ie rpc.InterceptError, txt string) string {
msg := ""
switch ie {
case rpc.InterceptError_InterceptOk:
case rpc.InterceptError_NoPreviewHost:
msg = `Your cluster is not configured for Preview URLs.
(Could not find a Host resource that enables Path-type Preview URLs.)
Please specify one or more header matches using --match.`
case rpc.InterceptError_NoConnection:
msg = "Not connected (use 'edgectl connect' to connect to your cluster)"
case rpc.InterceptError_NoTrafficManager:
msg = "Intercept unavailable: no traffic manager"
case rpc.InterceptError_TrafficManagerConnecting:
msg = "Connecting to traffic manager..."
case rpc.InterceptError_AlreadyExists:
msg = fmt.Sprintf("Intercept with name %q already exists", txt)
case rpc.InterceptError_NoAcceptableDeployment:
msg = fmt.Sprintf("No interceptable deployment matching %s found", txt)
case rpc.InterceptError_TrafficManagerError:
msg = txt
case rpc.InterceptError_AmbiguousMatch:
st := &strings.Builder{}
fmt.Fprintf(st, "Found more than one possible match:")
for idx, match := range strings.Split(txt, "\n") {
dn := strings.Split(match, "\t")
fmt.Fprintf(st, "\n%4d: %s in namespace %s", idx+1, dn[1], dn[0])
}
msg = st.String()
case rpc.InterceptError_FailedToEstablish:
msg = fmt.Sprintf("Failed to establish intercept: %s", txt)
case rpc.InterceptError_FailedToRemove:
msg = fmt.Sprintf("Error while removing intercept: %v", txt)
case rpc.InterceptError_NotFound:
msg = fmt.Sprintf("Intercept named %q not found", txt)
}
return msg
}