Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

CRD 生成

这些标记描述了如何根据一系列 Go 类型和包构建自定义资源定义。实际验证模式的生成由验证标记描述。

请参见生成 CRD以获取示例。

// +versionName
string
overrides the API group version for this package (defaults to the package name). Example: +versionName=v1beta1
string
// +kubebuilder:unservedversion
does not serve this version.

若需要放弃某个版本以支持更新版本,这很有用。若该版本是 storage version,仍会存储在 etcd 中,但不会通过 API 提供服务。

Example:

// +kubebuilder:unservedversion
type MyCRDv1alpha1 struct {
    // This version is no longer served
}
// +kubebuilder:subresource:status
enables the "/status" subresource on a CRD.

status 子资源允许你独立于资源其余 spec 更新 status 字段,并在更新根对象时防止更新 status 子资源。这有助于将用户提供的 spec 与系统提供的 status 分开。

Example:

// +kubebuilder:subresource:status
type MyCRD struct {
    metav1.TypeMeta
    metav1.ObjectMeta
    Spec   MyCRDSpec
    Status MyCRDStatus
}
// +kubebuilder:subresource:scale
selectorpath
string
specpath
string
statuspath
string
enables the "/scale" subresource on a CRD.

scale 子资源允许你对 CRD 使用 kubectl scale 和 HorizontalPodAutoscaler。

Example:

// +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.selector
type MyCRD struct {
    metav1.TypeMeta
    metav1.ObjectMeta
    Spec MyCRDSpec
    Status MyCRDStatus
}
selectorpath
string
specifies the jsonpath to the pod label selector field for the scale's status.

selector 字段必须是选择器的_字符串_形式(序列化形式)。设置 pod 标签选择器是你的类型与 HorizontalPodAutoscaler 配合工作的必要条件。通常为 .status.selector。

specpath
string
specifies the jsonpath to the replicas field for the scale's spec.

此处存储期望副本数(通常为 .spec.replicas)。

statuspath
string
specifies the jsonpath to the replicas field for the scale's status.

此处存储实际副本数(通常为 .status.replicas)。

// +kubebuilder:storageversion
marks this version as the "storage version" for the CRD for conversion.

当为 CRD 启用 conversion(即不是 trivial-versions/单版本 CRD)时,会将一个版本设为存储在 etcd 中的 “storage version”。尝试存储任何其他版本都会通过 conversion webhook 转换为 storage version。

Example:

// +kubebuilder:storageversion
type MyCRDv2 struct {
    metav1.TypeMeta
    metav1.ObjectMeta
    Spec MyCRDSpec
}
// +kubebuilder:skipversion
removes the particular version of the CRD from the CRDs spec.

若需要跳过为「内部」资源版本生成和列出版本条目,这很有用;使用 Kubernetes 上游 conversion-gen 工具时通常会存在此类版本。

Example:

// +kubebuilder:skipversion
type MyCRDInternal struct {
    // internal version not served by API
}
// +kubebuilder:skip
don't consider this package as an API version. Use this to exclude internal or helper packages from CRD generation.
// +kubebuilder:selectablefield
JSONPath
string
adds a field that may be used with field selectors.

字段选择器允许用户在列出时按字段值过滤资源。例如:kubectl get mycrds --field-selector status.phase=Running。

Example:

// +kubebuilder:selectablefield:JSONPath=".status.phase"
type MyCRD struct {
    metav1.TypeMeta
    metav1.ObjectMeta
    Status MyCRDStatus
}
JSONPath
string
specifies the jsonpath expression which is used to produce a field selector value.

路径相对于资源根。示例:.status.phase。

// +kubebuilder:resource
categories
string
path
string
scope
string
shortName
string
singular
string
configures naming and scope for a CRD.

Example:

// +kubebuilder:resource:path=mycrdplural,singular=mycrdsingular,shortName=mc;mcrd,categories=all,scope=Namespaced
type MyCRD struct {
    metav1.TypeMeta
    metav1.ObjectMeta
}
categories
string
specifies which group aliases this resource is part of.

Group alias 用于一次处理一组资源。最常见的是 “all”,覆盖约三分之一的 Kubernetes 基础资源,通常用于「面向用户」的资源。这允许用户运行 kubectl get all 这类命令时包含你的 CRD。

path
string
specifies the plural "resource" for this CRD.

它通常对应 Kind 的复数小写形式。例如,若 Kind 为 “MyCRD”,路径可能是 “mycrds”。参见 https://book.kubebuilder.io/cronjob-tutorial/gvks.html。

scope
string
overrides the scope of the CRD (Cluster vs Namespaced).

Scope 默认为 “Namespaced”。集群作用域(“Cluster”)资源不存在于命名空间中,可从集群任意位置访问。

shortName
string
specifies aliases for this CRD.

短名称常用于反复操作同一资源的场景。例如 “rs” 表示 “replicaset”,“crd” 表示 customresourcedefinition。可用分号分隔指定多个短名称。

singular
string
overrides the singular form of your resource.

单数形式否则从复数(path)默认推导。用于 API 响应与 kubectl 输出。

// +kubebuilder:printcolumn
JSONPath
string
description
string
format
string
name
string
priority
int
type
string
adds a column to "kubectl get" output for this CRD.

这允许你自定义用户对 CRD 运行 kubectl get 时显示的列。

Example:

// +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.phase`
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
type MyCRD struct {
    metav1.TypeMeta
    metav1.ObjectMeta
}
JSONPath
string
specifies the jsonpath expression used to extract the value of the column.

路径相对于资源根。示例:.status.phase 或 .spec.replicas。

description
string
specifies optional help text for this column.

显示行为取决于客户端;参见 Kubernetes API 文档中的 CustomResourceColumnDefinition。

format
string
specifies the format of the column.

可以是与类型对应的任意 OpenAPI 数据格式,列表见 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types。

name
string
specifies the name of the column as it will appear in the header.
priority
int
indicates how important it is that this column be displayed.

优先级较低(数字_较大_)的列会在终端宽度过小时隐藏。优先级为 0 的列始终显示。

type
string
indicates the type of the column.

可以是 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types 中列出的任意 OpenAPI 数据类型。常见值:“string”、“integer”、“number”、“boolean”、“date”。

// +kubebuilder:metadata
annotations
string
labels
string
configures the additional annotations or labels for this CRD.

例如为带 Kubernetes groups 的 CRD 添加注解 “api-approved.kubernetes.io”,或为需要 CA 注入的 CRD 添加注解 “cert-manager.io/inject-ca-from-secret”。

Example:

// +kubebuilder:metadata:annotations="api-approved.kubernetes.io/v1=https://github.com/myorg/myrepo/pull/123"
// +kubebuilder:metadata:labels="app=myapp"
type MyCRD struct {
    metav1.TypeMeta
    metav1.ObjectMeta
}
annotations
string
will be added into the annotations of this CRD.

格式:“key=value”。可通过重复标记指定多个注解。

labels
string
will be added into the labels of this CRD.

格式:“key=value”。可通过重复标记指定多个标签。

// +kubebuilder:externalDocs
description
string
url
string
specifies external documentation for this field or type.

url 为必填且必须是有效 URL。description 可选,用于简短描述外部文档。

description
string
is a short description of the target documentation.
url
string
specifies the URL for the target documentation.
// +kubebuilder:externalDocs
description
string
url
string
specifies external documentation for this field or type.

url 为必填且必须是有效 URL。description 可选,用于简短描述外部文档。

description
string
is a short description of the target documentation.
url
string
specifies the URL for the target documentation.
// +kubebuilder:deprecatedversion
warning
string
marks this version as deprecated.

已弃用版本在使用时会显示警告消息。这对于告知用户应迁移到更新版本很有用。

Example:

// +kubebuilder:deprecatedversion:warning="v1alpha1 is deprecated; use v1 instead"
type MyCRDv1alpha1 struct {
    metav1.TypeMeta
    metav1.ObjectMeta
}
warning
string
message to be shown on the deprecated version.

当用户与已弃用版本交互时显示此消息。

// +k8s:enum
indicates that the given type is an enum; all const values of this type are considered values in the enum
// +groupName
string
specifies the API group name for this package. Example: +groupName=mygroup.example.com
string