Skip to content

Commit 02699bd

Browse files
committed
Simplify webhook path handling
1 parent f34a9a1 commit 02699bd

File tree

2 files changed

+5
-15
lines changed

2 files changed

+5
-15
lines changed

pkg/webhook/sharder/add.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
)
3333

3434
// webhookPathPrefix is the path prefix at which the handler should be registered.
35-
const webhookPathPrefix = "/webhooks/sharder/"
35+
const webhookPathPrefix = "/webhooks/sharder/controllerring/"
3636

3737
// AddToManager adds Handler to the given manager.
3838
func (h *Handler) AddToManager(mgr manager.Manager) error {
@@ -53,12 +53,10 @@ func (h *Handler) AddToManager(mgr manager.Manager) error {
5353
return nil
5454
}
5555

56-
const pathControllerRing = "controllerring"
57-
5856
// WebhookPathForControllerRing returns the webhook handler path that should be used for implementing the given
5957
// ControllerRing. It is the reverse of ControllerRingForWebhookPath.
6058
func WebhookPathForControllerRing(ring *shardingv1alpha1.ControllerRing) string {
61-
return path.Join(webhookPathPrefix, pathControllerRing, ring.Name)
59+
return path.Join(webhookPathPrefix, ring.Name)
6260
}
6361

6462
// ControllerRingForWebhookPath returns the ControllerRing that is associated with the given webhook handler path.
@@ -68,18 +66,12 @@ func ControllerRingForWebhookPath(requestPath string) (*shardingv1alpha1.Control
6866
return nil, fmt.Errorf("unexpected request path: %s", requestPath)
6967
}
7068

71-
parts := strings.SplitN(strings.TrimPrefix(requestPath, webhookPathPrefix), "/", 3)
72-
if len(parts) != 2 {
73-
return nil, fmt.Errorf("unexpected request path: %s", requestPath)
74-
}
75-
if parts[0] != pathControllerRing {
76-
return nil, fmt.Errorf("unexpected request path: %s", requestPath)
77-
}
78-
if parts[1] == "" {
69+
name := strings.TrimPrefix(requestPath, webhookPathPrefix)
70+
if name == "" {
7971
return nil, fmt.Errorf("unexpected request path: %s", requestPath)
8072
}
8173

82-
return &shardingv1alpha1.ControllerRing{ObjectMeta: metav1.ObjectMeta{Name: parts[1]}}, nil
74+
return &shardingv1alpha1.ControllerRing{ObjectMeta: metav1.ObjectMeta{Name: name}}, nil
8375
}
8476

8577
type ctxKey int

pkg/webhook/sharder/add_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ var _ = Describe("webhook path", func() {
4646

4747
Expect(ControllerRingForWebhookPath("/foo")).Error().To(matchError)
4848
Expect(ControllerRingForWebhookPath("/webhooks")).Error().To(matchError)
49-
Expect(ControllerRingForWebhookPath("/webhooks/foo")).Error().To(matchError)
5049
Expect(ControllerRingForWebhookPath("/webhooks/sharder")).Error().To(matchError)
5150
Expect(ControllerRingForWebhookPath("/webhooks/sharder/controllerring")).Error().To(matchError)
5251
Expect(ControllerRingForWebhookPath("/webhooks/sharder/controllerring/")).Error().To(matchError)
53-
Expect(ControllerRingForWebhookPath("/webhooks/sharder/foo/bar")).Error().To(matchError)
5452
})
5553

5654
It("should return a ControllerRing with name", func() {

0 commit comments

Comments
 (0)