Skip to content

Commit ad57a97

Browse files
authored
Merge pull request #439 from anthonyho007/improve-loggings
✨ Improve webhook loggings
2 parents ca33b65 + 13d4073 commit ad57a97

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

pkg/webhook/admission/http.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8181
wh.writeResponse(w, reviewResponse)
8282
return
8383
}
84+
wh.log.V(1).Info("received request", "UID", req.UID, "kind", req.Kind, "resource", req.Resource)
8485

8586
// TODO: add panic-recovery for Handle
8687
reviewResponse = wh.Handle(r.Context(), req)
@@ -96,5 +97,8 @@ func (wh *Webhook) writeResponse(w io.Writer, response Response) {
9697
if err != nil {
9798
wh.log.Error(err, "unable to encode the response")
9899
wh.writeResponse(w, Errored(http.StatusInternalServerError, err))
100+
} else {
101+
res := responseAdmissionReview.Response
102+
wh.log.V(1).Info("wrote response", "UID", res.UID, "allowed", res.Allowed, "result", res.Result)
99103
}
100104
}

pkg/webhook/admission/http_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3131

3232
admissionv1beta1 "k8s.io/api/admission/v1beta1"
33+
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3334
)
3435

3536
var _ = Describe("Admission Webhooks", func() {
@@ -88,6 +89,7 @@ var _ = Describe("Admission Webhooks", func() {
8889
}
8990
webhook := &Webhook{
9091
Handler: &fakeHandler{},
92+
log: logf.RuntimeLog.WithName("webhook"),
9193
}
9294

9395
expected := []byte(`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"code":200}}}
@@ -111,6 +113,7 @@ var _ = Describe("Admission Webhooks", func() {
111113
return Allowed(ctx.Value(key).(string))
112114
},
113115
},
116+
log: logf.RuntimeLog.WithName("webhook"),
114117
}
115118

116119
expected := []byte(fmt.Sprintf(`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"reason":%q,"code":200}}}

pkg/webhook/admission/webhook_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/runtime"
3030
machinerytypes "k8s.io/apimachinery/pkg/types"
3131

32+
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3233
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3334
)
3435

@@ -45,6 +46,7 @@ var _ = Describe("Admission Webhooks", func() {
4546
}
4647
webhook := &Webhook{
4748
Handler: handler,
49+
log: logf.RuntimeLog.WithName("webhook"),
4850
}
4951

5052
return webhook
@@ -94,6 +96,7 @@ var _ = Describe("Admission Webhooks", func() {
9496
},
9597
}
9698
}),
99+
log: logf.RuntimeLog.WithName("webhook"),
97100
}
98101

99102
By("invoking the webhook")
@@ -110,6 +113,7 @@ var _ = Describe("Admission Webhooks", func() {
110113
Handler: HandlerFunc(func(ctx context.Context, req Request) Response {
111114
return Patched("", jsonpatch.Operation{Operation: "add", Path: "/a", Value: 2}, jsonpatch.Operation{Operation: "replace", Path: "/b", Value: 4})
112115
}),
116+
log: logf.RuntimeLog.WithName("webhook"),
113117
}
114118

115119
By("invoking the webhook")
@@ -135,6 +139,7 @@ var _ = Describe("Admission Webhooks", func() {
135139
handler := &fakeHandler{}
136140
webhook := &Webhook{
137141
Handler: handler,
142+
log: logf.RuntimeLog.WithName("webhook"),
138143
}
139144
Expect(setFields(webhook)).To(Succeed())
140145
Expect(inject.InjectorInto(setFields, webhook)).To(BeTrue())
@@ -154,6 +159,7 @@ var _ = Describe("Admission Webhooks", func() {
154159
handler := &fakeHandler{}
155160
webhook := &Webhook{
156161
Handler: handler,
162+
log: logf.RuntimeLog.WithName("webhook"),
157163
}
158164
Expect(setFields(webhook)).To(Succeed())
159165
Expect(inject.InjectorInto(setFields, webhook)).To(BeTrue())

pkg/webhook/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (s *Server) Register(path string, hook http.Handler) {
106106
// TODO(directxman12): call setfields if we've already started the server
107107
s.webhooks[path] = hook
108108
s.WebhookMux.Handle(path, instrumentedHook(path, hook))
109+
log.Info("registering webhook", "path", path)
109110
}
110111

111112
// instrumentedHook adds some instrumentation on top of the given webhook.
@@ -125,6 +126,8 @@ func (s *Server) Start(stop <-chan struct{}) error {
125126
s.defaultingOnce.Do(s.setDefaults)
126127

127128
baseHookLog := log.WithName("webhooks")
129+
baseHookLog.Info("starting webhook server")
130+
128131
// inject fields here as opposed to in Register so that we're certain to have our setFields
129132
// function available.
130133
for hookPath, webhook := range s.webhooks {
@@ -164,13 +167,16 @@ func (s *Server) Start(stop <-chan struct{}) error {
164167
return err
165168
}
166169

170+
log.Info("serving webhook server", "host", s.Host, "port", s.Port)
171+
167172
srv := &http.Server{
168173
Handler: s.WebhookMux,
169174
}
170175

171176
idleConnsClosed := make(chan struct{})
172177
go func() {
173178
<-stop
179+
log.Info("shutting down webhook server")
174180

175181
// TODO: use a context with reasonable timeout
176182
if err := srv.Shutdown(context.Background()); err != nil {

0 commit comments

Comments
 (0)