This guide demonstrates how to install and write extensions for [kubectl](/docs/reference/kubectl/kubectl/). By thinking of core `kubectl` commands as essential building blocks for interacting with a Kubernetes cluster, a cluster administrator can think
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`.
This guide demonstrates how to install and write extensions for [kubectl](/docs/reference/kubectl/kubectl/).
By thinking of core `kubectl` commands as essential building blocks for interacting with a Kubernetes cluster,
a cluster administrator can think 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`.
`kubectl` provides a command `kubectl plugin list` that searches your `PATH` for valid plugin executables.
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.
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.
A warning will be included for any files beginning with `kubectl-` that are *not* executable.
A warning will also be included for any valid plugin files that overlap each other's name.
@ -73,7 +76,7 @@ You can use [Krew](https://krew.dev/) to discover and install `kubectl`
`kubectl` allows plugins to add custom create commands of the shape `kubectl create something` by providing a `kubectl-create-something` binary in the `PATH`.
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.
It is currently not possible to create plugins that overwrite existing `kubectl` commands or extend commands other than `create`.
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 attach vm` by naming your plugin `kubectl-attach-vm` will cause that plugin to be ignored.
`kubectl plugin list` shows warnings for any valid plugins that attempt to do this.
@ -106,7 +122,7 @@ It is currently not possible to create plugins that overwrite existing `kubectl`
You can write a plugin in any programming language or script that allows you to write command-line commands.
-->
## 编写 kubectl 插件
## 编写 kubectl 插件 {#writing-kubectl-plugins}
你可以用任何编程语言或脚本编写插件,允许你编写命令行命令。
@ -125,7 +141,7 @@ install the plugin executable somewhere in your `PATH`.
<!--
### Example plugin
-->
### 示例插件
### 示例插件 {#example-plugin}
```
#!/bin/bash
@ -150,7 +166,7 @@ echo "I am a plugin named kubectl-foo"
<!--
### Using a plugin
-->
### 使用插件
### 使用插件 {#using-a-plugin}
<!--
To use a plugin, make the plugin executable:
@ -229,7 +245,7 @@ Additionally, the first argument that is passed to a plugin will always be the f
As seen in the example above, a plugin determines the command path that it will implement based on its filename. Every sub-command in the command path that a plugin targets, is separated by a dash (`-`).
For example, a plugin that wishes to be invoked whenever the command `kubectl foo bar baz` is invoked by the user, would have the filename of `kubectl-foo-bar-baz`.
例如,当用户调用命令 `kubectl foo bar baz` 时,希望调用该命令的插件的文件名为 `kubectl-foo-bar-baz`。
@ -237,7 +253,7 @@ For example, a plugin that wishes to be invoked whenever the command `kubectl fo
<!--
#### Flags and argument handling
-->
#### 参数和标记处理
#### 参数和标记处理 {#flags-and-argument-handling}
<!--
The plugin mechanism does _not_ create any custom, plugin-specific values or environment variables for a plugin process.
@ -318,7 +334,7 @@ As you can see, your plugin was found based on the `kubectl` command specified b
Although the `kubectl` plugin mechanism uses the dash (`-`) in plugin filenames to separate the sequence of sub-commands processed by the plugin, it is still possible to create a plugin
command containing dashes in its commandline invocation by using underscores (`_`) in its filename.
@ -373,7 +389,7 @@ It is possible to have multiple plugins with the same filename in different loca
For example, given a `PATH` with the following value: `PATH=/usr/local/bin/plugins:/usr/local/bin/moreplugins`, a copy of plugin `kubectl-foo` could exist in `/usr/local/bin/plugins` and `/usr/local/bin/moreplugins`,
such that the output of the `kubectl plugin list` command is:
@ -413,7 +429,7 @@ A way to resolve this issue is to ensure that the location of the plugin that yo
There is another kind of overshadowing that can occur with plugin filenames. Given two plugins present in a user's `PATH`: `kubectl-foo-bar` and `kubectl-foo-bar-baz`, the `kubectl` plugin mechanism will always choose the longest possible plugin name for a given user command. Some examples below, clarify this further:
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.