Skip to content

Commit 44a2a5d

Browse files
committed
Add shell completion proposal for kubectl plugins
This proposal was discussed in kubernetes/kubernetes#105867 Signed-off-by: Marc Khouzam <[email protected]>
1 parent 25f8b4c commit 44a2a5d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

keps/sig-cli/2379-kubectl-plugins/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Scenarios](#scenarios)
1515
- [Implementation Details/Design](#implementation-detailsdesign)
1616
- [Naming Conventions](#naming-conventions)
17+
- [Shell Completion Support](#shell-completion-support)
1718
- [Implementation Notes/Constraints](#implementation-notesconstraints)
1819
- [Risks and Mitigations](#risks-and-mitigations)
1920
- [Graduation Criteria](#graduation-criteria)
@@ -156,6 +157,40 @@ Under this proposal, `kubectl` would identify plugins by looking for filenames b
156157
A search for these names would occur on a user's `PATH`. Only files that are executable and begin with this prefix
157158
would be identified.
158159

160+
#### Shell Completion Support
161+
162+
As an extension to this proposal, this section describes how shell completion support can be added to the implementation
163+
of a `kubectl` plugin.
164+
165+
For a plugin to support shell completion, it will need to provide an independent executable file using the prefix
166+
`kubectl_complete-` followed by the plugin name. For example, a plugin named `kubectl-foo` would
167+
provide a completion executable named `kubectl_complete-foo`.
168+
169+
Similarly to the plugin executable, the completion executable will need to be found on `PATH`. When
170+
`kubectl` requires shell completions for the plugin, it will look for and execute the
171+
`kubectl_complete-<plugin>` file, passing it the command-line that needs to be completed as multiple
172+
arguments. The `kubectl_complete-<plugin>` executable will need to have the logic to determine what the
173+
proper completion choices are and output them to standard output to be consumed by `kubectl`.
174+
175+
For a richer completion experience, `kubectl` will support Cobra's shell completion directive system which will
176+
allow plugins to optionally control certain behaviors of the shell when it performs shell completion. Please refer to
177+
[the Cobra documentation](https://github.com/spf13/cobra/blob/master/shell_completions.md#dynamic-completion-of-nouns) for details.
178+
179+
Supporting shell completion and therefore providing a `kubectl_complete-<plugin>` executable is entirely
180+
optional. If it is not provided, `kubectl` will simply not perform any special shell completion for the
181+
plugin and will instead default to file completion. Note that providing a `kubectl_complete-<plugin>`
182+
executable is backwards-compatible and will simply be ignored when using older versions of `kubectl`.
183+
184+
It is worth mentioning that if a `kubectl` plugin is implemented using [the Cobra project](https://github.com/spf13/cobra),
185+
it can use Cobra's built-in shell completion support directly. As an example, if we have a plugin named `myplugin`
186+
which uses Cobra, its `kubectl_complete-myplugin` executable can simply be:
187+
```sh
188+
#!/usr/bin/env sh
189+
190+
kubectl myplugin __complete "$@"
191+
```
192+
Then all that is needed is to name this file `kubectl_complete-myplugin`, put it somewhere on `$PATH` and give it executable permissions.
193+
159194
### Implementation Notes/Constraints
160195

161196
The current implementation details for the proposed design rely on using a plugin executable's name to determine what

0 commit comments

Comments
 (0)