KEP(draft): Make kubectl smarter with prefix

12 views
Skip to first unread message

Peng Liu

unread,
Mar 14, 2021, 12:41:47 PM3/14/21
to kubernetes-sig-cli
Hi sig-cli:

I would proposal kubectl sub commands to recognize prefix. It had been talk in an issue before, but have not got a clear conclusion yet, please have a look at my KEP and evaluate if it is necessary. Thanks!

Summary

Currently prefix match logic is only implemented in kubectl describe and it seems not complete and not strong enough. Other command such as kubectl get, kubectl edit, kubectl exec especially kubectl logs do not have this feature.But this has been requested for a long time since 2015 and I think this is really useful if you use kubectl heavily.

This KEP describes how to implement prefix match globally in kubectl.

Motivation

The main motivation for doing this is due to the following two use cases.

use case1: save the keyboard!

Let's say we are a developing a crd controller called my-super-awesome-crd-controller. After we deployed it to my namespace which is named my-namespace, we want to see if it is running, we will type the following command which contain 66 letters.bash kubectl get deploy -n my-namespace my-super-awesome-crd-controller

But if we have prefix this would be:bash kubectl get deploy -n my-namespace my

use case2: make command repeatable

And in development env, we always need to see instant logs using kubectl logs -f.

kubectl -n my-namespace get pod|grep my # copy pod name in the output kubectl -n my-namespace logs -f POD-NAME-COPIED-FROM-LAST-COMMAND

It needs two commands since pod name changes everytime we start a new one.It maybe stupid, but it is quite straight forward.

If we are familiar with awk we maybe smarter:

kubectl -n my-namespace logs -f $(kubectl -n my-namespace get pods | awk '/my-super/ {print $1;exit}')

Or we may use label selector but still a lot of letters to type.

If we have prefix, this would be much easier

kubectl -n my-namespace logs -f my
Proposal

Although moby/containerd and kubernetes are totally different project, some usage habits are similar.And in the communicate there are other life saving repo are very popular, like autojump.

Let take moby at a look.docker inspect docker exec docker logsdocker load all support prefix shortcut which is really convenient.

We could make the following commands recognize prefix: - kubectl get - kubectl edit - kubectl exec - kubectl logs

If the for the same prefix etcd returns multi records we may simply return error.

Risks and Mitigations

We should consider about if this will mislead user to make action on unwanted resources. Or add a command line flag to turn this on or off.

Design Details

TODO I will add this after discuss.

Alternatives

We could use zsh autocomplete plugin to accomplish this but it has two fallback:

  • autocomplete plugin is not repeatable, we always need to manually type taband select to get what we want. Shortcut prefix is more efficiently.
  • autocomplete plugin may become very slow on largge cluster.

Peng Liu

unread,
Mar 14, 2021, 12:41:50 PM3/14/21
to kubernetes-sig-cli

Daniel Smith

unread,
Mar 15, 2021, 12:38:47 PM3/15/21
to Peng Liu, kubernetes-sig-cli
I think it's very dangerous to have kubectl expand prefixes at invocation time. Imagine running the same delete command twice accidentally and the second time it deletes a thing that happened to be a suffix of the first thing!

However, I'd be in favor of making tab completion (e.g. in bash) do this.

However, some of the completions may require very expensive lists from the server, so I'm not very excited about that.

--
You received this message because you are subscribed to the Google Groups "kubernetes-sig-cli" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-sig-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kubernetes-sig-cli/43477517-5126-4cb5-8f5d-97b85f448264n%40googlegroups.com.

Antoine Pelisse

unread,
Mar 15, 2021, 12:55:09 PM3/15/21
to Daniel Smith, Peng Liu, kubernetes-sig-cli
Yeah, I'm also concerned about this feature. `kubectl describe` does that only because it is only meant for human consumption (rather than scripts). `kubectl get` (and others) shouldn't try to "do what you mean" (dwim).

The completion already exists, and I'm sure it's slow on big clusters, but I'm not sure why you would expect it to be faster in kubectl, since there isn't, AFAIK, a way to select the resources by prefix in the apiserver.

If we really wanted to go that route, I'd be much more open to a glob pattern matching:

    $ kubectl get pod my-deployment-name-*

Phillip Wittrock

unread,
Mar 15, 2021, 1:42:18 PM3/15/21
to Antoine Pelisse, Daniel Smith, Peng Liu, kubernetes-sig-cli
> Or we may use label selector but still a lot of letters to type.

Isn't this just -l MY_LABEL ?  If you set the prefix as a label isn't it the same number of characters?

Reply all
Reply to author
Forward
0 new messages