Skip to content

Fix debug command auto-completion #2009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions pkg/cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
kcmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/polymorphichelpers"
"k8s.io/kubectl/pkg/scheme"
"k8s.io/kubectl/pkg/util/completion"
"k8s.io/kubectl/pkg/util/interrupt"
"k8s.io/kubectl/pkg/util/templates"
"k8s.io/pod-security-admission/api"
Expand Down Expand Up @@ -206,10 +207,11 @@ func NewDebugOptions(streams genericiooptions.IOStreams) *DebugOptions {
func NewCmdDebug(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := NewDebugOptions(streams)
cmd := &cobra.Command{
Use: "debug RESOURCE/NAME [ENV1=VAL1 ...] [-c CONTAINER] [flags] [-- COMMAND]",
Short: "Launch a new instance of a pod for debugging",
Long: debugLong,
Example: debugExample,
Use: "debug RESOURCE/NAME [ENV1=VAL1 ...] [-c CONTAINER] [flags] [-- COMMAND]",
Short: "Launch a new instance of a pod for debugging",
Long: debugLong,
Example: debugExample,
ValidArgsFunction: debugCompletionFunc(f),
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(o.Complete(cmd, f, args))
kcmdutil.CheckErr(o.Validate())
Expand All @@ -223,7 +225,6 @@ func NewCmdDebug(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra.

},
}

addDebugFlags(cmd, o)

return cmd
Expand Down Expand Up @@ -1349,3 +1350,58 @@ func (o *DebugOptions) resolveImageStreamTag(namespace, name, tag string) (strin
}
return image, nil
}

// debugCompletionFunc Returns a completion function that completes:
// 1- pod names that match the toComplete prefix
// 2- resource types acceptable by debug command which match the toComplete prefix
func debugCompletionFunc(f kcmdutil.Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many built-in completion functions in kubectl and it is always better to use them. For example, upstream debug uses https://github.com/kubernetes/kubectl/blob/72b3a7e9b0e99ba874ffe7dd85f9dfd756239dd1/pkg/cmd/cmd.go#L393 and some of the oc commands use

cmd.ValidArgsFunction = completion.SpecifiedResourceTypeAndNameCompletionFunc(f, validArgs)
. I think, in our case we can use ResourceTypeAndNameCompletionFunc like kubectl debug.

Copy link
Author

@sudomakeinstall2 sudomakeinstall2 Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this first but it doesn't really fit here. It fits more with oc ACTION resource name rather than oc ACTION resource/name.

For example

$ oc debug [tab]
nodes pods deployments ...

$ oc debug nod[tab] -> oc debug nodes [cursor is placed after space]

but you can't use $ oc debug nodes my-node, oc debug only accepts $ oc debug resource/name
kubectl defines doPodResourceCompletion for this reason.

I don't think upstream debug have auto completion.

return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var comps []string
directive := cobra.ShellCompDirectiveNoFileComp
if len(args) != 0 {
return comps, directive
}

directive = cobra.ShellCompDirectiveNoFileComp
slashIdx := strings.Index(toComplete, "/")
if slashIdx == -1 {
// Standard case, complete pod names
comps = completion.CompGetResource(f, "pod", toComplete)

validResources := []string{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list is not fully complete. oc debug supports resources

func (o *DebugOptions) approximatePodTemplateForObject(object runtime.Object) (*corev1.PodTemplateSpec, error) {
more than this list
/hold

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added imagestreamimages, imagestreamtags and cronjobs.
I didn't add deploymentConfig since it is deprecated since 4.14.

I can add it if you think it should be here.

"cronjobs",
"daemonsets",
"deployments",
"imagestreamimages",
"imagestreamtags",
"jobs",
"nodes",
"pods",
"replicasets",
"replicationcontrollers",
"statefulsets",
}

if len(comps) == 0 {
// If there are no pods to complete, we will only be completing
// <type>/. We should disable adding a space after the /.
directive |= cobra.ShellCompDirectiveNoSpace
}

for _, resource := range validResources {
if strings.HasPrefix(resource, toComplete) {
comps = append(comps, fmt.Sprintf("%s/", resource))
}
}
} else {
// Dealing with the <type>/<name> form, use the specified resource type
resourceType := toComplete[:slashIdx]
toComplete = toComplete[slashIdx+1:]
nameComps := completion.CompGetResource(f, resourceType, toComplete)
for _, c := range nameComps {
comps = append(comps, fmt.Sprintf("%s/%s", resourceType, c))
}
}
return comps, directive
}
}