Skip to content

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
@openshift-ci
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]>
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jul 16, 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
ci/prow/e2e-aws-ovn-serial-1of2 73ca38e link true /test e2e-aws-ovn-serial-1of2

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.

@openshift-bot
Copy link
Contributor

Issues go stale after 90d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle stale

@openshift-ci openshift-ci bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 15, 2025
@openshift-bot
Copy link
Contributor

Stale issues rot after 30d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.
Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle rotten
/remove-lifecycle stale

@openshift-ci openshift-ci bot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Nov 14, 2025
@coderabbitai
Copy link

coderabbitai bot commented Nov 14, 2025

Walkthrough

A new completion helper is imported and wired into the debug command. A debugCompletionFunc is introduced that provides pod name suggestions when no slash is present and resource/name pair suggestions when a slash is present. Shell completion behavior is adjusted based on pod availability.

Changes

Cohort / File(s) Change Summary
Shell Completion Enhancement
pkg/cli/debug/debug.go
Adds completion helper import and new debugCompletionFunc to provide pod name and resource/name pair suggestions. Wires completion function into debug command via ValidArgsFunction. Conditionally returns suggestions and adjusts shell completion behavior based on slash presence and pod availability.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • Verify the logic for detecting slash presence in the input string and branching to appropriate completion suggestions
  • Ensure pod and resource-type suggestions are correctly aggregated and formatted
  • Confirm proper integration of debugCompletionFunc with ValidArgsFunction in the debug command
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
pkg/cli/debug/debug.go (1)

1365-1365: Redundant directive assignment.

The directive is assigned the same value on line 1360 and nothing modifies it before line 1365, making this assignment redundant.

Apply this diff to remove the redundancy:

-		directive = cobra.ShellCompDirectiveNoFileComp
 		slashIdx := strings.Index(toComplete, "/")
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between beeaf09 and 73ca38e.

📒 Files selected for processing (1)
  • pkg/cli/debug/debug.go (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**

⚙️ CodeRabbit configuration file

-Focus on major issues impacting performance, readability, maintainability and security. Avoid nitpicks and avoid verbosity.

Files:

  • pkg/cli/debug/debug.go
🔇 Additional comments (4)
pkg/cli/debug/debug.go (4)

210-214: LGTM!

The integration of ValidArgsFunction with the cobra command is correct and follows standard patterns for adding shell completion support.


1354-1363: LGTM!

The function signature and early return logic are correct. Properly handles the case where arguments are already provided.


1367-1395: LGTM!

The completion logic for pod names and resource types is well-structured. The NoSpace directive is correctly applied when no pods exist to prevent unwanted spaces after resource type completions like "pods/". The resource list aligns with supported types in approximatePodTemplateForObject.


1396-1404: LGTM!

The handling of type/name completion format correctly extracts the resource type and completes resource names. The implementation properly formats results as "type/name" pairs.

kcmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/polymorphichelpers"
"k8s.io/kubectl/pkg/scheme"
"k8s.io/kubectl/pkg/util/completion"
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Verify kubectl version compatibility for completion utilities.

The import of k8s.io/kubectl/pkg/util/completion is appropriate for this use case. However, please verify that the completion utilities used (specifically CompGetResource) are available and stable in kubectl version 0.32.1.


🌐 Web query:

Is CompGetResource available in kubectl v0.32.1 and what is its function signature?

💡 Result:

I can’t find any reference to a symbol named "CompGetResource" in kubectl or Kubernetes repos. Do you have a link, file path, or the context where you saw that name (plugin, code snippet, or error)? With that I’ll look up whether it exists in kubectl v0.32.1 and its signature.


I need to examine the actual code to see what functions from the completion package are being used.


---

🏁 Script executed:

```shell
#!/bin/bash
# Search for actual uses of the completion package in debug.go
rg -n 'completion\.' pkg/cli/debug/debug.go -A 2 -B 2

Length of output: 457


🏁 Script executed:

#!/bin/bash
# Also check the full debugCompletionFunc implementation around lines 1354-1407
sed -n '1354,1407p' pkg/cli/debug/debug.go

Length of output: 1762


🏁 Script executed:

#!/bin/bash
# Check for any direct imports or usage patterns of completion functions
rg -n 'CompGet|completion\.(Get|Suggest|Complete)' pkg/cli/debug/debug.go -C 3

Length of output: 618


🏁 Script executed:

#!/bin/bash
# Look at the command modification around line 214
sed -n '210,220p' pkg/cli/debug/debug.go

Length of output: 451


🌐 Web query:

kubectl k8s.io/kubectl/pkg/util/completion package functions v0.32.1

💡 Result:

For kubectl v0.32.1, the k8s.io/kubectl/pkg/util/completion package exposes (not exhaustive but main) functions:

  • ClusterCompletionFunc. [1]
  • ContextCompletionFunc. [1]
  • ResourceTypeAndNameCompletionFunc. [1]
  • SpecifiedResourceTypeAndNameCompletionFunc. [1]
  • SetFactoryForCompletion. [1]
  • CreateApplyAnnotation. [2]
  • CreateOrUpdateAnnotation. [2]
  • GetModifiedConfiguration / GetOriginalConfiguration (related helpers used with annotations). [2]
  • Umask. [1]

Sources: GitHub pkg tree (v0.32.1) and Go package docs. [3][1][2]


Fix function call: completion.CompGetResource does not exist in kubectl v0.32.1.

The import at line 42 is valid, but the code calls completion.CompGetResource() at lines 1369 and 1400, which is not available in kubectl v0.32.1's k8s.io/kubectl/pkg/util/completion package. The available functions are ResourceTypeAndNameCompletionFunc and SpecifiedResourceTypeAndNameCompletionFunc. Update the completion function to use the correct API from kubectl v0.32.1.

🤖 Prompt for AI Agents
In pkg/cli/debug/debug.go (import line ~42 and usage at lines ~1369 and ~1400),
the code calls the non-existent completion.CompGetResource; replace those calls
with the kubectl v0.32.1 compatible completion functions: use
completion.ResourceTypeAndNameCompletionFunc(...) for generic resource type+name
completion, or completion.SpecifiedResourceTypeAndNameCompletionFunc(...) when
your command already fixes the resource type; adjust the call signature to match
the chosen function (pass the command/resource type arguments expected by the
function) and remove references to CompGetResource.

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. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants