-
Notifications
You must be signed in to change notification settings - Fork 407
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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" | ||||
|
@@ -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()) | ||||
|
@@ -223,7 +225,6 @@ func NewCmdDebug(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra. | |||
|
||||
}, | ||||
} | ||||
|
||||
addDebugFlags(cmd, o) | ||||
|
||||
return cmd | ||||
|
@@ -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) { | ||||
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{ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This list is not fully complete. oc debug supports resources Line 1021 in beeaf09
/hold There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added imagestreamimages, imagestreamtags and cronjobs. 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 | ||||
} | ||||
} |
There was a problem hiding this comment.
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
oc/pkg/cli/rollout/rollout.go
Line 83 in beeaf09
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 thanoc ACTION resource/name
.For example
but you can't use
$ oc debug nodes my-node
, oc debug only accepts$ oc debug resource/name
kubectl
definesdoPodResourceCompletion
for this reason.I don't think upstream debug have auto completion.