-
Notifications
You must be signed in to change notification settings - Fork 688
/
main.go
339 lines (315 loc) · 10.5 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/datawire/ambassador/internal/pkg/edgectl"
"github.com/datawire/ambassador/internal/pkg/edgectl/client"
"github.com/datawire/ambassador/internal/pkg/edgectl/daemon"
install "github.com/datawire/ambassador/internal/pkg/edgectl/install"
)
// Version is inserted at build using --ldflags -X
var Version = "(unknown version)"
func main() {
edgectl.SetVersion(Version)
rootCmd := getRootCommand()
var cg []client.CmdGroup
if client.DaemonWorks() {
cg = []client.CmdGroup{
{
GroupName: "Management Commands",
CmdNames: []string{"install", "upgrade", "login", "license"},
},
{
GroupName: "Development Commands",
CmdNames: []string{"status", "connect", "disconnect", "intercept"},
},
{
GroupName: "Advanced Commands",
CmdNames: []string{"daemon", "pause", "resume", "quit"},
},
{
GroupName: "Other Commands",
CmdNames: []string{"version", "help"},
},
}
} else {
cg = []client.CmdGroup{
{
GroupName: "Management Commands",
CmdNames: []string{"install", "upgrade", "login", "license"},
},
{
GroupName: "Other Commands",
CmdNames: []string{"version", "help"},
},
}
}
usageFunc := client.NewCmdUsage(rootCmd, cg)
rootCmd.SetUsageFunc(usageFunc)
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func getRootCommand() *cobra.Command {
myName := "Edge Control"
if !client.IsServerRunning() {
myName = "Edge Control (daemon unavailable)"
}
myHelp := myName + `
https://www.getambassador.io/docs/edge-stack/latest/topics/install/
`
rootCmd := &cobra.Command{
Use: "edgectl",
Short: myName,
Long: myHelp,
SilenceUsage: true, // https://github.com/spf13/cobra/issues/340
}
_ = rootCmd.PersistentFlags().Bool(
"no-report", false, "turn off anonymous crash reports and log submission on failure",
)
// Hidden/internal commands. These are called by Edge Control itself from
// the correct context and execute in-place immediately.
rootCmd.AddCommand(&cobra.Command{
Use: "daemon-foreground",
Short: "Launch Edge Control Daemon in the foreground (debug)",
Args: cobra.ExactArgs(2),
Hidden: true,
RunE: func(_ *cobra.Command, args []string) error {
return daemon.RunAsDaemon(args[0], args[1])
},
})
teleproxyCmd := &cobra.Command{
Use: "teleproxy",
Short: "Impersonate Teleproxy (for internal use)",
Hidden: true,
}
teleproxyCmd.AddCommand(&cobra.Command{
Use: "intercept",
Short: "Impersonate Teleproxy Intercept (for internal use)",
Args: cobra.ExactArgs(2),
Hidden: true,
RunE: func(_ *cobra.Command, args []string) error {
return daemon.RunAsTeleproxyIntercept(args[0], args[1])
},
})
teleproxyCmd.AddCommand(&cobra.Command{
Use: "bridge",
Short: "Impersonate Teleproxy Bridge (for internal use)",
Args: cobra.ExactArgs(2),
Hidden: true,
RunE: func(_ *cobra.Command, args []string) error {
return daemon.RunAsTeleproxyBridge(args[0], args[1])
},
})
rootCmd.AddCommand(teleproxyCmd)
// Client commands. These are never sent to the daemon.
if client.DaemonWorks() {
daemonCmd := &cobra.Command{
Use: "daemon",
Short: "Launch Edge Control Daemon in the background (sudo)",
Long: daemon.Help,
Args: cobra.ExactArgs(0),
RunE: client.LaunchDaemon,
}
_ = daemonCmd.Flags().String(
"dns", "",
"DNS IP address to intercept. Defaults to the first nameserver listed in /etc/resolv.conf.",
)
_ = daemonCmd.Flags().String(
"fallback", "",
"DNS fallback, how non-cluster DNS queries are resolved. Defaults to Google DNS (8.8.8.8).",
)
rootCmd.AddCommand(daemonCmd)
rootCmd.AddCommand(&cobra.Command{
Use: "status",
Short: "Show connectivity status",
Args: cobra.ExactArgs(0),
RunE: client.Status,
})
cr := &client.ConnectInfo{}
connectCmd := &cobra.Command{
Use: "connect [flags] [-- additional kubectl arguments...]",
Short: "Connect to a cluster",
RunE: cr.Connect,
}
connectFlags := connectCmd.Flags()
connectFlags.StringVarP(&cr.Context,
"context", "c", "",
"The Kubernetes context to use. Defaults to the current kubectl context.",
)
connectFlags.StringVarP(&cr.Namespace,
"namespace", "n", "",
"The Kubernetes namespace to use. Defaults to kubectl's default for the context.",
)
connectFlags.StringVarP(&cr.ManagerNS,
"manager-namespace", "m", "ambassador",
"The Kubernetes namespace in which the Traffic Manager is running.",
)
connectFlags.BoolVar(&cr.IsCI, "ci", false, "This session is a CI run.")
connectFlags.BoolVar(&cr.LegacyMode, "legacy", false, "Run in legacy mode.")
connectFlags.MarkHidden("legacy")
rootCmd.AddCommand(connectCmd)
rootCmd.AddCommand(&cobra.Command{
Use: "disconnect",
Short: "Disconnect from the connected cluster",
Args: cobra.ExactArgs(0),
RunE: client.Disconnect,
})
rootCmd.AddCommand(&cobra.Command{
Use: "pause",
Short: "Turn off network overrides (to use a VPN)",
Args: cobra.ExactArgs(0),
RunE: client.Pause,
})
rootCmd.AddCommand(&cobra.Command{
Use: "resume",
Short: "Turn network overrides on (after using edgectl pause)",
Aliases: []string{"unpause"},
Args: cobra.ExactArgs(0),
RunE: client.Resume,
})
rootCmd.AddCommand(&cobra.Command{
Use: "quit",
Short: "Tell Edge Control Daemon to quit (for upgrades)",
Args: cobra.ExactArgs(0),
RunE: client.Quit,
})
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Show program's version number and exit",
Args: cobra.ExactArgs(0),
RunE: client.Version,
})
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: client.AvailableIntercepts,
})
interceptCmd.AddCommand(&cobra.Command{
Use: "list",
Short: "List current intercepts",
Args: cobra.ExactArgs(0),
RunE: client.ListIntercepts,
})
interceptCmd.AddCommand(&cobra.Command{
Use: "remove [flags] DEPLOYMENT",
Aliases: []string{"delete"},
Short: "Deactivate and remove an existent intercept",
Args: cobra.MinimumNArgs(1),
RunE: client.RemoveIntercept,
})
intercept := client.InterceptInfo{}
interceptAddCmd := &cobra.Command{
Use: "add [flags] DEPLOYMENT -t [HOST:]PORT ([-p] | -m HEADER=REGEX ...)",
Short: "Add a deployment intercept",
Args: cobra.ExactArgs(1),
RunE: intercept.AddIntercept,
}
interceptAddCmd.Flags().StringVarP(&intercept.Name, "name", "n", "", "a name for this intercept")
interceptAddCmd.Flags().StringVar(&intercept.Prefix, "prefix", "/", "prefix to intercept")
interceptAddCmd.Flags().BoolVarP(&intercept.Preview, "preview", "p", true, "use a preview URL") // this default is unused
interceptAddCmd.Flags().BoolVarP(&intercept.GRPC, "grpc", "", false, "intercept GRPC traffic")
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")
interceptCmd.AddCommand(interceptAddCmd)
interceptCG := []client.CmdGroup{
{
GroupName: "Available Commands",
CmdNames: []string{"available", "list", "add", "remove"},
},
}
interceptCmd.SetUsageFunc(client.NewCmdUsage(interceptCmd, interceptCG))
rootCmd.AddCommand(interceptCmd)
} else {
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Show program's version number and exit",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
fmt.Println("Client", edgectl.DisplayVersion())
fmt.Println("Daemon unavailable on this platform")
return nil
},
})
}
loginCmd := &cobra.Command{
Use: "login [flags] HOSTNAME",
Short: "Log in to Ambassador Cloud\nIf a HOSTNAME is specified, log in to the Ambassador Edge Policy Console",
Args: cobra.RangeArgs(0, 1),
RunE: client.AESLogin,
}
_ = loginCmd.Flags().StringP(
"context", "c", "",
"The Kubernetes context to use. Defaults to the current kubectl context.",
)
_ = loginCmd.Flags().StringP(
"namespace", "n", "ambassador",
"The Kubernetes namespace to use. Defaults to ambassador.",
)
_ = loginCmd.Flags().Bool("url", false, "Just show the URL (don't launch a browser)")
_ = loginCmd.Flags().Bool("token", false, "Also display the login token")
rootCmd.AddCommand(loginCmd)
licenseCmd := &cobra.Command{
Use: "license [flags] LICENSE_KEY",
Short: "Set or update the Ambassador Edge Stack license key",
Args: cobra.ExactArgs(1),
RunE: client.AESLicense,
}
_ = licenseCmd.Flags().StringP(
"context", "c", "",
"The Kubernetes context to use. Defaults to the current kubectl context.",
)
_ = licenseCmd.Flags().StringP(
"namespace", "n", "ambassador",
"The Kubernetes namespace to use. Defaults to ambassador.",
)
rootCmd.AddCommand(licenseCmd)
installCmd := &cobra.Command{
Use: "install",
Short: "Install the Ambassador Edge Stack in your cluster",
Args: cobra.ExactArgs(0),
RunE: install.AESInstall,
}
_ = installCmd.Flags().StringP(
"cloud-connect-token", "", "",
"You can get a token to connect your cluster to the service catalog in Ambassador Cloud by logging in via 'edgectl login'",
)
_ = installCmd.Flags().StringP(
"context", "c", "",
"The Kubernetes context to use. Defaults to the current kubectl context.",
)
_ = installCmd.Flags().BoolP(
"verbose", "v", false,
"Show all output. Defaults to sending most output to the logfile.",
)
rootCmd.AddCommand(installCmd)
upgradeCmd := &cobra.Command{
Use: "upgrade",
Short: "Upgrade an Ambassador API Gateway installation managed by the Operator to Ambassador Edge Stack",
Args: cobra.ExactArgs(0),
RunE: install.AOSSUpgrade,
}
_ = upgradeCmd.Flags().StringP(
"context", "c", "",
"The Kubernetes context to use. Defaults to the current kubectl context.",
)
_ = upgradeCmd.Flags().BoolP(
"verbose", "v", false,
"Show all output. Defaults to sending most output to the logfile.",
)
rootCmd.AddCommand(upgradeCmd)
rootCmd.InitDefaultHelpCmd()
return rootCmd
}