-
-
Notifications
You must be signed in to change notification settings - Fork 685
/
cli.go
258 lines (230 loc) · 7.09 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
package debug
import (
"strings"
"github.com/urfave/cli/v2"
"github.com/slimtoolkit/slim/pkg/app"
"github.com/slimtoolkit/slim/pkg/app/master/command"
)
//Debug container
const (
Name = "debug"
Usage = "Debug the target container from a debug (side-car) container"
Alias = "dbg"
)
const (
DockerRuntime = "docker"
KubernetesRuntime = "k8s"
KubeconfigDefault = "${HOME}/.kube/config"
NamespaceDefault = "default"
)
type NVPair struct {
Name string
Value string
}
type CommandParams struct {
/// the runtime environment type
Runtime string
/// the running container which we want to attach to
TargetRef string
/// the target namespace (k8s runtime)
TargetNamespace string
/// the target pod (k8s runtime)
TargetPod string
/// the name/id of the container image used for debugging
DebugContainerImage string
/// ENTRYPOINT used launching the debugging container
Entrypoint []string
/// CMD used launching the debugging container
Cmd []string
/// WORKDIR used launching the debugging container
Workdir string
/// Environment variables used launching the debugging container
EnvVars []NVPair
/// launch the debug container with an interactive terminal attached (like '--it' in docker)
DoTerminal bool
/// make it look like shell is running in the target container
DoRunAsTargetShell bool
/// Kubeconfig file path (k8s runtime)
Kubeconfig string
/// Debug session container name
Session string
/// Simple (non-debug) action - list namespaces
ActionListNamespaces bool
/// Simple (non-debug) action - list pods
ActionListPods bool
/// Simple (non-debug) action - list debuggable container
ActionListDebuggableContainers bool
/// Simple (non-debug) action - list debug sessions
ActionListSessions bool
/// Simple (non-debug) action - show debug sessions logs
ActionShowSessionLogs bool
/// Simple (non-debug) action - connect to an existing debug session
ActionConnectSession bool
}
func ParseNameValueList(list []string) []NVPair {
var pairs []NVPair
for _, val := range list {
val = strings.TrimSpace(val)
if val == "" {
continue
}
parts := strings.SplitN(val, "=", 2)
if len(parts) != 2 {
continue
}
nv := NVPair{Name: parts[0], Value: parts[1]}
pairs = append(pairs, nv)
}
return pairs
}
var CLI = &cli.Command{
Name: Name,
Aliases: []string{Alias},
Usage: Usage,
Flags: []cli.Flag{
cflag(FlagRuntime),
cflag(FlagTarget),
cflag(FlagNamespace),
cflag(FlagPod),
cflag(FlagDebugImage),
cflag(FlagEntrypoint),
cflag(FlagCmd),
cflag(FlagWorkdir),
cflag(FlagEnv),
cflag(FlagTerminal),
cflag(FlagRunAsTargetShell),
cflag(FlagListSessions),
cflag(FlagShowSessionLogs),
cflag(FlagConnectSession),
cflag(FlagSession),
cflag(FlagListNamespaces),
cflag(FlagListPods),
cflag(FlagListDebuggableContainers),
cflag(FlagListDebugImage),
cflag(FlagKubeconfig),
},
Action: func(ctx *cli.Context) error {
gcvalues := command.GlobalFlagValues(ctx)
xc := app.NewExecutionContext(
Name,
gcvalues.QuietCLIMode,
gcvalues.OutputFormat)
if ctx.Bool(FlagListDebugImage) {
xc.Out.State("action.list_debug_images")
for k, v := range debugImages {
xc.Out.Info("debug.image", ovars{"name": k, "description": v})
}
return nil
}
commandParams := &CommandParams{
Runtime: ctx.String(FlagRuntime),
TargetRef: ctx.String(FlagTarget),
TargetNamespace: ctx.String(FlagNamespace),
TargetPod: ctx.String(FlagPod),
DebugContainerImage: ctx.String(FlagDebugImage),
DoTerminal: ctx.Bool(FlagTerminal),
DoRunAsTargetShell: ctx.Bool(FlagRunAsTargetShell),
Kubeconfig: ctx.String(FlagKubeconfig),
Workdir: ctx.String(FlagWorkdir),
EnvVars: ParseNameValueList(ctx.StringSlice(FlagEnv)),
Session: ctx.String(FlagSession),
ActionListNamespaces: ctx.Bool(FlagListNamespaces),
ActionListPods: ctx.Bool(FlagListPods),
ActionListDebuggableContainers: ctx.Bool(FlagListDebuggableContainers),
ActionListSessions: ctx.Bool(FlagListSessions),
ActionShowSessionLogs: ctx.Bool(FlagShowSessionLogs),
ActionConnectSession: ctx.Bool(FlagConnectSession),
}
if commandParams.Runtime != KubernetesRuntime &&
(commandParams.ActionListNamespaces ||
commandParams.ActionListPods) {
var actionName string
if commandParams.ActionListNamespaces {
actionName = FlagListNamespaces
}
if commandParams.ActionListPods {
actionName = FlagListPods
}
xc.Out.Error("param", "unsupported runtime flag")
xc.Out.State("exited",
ovars{
"runtime.provided": commandParams.Runtime,
"runtime.required": KubernetesRuntime,
"action": actionName,
"exit.code": -1,
})
xc.Exit(-1)
}
var err error
if rawEntrypoint := ctx.String(FlagEntrypoint); rawEntrypoint != "" {
commandParams.Entrypoint, err = command.ParseExec(rawEntrypoint)
if err != nil {
return err
}
}
if rawCmd := ctx.String(FlagCmd); rawCmd != "" {
commandParams.Cmd, err = command.ParseExec(rawCmd)
if err != nil {
return err
}
}
//explicitly setting the entrypoint and/or cmd clears
//implies a custom debug session where the 'RATS' setting should be ignored
if len(commandParams.Entrypoint) > 0 || len(commandParams.Cmd) > 0 {
commandParams.DoRunAsTargetShell = false
}
if commandParams.DoRunAsTargetShell {
commandParams.DoTerminal = true
}
if !commandParams.ActionListNamespaces &&
!commandParams.ActionListPods &&
!commandParams.ActionListDebuggableContainers &&
!commandParams.ActionListSessions &&
!commandParams.ActionShowSessionLogs &&
!commandParams.ActionConnectSession &&
commandParams.TargetRef == "" {
if ctx.Args().Len() < 1 {
if commandParams.Runtime != KubernetesRuntime {
xc.Out.Error("param.target", "missing target")
cli.ShowCommandHelp(ctx, Name)
return nil
}
//NOTE:
//It's ok to not specify the target container for k8s
//We'll pick the default or first container in the target pod
} else {
commandParams.TargetRef = ctx.Args().First()
if ctx.Args().Len() > 1 && ctx.Args().Slice()[1] == "--" {
//NOTE:
//Keep the original 'no terminal' behavior
//use this shortcut mode as a way to quickly
//run one off commands in the debugged container
//When there's 'no terminal' we show
//the debugger container log at the end.
//TODO: revisit the behavior later...
cmdSlice := ctx.Args().Slice()[2:]
var cmdClean []string
for _, v := range cmdSlice {
v = strings.TrimSpace(v)
if v != "" {
cmdClean = append(cmdClean, v)
}
}
if len(cmdClean) > 0 {
commandParams.Cmd = cmdClean
commandParams.DoTerminal = false
commandParams.DoRunAsTargetShell = false
}
}
}
}
if commandParams.DebugContainerImage == "" {
commandParams.DebugContainerImage = BusyboxImage
}
OnCommand(
xc,
gcvalues,
commandParams)
return nil
},
}