7.4. 编写插件


您可以使用任何编程语言或脚本编写插件,允许您编写 CLI 命令。插件不一定需要有二进制组件。它可完全依赖操作系统实用程序,如 echosedgrep。或者,它可能依赖 oc 二进制文件。

oc 插件唯一的强要求是 plugin.yaml 描述符文件。此文件至少负责声明注册插件所需的最小属性,并且必须位于 Search Order 部分中指定的其中一个位置。

7.4.1. plugin.yaml Descriptor

描述符文件支持以下属性:

name: "great-plugin"              # REQUIRED: the plug-in command name, to be invoked under 'kubectl'
shortDesc: "great-plugin plug-in" # REQUIRED: the command short description, for help
longDesc: ""                      # the command long description, for help
example: ""                       # command example(s), for help
command: "./example"              # REQUIRED: the command, binary, or script to invoke when running the plug-in
flags:                            # flags supported by the plug-in
  - name: "flag-name"             # REQUIRED for each flag: flag name
    shorthand: "f"                # short version of the flag name
    desc: "example flag"          # REQUIRED for each flag: flag description
    defValue: "extreme"           # default value of the flag
tree:                             # allows the declaration of subcommands
  - ...                           # subcommands support the same set of attributes
Copy to Clipboard Toggle word wrap

上述描述符声明了 great-plugin 插件,它具有一个名为 -f | --flag-name 的标签。它可以通过以下命令调用:

$ oc plugin great-plugin -f value
Copy to Clipboard Toggle word wrap

调用插件时,它将调用 示例二进制文件或 脚本,该二进制文件位于与描述符文件相同的目录中,传递多个参数和环境变量。Accessing Runtime Attributes 部分论述了 示例 命令如何访问标志值和其他运行时上下文。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat