-
Notifications
You must be signed in to change notification settings - Fork 688
/
cli.go
381 lines (352 loc) · 11.8 KB
/
cli.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
package edgectl
import (
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/datawire/ambassador/pkg/metriton"
"github.com/datawire/ambassador/pkg/supervisor"
)
func (d *Daemon) handleCommand(p *supervisor.Process, conn net.Conn, data *ClientMessage) error {
out := NewEmitter(conn)
rootCmd := d.GetRootCommand(p, out, data)
rootCmd.SetOutput(conn) // FIXME replace with SetOut and SetErr
rootCmd.PersistentPreRun = func(cmd *cobra.Command, _ []string) {
if batch, _ := cmd.Flags().GetBool("batch"); batch {
out.SetKV()
}
}
rootCmd.SetArgs(data.Args[1:])
err := rootCmd.Execute()
if err != nil {
out.SendExit(1)
}
return out.Err()
}
func (d *Daemon) GetRootCommand(p *supervisor.Process, out *Emitter, data *ClientMessage) *cobra.Command {
reporter := &metriton.Reporter{
Application: "edgectl",
Version: Version,
GetInstallID: func(_ *metriton.Reporter) (string, error) { return data.InstallID, nil },
BaseMetadata: map[string]interface{}{"mode": "daemon"},
}
rootCmd := &cobra.Command{
Use: "edgectl",
Short: "Edge Control",
SilenceUsage: true, // https://github.com/spf13/cobra/issues/340
}
_ = rootCmd.PersistentFlags().Bool("batch", false, "Emit machine-readable output")
_ = rootCmd.PersistentFlags().MarkHidden("batch")
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Show program's version number and exit",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
out.Println("Client", data.ClientVersion)
out.Println("Daemon", DisplayVersion())
out.Send("daemon.version", Version)
out.Send("daemon.apiVersion", apiVersion)
return out.Err()
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "status",
Short: "Show connectivity status",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
if err := d.Status(p, out); err != nil {
return err
}
return out.Err()
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "pause",
Short: "Turn off network overrides (to use a VPN)",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
if d.network == nil {
out.Println("Network overrides are already paused")
out.Send("paused", true)
return out.Err()
}
if d.cluster != nil {
out.Println("Edge Control is connected to a cluster.")
out.Println("See \"edgectl status\" for details.")
out.Println("Please disconnect before pausing.")
out.Send("paused", false)
out.SendExit(1)
return out.Err()
}
if err := d.network.Close(); err != nil {
p.Logf("pause: %v", err)
out.Printf("Unexpected error while pausing: %v\n", err)
}
d.network = nil
out.Println("Network overrides paused.")
out.Println("Use \"edgectl resume\" to reestablish network overrides.")
out.Send("paused", true)
return out.Err()
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "resume",
Short: "Turn network overrides on (after using edgectl pause)",
Aliases: []string{"unpause"},
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
if d.network != nil {
if d.network.IsOkay() {
out.Println("Network overrides are established (not paused)")
} else {
out.Println("Network overrides are being reestablished...")
}
out.Send("paused", false)
return out.Err()
}
if err := d.MakeNetOverride(p); err != nil {
p.Logf("resume: %v", err)
out.Printf("Unexpected error establishing network overrides: %v", err)
}
out.Send("paused", d.network == nil)
return out.Err()
},
})
connectCmd := &cobra.Command{
Use: "connect [flags] [-- additional kubectl arguments...]",
Short: "Connect to a cluster",
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := reporter.Report(p.Context(), map[string]interface{}{"action": "connect"}); err != nil {
p.Logf("report failed: %+v", err)
}
context, _ := cmd.Flags().GetString("context")
namespace, _ := cmd.Flags().GetString("namespace")
managerNs, _ := cmd.Flags().GetString("manager-namespace")
isCI, _ := cmd.Flags().GetBool("ci")
if err := d.Connect(
p, out, data.RAI,
context, namespace, managerNs, args,
data.InstallID, isCI,
); err != nil {
return err
}
return out.Err()
},
}
_ = connectCmd.Flags().StringP(
"context", "c", "",
"The Kubernetes context to use. Defaults to the current kubectl context.",
)
_ = connectCmd.Flags().StringP(
"namespace", "n", "",
"The Kubernetes namespace to use. Defaults to kubectl's default for the context.",
)
_ = connectCmd.Flags().StringP(
"manager-namespace", "m", "ambassador",
"The Kubernetes namespace in which the Traffic Manager is running.",
)
_ = connectCmd.Flags().Bool("ci", false, "This session is a CI run.")
rootCmd.AddCommand(connectCmd)
rootCmd.AddCommand(&cobra.Command{
Use: "disconnect",
Short: "Disconnect from the connected cluster",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
if err := d.Disconnect(p, out); err != nil {
return err
}
return out.Err()
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "quit",
Short: "Tell Edge Control Daemon to quit (for upgrades)",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
out.Println("Edge Control Daemon quitting...")
out.Send("quit", true)
p.Supervisor().Shutdown()
return out.Err()
},
})
interceptCmd := &cobra.Command{
Use: "intercept",
Long: "Manage deployment intercepts. An intercept arranges for a subset of requests to be " +
"diverted to the local machine.",
Short: "Manage deployment intercepts",
}
interceptCmd.AddCommand(&cobra.Command{
Use: "available",
Aliases: []string{"avail"},
Short: "List deployments available for intercept",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
msg := d.interceptMessage()
if msg != "" {
out.Println(msg)
out.Send("intercept", msg)
return out.Err()
}
out.Send("interceptable", len(d.trafficMgr.interceptables))
switch {
case len(d.trafficMgr.interceptables) == 0:
out.Println("No interceptable deployments")
default:
out.Printf("Found %d interceptable deployment(s):\n", len(d.trafficMgr.interceptables))
for idx, deployment := range d.trafficMgr.interceptables {
fields := strings.SplitN(deployment, "/", 2)
appName := fields[0]
appNamespace := d.cluster.namespace
if len(fields) > 1 {
appNamespace = fields[0]
appName = fields[1]
}
out.Printf("%4d. %s in namespace %s\n", idx+1, appName, appNamespace)
out.Send(fmt.Sprintf("interceptable.deployment.%d", idx+1), deployment)
}
}
return out.Err()
},
})
interceptCmd.AddCommand(&cobra.Command{
Use: "list",
Short: "List current intercepts",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
if err := d.ListIntercepts(p, out); err != nil {
return err
}
return out.Err()
},
})
interceptCmd.AddCommand(&cobra.Command{
Use: "remove [flags] DEPLOYMENT",
Aliases: []string{"delete"},
Short: "Deactivate and remove an existent intercept",
Args: cobra.MinimumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
name := strings.TrimSpace(args[0])
if err := d.RemoveIntercept(p, out, name); err != nil {
return err
}
return out.Err()
},
})
intercept := InterceptInfo{}
interceptPreview := true
var interceptAddCmdFlags *pflag.FlagSet
interceptAddCmd := &cobra.Command{
Use: "add [flags] DEPLOYMENT -t [HOST:]PORT ([-p] | -m HEADER=REGEX ...)",
Short: "Add a deployment intercept",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
intercept.Deployment = args[0]
if intercept.Name == "" {
intercept.Name = fmt.Sprintf("cept-%d", time.Now().Unix())
}
// if intercept.Namespace == "" {
// intercept.Namespace = "default"
// }
if intercept.Prefix == "" {
intercept.Prefix = "/"
}
var host, portStr string
hp := strings.SplitN(intercept.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)
if err != nil {
out.Printf("Failed to parse %q as HOST:PORT: %v\n", intercept.TargetHost, err)
out.Send("failed", "parse target")
out.SendExit(1)
return nil
}
intercept.TargetHost = host
intercept.TargetPort = 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 := interceptAddCmdFlags.Changed("preview")
userSetMatchFlag := len(intercept.Patterns) > 0
if userSetPreviewFlag && interceptPreview {
// User specified --preview (or --preview=true) at the command line
if userSetMatchFlag {
out.Println("Error: Cannot use --match and --preview at the same time")
out.Send("failed", "both match and preview")
out.SendExit(1)
return nil
} else {
// ok: --preview=true and no --match
}
} else if userSetPreviewFlag && !interceptPreview {
// User specified --preview=false at the command line
if userSetMatchFlag {
// ok: --preview=false and at least one --match
} else {
out.Println("Error: Must specify --match when using --preview=false")
out.Send("failed", "neither match nor preview")
out.SendExit(1)
return nil
}
} else {
// User did not specify --preview at the command line
if userSetMatchFlag {
// ok: at least one --match
interceptPreview = false
} else {
// ok: neither --match nor --preview, fall back to preview
interceptPreview = true
}
}
if interceptPreview {
if d.trafficMgr.previewHost == "" {
out.Println("Your cluster is not configured for Preview URLs.")
out.Println("(Could not find a Host resource that enables Path-type Preview URLs.)")
out.Println("Please specify one or more header matches using --match.")
out.Send("failed", "preview requested but unavailable")
out.SendExit(1)
return nil
}
intercept.Patterns = make(map[string]string)
intercept.Patterns["x-service-preview"] = data.InstallID
}
if err := d.AddIntercept(p, out, &intercept); err != nil {
return err
}
if url := intercept.PreviewURL(d.trafficMgr.previewHost); url != "" {
out.Println("Share a preview of your changes with anyone by visiting\n ", url)
}
return out.Err()
},
}
interceptAddCmd.Flags().StringVarP(&intercept.Name, "name", "n", "", "a name for this intercept")
interceptAddCmd.Flags().StringVar(&intercept.Prefix, "prefix", "/", "prefix to intercept")
interceptAddCmd.Flags().BoolVarP(&interceptPreview, "preview", "p", true, "use a preview URL") // this default is unused
interceptAddCmd.Flags().StringVarP(&intercept.TargetHost, "target", "t", "", "the [HOST:]PORT to forward to")
_ = interceptAddCmd.MarkFlagRequired("target")
interceptAddCmd.Flags().StringToStringVarP(&intercept.Patterns, "match", "m", nil, "match expression (HEADER=REGEX)")
interceptAddCmd.Flags().StringVarP(&intercept.Namespace, "namespace", "", "", "Kubernetes namespace in which to create mapping for intercept")
interceptAddCmdFlags = interceptAddCmd.Flags()
interceptCmd.AddCommand(interceptAddCmd)
interceptCG := []CmdGroup{
{
GroupName: "Available Commands",
CmdNames: []string{"available", "list", "add", "remove"},
},
}
interceptCmd.SetUsageFunc(NewCmdUsage(interceptCmd, interceptCG))
rootCmd.AddCommand(interceptCmd)
return rootCmd
}