controller-gen 命令行工具

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 ... 通配符的正常规则)。

发电机

每个不同的生成器通过命令行选项进行配置。多个生成器可以在一次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
headerFile
string
roleName
string
year
string
generates ClusterRole objects.
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.

输出规则

输出规则配置了给定生成器如何输出其结果。始终有一个全局的“回退“输出规则(指定为 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.

其他选项

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

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

string