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

Webhook(网络钩子)

这些标记描述了如何生成 webhook 配置。使用这些标记可以让您的 webhook 描述与实现它们的代码保持紧密关联。

// +kubebuilder:webhookconfiguration
mutating
bool
name
string
specifies how a webhook should be served.

它仅指定服务该 webhook 的应用所固有的细节(例如它能处理的资源,或它服务的路径)。

示例(校验 Webhook):

// +kubebuilder:webhook:path=/validate-mygroup-v1-myresource,mutating=false,failurePolicy=fail,sideEffects=None,groups=mygroup.example.com,resources=myresources,verbs=create;update,versions=v1,name=myresource.kb.io,admissionReviewVersions=v1

示例(变更 Webhook):

// +kubebuilder:webhook:path=/mutate-mygroup-v1-myresource,mutating=true,failurePolicy=fail,sideEffects=None,groups=mygroup.example.com,resources=myresources,verbs=create;update,versions=v1,name=myresource.kb.io,admissionReviewVersions=v1
mutating
bool
marks this as a mutating webhook (it's validating only if false)

Mutating webhook 允许在响应中更改对象,并在所有 validating webhook 之前 调用。Mutating webhook 也可以像 validating webhook 一样选择拒绝对象。

name
string
indicates the name of this webhook configuration. Should be a domain with at least three segments separated by dots

示例:“myresource.mygroup.example.com”。

// +kubebuilder:webhook
admissionReviewVersions
string
failurePolicy
string
groups
string
matchPolicy
string
mutating
bool
name
string
patch
string
path
string
reinvocationPolicy
string
resources
string
serviceName
string
serviceNamespace
string
servicePort
int
sideEffects
string
timeoutSeconds
int
url
string
verbs
string
versions
string
webhookVersions
string
specifies how a webhook should be served.

它仅指定服务该 webhook 的应用所固有的细节(例如它能处理的资源,或它服务的路径)。

示例(校验 Webhook):

// +kubebuilder:webhook:path=/validate-mygroup-v1-myresource,mutating=false,failurePolicy=fail,sideEffects=None,groups=mygroup.example.com,resources=myresources,verbs=create;update,versions=v1,name=myresource.kb.io,admissionReviewVersions=v1

示例(变更 Webhook):

// +kubebuilder:webhook:path=/mutate-mygroup-v1-myresource,mutating=true,failurePolicy=fail,sideEffects=None,groups=mygroup.example.com,resources=myresources,verbs=create;update,versions=v1,name=myresource.kb.io,admissionReviewVersions=v1
admissionReviewVersions
string
is an ordered list of preferred `AdmissionReview`

Webhook 期望的版本。API 服务器会尝试使用列表中它支持的第一个版本。若指定的版本均不受支持,API 调用将失败。常见值:“v1” 或 “v1;v1beta1”。

failurePolicy
string
specifies what should happen if the API server cannot reach the webhook.

可以是 “ignore”(跳过 webhook 并继续)或 “fail”(拒绝相关对象)。大多数 webhook 应使用 “fail” 以确保始终执行 webhook 逻辑。

groups
string
specifies the API groups that this webhook receives requests for.

使用 “” 匹配所有 group。多个 group 用分号分隔。示例:“apps;batch” 或 “”。

matchPolicy
string
defines how the "rules" list is used to match incoming requests.

允许的值为 “Exact”(仅当完全匹配指定规则时匹配)或 “Equivalent”(若请求修改 rules 中列出的资源则匹配,即使通过另一 API group 或 version)。未指定时默认为 “Equivalent”。

mutating
bool
marks this as a mutating webhook (it's validating only if false)

Mutating webhook 允许在响应中更改对象,并在所有 validating webhook 之前 调用。Mutating webhook 也可以像 validating webhook 一样选择拒绝对象。

name
string
indicates the name of this webhook configuration. Should be a domain with at least three segments separated by dots

示例:“myresource.mygroup.example.com”。

patch
string
applies a strategic merge patch to customize the generated webhook configuration.

这允许你设置未直接作为 marker 参数公开的任意 webhook 字段,例如 namespaceSelector、objectSelector 或 matchConditions。该补丁是遵循 Kubernetes strategic merge patch 语义的 JSON 对象,在处理完所有其他 marker 参数后应用到 webhook 配置。

使用反引号以避免在 JSON 中转义引号。

常见用例:

  • 使用 namespaceSelector 将 Webhook 作用域限制到特定命名空间
  • 使用 objectSelector 按对象标签过滤 Webhook 调用
  • 在单个补丁中组合多个自定义

示例(限制到带标签的命名空间):

// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,...,patch=`{"namespaceSelector":{"matchLabels":{"webhook-enabled":"true"}}}`

示例(使用 matchExpressions 按对象标签过滤):

// +kubebuilder:webhook:path=/validate-v1-deployment,...,patch=`{"objectSelector":{"matchExpressions":[{"key":"tier","operator":"In","values":["frontend","backend"]}]}}`

示例(组合命名空间与对象选择器):

// +kubebuilder:webhook:...,patch=`{"namespaceSelector":{"matchLabels":{"env":"production"}},"objectSelector":{"matchLabels":{"managed-by":"my-operator"}}}`

示例(覆盖超时):

// +kubebuilder:webhook:...,patch=`{"timeoutSeconds":25}`

示例(通过设为 null 移除字段):

// +kubebuilder:webhook:...,patch=`{"timeoutSeconds":null}`

将字段设为 null 会移除它。对没有 merge key 的列表字段(如 rules)打补丁会替换整个列表。字段名区分大小写,补丁中的未知字段会导致错误。

path
string
specifies that path that the API server should connect to this webhook on. Must be

根据类型加上 ‘/validate-’ 或 ‘/mutate-’ 前缀,后接 $GROUP-$VERSION-$KIND,其中所有值均为小写,group 中的句点替换为连字符。例如,类型 batch.tutorial.kubebuilder.io/v1,Kind=CronJob 的 validating webhook 路径为 /validate-batch-tutorial-kubebuilder-io-v1-cronjob

reinvocationPolicy
string
allows mutating webhooks to request reinvocation after other mutations.

为使 mutating admission 插件能观察到其他插件所做的更改,若 mutating webhook 修改了对象,内置 mutating admission 插件会重新运行;mutating webhook 也可指定 reinvocationPolicy 以控制是否被再次调用。可为 “Never” 或 “IfNeeded”。默认为 “Never”。

resources
string
specifies the API resources that this webhook receives requests for.

使用 “” 匹配所有资源。多个资源用分号分隔。示例:“deployments;pods” 或 “”。

serviceName
string
indicates the name of the K8s Service the webhook uses.

未指定时默认为 “webhook-service”。

serviceNamespace
string
indicates the namespace of the K8s Service the webhook uses.

未指定时默认为 “system”。

servicePort
int
indicates the port of the K8s Service the webhook uses.

未指定时默认为 443。

sideEffects
string
specify whether calling the webhook will have side effects.

这会影响 dry run 和 kubectl diff:若 sideEffect 为 “Unknown”(默认)或 “Some”,则 API 服务器不会在 dry-run 请求上调用 webhook,而是失败。若值为 “None”,则 webhook 无副作用,API 服务器会在 dry-run 时调用它。若值为 “NoneOnDryRun”,则 webhook 负责检查请求中 AdmissionReview 的 “dryRun” 属性,并在该值为 “true” 时避免副作用。大多数 webhook 应使用 “None”。

timeoutSeconds
int
allows configuring how long the API server should wait for a webhook to respond before treating the call as a failure.

若在 webhook 响应前超时到期,将根据 failure policy 忽略 webhook 调用或拒绝 API 调用。超时值必须在 1 到 30 秒之间。admission webhook 的超时默认 10 秒。

url
string
allows mutating webhooks configuration to specify an external URL when generating

清单中使用外部 URL,而非内部服务通信。格式应为 https://address:port/path。指定此选项时,会从 webhook 清单中移除 serviceConfig.Service。URL 配置应放在引号中。指定 path 时不能指定 url。

verbs
string
specifies the Kubernetes API verbs that this webhook receives requests for.

只能指定类似修改的动词。可为 “create”、“update”、“delete”、“connect” 或 “*”(表示全部)。多个动词用分号分隔。示例:“create;update”。

versions
string
specifies the API versions that this webhook receives requests for.

使用 “” 匹配所有版本。多个版本用分号分隔。示例:“v1;v1beta1” 或 “”。

webhookVersions
string
specifies the target API versions of the {Mutating,Validating}WebhookConfiguration objects

自身生成。唯一支持的值是 v1。默认为 v1。