7.4. 编写插件
您可以使用任何编程语言或脚本编写插件,允许您编写 CLI 命令。插件不一定需要有二进制组件。它可完全依赖操作系统实用程序,如 echo
、sed
或 grep
。或者,它可能依赖 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
上述描述符声明了 great-plugin
插件,它具有一个名为 -f | --flag-name
的标签。它可以通过以下命令调用:
$ oc plugin great-plugin -f value
调用插件时,它将调用 示例二进制文件或
脚本,该二进制文件位于与描述符文件相同的目录中,传递多个参数和环境变量。Accessing Runtime Attributes 部分论述了 示例
命令如何访问标志值和其他运行时上下文。