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 ... 通配符的正常规则)。
发电机
通过 CLI 选项配置每个不同的生成器。可在单次调用 controller-gen 时使用多个生成器。
- // +applyconfiguration
-
- externalApplyConfigurations
- string
- headerFile
- string
generates code containing apply configuration type implementations.
- externalApplyConfigurations
- string
provides mappings between external types and their applyconfiguration packages.
用于引用输入 Go 结构体所引用的外部类型的 apply configuration 类型。每个条目格式应为:<package>.<TypeName>@<applyconfiguration-package>
例如,引用 corev1.LocalObjectReference 的 apply configuration:k8s.io/api/core/v1.LocalObjectReference@k8s.io/client-go/applyconfigurations/core/v1
- headerFile
- string
specifies the header text (e.g. license) to prepend to generated files.
- // +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
因为不推荐使用它们。
当为 true 时,目前还允许以下额外类型:float32 float64
未指定时,默认为 false
- crdVersions
- string
specifies the target API versions of the CRD type itself to
生成。默认为 v1。
目前唯一支持的值是 v1。
列出的第一个版本将被视为 “默认” 版本,输出文件名不会带版本后缀。
需要使用 “v1” 才能获得 defaulting 等特性支持,并需要支持它的 API 服务器(Kubernetes 1.16+)。
- deprecatedV1beta1CompatibilityPreserveUnknownFields
- bool
indicates whether
是否应关闭此资源的字段裁剪。
指定默认为 false 且省略的 spec.preserveUnknownFields 值。此值仅可为用
apiextensions.k8s.io/v1beta1创建的 CustomResourceDefinition 指定。可为兼容性设置该字段,但强烈不建议;资源作者应改用结构化 OpenAPI schema。
关于字段裁剪与 v1beta1 资源兼容性的更多信息,参见 https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-pruning。
- 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.
未指定时,默认为 false。
- maxDescLen
- int
specifies the maximum description length for fields in CRD's OpenAPI schema.
0 表示完全丢弃所有字段的描述。n 表示将描述限制为最多 n 个字符,若超过 n 个字符则截断到最近的句子边界。
- 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 方法实现。
- 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.
- // +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.
- // +schemapatch
-
- generateEmbeddedObjectMeta
- bool
- manifests
- string
- maxDescLen
- int
patches existing CRDs with new schemata.
它会为每个可用的 “CRD Version”(CRD 类型本身的 API 版本,例如 apiextensions/v1)生成输出。
- 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 表示完全丢弃所有字段的描述。n 表示将描述限制为最多 n 个字符,若超过 n 个字符则截断到最近的句子边界。
- // +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.
输出规则
输出规则配置了给定生成器如何输出其结果。始终有一个全局的“回退“输出规则(指定为 output:<rule>),加上每个生成器的覆盖规则(指定为 output:<generator>:<rule>)。
为简洁起见,下面省略了每个生成器的输出规则(output:<generator>:<rule>)。它们等同于此处列出的全局后备选项。
- // +output:stdout
-
outputs everything to standard-out, with no separation.
通常适用于单产物输出。
- // +output:none
-
skips outputting anything.
- // +output:dir
-
- string
outputs each artifact to the given directory, regardless
以及它是否与 package 关联。
- string
- // +output:artifacts
-
- code
- string
- config
- string
outputs artifacts to different locations, depending on
无论它们是否与 package 关联。
非包关联产物输出到 Config 目录,包关联产物输出到其包源文件目录,除非在 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.
其他选项
- // +paths
-
- string
represents the package roots that controller-gen uses as input
用于代码生成。
可使用花括号形式或分号分隔列表指定多个路径。
每个路径会被分类为文件系统路径或 Go 包/模块名,遵循与 “go” 命令相同的规则。若路径为绝对路径,或以 “.” 或 “..” 开头,则视为文件系统路径。所有其他路径视为 Go 包或模块名。有关路径分类的更多信息,请运行 “go help packages”。
Examples:
// brace-delimited form (preferred when specifying multiple paths) paths="{./api/..., ./pkg/...}" // semicolon-separated form paths=./api/...;./pkg/... // filesystem paths (absolute or beginning with . or ..) paths=./... paths=./api/v1 paths=/home/user/project/api // Go package or module names paths=sigs.k8s.io/controller-tools/pkg/... paths=example.com/myproject/api/v1当文件系统路径以 “…” 结尾时,controller-gen 会跨越模块边界,并包含子目录中发现的任何嵌套 Go 模块中的包(包含自己 go.mod 文件的目录)。这在多 Go 模块仓库中、希望单次调用跨所有模块运行生成时很有用。此行为不适用于 Go 包或模块名,后者遵循标准 Go 模块规则并停留在单个模块内。
Example:
// given this layout: // myrepo/go.mod // myrepo/api/v1/ // myrepo/subproject/go.mod (nested module) // myrepo/subproject/api/ // this includes packages from both modules: paths=./... // this includes only the root module: paths=sigs.k8s.io/myrepo/...要将生成限制到单个 module,请使用 Go package 或 module 名称而非文件系统路径。
- string