Skip to content

Commit 4dc10f9

Browse files
committed
Add examples
1 parent 9a73ff4 commit 4dc10f9

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

pkg/webhook/admission/webhook.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,10 @@ type StandaloneOptions struct {
219219
// Logger to be used by the webhook.
220220
// If none is set, it defaults to log.Log global logger.
221221
Logger logr.Logger
222-
// Path the webhook will be served at.
223-
// Used for labelling prometheus metrics.
224-
Path string
222+
// MetricsPath is used for labelling prometheus metrics
223+
// by the path is served on.
224+
// If none is set, prometheus metrics will not be generated.
225+
MetricsPath string
225226
}
226227

227228
// StandaloneWebhook prepares a webhook for use without a webhook.Server,
@@ -245,8 +246,8 @@ func StandaloneWebhook(hook *Webhook, opts StandaloneOptions) (http.Handler, err
245246
}
246247
hook.log = opts.Logger
247248

248-
if opts.Path == "" {
249+
if opts.MetricsPath == "" {
249250
return hook, nil
250251
}
251-
return metrics.InstrumentedHook(opts.Path, hook), nil
252+
return metrics.InstrumentedHook(opts.MetricsPath, hook), nil
252253
}

pkg/webhook/example_test.go

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ package webhook_test
1818

1919
import (
2020
"context"
21+
"net/http"
2122

23+
"k8s.io/client-go/kubernetes/scheme"
2224
ctrl "sigs.k8s.io/controller-runtime"
25+
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
26+
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
2327
. "sigs.k8s.io/controller-runtime/pkg/webhook"
2428
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2529
)
2630

27-
func Example() {
28-
// Build webhooks
31+
var (
32+
// Build webhooks used for the various server
33+
// configuration options
34+
//
2935
// These handlers could be also be implementations
3036
// of the AdmissionHandler interface for more complex
3137
// implementations.
32-
mutatingHook := &Admission{
38+
mutatingHook = &Admission{
3339
Handler: admission.HandlerFunc(func(ctx context.Context, req AdmissionRequest) AdmissionResponse {
3440
return Patched("some changes",
3541
JSONPatchOp{Operation: "add", Path: "/metadata/annotations/access", Value: "granted"},
@@ -38,12 +44,16 @@ func Example() {
3844
}),
3945
}
4046

41-
validatingHook := &Admission{
47+
validatingHook = &Admission{
4248
Handler: admission.HandlerFunc(func(ctx context.Context, req AdmissionRequest) AdmissionResponse {
4349
return Denied("none shall pass!")
4450
}),
4551
}
52+
)
4653

54+
// This example registers a webhooks to a webhook server
55+
// that gets ran by a controller manager.
56+
func Example() {
4757
// Create a manager
4858
// Note: GetConfigOrDie will os.Exit(1) w/o any message if no kube-config can be found
4959
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
@@ -70,3 +80,70 @@ func Example() {
7080
panic(err)
7181
}
7282
}
83+
84+
// This example creates a webhook server that can be
85+
// ran without a controller manager.
86+
func ExampleStandaloneServer() {
87+
// Create a webhook server
88+
hookServer := &Server{
89+
Port: 8443,
90+
}
91+
92+
// Register the webhooks in the server.
93+
hookServer.Register("/mutating", mutatingHook)
94+
hookServer.Register("/validating", validatingHook)
95+
96+
// Start the server without a manger
97+
err := hookServer.StartStandalone(signals.SetupSignalHandler(), scheme.Scheme)
98+
if err != nil {
99+
// handle error
100+
panic(err)
101+
}
102+
}
103+
104+
// This example creates a standalone webhook handler
105+
// and runs it on a vanilla go HTTP server to demonstrate
106+
// how you could run a webhook on an existing server
107+
// without a controller manager.
108+
func ExampleArbitraryHTTPServer() {
109+
// Assume you have an existing HTTP server at your disposal
110+
// configured as desired (e.g. with TLS).
111+
// For this example just create a basic http.ServeMux
112+
mux := http.NewServeMux()
113+
port := ":8000"
114+
115+
// Create the standalone HTTP handlers from our webhooks
116+
mutatingHookHandler, err := admission.StandaloneWebhook(mutatingHook, admission.StandaloneOptions{
117+
Scheme: scheme.Scheme,
118+
// Logger let's you optionally pass
119+
// a custom logger (defaults to log.Log global Logger)
120+
Logger: logf.RuntimeLog.WithName("mutating-webhook"),
121+
// MetricsPath let's you optionally
122+
// provide the path it will be served on
123+
// to be used for labelling prometheus metrics
124+
// If none is set, prometheus metrics will not be generated.
125+
MetricsPath: "/mutating",
126+
})
127+
if err != nil {
128+
// handle error
129+
panic(err)
130+
}
131+
132+
validatingHookHandler, err := admission.StandaloneWebhook(validatingHook, admission.StandaloneOptions{
133+
Scheme: scheme.Scheme,
134+
Logger: logf.RuntimeLog.WithName("validating-webhook"),
135+
MetricsPath: "/validating",
136+
})
137+
if err != nil {
138+
// handle error
139+
panic(err)
140+
}
141+
142+
// Register the webhook handlers to your server
143+
mux.Handle("/mutating", mutatingHookHandler)
144+
mux.Handle("/validating", validatingHookHandler)
145+
146+
// Run your handler
147+
http.ListenAndServe(port, mux)
148+
149+
}

0 commit comments

Comments
 (0)