Skip to content

Commit ef1d34e

Browse files
author
Mengqi Yu
committed
change how to set webhook type and how we default path in builder
1 parent 2681764 commit ef1d34e

File tree

4 files changed

+59
-52
lines changed

4 files changed

+59
-52
lines changed

example/main.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"sigs.k8s.io/controller-runtime/pkg/source"
3535
"sigs.k8s.io/controller-runtime/pkg/webhook"
3636
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"
37-
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
3837
)
3938

4039
var log = logf.Log.WithName("example-controller")
@@ -76,8 +75,7 @@ func main() {
7675
// Setup webhooks
7776
mutatingWebhook, err := builder.NewWebhookBuilder().
7877
Name("mutating.k8s.io").
79-
Type(types.WebhookTypeMutating).
80-
Path("/mutating-pods").
78+
Mutating().
8179
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
8280
WithManager(mgr).
8381
ForType(&corev1.Pod{}).
@@ -90,8 +88,7 @@ func main() {
9088

9189
validatingWebhook, err := builder.NewWebhookBuilder().
9290
Name("validating.k8s.io").
93-
Type(types.WebhookTypeValidating).
94-
Path("/validating-pods").
91+
Validating().
9592
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
9693
WithManager(mgr).
9794
ForType(&corev1.Pod{}).
@@ -103,7 +100,7 @@ func main() {
103100
}
104101

105102
as, err := webhook.NewServer("foo-admission-server", mgr, webhook.ServerOptions{
106-
Port: 443,
103+
Port: 9876,
107104
CertDir: "/tmp/cert",
108105
KVMap: map[string]interface{}{"foo": "bar"},
109106
BootstrapOptions: &webhook.BootstrapOptions{

example/mutatingwebhook.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ func mutatePodsFn(ctx context.Context, pod *corev1.Pod) error {
5959
if !ok {
6060
return fmt.Errorf("the value associated with %v is expected to be a string", "foo")
6161
}
62-
anno := pod.GetAnnotations()
62+
anno := pod.Annotations
63+
if anno == nil {
64+
anno = map[string]string{}
65+
}
6366
anno["example-mutating-admission-webhook"] = v
64-
pod.SetAnnotations(anno)
67+
pod.Annotations = anno
6568
return nil
6669
}

pkg/webhook/admission/builder/builder.go

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,19 @@ func (b *WebhookBuilder) Name(name string) *WebhookBuilder {
7373
return b
7474
}
7575

76-
// Type sets the type of the admission webhook
77-
// This is required
78-
func (b *WebhookBuilder) Type(t types.WebhookType) *WebhookBuilder {
79-
b.t = &t
76+
// Mutating sets the type to mutating admission webhook
77+
// Only one of Mutating and Validating can be invoked.
78+
func (b *WebhookBuilder) Mutating() *WebhookBuilder {
79+
m := types.WebhookTypeMutating
80+
b.t = &m
81+
return b
82+
}
83+
84+
// Validating sets the type to validating admission webhook
85+
// Only one of Mutating and Validating can be invoked.
86+
func (b *WebhookBuilder) Validating() *WebhookBuilder {
87+
m := types.WebhookTypeValidating
88+
b.t = &m
8089
return b
8190
}
8291

@@ -165,48 +174,48 @@ func (b *WebhookBuilder) Build() (*admission.Webhook, error) {
165174
Handlers: b.handlers,
166175
}
167176

168-
if len(b.path) == 0 {
169-
if *b.t == types.WebhookTypeMutating {
170-
b.path = "/mutatingwebhook"
171-
} else if *b.t == types.WebhookTypeValidating {
172-
b.path = "/validatingwebhook"
173-
}
174-
}
175-
w.Path = b.path
176-
177177
if b.rules != nil {
178178
w.Rules = b.rules
179-
return w, nil
180-
}
179+
} else {
180+
if b.manager == nil {
181+
return nil, errors.New("manager should be set using WithManager")
182+
}
183+
gvk, err := apiutil.GVKForObject(b.apiType, b.manager.GetScheme())
184+
if err != nil {
185+
return nil, err
186+
}
187+
mapper := b.manager.GetRESTMapper()
188+
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
189+
if err != nil {
190+
return nil, err
191+
}
181192

182-
if b.manager == nil {
183-
return nil, errors.New("manager should be set using WithManager")
184-
}
185-
gvk, err := apiutil.GVKForObject(b.apiType, b.manager.GetScheme())
186-
if err != nil {
187-
return nil, err
188-
}
189-
mapper := b.manager.GetRESTMapper()
190-
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
191-
if err != nil {
192-
return nil, err
193+
if b.operations == nil {
194+
b.operations = []admissionregistrationv1beta1.OperationType{
195+
admissionregistrationv1beta1.Create,
196+
admissionregistrationv1beta1.Update,
197+
}
198+
}
199+
w.Rules = []admissionregistrationv1beta1.RuleWithOperations{
200+
{
201+
Operations: b.operations,
202+
Rule: admissionregistrationv1beta1.Rule{
203+
APIGroups: []string{gvk.Group},
204+
APIVersions: []string{gvk.Version},
205+
Resources: []string{mapping.Resource.Resource},
206+
},
207+
},
208+
}
193209
}
194210

195-
if b.operations == nil {
196-
b.operations = []admissionregistrationv1beta1.OperationType{
197-
admissionregistrationv1beta1.Create,
198-
admissionregistrationv1beta1.Update,
211+
if len(b.path) == 0 {
212+
if *b.t == types.WebhookTypeMutating {
213+
b.path = "/mutate-" + w.Rules[0].Resources[0]
214+
} else if *b.t == types.WebhookTypeValidating {
215+
b.path = "/validate-" + w.Rules[0].Resources[0]
199216
}
200217
}
201-
w.Rules = []admissionregistrationv1beta1.RuleWithOperations{
202-
{
203-
Operations: b.operations,
204-
Rule: admissionregistrationv1beta1.Rule{
205-
APIGroups: []string{gvk.Group},
206-
APIVersions: []string{gvk.Version},
207-
Resources: []string{mapping.Resource.Resource},
208-
},
209-
},
210-
}
218+
w.Path = b.path
219+
211220
return w, nil
212221
}

pkg/webhook/admission/doc.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ Build webhooks
2121
2222
webhook1, err := NewWebhookBuilder().
2323
Name("foo.k8s.io").
24-
Type(WebhookTypeMutating).
25-
Path("/mutating-pods").
24+
Mutating().
2625
Operations(admissionregistrationv1beta1.Create).
2726
ForType(&corev1.Pod{}).
2827
WithManager(mgr).
@@ -33,8 +32,7 @@ Build webhooks
3332
3433
webhook2, err := NewWebhookBuilder().
3534
Name("bar.k8s.io").
36-
Type(WebhookTypeValidating).
37-
Path("/validating-deployment").
35+
Validating().
3836
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
3937
ForType(&appsv1.Deployment{}).
4038
WithManager(mgr).

0 commit comments

Comments
 (0)