编写 Krew 插件清单
每个 Krew 插件都有一个“插件清单”,这是一个 YAML 文件,用于描述该插件、如何下载该插件以及如何将该插件安装到计算机上。
插件清单必须与插件同名(例如,插件 foo
有一个名为 foo.yaml
的清单文件)。
强烈建议您查看 示例插件清单,复制一个现有的插件清单,并根据您的需要对其进行改编,而不是从头开始。
示例插件清单
下面是仅支持 Linux 和 macOS 的基于 Bash 的插件的示例清单文件
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
# 'name' must match the filename of the manifest. The name defines how
# the plugin is invoked, for example: `kubectl restart`
name: restart
spec:
# 'version' is a valid semantic version string (see semver.org). Note the prefix 'v' is required.
version: "v0.0.1"
# 'homepage' usually links to the GitHub repository of the plugin
homepage: https://github.com/achanda/kubectl-restart
# 'shortDescription' explains what the plugin does in only a few words
shortDescription: "Restarts a pod with the given name"
description: |
Restarts a pod with the given name. The existing pod
will be deleted and created again, not a true restart.
# 'platforms' specify installation methods for various platforms (os/arch)
# See all supported platforms below.
platforms:
- selector:
matchExpressions:
- key: "os"
operator: "In"
values:
- darwin
- linux
# 'uri' specifies .zip or .tar.gz archive URL of a plugin
uri: https://github.com/achanda/kubectl-restart/archive/v0.0.3.zip
# 'sha256' is the sha256sum of the url (archive file) above
sha256: d7079b79bf4e10e55ded435a2e862efe310e019b6c306a8ff04191238ef4b2b4
# 'files' lists which files should be extracted out from downloaded archive
files:
- from: "kubectl-restart-*/restart.sh"
to: "./restart.sh"
- from: "LICENSE"
to: "."
# 'bin' specifies the path to the the plugin executable among extracted files
bin: restart.sh
文档字段
-
shortDescription:
(必需):为您的插件添加标题。它不应超过几个字(以便在kubectl krew search
输出中正确显示)。避免使用冗余的单词。 -
description:
(必需):解释您的插件在高级别上做了什么,以便帮助用户决定是否要安装该插件。避免在插件中包含命令或选项列表;改为在您的插件中实现-h
/--help
。 -
caveats:
如果您的插件需要额外的步骤才能工作,请使用此字段;例如,如果需要在用户系统上安装某些程序,请在此处列出它们。避免将此字段用作文档字段。在首次安装插件后,会向用户显示
caveats
。
指定插件下载选项
Krew 插件必须打包为 .zip
或 .tar.gz
存档文件,并且应该可以从用户的计算机下载。相关字段如下:
uri
:存档文件 URL(.zip
或.tar.gz
)sha256
:存档文件的 sha256 校验和
platforms:
- uri: https://github.com/foo/bar/archive/v1.2.3.zip
sha256: "29C9C411AF879AB85049344B81B8E8A9FBC1D657D493694E2783A2D0DB240775"
...
指定平台特定说明
Krew 可以安装同一插件到不同的操作系统(例如 windows
、darwin
(macOS)和 linux
)以及不同的架构(例如 amd64
、386
、arm
、arm64
和 ppc64le
)。
所有受支持平台
对插件的支持平台与分发 Krew 本身所用的平台相同。请参阅 发布页面 上的所有受支持平台。
要支持多个平台,您可能需要在插件清单中定义多个 platforms
。selector
字段分别使用关键字 os
和 arch
与操作系统和架构匹配。
示例:与 Linux 或 macOS 平台匹配,任何架构
...
platforms:
- selector:
matchExpressions:
- {key: "os", operator: "In", values: [darwin, linux]}
示例:与 Windows,64 位匹配
platforms:
- selector:
matchLabels:
os: windows
arch: amd64
示例:与 Linux(任何架构)匹配
platforms:
- selector:
matchLabels:
os: linux
os
和 arch
的可能值来自 Go 运行时。运行 go tool dist list
以查看所有可能的平台和架构。
指定要安装的文件
每个操作系统可能需要从存档文件安装不同的文件集。
可以使用 files
字段指定从存档文件中提取文件时应当复制到插件目录的文件。
-
示例:提取所有文件。如果未指定
files:
列表,则默认为files: - from: * to: .
注意:如果适用,最佳做法是完全跳过
files:
列表。 -
示例:复制特定文件。
files: - from: LICENSE to: . - from: bin/foo-windows.exe to: foo.exe
-
示例:使用通配符与多个文件匹配。
files: - from: "*/*.sh" to: "."
此操作保留文件在目标目录中的目录结构。
指定插件可执行文件
每个 platform
字段都需要插件安装目录中插件可执行文件的路径。
例如,如果您插件的可执行文件名为 foo.sh
,请指定
platforms:
- bin: "./foo.sh"
...
安装完成之后,Krew 会创建一个名为 kubectl-foo
的符号链接(在 Windows 上为 kubectl-foo.exe
)指向您的插件可执行文件。符号链接的名称来自插件名称(具体来说是 metadata.name
字段)。
下划线转换注意:如果您的插件名称中包含连字符,Krew 会自动将它们转换为下划线,以便 kubectl 找到您的插件。
例如,如果您的插件名称是
view-logs
,并且您的插件二进制文件名为run.sh
,那么 Krew 会自动创建一个名为kubectl-view_logs
的符号链接。
对 Windows 入口点的说明:目前在 Windows 上仅支持
.exe
入口点,不支持.bat
和.ps1
,请参阅 此项问题。