Skip to content

Commit 7abf2ad

Browse files
committed
update plugin documentation under docs>tasks>extend-kubectl
1 parent c038d89 commit 7abf2ad

File tree

1 file changed

+86
-69
lines changed

1 file changed

+86
-69
lines changed

content/en/docs/tasks/extend-kubectl/kubectl-plugins.md

Lines changed: 86 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
title: Extend kubectl with plugins
33
reviewers:
4-
- fabianofranz
4+
- juanvallejo
5+
- soltysh
56
description: With kubectl plugins, you can extend the functionality of the kubectl command by adding new subcommands.
67
content_template: templates/task
78
---
@@ -10,131 +11,147 @@ content_template: templates/task
1011

1112
{{< feature-state state="alpha" >}}
1213

13-
This guide shows you how to install and write extensions for [kubectl](/docs/user-guide/kubectl/). Usually called *plugins* or *binary extensions*, this feature allows you to extend the default set of commands available in `kubectl` by adding new subcommands to perform new tasks and extend the set of features available in the main distribution of `kubectl`.
14+
This guide shows you how to install and write extensions for [kubectl](/docs/user-guide/kubectl/). By thinking of core `kubectl` commands as essential building blocks for interacting with a Kubernetes cluster, we can think
15+
of plugins as a means of utilizing these building blocks to create more complex behavior. Plugins extend `kubectl` with new sub-commands, allowing for new and custom features not included in the main distribution of `kubectl`.
1416

1517
{{% /capture %}}
1618

1719
{{% capture prerequisites %}}
1820

1921
You need to have a working `kubectl` binary installed.
2022
{{< note >}}
21-
**Note:** Plugins were officially introduced as an alpha feature in the v1.8.0 release. So, while some parts of the plugins feature were already available in previous versions, a `kubectl` version of 1.8.0 or later is recommended.
23+
**Note:** Plugins were officially introduced as an alpha feature in the v1.8.0 release. They have been re-worked in the v1.12.0 release to support a wider range of use-cases. So, while some parts of the plugins feature were already available in previous versions, a `kubectl` version of 1.12.0 or later is recommended if you are following these docs.
2224
{{< /note >}}
2325

24-
Until a GA version is released, plugins will only be available under the `kubectl plugin` subcommand.
26+
Until a GA version is released, plugins should be considered unstable, and their underlying mechanism is prone to change.
2527

2628
{{% /capture %}}
2729

2830
{{% capture steps %}}
2931

3032
## Installing kubectl plugins
3133

32-
A plugin is nothing more than a set of files: at least a **plugin.yaml** descriptor, and likely one or more binary, script, or assets files. To install a plugin, copy those files to one of the locations in the filesystem where `kubectl` searches for plugins.
34+
A plugin is nothing more than a standalone executable file, whose name begins with `kubectl-`. To install a plugin, simply move this executable file to anywhere on your PATH.
3335

3436
{{< note >}}
35-
**Note:** Kubernetes does not provide a package manager or anything similar to install or update plugins. It is your responsibility to place the plugin files in the correct location. We recommend that each plugin be stored in its own directory so that installing a plugin distributed as a compressed file is as simple as extracting it to one of the locations specified in the [Plugin loader](#plugin-loader) section.
37+
**Note:** Kubernetes does not provide a package manager or anything similar to install or update plugins. It is your responsibility to ensure that plugin executables have a filename that begins with `kubectl-`, and that they are placed somewhere on your PATH.
3638
{{< /note >}}
3739

38-
### Plugin loader
40+
### Discovering plugins
3941

40-
The plugin loader is responsible for searching plugin files in the filesystem locations specified below, and checking if the plugin provides the minimum amount of information required for it to run. Files placed in the right location that don't provide the minimum amount of information, for example an incomplete *plugin.yaml* descriptor, are ignored.
42+
`kubectl` provides a command `kubectl plugin list` that searches your PATH for valid plugin executables.
43+
Executing this command causes a traversal of all files in your PATH. Any files that are executable, and begin with `kubectl-` will show up *in the order in which they are present in your PATH* in this command's output.
44+
A warning will be included for any files beginning with `kubectl-` that are *not* executable.
45+
A warning will also be included for any valid plugin files that overlap each other's name.
4146

42-
#### Search order
47+
#### Limitations
4348

44-
The plugin loader uses the following search order:
49+
It is currently not possible to create plugins that overwrite existing `kubectl` commands. For example, creating a plugin `kubectl-version` will cause that plugin to never be executed, as the existing `kubectl version` command will always take precedence over it. Due to this limitation, it is also *not* possible to use plugins to add new subcommands to existing `kubectl` commands. For example, adding a subcommand `kubectl create foo` by naming your plugin `kubectl-create-foo` will cause that plugin to be ignored. Warnings will appear under the output of `kubectl plugin list` for any valid plugins that attempt to do this.
4550

46-
1. `${KUBECTL_PLUGINS_PATH}` If specified, the search stops here.
47-
2. `${XDG_DATA_DIRS}/kubectl/plugins`
48-
3. `~/.kube/plugins`
49-
50-
If the `KUBECTL_PLUGINS_PATH` environment variable is present, the loader uses it as the only location to look for plugins.
51-
The `KUBECTL_PLUGINS_PATH` environment variable is a list of directories. In Linux and Mac, the list is colon-delimited. In
52-
Windows, the list is semicolon-delimited.
51+
## Writing kubectl plugins
5352

54-
If `KUBECTL_PLUGINS_PATH` is not present, the loader searches these additional locations:
53+
You can write a plugin in any programming language or script that allows you to write command-line commands.
5554

56-
First, one or more directories specified according to the
57-
[XDG System Directory Structure](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
58-
specification. Specifically, the loader locates the directories specified by the `XDG_DATA_DIRS` environment variable,
59-
and then searches `kubectl/plugins` directory inside of those.
60-
If `XDG_DATA_DIRS` is not specified, it defaults to `/usr/local/share:/usr/share`.
55+
There is no plugin installation or pre-loading required. Plugin executables receive the inherited environment from the `kubectl` binary.
56+
A plugin determines which command path it wishes to implement based on its name. For example, a plugin wanting to provide a new command
57+
`kubectl foo`, would simply be named `kubectl-foo`, and live somewhere in the user's PATH.
6158

62-
Second, the `plugins` directory under the user's kubeconfig dir. In most cases, this is `~/.kube/plugins`.
59+
### Example plugin
6360

64-
```shell
65-
# Loads plugins from both /path/to/dir1 and /path/to/dir2
66-
KUBECTL_PLUGINS_PATH=/path/to/dir1:/path/to/dir2 kubectl plugin -h
61+
```
62+
#!/bin/bash
63+
64+
# optional argument handling
65+
if [[ "$1" == "version" ]]
66+
then
67+
echo "1.0.0"
68+
exit 0
69+
fi
70+
71+
# optional argument handling
72+
if [[ "$1" == "config" ]]
73+
then
74+
echo $KUBECONFIG
75+
exit 0
76+
fi
77+
78+
echo "I am a plugin named kubectl-foo"
6779
```
6880

69-
## Writing kubectl plugins
81+
### Using a plugin
7082

71-
You can write a plugin in any programming language or script that allows you to write command-line commands.
72-
A plugin does not necessarily need to have a binary component. It could rely entirely on operating system utilities
73-
like `echo`, `sed`, or `grep`. Or it could rely on the `kubectl` binary.
83+
To use the above plugin, simply make it executable:
7484

75-
The only strong requirement for a `kubectl` plugin is the `plugin.yaml` descriptor file. This file is responsible for declaring at least the minimum attributes required to register a plugin and must be located under one of the locations specified in the [Search order](#search-order) section.
85+
```
86+
sudo chmod +x ./kubectl-foo
87+
```
7688

77-
### The plugin.yaml descriptor
89+
and place it anywhere in your PATH:
7890

79-
The descriptor file supports the following attributes:
91+
```
92+
sudo mv ./kubectl-foo /usr/local/bin
93+
```
94+
95+
You may now invoke your plugin as a `kubectl` command:
8096

8197
```
82-
name: "targaryen" # REQUIRED: the plugin command name, to be invoked under 'kubectl'
83-
shortDesc: "Dragonized plugin" # REQUIRED: the command short description, for help
84-
longDesc: "" # the command long description, for help
85-
example: "" # command example(s), for help
86-
command: "./dracarys" # REQUIRED: the command, binary, or script to invoke when running the plugin
87-
flags: # flags supported by the plugin
88-
- name: "heat" # REQUIRED for each flag: flag name
89-
shorthand: "h" # short version of the flag name
90-
desc: "Fire heat" # REQUIRED for each flag: flag description
91-
defValue: "extreme" # default value of the flag
92-
tree: # allows the declaration of subcommands
93-
- ... # subcommands support the same set of attributes
98+
$ kubectl foo
99+
I am a plugin named kubectl-foo
94100
```
95101

96-
The preceding descriptor declares the `kubectl plugin targaryen` plugin, which has one flag named `-h | --heat`.
97-
When the plugin is invoked, it calls the `dracarys` binary or script, which is located in the same directory as the descriptor file. The [Accessing runtime attributes](#accessing-runtime-attributes) section describes how the `dracarys` command accesses the flag value and other runtime context.
102+
All args and flags are passed as-is to the executable:
98103

99-
### Recommended directory structure
104+
```
105+
$ kubectl foo version
106+
1.0.0
107+
```
100108

101-
It is recommended that each plugin has its own subdirectory in the filesystem, preferably with the same name as the plugin command. The directory must contain the `plugin.yaml` descriptor and any binary, script, asset, or other dependency it might require.
109+
All environment variables are also passed as-is to the executable:
102110

103-
For example, the directory structure for the `targaryen` plugin could look like this:
111+
```bash
112+
$ export KUBECONFIG=~/.kube/config
113+
$ kubectl foo config
114+
/home/<user>/.kube/config
104115

116+
$ KUBECONFIG=/etc/kube/config kubectl foo config
117+
/etc/kube/config
105118
```
106-
~/.kube/plugins/
107-
└── targaryen
108-
├── plugin.yaml
109-
└── dracarys
110-
```
111119

112-
### Accessing runtime attributes
120+
Additionally, the first argument that is passed to a plugin will always be the full path to the location where it was invoked (`$0` would equal `/usr/local/bin/kubectl-foo` in our example above).
121+
122+
### Checking for plugin warnings
113123

114-
In most use cases, the binary or script file you write to support the plugin must have access to some contextual information provided by the plugin framework. For example, if you declared flags in the descriptor file, your plugin must have access to the user-provided flag values at runtime. The same is true for global flags. The plugin framework is responsible for doing that, so plugin writers don't need to worry about parsing arguments. This also ensures the best level of consistency between plugins and regular `kubectl` commands.
124+
You can use the aforementioned `kubectl plugin list` command to ensure that your plugin is visible by `kubectl`, and verify that there are no warnings preventing it from being called as a `kubectl` command.
115125

116-
Plugins have access to runtime context attributes through environment variables. So to access the value provided through a flag, for example, just look for the value of the proper environment variable using the appropriate function call for your binary or script.
126+
```
127+
$ kubectl plugin list
128+
The following kubectl-compatible plugins are available:
117129
118-
The supported environment variables are:
130+
test/fixtures/pkg/kubectl/plugins/kubectl-foo
131+
/usr/local/bin/kubectl-foo
132+
- warning: /usr/local/bin/kubectl-foo is overshadowed by a similarly named plugin: test/fixtures/pkg/kubectl/plugins/kubectl-foo
133+
plugins/kubectl-invalid
134+
- warning: plugins/kubectl-invalid identified as a kubectl plugin, but it is not executable
119135
120-
* `KUBECTL_PLUGINS_CALLER`: The full path to the `kubectl` binary that was used in the current command invocation.
121-
As a plugin writer, you don't have to implement logic to authenticate and access the Kubernetes API. Instead, you can invoke `kubectl` to obtain the information you need, through something like `kubectl get --raw=/apis`.
136+
error: 2 plugin warnings were found
137+
```
122138

123-
* `KUBECTL_PLUGINS_CURRENT_NAMESPACE`: The current namespace that is the context for this call. This is the actual namespace to be used, meaning it was already processed in terms of the precedence between what was provided through the kubeconfig, the `--namespace` global flag, environment variables, and so on.
139+
### Using the command line runtime package
124140

125-
* `KUBECTL_PLUGINS_DESCRIPTOR_*`: One environment variable for every attribute declared in the `plugin.yaml` descriptor.
126-
For example, `KUBECTL_PLUGINS_DESCRIPTOR_NAME`, `KUBECTL_PLUGINS_DESCRIPTOR_COMMAND`.
141+
As part of the plugin mechanism update in the v1.12.0 release, an additional set of utilities have been made available to plugin authors. These utilities
142+
exist under the [k8s.io/cli-runtime](https://github.com/kubernetes/cli-runtime) repository, and can be used by plugins written in Go to parse and update
143+
a user's KUBECONFIG file, obtain REST clients to talk to the API server, and automatically bind flags associated with configuration and printing.
127144

128-
* `KUBECTL_PLUGINS_GLOBAL_FLAG_*`: One environment variable for every global flag supported by `kubectl`.
129-
For example, `KUBECTL_PLUGINS_GLOBAL_FLAG_NAMESPACE`, `KUBECTL_PLUGINS_GLOBAL_FLAG_V`.
145+
Plugins *do not* have to be written in Go in order to be recognized as valid plugins by `kubectl`, but they do have to use Go in order to take advantage of
146+
the tools and utilities in the CLI Runtime repository.
130147

131-
* `KUBECTL_PLUGINS_LOCAL_FLAG_*`: One environment variable for every local flag declared in the `plugin.yaml` descriptor. For example, `KUBECTL_PLUGINS_LOCAL_FLAG_HEAT` in the preceding `targaryen` example.
148+
See the [Sample CLI Plugin](https://github.com/kubernetes/sample-cli-plugin) for an example usage of the tools provided in the CLI Runtime repo.
132149

133150
{{% /capture %}}
134151

135152
{{% capture whatsnext %}}
136153

137-
* Check the repository for [some more examples](https://github.com/kubernetes/kubernetes/tree/master/pkg/kubectl/plugins/examples) of plugins.
154+
* Check the Sample CLI Plugin repository for [a detailed example](https://github.com/kubernetes/sample-cli-plugin) of a plugin written in Go.
138155
* In case of any questions, feel free to reach out to the [CLI SIG team](https://github.com/kubernetes/community/tree/master/sig-cli).
139156
* Binary plugins is still an alpha feature, so this is the time to contribute ideas and improvements to the codebase. We're also excited to hear about what you're planning to implement with plugins, so [let us know](https://github.com/kubernetes/community/tree/master/sig-cli)!
140157

0 commit comments

Comments
 (0)