Skip to content

Commit e08eee7

Browse files
committed
Move webhook examples to an actual Go example file
This moves the examples to an actual Go example file, so that we can be sure that they actually compile.
1 parent 107c8e9 commit e08eee7

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

pkg/webhook/alias.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package webhook
1818

1919
import (
20+
"github.com/appscode/jsonpatch"
2021
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2122
)
2223

@@ -47,6 +48,9 @@ type AdmissionHandler = admission.Handler
4748
// AdmissionDecoder knows how to decode objects from admission requests.
4849
type AdmissionDecoder = admission.Decoder
4950

51+
// JSONPatchOp represents a single JSONPatch patch operation.
52+
type JSONPatchOp = jsonpatch.Operation
53+
5054
var (
5155
// Allowed indicates that the admission request should be allowed for the given reason.
5256
Allowed = admission.Allowed

pkg/webhook/doc.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,6 @@ limitations under the License.
1818
Package webhook provides methods to build and bootstrap a webhook server.
1919
2020
Currently, it only supports admission webhooks. It will support CRD conversion webhooks in the near future.
21-
22-
Build webhooks
23-
24-
webhook1 := &Webhook{
25-
Path: "/mutating-pods",
26-
Handler: mutatingHandler,
27-
}
28-
29-
webhook2 := &Webhook{
30-
Path: "/validating-pods",
31-
Handler: validatingHandler,
32-
}
33-
34-
Create a webhook server.
35-
36-
hookServer := &Server{
37-
CertDir: "/tmp/cert",
38-
}
39-
mgr.Add(hookServer)
40-
41-
Register the webhooks in the server.
42-
43-
err = hookServer.Register(webhook1, webhook2)
44-
if err != nil {
45-
// handle error
46-
}
47-
48-
Start the server by starting the manager
49-
50-
err := mgr.Start(signals.SetupSignalHandler())
51-
if err != nil {
52-
// handle error
53-
}
5421
*/
5522
package webhook
5623

pkg/webhook/example_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
package webhook_test
18+
19+
import (
20+
"context"
21+
22+
ctrl "sigs.k8s.io/controller-runtime"
23+
. "sigs.k8s.io/controller-runtime/pkg/webhook"
24+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
25+
)
26+
27+
var (
28+
mgr ctrl.Manager
29+
)
30+
31+
func Example() {
32+
// Build webhooks
33+
// These handlers could be also be implementations
34+
// of the AdmissionHandler interface for more complex
35+
// implementations.
36+
mutatingHook := &Admission{
37+
Handler: admission.HandlerFunc(func(ctx context.Context, req AdmissionRequest) AdmissionResponse {
38+
return Patched("some changes",
39+
JSONPatchOp{Operation: "add", Path: "/metadata/annotations/access", Value: "granted"},
40+
JSONPatchOp{Operation: "add", Path: "/metadata/annotations/reason", Value: "not so secret"},
41+
)
42+
}),
43+
}
44+
45+
validatingHook := &Admission{
46+
Handler: admission.HandlerFunc(func(ctx context.Context, req AdmissionRequest) AdmissionResponse {
47+
return Denied("none shall pass!")
48+
}),
49+
}
50+
51+
// Create a webhook server.
52+
hookServer := &Server{
53+
Port: 8443,
54+
}
55+
mgr.Add(hookServer)
56+
57+
// Register the webhooks in the server.
58+
hookServer.Register("/mutating", mutatingHook)
59+
hookServer.Register("/validating", validatingHook)
60+
61+
// Start the server by starting a previously-set-up manager
62+
err := mgr.Start(ctrl.SetupSignalHandler())
63+
if err != nil {
64+
// handle error
65+
panic(err)
66+
}
67+
}

0 commit comments

Comments
 (0)