Skip to content

Commit 47fc5ab

Browse files
author
Mengqi Yu
committed
update godoc for webhook
1 parent 79f0601 commit 47fc5ab

File tree

4 files changed

+192
-27
lines changed

4 files changed

+192
-27
lines changed

pkg/doc.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ system must be read for each Reconciler.
4949
5050
* Controller require Watches to be configured to enqueue reconcile.Requests in response to events.
5151
52+
Webhook
53+
54+
Admission Webhook is a mechanism for extending kubernetes APIs. The API server will send AdmissionRequests to
55+
the webhooks according to the configured target event type (object Create, Update, Delete). The webhooks may
56+
mutate and (or) validate the object embedded in the AdmissionReview requests.
57+
58+
There are 2 types of admission webhook: mutating and validating admission webhook.
59+
Mutating webhook is used to mutate a core API object or a CRD instance before the API server admit it.
60+
Validting webhook is used to validate if a core API object or a CRD instance meet certain requirements.
61+
62+
* Admission Webhooks require Handler(s) to be provided to process the received AdmissionReview requests.
63+
5264
Reconciler
5365
5466
Reconciler is a function provided to a Controller that may be called at anytime with the Name and Namespace of an object.

pkg/webhook/admission/builder/doc.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/*
18+
Package builder provides methods to build admission webhooks.
19+
20+
For most users, we recommend to use Operations and ForType to construct a webhook.
21+
22+
webhook1, err := NewWebhookBuilder().
23+
Mutating().
24+
Operations(admissionregistrationv1beta1.Create).
25+
ForType(&corev1.Pod{}).
26+
WithManager(mgr).
27+
Handlers(mutatingHandler11, mutatingHandler12).
28+
Build()
29+
if err != nil {
30+
// handle error
31+
}
32+
33+
webhook2, err := NewWebhookBuilder().
34+
Validating().
35+
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
36+
ForType(&appsv1.Deployment{}).
37+
WithManager(mgr).
38+
Handlers(validatingHandler21).
39+
Build()
40+
if err != nil {
41+
// handle error
42+
}
43+
44+
Note: To build a webhook for CRD, you need to ensure the manager is
45+
constructed from the scheme that understands your CRD.
46+
47+
There are more options for configuring a webhook. e.g. Name, Path, FailurePolicy, NamespaceSelector.
48+
Here is another example:
49+
50+
webhook3, err := NewWebhookBuilder().
51+
Name("foo.example.com").
52+
Path("/mutatepods").
53+
Mutating().
54+
Operations(admissionregistrationv1beta1.Create).
55+
ForType(&corev1.Pod{}).
56+
FailurePolicy(admissionregistrationv1beta1.Fail).
57+
WithManager(mgr).
58+
Handlers(mutatingHandler31, mutatingHandler32).
59+
Build()
60+
if err != nil {
61+
// handle error
62+
}
63+
64+
For some advanced use cases like subresources, etc., you can directly use Rules instead of use Operations and ForType.
65+
Here is an example:
66+
67+
webhook4, err := NewWebhookBuilder().
68+
Validating().
69+
Rules(admissionregistrationv1beta1.RuleWithOperations{
70+
Operations: []admissionregistrationv1beta1.OperationType{admissionregistrationv1beta1.Create},
71+
Rule: admissionregistrationv1beta1.Rule{
72+
APIGroups: []string{"apps", "batch"},
73+
APIVersions: []string{"v1"},
74+
Resources: []string{"*"},
75+
},
76+
}).
77+
WithManager(mgr).
78+
Handlers(validatingHandler41).
79+
Build()
80+
if err != nil {
81+
// handle error
82+
}
83+
*/
84+
package builder

pkg/webhook/admission/doc.go

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,81 @@ limitations under the License.
1515
*/
1616

1717
/*
18-
Package admission provides functions to build and bootstrap an admission webhook server for a k8s cluster.
19-
20-
Build webhooks
21-
22-
webhook1, err := NewWebhookBuilder().
23-
Name("foo.k8s.io").
24-
Mutating().
25-
Operations(admissionregistrationv1beta1.Create).
26-
ForType(&corev1.Pod{}).
27-
WithManager(mgr).
28-
Build(mutatingHandler1, mutatingHandler2)
29-
if err != nil {
30-
// handle error
31-
}
32-
33-
webhook2, err := NewWebhookBuilder().
34-
Name("bar.k8s.io").
35-
Validating().
36-
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
37-
ForType(&appsv1.Deployment{}).
38-
WithManager(mgr).
39-
Build(validatingHandler1)
40-
if err != nil {
41-
// handle error
18+
Package admission provides implementation for admission webhook and methods to implement admission webhook handlers.
19+
20+
The following snippet is an example implementation of mutating handler.
21+
22+
type Mutator struct {
23+
client client.Client
24+
decoder types.Decoder
25+
}
26+
27+
func (m *Mutator) mutatePodsFn(ctx context.Context, pod *corev1.Pod) error {
28+
// your logic to mutate the passed-in pod.
29+
}
30+
31+
func (m *Mutator) Handle(ctx context.Context, req types.Request) types.Response {
32+
pod := &corev1.Pod{}
33+
err := m.decoder.Decode(req, pod)
34+
if err != nil {
35+
return admission.ErrorResponse(http.StatusBadRequest, err)
36+
}
37+
// Do deepcopy before actually mutate the object.
38+
copy := pod.DeepCopy()
39+
err = m.mutatePodsFn(ctx, copy)
40+
if err != nil {
41+
return admission.ErrorResponse(http.StatusInternalServerError, err)
42+
}
43+
return admission.PatchResponse(pod, copy)
44+
}
45+
46+
// InjectClient injects the client into the Handler
47+
func (m *Mutator) InjectClient(c client.Client) error {
48+
h.client = c
49+
return nil
50+
}
51+
52+
// InjectDecoder injects the decoder into the Handler
53+
func (m *Mutator) InjectDecoder(d types.Decoder) error {
54+
h.decoder = d
55+
return nil
56+
}
57+
58+
The following snippet is an example implementation of validating handler.
59+
60+
type Handler struct {
61+
client client.Client
62+
decoder types.Decoder
63+
}
64+
65+
func (v *Validator) validatePodsFn(ctx context.Context, pod *corev1.Pod) (bool, string, error) {
66+
// your business logic
67+
}
68+
69+
func (v *Validator) Handle(ctx context.Context, req types.Request) types.Response {
70+
pod := &corev1.Pod{}
71+
err := h.decoder.Decode(req, pod)
72+
if err != nil {
73+
return admission.ErrorResponse(http.StatusBadRequest, err)
74+
}
75+
76+
allowed, reason, err := h.validatePodsFn(ctx, pod)
77+
if err != nil {
78+
return admission.ErrorResponse(http.StatusInternalServerError, err)
79+
}
80+
return admission.ValidationResponse(allowed, reason)
81+
}
82+
83+
// InjectClient injects the client into the Handler
84+
func (v *Validator) InjectClient(c client.Client) error {
85+
h.client = c
86+
return nil
87+
}
88+
89+
// InjectDecoder injects the decoder into the Handler
90+
func (v *Validator) InjectDecoder(d types.Decoder) error {
91+
h.decoder = d
92+
return nil
4293
}
4394
*/
4495
package admission

pkg/webhook/doc.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ limitations under the License.
1515
*/
1616

1717
/*
18-
Package webhook provides functions to build and bootstrap an admission webhook server for a k8s cluster.
18+
Package webhook provides methods to build and bootstrap a webhook server.
19+
20+
Currently, it only supports hosting admission webhooks.
1921
2022
Build webhooks
2123
@@ -46,9 +48,25 @@ Build webhooks
4648
// handle error
4749
}
4850
49-
Create a server for webhooks.
51+
Create a webhook server.
5052
51-
as, err := NewServer("baz-admission-server", mrg, ServerOptions{})
53+
as, err := NewServer("baz-admission-server", mgr, ServerOptions{
54+
CertDir: "/tmp/cert",
55+
BootstrapOptions: &BootstrapOptions{
56+
Secret: &apitypes.NamespacedName{
57+
Namespace: "default",
58+
Name: "foo-admission-server-secret",
59+
},
60+
Service: &Service{
61+
Namespace: "default",
62+
Name: "foo-admission-server-service",
63+
// Selectors should select the pods that runs this webhook server.
64+
Selectors: map[string]string{
65+
"app": "foo-admission-server",
66+
},
67+
},
68+
},
69+
})
5270
if err != nil {
5371
// handle error
5472
}

0 commit comments

Comments
 (0)