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

Conversation

sudomakeinstall2
Copy link

@sudomakeinstall2 sudomakeinstall2 commented Apr 15, 2025

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

examples:

$ oc get pods
NAME                                                READY   STATUS
RESTARTS   AGE
openshift-adp-controller-manager-68d8974985-gsn5b   1/1     Running   0
19d

$ oc debug [TAB]
cronjobs/                                          nodes/
daemonsets/
openshift-adp-controller-manager-68d8974985-gsn5b
deployments/                                       pods/
imagestreamimages/                                 replicasets/
imagestreamtags/
replicationcontrollers/
jobs/                                              statefulsets/

$ 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

@openshift-ci openshift-ci bot requested review from deads2k and ingvagabund April 15, 2025 16:57
Copy link
Contributor

openshift-ci bot commented Apr 15, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sudomakeinstall2
Once this PR has been reviewed and has the lgtm label, please assign deads2k for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sudomakeinstall2
Copy link
Author

/retest

1 similar comment
@sudomakeinstall2
Copy link
Author

/retest

// debugCompletionFunc Returns a completion function that completes:
// 1- pod names that match the toComplete prefix
// 2- resource types containing pods and nodes 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.

// 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.

@openshift-ci openshift-ci bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 16, 2025
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`

examples:
```
$ oc get pods
NAME                                                READY   STATUS
RESTARTS   AGE
openshift-adp-controller-manager-68d8974985-gsn5b   1/1     Running   0
19d

$ oc debug [TAB]
cronjobs/                                          nodes/
daemonsets/
openshift-adp-controller-manager-68d8974985-gsn5b
deployments/                                       pods/
imagestreamimages/                                 replicasets/
imagestreamtags/
replicationcontrollers/
jobs/                                              statefulsets/

$ 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]>
Copy link
Contributor

openshift-ci bot commented May 23, 2025

@sudomakeinstall2: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-metal-ipi-ovn-ipv6 73ca38e link false /test e2e-metal-ipi-ovn-ipv6
ci/prow/okd-scos-e2e-aws-ovn 73ca38e link false /test okd-scos-e2e-aws-ovn
ci/prow/e2e-aws-ovn-upgrade 73ca38e link true /test e2e-aws-ovn-upgrade

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants