controller-gen CLI

Kubebuilder 使用 controller-gen 来生成实用代码与 Kubernetes YAML。生成行为由 Go 代码中的特殊“标记注释”控制。

controller-gen 由不同的“生成器”(指定生成什么)与“输出规则”(指定输出位置与方式)组成。

二者均通过以标记格式书写的命令行选项进行配置。

例如,下述命令:

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

会生成 CRD 与 RBAC;其中 CRD YAML 被放入 config/crd/bases。RBAC 使用默认输出规则(config/rbac)。该命令会遍历当前目录树中的所有包(遵循 Go ... 通配符的规则)。

生成器(Generators)

每个生成器通过一个 CLI 选项进行配置。你可以在一次 controller-gen 调用中启用多个生成器。

// +webhook
headerFile
string
year
string
generates (partial) {Mutating,Validating}WebhookConfiguration objects.
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.
// +schemapatch
generateEmbeddedObjectMeta
bool
manifests
string
maxDescLen
int
patches existing CRDs with new schemata.

It will generate output for each "CRD Version" (API version of the CRD type itself) , e.g. apiextensions/v1) available.

generateEmbeddedObjectMeta
bool
specifies if any embedded ObjectMeta in the CRD should be generated
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
fileName
string
headerFile
string
roleName
string
year
string
generates ClusterRole objects.
fileName
string
sets the file name for the generated manifest(s). If not set, defaults to "role.yaml".
headerFile
string
specifies the header text (e.g. license) to prepend to generated files.
roleName
string
sets the name of the generated ClusterRole.
year
string
specifies the year to substitute for " YEAR" in the header file.
// +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
allowDangerousTypes
bool
crdVersions
string
deprecatedV1beta1CompatibilityPreserveUnknownFields
bool
generateEmbeddedObjectMeta
bool
headerFile
string
ignoreUnexportedFields
bool
maxDescLen
int
year
string
generates CustomResourceDefinition objects.
allowDangerousTypes
bool
allows types which are usually omitted from CRD generation

because they are not recommended.

Currently the following additional types are allowed when this is true: float32 float64

Left unspecified, the default is false

crdVersions
string
specifies the target API versions of the CRD type itself to

generate. Defaults to v1.

Currently, the only supported value is v1.

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+).

deprecatedV1beta1CompatibilityPreserveUnknownFields
bool
indicates whether

or not we should turn off field pruning for this resource.

Specifies spec.preserveUnknownFields value that is false and omitted by default. This value can only be specified for CustomResourceDefinitions that were created with apiextensions.k8s.io/v1beta1.

The field can be set for compatibility reasons, although strongly discouraged, resource authors should move to a structural OpenAPI schema instead.

See https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-pruning for more information about field pruning and v1beta1 resources compatibility.

generateEmbeddedObjectMeta
bool
specifies if any embedded ObjectMeta in the CRD should be generated
headerFile
string
specifies the header text (e.g. license) to prepend to generated files.
ignoreUnexportedFields
bool
indicates that we should skip unexported fields.

Left unspecified, the default is false.

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.

year
string
specifies the year to substitute for " YEAR" in the header file.
// +applyconfiguration
headerFile
string
generates code containing apply configuration type implementations.
headerFile
string
specifies the header text (e.g. license) to prepend to generated files.

输出规则(Output Rules)

输出规则决定某个生成器如何输出产物。总会存在一个全局“兜底”输出规则(output:<rule>),也可以为某个生成器单独覆盖(output:<generator>:<rule>)。

为简洁起见,下方省略了逐生成器的输出规则写法(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.

其他选项(Other Options)

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

Multiple paths can be specified using "{path1, path2, path3}".

string