Skip to content

Commit 752480a

Browse files
author
Mengqi Yu
committed
add identity support and disable knob for handler
1 parent 79f0601 commit 752480a

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

pkg/webhook/admission/builder/builder.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/util/sets"
2627
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2728
"sigs.k8s.io/controller-runtime/pkg/manager"
2829
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -43,6 +44,12 @@ type WebhookBuilder struct {
4344
// handlers[1] mutates a pod for a different feature bar.
4445
handlers []admission.Handler
4546

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+
4653
// t specifies the type of the webhook.
4754
// Currently, Mutating and Validating are supported.
4855
t *types.WebhookType
@@ -156,6 +163,12 @@ func (b *WebhookBuilder) Handlers(handlers ...admission.Handler) *WebhookBuilder
156163
return b
157164
}
158165

166+
// DisableHandlers disable handlers based on name..
167+
func (b *WebhookBuilder) DisableHandlers(names ...string) *WebhookBuilder {
168+
b.disabledNames = names
169+
return b
170+
}
171+
159172
func (b *WebhookBuilder) validate() error {
160173
if b.t == nil {
161174
return errors.New("webhook type cannot be nil")
@@ -169,13 +182,29 @@ func (b *WebhookBuilder) validate() error {
169182
return nil
170183
}
171184

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+
172199
// Build creates the Webhook based on the options provided.
173200
func (b *WebhookBuilder) Build() (*admission.Webhook, error) {
174201
err := b.validate()
175202
if err != nil {
176203
return nil, err
177204
}
178205

206+
b.disableHandlers()
207+
179208
w := &admission.Webhook{
180209
Name: b.name,
181210
Type: *b.t,

pkg/webhook/admission/webhook.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ type Handler interface {
4242
Handle(context.Context, atypes.Request) atypes.Response
4343
}
4444

45+
// NameGetter gets the name of the Handler
46+
type NameGetter interface {
47+
// Name returns the identity of the Handler.
48+
Name() string
49+
}
50+
4551
// HandlerFunc implements Handler interface using a single function.
4652
type HandlerFunc func(context.Context, atypes.Request) atypes.Response
4753

pkg/webhook/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type BootstrapOptions struct {
8989
// If neither Service nor Host is unspecified, Host will be defaulted to "localhost".
9090
Host *string
9191

92+
DisabledHandlers []string
93+
9294
// certProvisioner is constructed using certGenerator and certWriter
9395
certProvisioner *cert.Provisioner // nolint: structcheck
9496

0 commit comments

Comments
 (0)