使用kubectl插件将您的想法带入世界
作者: Cornelius Weig(TNG Technology Consulting GmbH)
kubectl
是与Kubernetes交互的最关键工具,必须解决多个用户角色,每个角色都有自己的需求和观点。
One way to make kubectl
do what you need is to build new functionality into kubectl
.
将命令构建到其中的挑战 kubectl
但是,说起来容易做起来难。成为如此重要的基石
Kubernetes, any meaningful change to kubectl
needs to undergo a Kubernetes
增强建议(KEP),其中预先讨论了预期的更改。
When it comes to implementation, you'll find that kubectl
is an ingenious and
复杂的工程。可能需要很长时间才能习惯
完成您想要实现的代码库的过程和样式。下一个
审核过程可能会经历几轮,直到所有方面
Kubernetes维护者的需求-毕竟,他们需要考虑
超过此功能的所有权并从合并之日起对其进行维护。
当一切顺利时,您终于可以欢喜了。您的代码将被发货
下一个Kubernetes版本。好吧,这可能意味着您需要等待
another 3 months to ship your idea in kubectl
if you are unlucky.
所以这是一切顺利的快乐之路。但是有好
reasons why your new functionality may never make it into kubectl
. For one,
kubectl
具有特殊的外观和风格,不会违反该样式
维护者可以接受的。例如,一个交互式命令
produces output with colors would be inconsistent with 的 rest of kubectl
.
此外,当涉及仅对微小比例有用的工具或命令时
of users, 的 maintainers may simply reject your proposal as kubectl
needs to
解决共同需求。
But this doesn’t mean you can’t ship your ideas to kubectl
users.
What if you didn’t have to change kubectl
to add functionality?
这是哪里 kubectl
外挂程式 闪耀。
Since kubectl
v1.12, you can simply
drop executables into your PATH
, which follows 的 naming pattern
kubectl-myplugin
. Then you can execute this plugin as kubectl myplugin
, and
it will just feel like a normal sub-command of kubectl
.
插件使您有机会尝试新体验,例如终端用户界面, 丰富多彩的输出,专业功能或其他创新想法。您可以 您是自己插件的所有者,因此可以发挥创意。
此外,插件为您想要的命令提供了安全的实验空间
propose to kubectl
. By pre-releasing as a plugin, you can push your
功能更快地提供给最终用户,并迅速收集反馈。例如,
the Kubectl调试 建议使用插件
to become a built-in command in kubectl
in a
韩国环保局)。
同时,插件作者可以发布功能并收集
使用插件机制反馈。
如何开始开发插件
如果您已经有了插件的想法,那么如何最好地实现它呢?
首先,您必须问自己是否可以将其实现为包装器
existing kubectl
functionality. If so, writing 的 plugin as a shell script
通常是最好的方法,因为生成的插件很小,
可以跨平台工作,并且具有很高的信任度,因为它不是
compiled.
另一方面,如果插件逻辑很复杂,则使用通用语言
通常更好。此处的典型选择是Go,因为您可以使用
excellent client-go
library to interact with 的 Kubernetes API. The Kubernetes
maintained 样本cli插件
演示一些最佳实践,可以用作新插件的模板
projects.
开发完成后,您只需要将插件发送到
Kubernetes用户。为了获得最佳的插件安装体验和可发现性,
您应该考虑通过
krew 插件管理器。深入了解
discussion about 的 technical details around kubectl
外挂程式, refer to 的
documentation on kubernetes.io.