-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
legacy_command.go
282 lines (253 loc) · 8.62 KB
/
legacy_command.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
package cli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/telepresenceio/telepresence/v2/pkg/client/errcat"
"github.com/telepresenceio/telepresence/v2/pkg/client/scout"
)
// Here we handle parsing legacy commands, as well as generating Telepresence
// commands from them. This will make it easier for users to migrate from
// legacy Telepresence. Note: This isn't exhaustive, but should capture the major
// flags that were used and have a correlated command in Telepresence.
type legacyCommand struct {
swapDeployment string
newDeployment bool
method bool
expose string
run bool
dockerRun bool
runShell bool
processCmd string
mount string
dockerMount string
envFile string
envJSON string
// kubectl-related flags
context string
namespace string
globalFlags []string
unsupportedFlags []string
}
// Unfortunately we have to do our own flag parsing if we see legacy telepresence
// flags because the run command might include something that cobra might detect
// as a flag e.g. --run python3 -m http.server. In python this was handled by
// using argparse.REMAINDER and there is no similar functionality within cobra.
// There is an open ticket to pass unknown flags to the command:
// https://github.com/spf13/cobra/issues/739
// but until that is addressed, we'll do the flag parsing ourselves (which isn't
// the worst because it's a legacy command so the flags won't be growing).
func parseLegacyCommand(args []string) *legacyCommand {
lc := &legacyCommand{}
// We don't want to over-index in case somebody has a command that has a
// flag but doesn't put the value after it. So we have this helper function
// to ensure we don't do that. It may mean the telepresence command at the
// end fails, but then they'll see the Telepresence error messge and can
// fix it from there.
getArg := func(i int) string {
if len(args) > i {
return args[i]
}
return ""
}
kubeFlags := pflag.NewFlagSet("Kubernetes flags", 0)
kubeConfig := genericclioptions.NewConfigFlags(false)
kubeConfig.Namespace = nil // "connect", don't take --namespace
kubeConfig.AddFlags(kubeFlags)
Parsing:
for i, v := range args {
switch {
case v == "--swap-deployment" || v == "-s":
lc.swapDeployment = getArg(i + 1)
case v == "--new-deployment" || v == "-n":
lc.newDeployment = true
case v == "--method" || v == "-m":
lc.method = true
case v == "--expose":
lc.expose = getArg(i + 1)
case v == "--mount":
lc.mount = getArg(i + 1)
case v == "--docker-mount":
lc.dockerMount = getArg(i + 1)
case v == "--env-json":
lc.envJSON = getArg(i + 1)
case v == "--env-file":
lc.envFile = getArg(i + 1)
case v == "--namespace":
lc.namespace = getArg(i + 1)
// The three run commands are terminal so we break
// out of the loop after encountering them.
// This also means if somebody uses --run and --docker-run
// in the same command, whichever is first will be used. I don't
// think this is terrible because I'm not sure how much we need to
// correct *incorrect* tp1 commands here, but it could be improved
case v == "--run":
lc.run = true
if nxtArg := getArg(i + 1); nxtArg != "" {
lc.processCmd = strings.Join(args[i+1:], " ")
}
break Parsing
case v == "--docker-run":
lc.dockerRun = true
if nxtArg := getArg(i + 1); nxtArg != "" {
lc.processCmd = strings.Join(args[i+1:], " ")
}
break Parsing
case v == "--run-shell":
lc.runShell = true
break Parsing
case len(v) > 2 && strings.HasPrefix(v, "--"):
g := v[2:]
if gf := kubeFlags.Lookup(g); gf != nil {
lc.globalFlags = append(lc.globalFlags, v)
if gv := getArg(i + 1); gv != "" && !strings.HasPrefix(gv, "-") {
lc.globalFlags = append(lc.globalFlags, gv)
}
continue Parsing
}
lc.unsupportedFlags = append(lc.unsupportedFlags, v)
}
}
return lc
}
// genTPCommand constructs a Telepresence command based on
// the values that are set in the legacyCommand struct
func (lc *legacyCommand) genTPCommand() (string, error) {
var cmdSlice []string
switch {
// if swapDeployment isn't empty, then our translation is
// an intercept subcommand
case lc.swapDeployment != "":
cmdSlice = append(cmdSlice, "intercept", lc.swapDeployment)
if lc.expose != "" {
cmdSlice = append(cmdSlice, "--port", lc.expose)
}
if lc.envFile != "" {
cmdSlice = append(cmdSlice, "--env-file", lc.envFile)
}
if lc.envJSON != "" {
cmdSlice = append(cmdSlice, "--env-json", lc.envJSON)
}
if lc.context != "" {
cmdSlice = append(cmdSlice, "--context", lc.context)
}
if lc.namespace != "" {
cmdSlice = append(cmdSlice, "--namespace", lc.namespace)
}
// This should be impossible based on how we currently parse commands.
// Just putting it here just in case the impossible happens.
if lc.run && lc.dockerRun {
return "", errcat.User.New("--run and --docker-run are mutually exclusive")
}
if lc.run {
if lc.mount != "" {
cmdSlice = append(cmdSlice, "--mount", lc.mount)
}
}
if lc.dockerRun {
if lc.dockerMount != "" {
cmdSlice = append(cmdSlice, "--docker-mount", lc.dockerMount)
}
cmdSlice = append(cmdSlice, "--docker-run")
}
cmdSlice = append(cmdSlice, lc.globalFlags...)
if lc.processCmd != "" {
cmdSlice = append(cmdSlice, "--", lc.processCmd)
}
if lc.runShell {
cmdSlice = append(cmdSlice, "--", "bash")
}
// If we have a run of some kind without a swapDeployment, then
// we translate to a connect
case lc.runShell:
cmdSlice = append(cmdSlice, "connect")
cmdSlice = append(cmdSlice, lc.globalFlags...)
cmdSlice = append(cmdSlice, "--", "bash")
case lc.run:
cmdSlice = append(cmdSlice, "connect")
cmdSlice = append(cmdSlice, lc.globalFlags...)
cmdSlice = append(cmdSlice, "--", lc.processCmd)
// Either not a legacyCommand or we don't know how to translate it to Telepresence
default:
return "", nil
}
return strings.Join(cmdSlice, " "), nil
}
// translateLegacyCmd tries to detect if a legacy Telepresence command was used
// and constructs a Telepresence command from that.
func translateLegacyCmd(args []string) (string, string, *legacyCommand, error) {
lc := parseLegacyCommand(args)
tpCmd, err := lc.genTPCommand()
if err != nil {
return "", "", lc, err
}
// There are certain elements of the telepresence 1 cli that we either
// don't have a perfect mapping for or want to explicitly let users know
// about changed behavior.
msg := ""
if len(lc.unsupportedFlags) > 0 {
msg += fmt.Sprintf("The following flags used don't have a direct translation to Telepresence: %s\n",
strings.Join(lc.unsupportedFlags, " "))
}
if lc.method {
msg += "Telepresence doesn't have proxying methods. You can use --docker-run for container, otherwise it works similarly to vpn-tcp\n"
}
if lc.newDeployment {
msg += "This flag is ignored since Telepresence uses one traffic-manager deployed in the ambassador namespace.\n"
}
return tpCmd, msg, lc, nil
}
// checkLegacyCmd is mostly a wrapper around translateLegacyCmd. The latter
// is separate to make for easier testing.
func checkLegacyCmd(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return nil
}
tpCmd, msg, lc, err := translateLegacyCmd(args)
if err != nil {
return err
}
ctx := cmd.Context()
scout := scout.NewReporter(ctx, "cli")
scout.Start(ctx)
defer scout.Close()
// Add metadata for the main legacy Telepresence commands so we can
// track usage and see what legacy commands people are still using.
if lc.swapDeployment != "" {
scout.SetMetadatum(ctx, "swap_deployment", true)
}
if lc.run {
scout.SetMetadatum(ctx, "run", true)
}
if lc.dockerRun {
scout.SetMetadatum(ctx, "docker_run", true)
}
if lc.runShell {
scout.SetMetadatum(ctx, "run_shell", true)
}
if lc.unsupportedFlags != nil {
scout.SetMetadatum(ctx, "unsupported_flags", lc.unsupportedFlags)
}
scout.Report(ctx, "Used legacy syntax")
// Generate output to user letting them know legacy Telepresence was used,
// what the Telepresence command is, and runs it.
if tpCmd != "" {
fmt.Fprintf(cmd.OutOrStderr(), "Legacy Telepresence command used\n")
if msg != "" {
fmt.Fprintln(cmd.OutOrStderr(), msg)
}
fmt.Fprintf(cmd.OutOrStderr(), "Command roughly translates to the following in Telepresence:\ntelepresence %s\n", tpCmd)
ctx := cmd.Context()
fmt.Fprintln(cmd.OutOrStderr(), "running...")
newCmd := Command(ctx)
newCmd.SetArgs(strings.Split(tpCmd, " "))
newCmd.SetOut(cmd.OutOrStderr())
newCmd.SetErr(cmd.OutOrStderr())
if err := newCmd.ExecuteContext(ctx); err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), err)
}
}
return nil
}