kubebuilder 中文文档由云原生社区主导翻译。任何问题可以在这儿提issue。issue模版可以参考这个

controller-gen CLI

KubeBuilder 使用了一个称为 controller-gen 用于生成通用代码和 Kubernetes YAML。 代码和配置的生成规则是被 Go 代码中的一些特殊标记注释控制的。

controller-gen 由不同的“generators”(指定生成什么)和“输出规则”(指定如何以及在何处输出结果)。

两者都是通过指定的命令行参数配置的,更详细的说明见 标记格式化

例如,

controller-gen paths=./... crd:trivialVersions=true rbac:roleName=controller-perms output:crd:artifacts:config=config/crd/bases

生成的 CRD 和 RBAC YAML 文件默认存储在config/crd/bases目录。 RBAC 规则默认输出到(config/rbac)。 主要考虑到当前目录结构中的每个包的关系。 (按照 go ... 的通配符规则)。

生成器

每个不同的生成器都是通过 CLI 选项配置的。controller-gen 一次运行也可以指定多个生成器。

webhook
generates (partial) {Mutating,Validating}WebhookConfiguration objects.
schemapatch
manifests
string
maxDescLen
int
patches existing CRDs with new schemata.

For legacy (v1beta1) single-version CRDs, it will simply replace the global schema. For legacy (v1beta1) multi-version CRDs, and any v1 CRDs, it will replace schemata of existing versions and clear the schema from any versions not specified in the Go code. It will not add new versions, or remove old ones. For legacy multi-version CRDs with identical schemata, it will take care of lifting the per-version schema up to the global schema. It will generate output for each “CRD Version“ (API version of the CRD type itself) , e.g. apiextensions/v1beta1 and apiextensions/v1) available.

manifests
string
contains the CustomResourceDefinition YAML files.
maxDescLen
int
specifies the maximum description length for fields in CRD's OpenAPI schema.

0 indicates drop the description for all fields completely. n indicates limit the description to at most n characters and truncate the description to closest sentence boundary if it exceeds n characters.

rbac
roleName
string
generates ClusterRole objects.
roleName
string
sets the name of the generated ClusterRole.
object
headerFile
string
year
string
generates code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
headerFile
string
specifies the header text (e.g. license) to prepend to generated files.
year
string
specifies the year to substitute for " YEAR" in the header file.
crd
crdVersions
string
maxDescLen
int
preserveUnknownFields
bool
trivialVersions
bool
generates CustomResourceDefinition objects.
crdVersions
string
specifies the target API versions of the CRD type itself to generate. Defaults to v1beta1.

The first version listed will be assumed to be the “default“ version and will not get a version suffix in the output filename. You‘ll need to use “v1“ to get support for features like defaulting, along with an API server that supports it (Kubernetes 1.16+).

maxDescLen
int
specifies the maximum description length for fields in CRD's OpenAPI schema.

0 indicates drop the description for all fields completely. n indicates limit the description to at most n characters and truncate the description to closest sentence boundary if it exceeds n characters.

preserveUnknownFields
bool
indicates whether or not we should turn off pruning.

Left unspecified, it‘ll default to true when only a v1beta1 CRD is generated (to preserve compatibility with older versions of this tool), or false otherwise. It‘s required to be false for v1 CRDs.

trivialVersions
bool
indicates that we should produce a single-version CRD.

Single “trivial-version“ CRDs are compatible with older (pre 1.13) Kubernetes API servers. The storage version‘s schema will be used as the CRD‘s schema. Only works with the v1beta1 CRD version.

输出规则

输出规则配置给定生成器如何输出其结果。 默认是一个全局 fallback 输出规则(指定为 output:<rule>), 另外还有 per-generator 的规则(指定为output:<generator>:<rule>),会覆盖掉 fallback 规则。

为简便起见,每个生成器的输出规则(output:<generator>:<rule>)默认省略。 相当于这里列出的全局备用选项。

output:artifacts
code
string
config
string
outputs artifacts to different locations, depending on whether they're package-associated or not.

Non-package associated artifacts are output to the Config directory, while package-associated ones are output to their package‘s source files‘ directory, unless an alternate path is specified in Code.

code
string
overrides the directory in which to write new code (defaults to where the existing code lives).
config
string
points to the directory to which to write configuration.
output:dir
string
outputs each artifact to the given directory, regardless of if it's package-associated or not.
string
output:none
skips outputting anything.
output:stdout
outputs everything to standard-out, with no separation.

Generally useful for single-artifact outputs.

其他选项

paths
string
represents paths and go-style path patterns to use as package roots.
string