Skip to content

Proposal: Auto-completion framework for plugins #7043

Closed

Description

@marckhouzam

When using a Helm plugin, it would be useful to have shell auto-completion as is available for Helm core commands. While auto-completion of plugin names is already supported (e.g.helm 2to<TAB> will already complete to helm 2to3), once the user has typed the plugin name, no further completion is available; for example, helm 2to3 con<TAB> and helm 2to3 convert <TAB> currently provide no completion choices. Using the proposed framework, the 2to3 plugin could allow helm 2to3 con<TAB> to complete to helm 2to3 convert while helm 2to3 convert <TAB> would complete to possible v2 releases to convert.

In an attempt to describe the proposed framework, I’ll start by giving a proposed user documentation for using the framework. Following that I provide some design decision explanations.

At the basis of this proposal is the reality that auto-completion choices for a plugin must be provided by the plugin itself: Helm cannot know them if the plugin does not provide them since Helm knows nothing about the plugin beyond how to call it.

Plugin developer documentation

Providing shell auto-completion

Starting with Helm 3.?, a plugin can optionally provide support for shell auto-completion as part of Helm’s existing auto-completion script.

Static auto-completion

If your plugin provides its own flags and/or sub-commands, it can inform Helm of them, through an optional completion.yaml file in your plugin’s root directory. The completion.yaml file has the form:

name: <pluginName>
flags:
- <flag 1>
- <flag 2>
validArgs:
- <arg value 1>
- <arg value 2>
commands:
  name: <commandName>
  flags:
  - <flag 1>
  - <flag 2>
  validArgs:
  - <arg value 1>
  - <arg value 2>
  commands:
     <and so on, recursively> 

Notes:

  1. All sections are optional but should be provided if applicable.
  2. Flags should not include the - or -- prefix.
  3. Both short and long flags can and should be specified. A short flag need not be associated with its corresponding long form; they simply should both be listed.
  4. Flags need not be ordered in any way, but need to be listed at the correct point in the sub-command hierarchy.
  5. Helm existing global flags are already handled by Helm’s auto-completion script. So plugins need not specify the following flags --debug, --namespace or -n, --kube-context, and --kubeconfig.
  6. The validArgs list provides a list of possible completions for the first parameter following a sub-command. It is not always possible to provide such a list in advance (see Dynamic Completion section below), in which case the validArgs section can be omitted.

The completion.yaml file is entirely optional. If it is not provided, Helm will simply not provide auto-completion for the plugin (unless Dynamic Completion is supported by the plugin; see section below).

For example, for the fullstatus plugin which has no sub-commands but accepts the same flags as the helm status command, the completion.yaml file should simply be:

name: fullstatus
flags:
- o
- output
- revision

A more intricate example for the 2to3 plugin, has a completion.yaml file of:

name: 2to3
commands:
- name: cleanup
  flags:
  - config-cleanup
  - dry-run
  - l
  - label
  - release-cleanup
  - s
  - release-storage
  - tiller-cleanup
  - t
  - tiller-ns
  - tiller-out-cluster
- name: convert
  flags:
  - delete-v2-releases
  - dry-run
  - l
  - label
  - s
  - release-storage
  - release-versions-max
  - t
  - tiller-ns
  - tiller-out-cluster
- name: move
  commands:
  - name: config
    flags:
    - dry-run

Dynamic completion

Helm 3.? also allows plugins to provide dynamic auto-completion. Dynamic auto-completion is the completion of parameter values or flag values that cannot be defined in advance. For example, completion of the names of helm releases currently available on the cluster. For the plugin to support dynamic auto-completion, it must provide an executable file in its root directory called plugin.complete. When the Helm completion script requires dynamic completions for the plugin, it will execute the plugin.complete file, passing it the command-line that needs to be completed. The plugin.complete script will need to have the logic to determine what the proper completion choices are and output them to be consumed by the completion script.

The output of the plugin.complete script should be a new-line separated list such as:

rel1 rel2 rel3

When plugin.complete is called, the plugin environment is set just like when the plugin's main script is called. Therefore, the $HELM_NAMESPACE, $KUBECONFIG, and all other plugin variables will already be set, and their corresponding flags will be removed.

The plugin.complete file can be in any executable format; it can be a shell script, a go program, or any other type of program that Helm can execute.
The plugin.complete file must have executable permissions for the user.

In some cases, dynamic completion will require to obtain information from the Kubernetes cluster. For example, the helm fullstatus plugin requires a release name as input. In the fullstatus plugin, for its plugin.complete script to provide completion for current release names, it can simply run helm list -q.

If you prefer to use the same executable for plugin execution and for plugin completion, you can choose to have your plugin.complete script call your main plugin executable with a special parameter of your choosing, requesting it to do the completion. In our example, plugin.complete could be implemented like this:

#!/usr/bin/env sh

# "$@" is the entire command-line that requires completion.
# It is important to double-quote the "$@" variable to preserve a possibly empty last parameter.
$HELM_PLUGIN_DIR/status.sh --complete "$@"

The fullstatus plugin's real script (status.sh) must then look for the --complete flag and if found, printout the proper completions.

Tips and tricks

  1. The shell will automatically filter out completion choices that don't match user input. A plugin can therefore return all relevant completions without removing the ones that don't match the user input. For example, if the command-line is helm fullstatus ngin<TAB>, the fullstatus.complete script can print all release names (of the default namespace), not just the ones starting with ngin; the shell will only retain the ones starting with ngin.
  2. To simplify dynamic completion support, especially if you have a complex plugin, you can have your plugin.complete script call your main plugin script and request completion choices. See the Dynamic Completion section above for an example.

Design details

My first idea for completion was for the completion script to call the plugin directly with a special flag to request completion choices, e.g., helm 2to3 --complete <command-line to complete>. However, this would make supporting completion mandatory for plugins, or else who knows what the plugin would do when being called with the flag.

My second attempt was to only add the plugin.complete script concept. If the script was present, it would indicate the plugin supported completion, and that script could safely be called to generate completion choices. The plugin.complete would handle all completions, meaning that sub-commands and flags would also be returned by this script. However, this makes it difficult on plugin developers, as they would have to handle all flag and sub-command parsing.

This is when I thought to introduce the completion.yaml configuration file for static completion. I find this approach makes it easier for plugin developers to provide static completion in a declarative fashion.

Static completion solution

When generating the auto-completion script and only then (i.e., when running the helm completion bash|zsh command), Helm will look for a completion.yaml file in each of the configured plugins' directory. If the file is found, it will parse its yaml content, and will create fake Cobra commands and flags to represent the plugins sub-commands and flags. Cobra will then generate the completion script including all plugins sub-commands and flags.

Dynamic completion solution

For dynamic completion, when helm loads a plugin, it will register for it a dynamic completion function, which, when triggered, will set the plugin environment variables and call plugin.complete, if it exists, passing it the entire command-line. That function will then take the output of plugin.complete and use it as completion choices.

Comments on the approach, the naming convention used, the yaml format, or any other topic is very welcomed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions