kubectl icon indicating copy to clipboard operation
kubectl copied to clipboard

kubectl without arguments should print all aliases defined in kuberc file

Open soltysh opened this issue 7 months ago • 8 comments

What would you like to be added: I'd like KUBECTL_KUBERC=true kubectl to print aliases similarly to how it prints the plugins, see below:

Subcommands provided by plugins:
  convert       The command convert is a plugin installed by the user
  krew          The command krew is a plugin installed by the user

Why is this needed: This will be a nice addition, and should help with debugging problems with kuberc file.

This was brought up in https://github.com/kubernetes/kubernetes/pull/125230/files#r1665566996

soltysh avatar Apr 30 '25 11:04 soltysh

I think, that would be a nice feature. But as far as I understand, cobra does not support printing aliases of the commands.

For example, I define an alias to kubectl get in the code. After that if I run kubectl, I can see the get command in the list of commands but alias of the get won't be printed.

I think, we can't support this.

ardaguclu avatar May 02 '25 10:05 ardaguclu

I believe we're controlling the output and the format of what you're getting when running just kubectl. It's defined in https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/util/templates/templater.go so we should be able to expand it to also include the alias section, most likely 😉

soltysh avatar May 07 '25 13:05 soltysh

Thank you for the pointer. In order to not overload the kubectl in the cases where there are many aliases, kuberc only loads the alias that is passed as args and ignores the others. So that when user types kubectl, aliases will not be loaded and return https://github.com/kubernetes/kubernetes/blob/7cb2bd78b22c4ac8d9a401920fbcf7e2b240522d/staging/src/k8s.io/kubectl/pkg/kuberc/kuberc.go#L236-L240.

Maybe we can add an exception for this (i.e. kubectl -- without any commands) to load all aliases to print on the terminal. That would be a very nice to have in the long term.

/priority backlog

ardaguclu avatar May 08 '25 07:05 ardaguclu

/triage accepted

ardaguclu avatar May 08 '25 07:05 ardaguclu

Maybe we can add an exception for this (i.e. kubectl -- without any commands) to load all aliases to print on the terminal. That would be a very nice to have in the long term.

I'm open to whatever will make the user's life easier 😅

soltysh avatar May 08 '25 15:05 soltysh

Hi @ardaguclu @soltysh

I'd like to work on this issue. Based on the discussion, here's my understanding of the implementation:

  1. Add an exception to the kuberc loading logic so that when kubectl is run without any commands (i.e., just kubectl or KUBECTL_KUBERC=true kubectl), all aliases defined in the kuberc file will be loaded.

  2. Modify the output formatting in the templater.go to display these aliases in a format similar to how plugins are displayed:

If there is nothing particularly problematic, I would like to proceed with the work. If there is anything I misunderstood, please give me feedback.

Antraxmin avatar May 14 '25 15:05 Antraxmin

I'd like to work on this issue. Based on the discussion, here's my understanding of the implementation:

1. Add an exception to the kuberc loading logic so that when `kubectl` is run without any commands (i.e., just `kubectl` or `KUBECTL_KUBERC=true kubectl`), all aliases defined in the kuberc file will be loaded.

No need to check for KUBECTL_KUBERC=true that's being handled outside the code which adds the preferences. For now when we find that there are no arguments passed to kubectl we just skip adding the aliases, rather than just adding them normally.

2. Modify the output formatting in the `templater.go` to display these aliases in a format similar to how plugins are displayed:

Once you address no. 1, that'd be the next step.

If there is nothing particularly problematic, I would like to proceed with the work. If there is anything I misunderstood, please give me feedback.

Go for it, and feel free to ping me on slack once you have a PR for review.

soltysh avatar May 19 '25 14:05 soltysh

@Antraxmin you still working on this?

If not I can take a crack at it!

moficodes avatar Jun 24 '25 04:06 moficodes

Hi @soltysh @ardaguclu,

I'd like to work on this issue if it's still available.

Creating the PR for review. Thanks!

/assign

rushmash91 avatar Jul 18 '25 08:07 rushmash91