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 验证

这些标记会修改 CRD 验证模式在其修改的类型和字段上的生成方式。每个标记大致对应于一个 OpenAPI/JSON 模式选项。

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

// +required
specifies that this field is required.
// +optional
specifies that this field is optional.
// +nullable
marks this field as allowing the "null" value.

通常不必如此,但对自定义序列化可能有帮助。

Example:

// +nullable
Description *string
// +kubebuilder:validation:items:XValidation
fieldPath
string
message
string
messageExpression
string
optionalOldSelf
bool
reason
string
rule
string
for array items marks a field as requiring a value for which a given

表达式求值为 true。

此标记可重复以指定多个表达式,所有这些表达式都必须求值为 true。

CEL 表达式可使用:self(当前值)和 oldSelf(先前值;仅在更新时,创建时为 null)。规则作用域为其在 schema 中出现的位置;self 即为该值。

Examples:

// Basic field validation using self
// +kubebuilder:validation:XValidation:rule="self.minReplicas <= self.replicas && self.replicas <= self.maxReplicas",message="replicas must be between minReplicas and maxReplicas"

// Validation with custom reason
// +kubebuilder:validation:XValidation:rule="self.x <= self.maxX",message="x cannot be greater than maxX",reason="FieldValueInvalid"

// Immutability check using oldSelf (e.g. on a field so self/oldSelf are that field's value)
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="field is immutable"

// Validation with field path for better error reporting
// +kubebuilder:validation:XValidation:rule="self >= 1",message="value must be at least 1",fieldPath=".spec.replicas"
fieldPath
string
is the path to the field that failed validation (for clearer error messages).

示例:FieldPath=“.spec.replicas”

message
string
is the text shown when validation fails.

若未设置,默认为 “failed rule: {Rule}”。不得包含换行。API 服务器有长度限制(例如 256 个字符)。完整细节见 k8s.io/apiextensions-apiserver 中的 ValidationRule。示例:Message=“replicas must be between minReplicas and maxReplicas”

messageExpression
string
is a CEL expression that returns the message shown when validation fails.

可以设置 Message 或 MessageExpression,不能同时设置。MessageExpression 必须返回字符串。若两者都设置,使用 MessageExpression。若都未设置,使用默认消息。表达式可使用与 Rule 相同的变量(例如 self、oldSelf)。结果不得包含换行,并受与 Message 相同的消息长度限制。示例:MessageExpression=“’replicas must be between ’ + string(self.minReplicas) + ’ and ’ + string(self.maxReplicas)”

optionalOldSelf
bool
OptionalOldSelf, when true, runs the rule on create and on update (even when there is no old value).

为 true 时,oldSelf 可能缺失:使用 oldSelf.hasValue() 检查,并用 oldSelf.value() 取值。规则始终运行;使用 oldSelf 前必须在规则中检查 oldSelf.hasValue()。仅当规则使用 oldSelf 时可设置。参见 k8s.io/apiextensions-apiserver 中的 ValidationRule。示例:OptionalOldSelf=true

reason
string
is a short code for why validation failed, returned to API callers.

支持的值:“FieldValueInvalid”、“FieldValueForbidden”、“FieldValueRequired”、“FieldValueDuplicate”。未设置时默认为 “FieldValueInvalid”。示例:Reason=“FieldValueInvalid”

rule
string
is the CEL expression that must evaluate to true.

作用域为该 XValidation 在 schema 中的位置;self 即为该值。示例:Rule=“self.minReplicas <= self.replicas && self.replicas <= self.maxReplicas”

// +kubebuilder:validation:items:XValidation
fieldPath
string
message
string
messageExpression
string
optionalOldSelf
bool
reason
string
rule
string
for array items marks a field as requiring a value for which a given

表达式求值为 true。

此标记可重复以指定多个表达式,所有这些表达式都必须求值为 true。

CEL 表达式可使用:self(当前值)和 oldSelf(先前值;仅在更新时,创建时为 null)。规则作用域为其在 schema 中出现的位置;self 即为该值。

Examples:

// Basic field validation using self
// +kubebuilder:validation:XValidation:rule=&#34;self.minReplicas &lt;= self.replicas &amp;&amp; self.replicas &lt;= self.maxReplicas&#34;,message=&#34;replicas must be between minReplicas and maxReplicas&#34;

// Validation with custom reason
// +kubebuilder:validation:XValidation:rule=&#34;self.x &lt;= self.maxX&#34;,message=&#34;x cannot be greater than maxX&#34;,reason=&#34;FieldValueInvalid&#34;

// Immutability check using oldSelf (e.g. on a field so self/oldSelf are that field&#39;s value)
// +kubebuilder:validation:XValidation:rule=&#34;self == oldSelf&#34;,message=&#34;field is immutable&#34;

// Validation with field path for better error reporting
// +kubebuilder:validation:XValidation:rule=&#34;self &gt;= 1&#34;,message=&#34;value must be at least 1&#34;,fieldPath=&#34;.spec.replicas&#34;
fieldPath
string
is the path to the field that failed validation (for clearer error messages).

示例:FieldPath=“.spec.replicas”

message
string
is the text shown when validation fails.

若未设置,默认为 “failed rule: {Rule}”。不得包含换行。API 服务器有长度限制(例如 256 个字符)。完整细节见 k8s.io/apiextensions-apiserver 中的 ValidationRule。示例:Message=“replicas must be between minReplicas and maxReplicas”

messageExpression
string
is a CEL expression that returns the message shown when validation fails.

可以设置 Message 或 MessageExpression,不能同时设置。MessageExpression 必须返回字符串。若两者都设置,使用 MessageExpression。若都未设置,使用默认消息。表达式可使用与 Rule 相同的变量(例如 self、oldSelf)。结果不得包含换行,并受与 Message 相同的消息长度限制。示例:MessageExpression=“’replicas must be between ’ + string(self.minReplicas) + ’ and ’ + string(self.maxReplicas)”

optionalOldSelf
bool
OptionalOldSelf, when true, runs the rule on create and on update (even when there is no old value).

为 true 时,oldSelf 可能缺失:使用 oldSelf.hasValue() 检查,并用 oldSelf.value() 取值。规则始终运行;使用 oldSelf 前必须在规则中检查 oldSelf.hasValue()。仅当规则使用 oldSelf 时可设置。参见 k8s.io/apiextensions-apiserver 中的 ValidationRule。示例:OptionalOldSelf=true

reason
string
is a short code for why validation failed, returned to API callers.

支持的值:“FieldValueInvalid”、“FieldValueForbidden”、“FieldValueRequired”、“FieldValueDuplicate”。未设置时默认为 “FieldValueInvalid”。示例:Reason=“FieldValueInvalid”

rule
string
is the CEL expression that must evaluate to true.

作用域为该 XValidation 在 schema 中的位置;self 即为该值。示例:Rule=“self.minReplicas <= self.replicas && self.replicas <= self.maxReplicas”

// +kubebuilder:validation:items:XIntOrString
for array items marks a fields as an IntOrString.

对 IntOrString 字段应用模式或其他校验时需要此项。关于该类型的已知信息在 collapse 阶段应用,因此在 marker 应用期间通常不可用。

Example:

// +kubebuilder:validation:XIntOrString
// +kubebuilder:validation:Pattern=&#34;^(\\d+|\\d+%|)$&#34;
Port intstr.IntOrString
// +kubebuilder:validation:items:XIntOrString
for array items marks a fields as an IntOrString.

对 IntOrString 字段应用模式或其他校验时需要此项。关于该类型的已知信息在 collapse 阶段应用,因此在 marker 应用期间通常不可用。

Example:

// +kubebuilder:validation:XIntOrString
// +kubebuilder:validation:Pattern=&#34;^(\\d+|\\d+%|)$&#34;
Port intstr.IntOrString
// +kubebuilder:validation:items:XEmbeddedResource
for array items marks a fields as an embedded resource with apiVersion, kind and metadata fields.

嵌入资源是具有 apiVersion、kind 和 metadata 字段的值。它们会根据当前运行的 apiserver 语义隐式校验。不必为这些字段添加额外 schema,但也可以添加。可与 PreserveUnknownFields 结合使用。

Example:

// +kubebuilder:validation:EmbeddedResource
Template runtime.RawExtension
// +kubebuilder:validation:items:XEmbeddedResource
for array items marks a fields as an embedded resource with apiVersion, kind and metadata fields.

嵌入资源是具有 apiVersion、kind 和 metadata 字段的值。它们会根据当前运行的 apiserver 语义隐式校验。不必为这些字段添加额外 schema,但也可以添加。可与 PreserveUnknownFields 结合使用。

Example:

// +kubebuilder:validation:EmbeddedResource
Template runtime.RawExtension
// +kubebuilder:validation:items:UniqueItems
bool
for array items specifies that all items in this list must be unique.

Example:

// +kubebuilder:validation:UniqueItems=true
Tags []string
bool
// +kubebuilder:validation:items:UniqueItems
bool
for array items specifies that all items in this list must be unique.

Example:

// +kubebuilder:validation:UniqueItems=true
Tags []string
bool
// +kubebuilder:validation:items:Type
string
for array items overrides the type for this field (which defaults to the equivalent of the Go type).

通常必须与自定义序列化配对。例如,metav1.Time 字段会标记为 “type: string” 和 “format: date-time”。

常见类型包括:“string”、“number”、“integer”、“boolean”、“array”、“object”。

Example:

// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format=date-time
Time metav1.Time
string
// +kubebuilder:validation:items:Type
string
for array items overrides the type for this field (which defaults to the equivalent of the Go type).

通常必须与自定义序列化配对。例如,metav1.Time 字段会标记为 “type: string” 和 “format: date-time”。

常见类型包括:“string”、“number”、“integer”、“boolean”、“array”、“object”。

Example:

// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format=date-time
Time metav1.Time
string
// +kubebuilder:validation:items:Pattern
string
for array items specifies that this string must match the given regular expression.

示例(DNS 子域):

// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
DNSName string
string
// +kubebuilder:validation:items:Pattern
string
for array items specifies that this string must match the given regular expression.

示例(DNS 子域):

// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
DNSName string
string
// +kubebuilder:validation:items:MultipleOf
for array items specifies that this field must have a numeric value that's a multiple of this one.

示例(值必须是 5 的倍数):

// +kubebuilder:validation:MultipleOf=5
Count int32
// +kubebuilder:validation:items:MultipleOf
for array items specifies that this field must have a numeric value that's a multiple of this one.

示例(值必须是 5 的倍数):

// +kubebuilder:validation:MultipleOf=5
Count int32
// +kubebuilder:validation:items:Minimum
for array items specifies the minimum numeric value that this field can have. Negative numbers are supported.

Example:

// +kubebuilder:validation:Minimum=0
Replicas int32
// +kubebuilder:validation:items:Minimum
for array items specifies the minimum numeric value that this field can have. Negative numbers are supported.

Example:

// +kubebuilder:validation:Minimum=0
Replicas int32
// +kubebuilder:validation:items:MinProperties
int
for array items restricts the number of keys in an object

Example:

// +kubebuilder:validation:MinProperties=1
Metadata map[string]string
int
// +kubebuilder:validation:items:MinProperties
int
for array items restricts the number of keys in an object

Example:

// +kubebuilder:validation:MinProperties=1
Metadata map[string]string
int
// +kubebuilder:validation:items:MinLength
int
for array items specifies the minimum length for this string.

Example:

// +kubebuilder:validation:MinLength=1
Name string
int
// +kubebuilder:validation:items:MinLength
int
for array items specifies the minimum length for this string.

Example:

// +kubebuilder:validation:MinLength=1
Name string
int
// +kubebuilder:validation:items:MinItems
int
for array items specifies the minimum length for this list.

Example:

// +kubebuilder:validation:MinItems=1
Endpoints []string
int
// +kubebuilder:validation:items:MinItems
int
for array items specifies the minimum length for this list.

Example:

// +kubebuilder:validation:MinItems=1
Endpoints []string
int
// +kubebuilder:validation:items:Maximum
for array items specifies the maximum numeric value that this field can have.

Example:

// +kubebuilder:validation:Maximum=100
Percentage int32
// +kubebuilder:validation:items:Maximum
for array items specifies the maximum numeric value that this field can have.

Example:

// +kubebuilder:validation:Maximum=100
Percentage int32
// +kubebuilder:validation:items:MaxProperties
int
for array items restricts the number of keys in an object

Example:

// +kubebuilder:validation:MaxProperties=10
Labels map[string]string
int
// +kubebuilder:validation:items:MaxProperties
int
for array items restricts the number of keys in an object

Example:

// +kubebuilder:validation:MaxProperties=10
Labels map[string]string
int
// +kubebuilder:validation:items:MaxLength
int
for array items specifies the maximum length for this string.

Example:

// +kubebuilder:validation:MaxLength=64
Name string
int
// +kubebuilder:validation:items:MaxLength
int
for array items specifies the maximum length for this string.

Example:

// +kubebuilder:validation:MaxLength=64
Name string
int
// +kubebuilder:validation:items:MaxItems
int
for array items specifies the maximum length for this list.

Example:

// +kubebuilder:validation:MaxItems=10
Items []string
int
// +kubebuilder:validation:items:MaxItems
int
for array items specifies the maximum length for this list.

Example:

// +kubebuilder:validation:MaxItems=10
Items []string
int
// +kubebuilder:validation:items:Format
string
for array items specifies additional "complex" formatting for this field.

例如,date-time 字段会标记为 “type: string” 和 “format: date-time”。

常见格式包括:“int32”、“int64”、“float”、“double”、“byte”、“date”、“date-time”、“password”。

Example:

// +kubebuilder:validation:Format=date-time
CreatedAt string
string
// +kubebuilder:validation:items:Format
string
for array items specifies additional "complex" formatting for this field.

例如,date-time 字段会标记为 “type: string” 和 “format: date-time”。

常见格式包括:“int32”、“int64”、“float”、“double”、“byte”、“date”、“date-time”、“password”。

Example:

// +kubebuilder:validation:Format=date-time
CreatedAt string
string
// +kubebuilder:validation:items:ExclusiveMinimum
bool
for array items indicates that the minimum is "up to" but not including that value.

示例(值必须大于 0,而非大于等于 0):

// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:ExclusiveMinimum=true
PositiveNumber float64
bool
// +kubebuilder:validation:items:ExclusiveMinimum
bool
for array items indicates that the minimum is "up to" but not including that value.

示例(值必须大于 0,而非大于等于 0):

// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:ExclusiveMinimum=true
PositiveNumber float64
bool
// +kubebuilder:validation:items:ExclusiveMaximum
bool
for array items indicates that the maximum is "up to" but not including that value.

示例(值必须小于 100,而非小于等于 100):

// +kubebuilder:validation:Maximum=100
// +kubebuilder:validation:ExclusiveMaximum=true
Percentage float64
bool
// +kubebuilder:validation:items:ExclusiveMaximum
bool
for array items indicates that the maximum is "up to" but not including that value.

示例(值必须小于 100,而非小于等于 100):

// +kubebuilder:validation:Maximum=100
// +kubebuilder:validation:ExclusiveMaximum=true
Percentage float64
bool
// +kubebuilder:validation:items:Enum
any
for array items specifies that this (scalar) field is restricted to the *exact* values specified here.

Example:

// +kubebuilder:validation:Enum=ClusterIP;NodePort;LoadBalancer
ServiceType string
any
// +kubebuilder:validation:items:Enum
any
for array items specifies that this (scalar) field is restricted to the *exact* values specified here.

Example:

// +kubebuilder:validation:Enum=ClusterIP;NodePort;LoadBalancer
ServiceType string
any
// +kubebuilder:validation:XValidation
fieldPath
string
message
string
messageExpression
string
optionalOldSelf
bool
reason
string
rule
string
marks a field as requiring a value for which a given

表达式求值为 true。

此标记可重复以指定多个表达式,所有这些表达式都必须求值为 true。

CEL 表达式可使用:self(当前值)和 oldSelf(先前值;仅在更新时,创建时为 null)。规则作用域为其在 schema 中出现的位置;self 即为该值。

Examples:

// Basic field validation using self
// +kubebuilder:validation:XValidation:rule=&#34;self.minReplicas &lt;= self.replicas &amp;&amp; self.replicas &lt;= self.maxReplicas&#34;,message=&#34;replicas must be between minReplicas and maxReplicas&#34;

// Validation with custom reason
// +kubebuilder:validation:XValidation:rule=&#34;self.x &lt;= self.maxX&#34;,message=&#34;x cannot be greater than maxX&#34;,reason=&#34;FieldValueInvalid&#34;

// Immutability check using oldSelf (e.g. on a field so self/oldSelf are that field&#39;s value)
// +kubebuilder:validation:XValidation:rule=&#34;self == oldSelf&#34;,message=&#34;field is immutable&#34;

// Validation with field path for better error reporting
// +kubebuilder:validation:XValidation:rule=&#34;self &gt;= 1&#34;,message=&#34;value must be at least 1&#34;,fieldPath=&#34;.spec.replicas&#34;
fieldPath
string
is the path to the field that failed validation (for clearer error messages).

示例:FieldPath=“.spec.replicas”

message
string
is the text shown when validation fails.

若未设置,默认为 “failed rule: {Rule}”。不得包含换行。API 服务器有长度限制(例如 256 个字符)。完整细节见 k8s.io/apiextensions-apiserver 中的 ValidationRule。示例:Message=“replicas must be between minReplicas and maxReplicas”

messageExpression
string
is a CEL expression that returns the message shown when validation fails.

可以设置 Message 或 MessageExpression,不能同时设置。MessageExpression 必须返回字符串。若两者都设置,使用 MessageExpression。若都未设置,使用默认消息。表达式可使用与 Rule 相同的变量(例如 self、oldSelf)。结果不得包含换行,并受与 Message 相同的消息长度限制。示例:MessageExpression=“’replicas must be between ’ + string(self.minReplicas) + ’ and ’ + string(self.maxReplicas)”

optionalOldSelf
bool
OptionalOldSelf, when true, runs the rule on create and on update (even when there is no old value).

为 true 时,oldSelf 可能缺失:使用 oldSelf.hasValue() 检查,并用 oldSelf.value() 取值。规则始终运行;使用 oldSelf 前必须在规则中检查 oldSelf.hasValue()。仅当规则使用 oldSelf 时可设置。参见 k8s.io/apiextensions-apiserver 中的 ValidationRule。示例:OptionalOldSelf=true

reason
string
is a short code for why validation failed, returned to API callers.

支持的值:“FieldValueInvalid”、“FieldValueForbidden”、“FieldValueRequired”、“FieldValueDuplicate”。未设置时默认为 “FieldValueInvalid”。示例:Reason=“FieldValueInvalid”

rule
string
is the CEL expression that must evaluate to true.

作用域为该 XValidation 在 schema 中的位置;self 即为该值。示例:Rule=“self.minReplicas <= self.replicas && self.replicas <= self.maxReplicas”

// +kubebuilder:validation:XValidation
fieldPath
string
message
string
messageExpression
string
optionalOldSelf
bool
reason
string
rule
string
marks a field as requiring a value for which a given

表达式求值为 true。

此标记可重复以指定多个表达式,所有这些表达式都必须求值为 true。

CEL 表达式可使用:self(当前值)和 oldSelf(先前值;仅在更新时,创建时为 null)。规则作用域为其在 schema 中出现的位置;self 即为该值。

Examples:

// Basic field validation using self
// +kubebuilder:validation:XValidation:rule=&#34;self.minReplicas &lt;= self.replicas &amp;&amp; self.replicas &lt;= self.maxReplicas&#34;,message=&#34;replicas must be between minReplicas and maxReplicas&#34;

// Validation with custom reason
// +kubebuilder:validation:XValidation:rule=&#34;self.x &lt;= self.maxX&#34;,message=&#34;x cannot be greater than maxX&#34;,reason=&#34;FieldValueInvalid&#34;

// Immutability check using oldSelf (e.g. on a field so self/oldSelf are that field&#39;s value)
// +kubebuilder:validation:XValidation:rule=&#34;self == oldSelf&#34;,message=&#34;field is immutable&#34;

// Validation with field path for better error reporting
// +kubebuilder:validation:XValidation:rule=&#34;self &gt;= 1&#34;,message=&#34;value must be at least 1&#34;,fieldPath=&#34;.spec.replicas&#34;
fieldPath
string
is the path to the field that failed validation (for clearer error messages).

示例:FieldPath=“.spec.replicas”

message
string
is the text shown when validation fails.

若未设置,默认为 “failed rule: {Rule}”。不得包含换行。API 服务器有长度限制(例如 256 个字符)。完整细节见 k8s.io/apiextensions-apiserver 中的 ValidationRule。示例:Message=“replicas must be between minReplicas and maxReplicas”

messageExpression
string
is a CEL expression that returns the message shown when validation fails.

可以设置 Message 或 MessageExpression,不能同时设置。MessageExpression 必须返回字符串。若两者都设置,使用 MessageExpression。若都未设置,使用默认消息。表达式可使用与 Rule 相同的变量(例如 self、oldSelf)。结果不得包含换行,并受与 Message 相同的消息长度限制。示例:MessageExpression=“’replicas must be between ’ + string(self.minReplicas) + ’ and ’ + string(self.maxReplicas)”

optionalOldSelf
bool
OptionalOldSelf, when true, runs the rule on create and on update (even when there is no old value).

为 true 时,oldSelf 可能缺失:使用 oldSelf.hasValue() 检查,并用 oldSelf.value() 取值。规则始终运行;使用 oldSelf 前必须在规则中检查 oldSelf.hasValue()。仅当规则使用 oldSelf 时可设置。参见 k8s.io/apiextensions-apiserver 中的 ValidationRule。示例:OptionalOldSelf=true

reason
string
is a short code for why validation failed, returned to API callers.

支持的值:“FieldValueInvalid”、“FieldValueForbidden”、“FieldValueRequired”、“FieldValueDuplicate”。未设置时默认为 “FieldValueInvalid”。示例:Reason=“FieldValueInvalid”

rule
string
is the CEL expression that must evaluate to true.

作用域为该 XValidation 在 schema 中的位置;self 即为该值。示例:Rule=“self.minReplicas <= self.replicas && self.replicas <= self.maxReplicas”

// +kubebuilder:validation:XIntOrString
marks a fields as an IntOrString.

对 IntOrString 字段应用模式或其他校验时需要此项。关于该类型的已知信息在 collapse 阶段应用,因此在 marker 应用期间通常不可用。

Example:

// +kubebuilder:validation:XIntOrString
// +kubebuilder:validation:Pattern=&#34;^(\\d+|\\d+%|)$&#34;
Port intstr.IntOrString
// +kubebuilder:validation:XIntOrString
marks a fields as an IntOrString.

对 IntOrString 字段应用模式或其他校验时需要此项。关于该类型的已知信息在 collapse 阶段应用,因此在 marker 应用期间通常不可用。

Example:

// +kubebuilder:validation:XIntOrString
// +kubebuilder:validation:Pattern=&#34;^(\\d+|\\d+%|)$&#34;
Port intstr.IntOrString
// +kubebuilder:validation:XEmbeddedResource
marks a fields as an embedded resource with apiVersion, kind and metadata fields.

嵌入资源是具有 apiVersion、kind 和 metadata 字段的值。它们会根据当前运行的 apiserver 语义隐式校验。不必为这些字段添加额外 schema,但也可以添加。可与 PreserveUnknownFields 结合使用。

Example:

// +kubebuilder:validation:EmbeddedResource
Template runtime.RawExtension
// +kubebuilder:validation:XEmbeddedResource
marks a fields as an embedded resource with apiVersion, kind and metadata fields.

嵌入资源是具有 apiVersion、kind 和 metadata 字段的值。它们会根据当前运行的 apiserver 语义隐式校验。不必为这些字段添加额外 schema,但也可以添加。可与 PreserveUnknownFields 结合使用。

Example:

// +kubebuilder:validation:EmbeddedResource
Template runtime.RawExtension
// +kubebuilder:validation:UniqueItems
bool
specifies that all items in this list must be unique.

Example:

// +kubebuilder:validation:UniqueItems=true
Tags []string
bool
// +kubebuilder:validation:UniqueItems
bool
specifies that all items in this list must be unique.

Example:

// +kubebuilder:validation:UniqueItems=true
Tags []string
bool
// +kubebuilder:validation:Type
string
overrides the type for this field (which defaults to the equivalent of the Go type).

通常必须与自定义序列化配对。例如,metav1.Time 字段会标记为 “type: string” 和 “format: date-time”。

常见类型包括:“string”、“number”、“integer”、“boolean”、“array”、“object”。

Example:

// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format=date-time
Time metav1.Time
string
// +kubebuilder:validation:Type
string
overrides the type for this field (which defaults to the equivalent of the Go type).

通常必须与自定义序列化配对。例如,metav1.Time 字段会标记为 “type: string” 和 “format: date-time”。

常见类型包括:“string”、“number”、“integer”、“boolean”、“array”、“object”。

Example:

// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format=date-time
Time metav1.Time
string
// +kubebuilder:validation:Schemaless
marks a field as being a schemaless object.

无 schema 的对象不会被内省,因此你必须自行提供类型与校验信息。此标签的一个用途是嵌入保存 JSONSchema 类型对象的字段。由于该字段禁用所有类型检查,建议仅作为最后手段使用。

Example:

// +kubebuilder:validation:Schemaless
JSONSchema apiextensionsv1.JSONSchemaProps
// +kubebuilder:validation:Required
specifies that all fields in this package are required by default. Prefer using field-level +required markers instead.
// +kubebuilder:validation:Required
specifies that this field is required.
// +kubebuilder:validation:Pattern
string
specifies that this string must match the given regular expression.

示例(DNS 子域):

// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
DNSName string
string
// +kubebuilder:validation:Pattern
string
specifies that this string must match the given regular expression.

示例(DNS 子域):

// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
DNSName string
string
// +kubebuilder:validation:Optional
specifies that all fields in this package are optional by default. Prefer using field-level +optional markers instead.
// +kubebuilder:validation:Optional
specifies that this field is optional.
// +kubebuilder:validation:MultipleOf
specifies that this field must have a numeric value that's a multiple of this one.

示例(值必须是 5 的倍数):

// +kubebuilder:validation:MultipleOf=5
Count int32
// +kubebuilder:validation:MultipleOf
specifies that this field must have a numeric value that's a multiple of this one.

示例(值必须是 5 的倍数):

// +kubebuilder:validation:MultipleOf=5
Count int32
// +kubebuilder:validation:Minimum
specifies the minimum numeric value that this field can have. Negative numbers are supported.

Example:

// +kubebuilder:validation:Minimum=0
Replicas int32
// +kubebuilder:validation:Minimum
specifies the minimum numeric value that this field can have. Negative numbers are supported.

Example:

// +kubebuilder:validation:Minimum=0
Replicas int32
// +kubebuilder:validation:MinProperties
int
restricts the number of keys in an object

Example:

// +kubebuilder:validation:MinProperties=1
Metadata map[string]string
int
// +kubebuilder:validation:MinProperties
int
restricts the number of keys in an object

Example:

// +kubebuilder:validation:MinProperties=1
Metadata map[string]string
int
// +kubebuilder:validation:MinLength
int
specifies the minimum length for this string.

Example:

// +kubebuilder:validation:MinLength=1
Name string
int
// +kubebuilder:validation:MinLength
int
specifies the minimum length for this string.

Example:

// +kubebuilder:validation:MinLength=1
Name string
int
// +kubebuilder:validation:MinItems
int
specifies the minimum length for this list.

Example:

// +kubebuilder:validation:MinItems=1
Endpoints []string
int
// +kubebuilder:validation:MinItems
int
specifies the minimum length for this list.

Example:

// +kubebuilder:validation:MinItems=1
Endpoints []string
int
// +kubebuilder:validation:Maximum
specifies the maximum numeric value that this field can have.

Example:

// +kubebuilder:validation:Maximum=100
Percentage int32
// +kubebuilder:validation:Maximum
specifies the maximum numeric value that this field can have.

Example:

// +kubebuilder:validation:Maximum=100
Percentage int32
// +kubebuilder:validation:MaxProperties
int
restricts the number of keys in an object

Example:

// +kubebuilder:validation:MaxProperties=10
Labels map[string]string
int
// +kubebuilder:validation:MaxProperties
int
restricts the number of keys in an object

Example:

// +kubebuilder:validation:MaxProperties=10
Labels map[string]string
int
// +kubebuilder:validation:MaxLength
int
specifies the maximum length for this string.

Example:

// +kubebuilder:validation:MaxLength=64
Name string
int
// +kubebuilder:validation:MaxLength
int
specifies the maximum length for this string.

Example:

// +kubebuilder:validation:MaxLength=64
Name string
int
// +kubebuilder:validation:MaxItems
int
specifies the maximum length for this list.

Example:

// +kubebuilder:validation:MaxItems=10
Items []string
int
// +kubebuilder:validation:MaxItems
int
specifies the maximum length for this list.

Example:

// +kubebuilder:validation:MaxItems=10
Items []string
int
// +kubebuilder:validation:Format
string
specifies additional "complex" formatting for this field.

例如,date-time 字段会标记为 “type: string” 和 “format: date-time”。

常见格式包括:“int32”、“int64”、“float”、“double”、“byte”、“date”、“date-time”、“password”。

Example:

// +kubebuilder:validation:Format=date-time
CreatedAt string
string
// +kubebuilder:validation:Format
string
specifies additional "complex" formatting for this field.

例如,date-time 字段会标记为 “type: string” 和 “format: date-time”。

常见格式包括:“int32”、“int64”、“float”、“double”、“byte”、“date”、“date-time”、“password”。

Example:

// +kubebuilder:validation:Format=date-time
CreatedAt string
string
// +kubebuilder:validation:ExclusiveMinimum
bool
indicates that the minimum is "up to" but not including that value.

示例(值必须大于 0,而非大于等于 0):

// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:ExclusiveMinimum=true
PositiveNumber float64
bool
// +kubebuilder:validation:ExclusiveMinimum
bool
indicates that the minimum is "up to" but not including that value.

示例(值必须大于 0,而非大于等于 0):

// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:ExclusiveMinimum=true
PositiveNumber float64
bool
// +kubebuilder:validation:ExclusiveMaximum
bool
indicates that the maximum is "up to" but not including that value.

示例(值必须小于 100,而非小于等于 100):

// +kubebuilder:validation:Maximum=100
// +kubebuilder:validation:ExclusiveMaximum=true
Percentage float64
bool
// +kubebuilder:validation:ExclusiveMaximum
bool
indicates that the maximum is "up to" but not including that value.

示例(值必须小于 100,而非小于等于 100):

// +kubebuilder:validation:Maximum=100
// +kubebuilder:validation:ExclusiveMaximum=true
Percentage float64
bool
// +kubebuilder:validation:ExactlyOneOf
string
specifies a list of field names that must conform to the ExactlyOneOf constraint.
string
// +kubebuilder:validation:Enum
any
specifies that this (scalar) field is restricted to the *exact* values specified here.

Example:

// +kubebuilder:validation:Enum=ClusterIP;NodePort;LoadBalancer
ServiceType string
any
// +kubebuilder:validation:Enum
any
specifies that this (scalar) field is restricted to the *exact* values specified here.

Example:

// +kubebuilder:validation:Enum=ClusterIP;NodePort;LoadBalancer
ServiceType string
any
// +kubebuilder:validation:EmbeddedResource
marks a fields as an embedded resource with apiVersion, kind and metadata fields.

嵌入资源是具有 apiVersion、kind 和 metadata 字段的值。它们会根据当前运行的 apiserver 语义隐式校验。不必为这些字段添加额外 schema,但也可以添加。可与 PreserveUnknownFields 结合使用。

Example:

// +kubebuilder:validation:EmbeddedResource
Template runtime.RawExtension
// +kubebuilder:validation:AtMostOneOf
string
specifies a list of field names that must conform to the AtMostOneOf constraint.
string
// +kubebuilder:validation:AtLeastOneOf
string
specifies a list of field names that must conform to the AtLeastOneOf constraint.
string
// +kubebuilder:title
any
sets the title for this field.

title 是使 OpenAPI 文档更友好的元数据,在文档工具中查看时让 schema 更易理解。它是不影响校验的元数据字段,但提供关于 schema 所表示内容的重要上下文。

Examples:

// Simple title
// +kubebuilder:title=&#34;Replica Count&#34;
Replicas int32

// Descriptive title
// +kubebuilder:title=&#34;Database Connection Configuration&#34;
DatabaseConfig DatabaseConfig
any
is the title text to be shown in OpenAPI documentation.
// +kubebuilder:title
any
sets the title for this field.

title 是使 OpenAPI 文档更友好的元数据,在文档工具中查看时让 schema 更易理解。它是不影响校验的元数据字段,但提供关于 schema 所表示内容的重要上下文。

Examples:

// Simple title
// +kubebuilder:title=&#34;Replica Count&#34;
Replicas int32

// Descriptive title
// +kubebuilder:title=&#34;Database Connection Configuration&#34;
DatabaseConfig DatabaseConfig
any
is the title text to be shown in OpenAPI documentation.
// +kubebuilder:example
any
sets the example value for this field.

示例值会被接受为对该字段有效的任意值。常见类型的格式包括:布尔:true,字符串:Cluster,数值:1.24,数组:{1,2},对象:{policy: "delete"})。示例应以 pruned 形式定义,仅做尽力校验。完整校验示例需要将包含它的 CRD 提交到 apiserver。

示例显示在 API 文档中,帮助用户理解期望格式。

用法示例:

// String example
// +kubebuilder:example=&#34;my-service&#34;
ServiceName string

// Integer example
// +kubebuilder:example=5
Replicas int32

// Boolean example
// +kubebuilder:example=false
Debug bool

// Array example
// +kubebuilder:example={8080,8443}
Ports []int

// Object example
// +kubebuilder:example={cpu: &#34;100m&#34;, memory: &#34;128Mi&#34;}
Resources map[string]string
any
is the example value to be shown in API documentation.
// +kubebuilder:default
any
sets the default value for this field.

默认值会被接受为对该字段有效的任意值。常见类型的格式包括:布尔:true,字符串:Cluster,数值:1.24,数组:{1,2},对象:{policy: "delete"})。默认值应以 pruned 形式定义,仅做尽力校验。完整校验默认值需要将包含它的 CRD 提交到 apiserver。

Examples:

// String default
// +kubebuilder:default=&#34;ClusterIP&#34;
ServiceType string

// Integer default
// +kubebuilder:default=3
Replicas int32

// Boolean default
// +kubebuilder:default=true
Enabled bool

// Array default
// +kubebuilder:default={80,443}
Ports []int

// Object default
// +kubebuilder:default={replicas: 1}
Config map[string]interface{}
any
is the default value. It can be any value valid for the field type.
// +k8s:required
specifies that this field is required.
// +k8s:optional
specifies that this field is optional.
// +k8s:immutable
marks a field as immutable. Once set, the value cannot be changed.

对于可选字段,允许从 unset 到 set 的单次转换。

注意:嵌套在可选字段下的不可变字段仍可通过取消设置可选父字段并重新设置来更新。

Examples:

// +k8s:immutable
// +required
Port intstr.IntOrString

// +k8s:immutable
// +optional
TargetPort intstr.IntOrString
// +default
value
any
sets the default value for this field.

默认值会被接受为对该字段有效的任意值。仅接受 JSON 格式的值。ref(...) 值会被忽略。常见类型的格式包括:布尔:true,字符串:"Cluster",数值:1.24,数组:[1,2],对象:{"policy": "delete"})。默认值应以 pruned 形式定义,仅做尽力校验。完整校验默认值需要将包含它的 CRD 提交到 apiserver。

Examples:

// String default (note the JSON quotes)
// +default=&#34;ClusterIP&#34;
ServiceType string

// Integer default
// +default=3
Replicas int32

// Boolean default
// +default=true
Enabled bool

// Array default (JSON format)
// +default=[80,443]
Ports []int

// Object default (JSON format)
// +default={&#34;policy&#34;: &#34;delete&#34;}
Config map[string]interface{}
value
any
is the default value in JSON format. It can be any value valid for the field type.