You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/zh-cn/docs/tasks/extend-kubectl/kubectl-plugins.md
+35-19Lines changed: 35 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,8 +15,10 @@ content_type: task
15
15
16
16
<!-- overview -->
17
17
<!--
18
-
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
19
-
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`.
18
+
This guide demonstrates how to install and write extensions for [kubectl](/docs/reference/kubectl/kubectl/).
19
+
By thinking of core `kubectl` commands as essential building blocks for interacting with a Kubernetes cluster,
20
+
a cluster administrator can think of plugins as a means of utilizing these building blocks to create more complex behavior.
21
+
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.
68
-
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.
70
+
Executing this command causes a traversal of all files in your `PATH`. Any files that are executable, and
71
+
begin with `kubectl-` will show up *in the order in which they are present in your `PATH`* in this command's output.
69
72
A warning will be included for any files beginning with `kubectl-` that are *not* executable.
70
73
A warning will also be included for any valid plugin files that overlap each other's name.
71
74
72
75
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.
102
+
It is currently not possible to create plugins that overwrite existing `kubectl` commands or extend commands other than `create`.
103
+
For example, creating a plugin `kubectl-version` will cause that plugin to never be executed, as the existing `kubectl version`
104
+
command will always take precedence over it.
105
+
Due to this limitation, it is also *not* possible to use plugins to add new subcommands to existing `kubectl` commands.
106
+
For example, adding a subcommand `kubectl attach vm` by naming your plugin `kubectl-attach-vm` will cause that plugin to be ignored.
91
107
92
108
`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`
106
122
107
123
You can write a plugin in any programming language or script that allows you to write command-line commands.
108
124
-->
109
-
## 编写 kubectl 插件
125
+
## 编写 kubectl 插件 {#writing-kubectl-plugins}
110
126
111
127
你可以用任何编程语言或脚本编写插件,允许你编写命令行命令。
112
128
@@ -125,7 +141,7 @@ install the plugin executable somewhere in your `PATH`.
125
141
<!--
126
142
### Example plugin
127
143
-->
128
-
### 示例插件
144
+
### 示例插件 {#example-plugin}
129
145
130
146
```
131
147
#!/bin/bash
@@ -150,7 +166,7 @@ echo "I am a plugin named kubectl-foo"
150
166
<!--
151
167
### Using a plugin
152
168
-->
153
-
### 使用插件
169
+
### 使用插件 {#using-a-plugin}
154
170
155
171
<!--
156
172
To use a plugin, make the plugin executable:
@@ -229,15 +245,15 @@ Additionally, the first argument that is passed to a plugin will always be the f
229
245
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 (`-`).
230
246
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`。
236
252
237
253
<!--
238
254
#### Flags and argument handling
239
255
-->
240
-
#### 参数和标记处理
256
+
#### 参数和标记处理 {#flags-and-argument-handling}
241
257
242
258
<!--
243
259
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
318
334
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
319
335
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
373
389
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`,
374
390
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
413
429
414
430
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.
0 commit comments