@@ -23,6 +23,7 @@ import (
23
23
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
24
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
25
"k8s.io/apimachinery/pkg/runtime"
26
+ "k8s.io/apimachinery/pkg/util/sets"
26
27
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
27
28
"sigs.k8s.io/controller-runtime/pkg/manager"
28
29
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -43,6 +44,12 @@ type WebhookBuilder struct {
43
44
// handlers[1] mutates a pod for a different feature bar.
44
45
handlers []admission.Handler
45
46
47
+ // disabledNames are names of handlers that will be disabled.
48
+ // each handler in handlers will be scanned.
49
+ // If the handler has implemented NameGetter interface and the name is the disabledNames list,
50
+ // it will not be register to the webhook when invoking Build method.
51
+ disabledNames []string
52
+
46
53
// t specifies the type of the webhook.
47
54
// Currently, Mutating and Validating are supported.
48
55
t * types.WebhookType
@@ -156,6 +163,12 @@ func (b *WebhookBuilder) Handlers(handlers ...admission.Handler) *WebhookBuilder
156
163
return b
157
164
}
158
165
166
+ // DisableHandlers disable handlers based on name..
167
+ func (b * WebhookBuilder ) DisableHandlers (names ... string ) * WebhookBuilder {
168
+ b .disabledNames = names
169
+ return b
170
+ }
171
+
159
172
func (b * WebhookBuilder ) validate () error {
160
173
if b .t == nil {
161
174
return errors .New ("webhook type cannot be nil" )
@@ -169,13 +182,29 @@ func (b *WebhookBuilder) validate() error {
169
182
return nil
170
183
}
171
184
185
+ func (b * WebhookBuilder ) disableHandlers () {
186
+ if len (b .disabledNames ) != 0 {
187
+ set := sets .NewString (b .disabledNames ... )
188
+ var handlers []admission.Handler
189
+ for _ , handler := range b .handlers {
190
+ if namedHandler , ok := handler .(admission.NameGetter ); ok && set .Has (namedHandler .Name ()) {
191
+ continue
192
+ }
193
+ handlers = append (handlers , handler )
194
+ }
195
+ b .handlers = handlers
196
+ }
197
+ }
198
+
172
199
// Build creates the Webhook based on the options provided.
173
200
func (b * WebhookBuilder ) Build () (* admission.Webhook , error ) {
174
201
err := b .validate ()
175
202
if err != nil {
176
203
return nil , err
177
204
}
178
205
206
+ b .disableHandlers ()
207
+
179
208
w := & admission.Webhook {
180
209
Name : b .name ,
181
210
Type : * b .t ,
0 commit comments