Skip to content

Commit

Permalink
翻译4.4.2 webhook for core types 章节
Browse files Browse the repository at this point in the history
  • Loading branch information
luffyao committed Aug 12, 2020
1 parent eceab5f commit 327949b
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions docs/book/src/reference/webhook-for-core-types.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
# Admission Webhook for Core Types
# 核心类型的准入 Webhook

It is very easy to build admission webhooks for CRDs, which has been covered in
the CronJob tutorial. Given that kubebuilder doesn't support webhook scaffolding
for core types, you have to use the library from controller-runtime to handle it.
There is an [example](https://github.com/kubernetes-sigs/controller-runtime/tree/master/examples/builtins)
in controller-runtime.
为 CRD 构建准入 webhook 非常容易,这在 CronJob 教程中已经介绍过了。由于 kubebuilder 不支持核心类型的 webhook 搭建,您必须使用 controller-runtime 的库来处理它。这里可以参考 controller-runtime 的一个 [示例](https://github.com/kubernetes-sigs/controller-runtime/tree/master/examples/builtins)

It is suggested to use kubebuilder to initialize a project, and then you can
follow the steps below to add admission webhooks for core types.
建议使用 kubebuilder 初始化一个项目,然后按照下面的步骤为核心类型添加准入 webhook。

## Implement Your Handler
## 实现处理程序

You need to have your handler implements the
[admission.Handler](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/webhook/admission?tab=doc#Handler)
interface.
你需要用自己的处理程序去实现 [admission.Handler](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/webhook/admission?tab=doc#Handler) 接口。

```go
type podAnnotator struct {
Expand All @@ -28,7 +21,7 @@ func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admiss
return admission.Errored(http.StatusBadRequest, err)
}

// mutate the fields in pod
//pod 中修改字段

marshaledPod, err := json.Marshal(pod)
if err != nil {
Expand All @@ -38,10 +31,9 @@ func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admiss
}
```

If you need a client, just pass in the client at struct construction time.
如果需要客户端,只需在结构构建时传入客户端。

If you add the `InjectDecoder` method for your handler, a decoder will be
injected for you.
如果你为你的处理程序添加了 `InjectDecoder` 方法,将会注入一个解码器。

```go
func (a *podAnnotator) InjectDecoder(d *admission.Decoder) error {
Expand All @@ -50,24 +42,24 @@ func (a *podAnnotator) InjectDecoder(d *admission.Decoder) error {
}
```

**Note**: in order to have controller-gen generate the webhook configuration for
you, you need to add markers. For example,
**注意**: 为了使得 controller-gen 能够为你生成 webhook 配置,你需要添加一些标记。例如,
`// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,failurePolicy=fail,groups="",resources=pods,verbs=create;update,versions=v1,name=mpod.kb.io`

## Update main.go
## 更新 main.go

Now you need to register your handler in the webhook server.
现在你需要在 webhook 服务端中注册你的处理程序。

```go
mgr.GetWebhookServer().Register("/mutate-v1-pod", &webhook.Admission{Handler: &podAnnotator{Client: mgr.GetClient()}})
```

You need to ensure the path here match the path in the marker.
您需要确保这里的路径与标记中的路径相匹配。

## Deploy
## 部署

Deploying it is just like deploying a webhook server for CRD. You need to
1) provision the serving certificate
2) deploy the server
部署它就像为 CRD 部署 webhook 服务端一样。你需要

You can follow the [tutorial](/cronjob-tutorial/running.md).
1) 提供服务证书
2) 部署服务端

你可以参考 [教程](/cronjob-tutorial/running.md)

0 comments on commit 327949b

Please sign in to comment.