Skip to content

Commit 441b0b6

Browse files
committed
Remove the extraneous name field from webhooks
It was only being used for registering metrics, and that can be done with the path instead.
1 parent 7a648a9 commit 441b0b6

File tree

3 files changed

+3
-38
lines changed

3 files changed

+3
-38
lines changed

pkg/webhook/admission/http.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var _ http.Handler = &Webhook{}
4949

5050
func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5151
startTS := time.Now()
52-
defer metrics.RequestLatency.WithLabelValues(wh.Name).Observe(time.Now().Sub(startTS).Seconds())
52+
defer metrics.RequestLatency.WithLabelValues(wh.Path).Observe(time.Now().Sub(startTS).Seconds())
5353

5454
var body []byte
5555
var err error
@@ -100,9 +100,9 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
100100
func (wh *Webhook) writeResponse(w io.Writer, response Response) {
101101
if response.Result.Code != 0 {
102102
if response.Result.Code == http.StatusOK {
103-
metrics.TotalRequests.WithLabelValues(wh.Name, "true").Inc()
103+
metrics.TotalRequests.WithLabelValues(wh.Path, "true").Inc()
104104
} else {
105-
metrics.TotalRequests.WithLabelValues(wh.Name, "false").Inc()
105+
metrics.TotalRequests.WithLabelValues(wh.Path, "false").Inc()
106106
}
107107
}
108108

pkg/webhook/admission/webhook.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"net/http"
23-
"regexp"
24-
"strings"
25-
"sync"
2623

2724
"github.com/appscode/jsonpatch"
2825
admissionv1beta1 "k8s.io/api/admission/v1beta1"
@@ -108,23 +105,11 @@ func (f HandlerFunc) Handle(ctx context.Context, req Request) Response {
108105

109106
// Webhook represents each individual webhook.
110107
type Webhook struct {
111-
// Name is the name of the webhook
112-
Name string
113108
// Path is the path this webhook will serve.
114109
Path string
115110
// Handler actually processes an admission request returning whether it was allowed or denied,
116111
// and potentially patches to apply to the handler.
117112
Handler Handler
118-
119-
once sync.Once
120-
}
121-
122-
func (w *Webhook) setDefaults() {
123-
if len(w.Name) == 0 {
124-
reg := regexp.MustCompile("[^a-zA-Z0-9]+")
125-
processedPath := strings.ToLower(reg.ReplaceAllString(w.Path, ""))
126-
w.Name = processedPath + ".example.com"
127-
}
128113
}
129114

130115
// Webhook implements Handler interface.
@@ -144,24 +129,13 @@ func (w *Webhook) Handle(ctx context.Context, req Request) Response {
144129
return resp
145130
}
146131

147-
// GetName returns the name of the webhook.
148-
func (w *Webhook) GetName() string {
149-
w.once.Do(w.setDefaults)
150-
return w.Name
151-
}
152-
153132
// GetPath returns the path that the webhook registered.
154133
func (w *Webhook) GetPath() string {
155-
w.once.Do(w.setDefaults)
156134
return w.Path
157135
}
158136

159137
// Validate validates if the webhook is valid.
160138
func (w *Webhook) Validate() error {
161-
w.once.Do(w.setDefaults)
162-
if len(w.Name) == 0 {
163-
return errors.New("field Name should not be empty")
164-
}
165139
if len(w.Path) == 0 {
166140
return errors.New("field Path should not be empty")
167141
}

pkg/webhook/admission/webhook_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,6 @@ var _ = Describe("Admission Webhooks", func() {
124124
Expect(resp.Patch).To(Equal([]byte(`[{"op":"add","path":"/a","value":2},{"op":"replace","path":"/b","value":4}]`)))
125125
})
126126

127-
It("should default a name if none is given", func() {
128-
wh := &Webhook{
129-
Path: "/somepath",
130-
Handler: &fakeHandler{},
131-
}
132-
133-
Expect(wh.GetName()).NotTo(BeEmpty())
134-
})
135-
136127
Context("validation", func() {
137128
It("should accept a properly populated webhook", func() {
138129
wh := &Webhook{

0 commit comments

Comments
 (0)