@@ -18,18 +18,24 @@ package webhook_test
18
18
19
19
import (
20
20
"context"
21
+ "net/http"
21
22
23
+ "k8s.io/client-go/kubernetes/scheme"
22
24
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"
23
27
. "sigs.k8s.io/controller-runtime/pkg/webhook"
24
28
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
25
29
)
26
30
27
- func Example () {
28
- // Build webhooks
31
+ var (
32
+ // Build webhooks used for the various server
33
+ // configuration options
34
+ //
29
35
// These handlers could be also be implementations
30
36
// of the AdmissionHandler interface for more complex
31
37
// implementations.
32
- mutatingHook : = & Admission {
38
+ mutatingHook = & Admission {
33
39
Handler : admission .HandlerFunc (func (ctx context.Context , req AdmissionRequest ) AdmissionResponse {
34
40
return Patched ("some changes" ,
35
41
JSONPatchOp {Operation : "add" , Path : "/metadata/annotations/access" , Value : "granted" },
@@ -38,12 +44,16 @@ func Example() {
38
44
}),
39
45
}
40
46
41
- validatingHook : = & Admission {
47
+ validatingHook = & Admission {
42
48
Handler : admission .HandlerFunc (func (ctx context.Context , req AdmissionRequest ) AdmissionResponse {
43
49
return Denied ("none shall pass!" )
44
50
}),
45
51
}
52
+ )
46
53
54
+ // This example registers a webhooks to a webhook server
55
+ // that gets ran by a controller manager.
56
+ func Example () {
47
57
// Create a manager
48
58
// Note: GetConfigOrDie will os.Exit(1) w/o any message if no kube-config can be found
49
59
mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {})
@@ -70,3 +80,70 @@ func Example() {
70
80
panic (err )
71
81
}
72
82
}
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