Skip to content

Commit 6526b83

Browse files
Fix oc debug autocompletion
Currently `oc debug` auto-completes to files. This commit adds auto-completion for this command. Auto-completion suggests pods and resources that are valid for the debug command. Implemenation is based on `completions.doPodResourceCompletion` expect that it includes `nodes` instead of `services`. examples: ``` $ oc get pods NAME READY STATUS RESTARTS AGE apiserver-5f58c584c4-6wvnr 2/2 Running 0 18d apiserver-5f58c584c4-dlcbk 2/2 Running 0 18d apiserver-5f58c584c4-ljlc9 2/2 Running 0 18d $ oc debug [TAB] apiserver-5f58c584c4-6wvnr daemonsets/ nodes/ replicationcontrollers/ apiserver-5f58c584c4-dlcbk deployments/ pods/ statefulsets/ apiserver-5f58c584c4-ljlc9 jobs/ replicasets/ $ oc debug nodes/vcl01-hv9-ctlplane-[TAB] nodes/vcl01-hv9-ctlplane-0.redhat.com nodes/vcl01-hv9-ctlplane-2.redhat.com nodes/vcl01-hv9-ctlplane-1.redhat.com ``` Signed-off-by: Saeid Askari <[email protected]>
1 parent beeaf09 commit 6526b83

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

pkg/cli/debug/debug.go

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
kcmdutil "k8s.io/kubectl/pkg/cmd/util"
4040
"k8s.io/kubectl/pkg/polymorphichelpers"
4141
"k8s.io/kubectl/pkg/scheme"
42+
"k8s.io/kubectl/pkg/util/completion"
4243
"k8s.io/kubectl/pkg/util/interrupt"
4344
"k8s.io/kubectl/pkg/util/templates"
4445
"k8s.io/pod-security-admission/api"
@@ -206,10 +207,11 @@ func NewDebugOptions(streams genericiooptions.IOStreams) *DebugOptions {
206207
func NewCmdDebug(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
207208
o := NewDebugOptions(streams)
208209
cmd := &cobra.Command{
209-
Use: "debug RESOURCE/NAME [ENV1=VAL1 ...] [-c CONTAINER] [flags] [-- COMMAND]",
210-
Short: "Launch a new instance of a pod for debugging",
211-
Long: debugLong,
212-
Example: debugExample,
210+
Use: "debug RESOURCE/NAME [ENV1=VAL1 ...] [-c CONTAINER] [flags] [-- COMMAND]",
211+
Short: "Launch a new instance of a pod for debugging",
212+
Long: debugLong,
213+
Example: debugExample,
214+
ValidArgsFunction: debugCompletionFunc(f),
213215
Run: func(cmd *cobra.Command, args []string) {
214216
kcmdutil.CheckErr(o.Complete(cmd, f, args))
215217
kcmdutil.CheckErr(o.Validate())
@@ -223,7 +225,6 @@ func NewCmdDebug(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra.
223225

224226
},
225227
}
226-
227228
addDebugFlags(cmd, o)
228229

229230
return cmd
@@ -1349,3 +1350,54 @@ func (o *DebugOptions) resolveImageStreamTag(namespace, name, tag string) (strin
13491350
}
13501351
return image, nil
13511352
}
1353+
1354+
// debugCompletionFunc Returns a completion function that completes:
1355+
// 1- pod names that match the toComplete prefix
1356+
// 2- resource types containing pods and nodes which match the toComplete prefix
1357+
func debugCompletionFunc(f kcmdutil.Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
1358+
return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
1359+
var comps []string
1360+
directive := cobra.ShellCompDirectiveNoFileComp
1361+
if len(args) != 0 {
1362+
return comps, directive
1363+
}
1364+
1365+
directive = cobra.ShellCompDirectiveNoFileComp
1366+
slashIdx := strings.Index(toComplete, "/")
1367+
if slashIdx == -1 {
1368+
// Standard case, complete pod names
1369+
comps = completion.CompGetResource(f, "pod", toComplete)
1370+
1371+
validResources := []string{
1372+
"daemonsets",
1373+
"deployments",
1374+
"jobs",
1375+
"nodes",
1376+
"pods",
1377+
"replicasets",
1378+
"replicationcontrollers",
1379+
"statefulsets"}
1380+
1381+
if len(comps) == 0 {
1382+
// If there are no pods to complete, we will only be completing
1383+
// <type>/. We should disable adding a space after the /.
1384+
directive |= cobra.ShellCompDirectiveNoSpace
1385+
}
1386+
1387+
for _, resource := range validResources {
1388+
if strings.HasPrefix(resource, toComplete) {
1389+
comps = append(comps, fmt.Sprintf("%s/", resource))
1390+
}
1391+
}
1392+
} else {
1393+
// Dealing with the <type>/<name> form, use the specified resource type
1394+
resourceType := toComplete[:slashIdx]
1395+
toComplete = toComplete[slashIdx+1:]
1396+
nameComps := completion.CompGetResource(f, resourceType, toComplete)
1397+
for _, c := range nameComps {
1398+
comps = append(comps, fmt.Sprintf("%s/%s", resourceType, c))
1399+
}
1400+
}
1401+
return comps, directive
1402+
}
1403+
}

0 commit comments

Comments
 (0)