Skip to content

Commit fb99b3c

Browse files
committed
add more loggings
1 parent 96b67f2 commit fb99b3c

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

pkg/webhook/admission/http.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4848
var reviewResponse Response
4949
if r.Body != nil {
5050
if body, err = ioutil.ReadAll(r.Body); err != nil {
51-
wh.log.Error(err, "unable to read the body from the incoming request")
51+
wh.log.Error(err, "unable to read the body from the incoming request", "request", r)
5252
reviewResponse = Errored(http.StatusBadRequest, err)
5353
wh.writeResponse(w, reviewResponse)
5454
return
@@ -95,7 +95,9 @@ func (wh *Webhook) writeResponse(w io.Writer, response Response) {
9595
}
9696
err := encoder.Encode(responseAdmissionReview)
9797
if err != nil {
98-
wh.log.Error(err, "unable to encode the response")
98+
wh.log.Error(err, "unable to encode the response", "response", responseAdmissionReview)
9999
wh.writeResponse(w, Errored(http.StatusInternalServerError, err))
100+
} else {
101+
wh.log.V(1).Info("wrote response", "admission response", responseAdmissionReview)
100102
}
101103
}

pkg/webhook/admission/http_test.go

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

3131
admissionv1beta1 "k8s.io/api/admission/v1beta1"
32+
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3233
)
3334

3435
var _ = Describe("Admission Webhooks", func() {
@@ -87,6 +88,7 @@ var _ = Describe("Admission Webhooks", func() {
8788
}
8889
webhook := &Webhook{
8990
Handler: &fakeHandler{},
91+
log: logf.RuntimeLog.WithName("webhook"),
9092
}
9193

9294
expected := []byte(`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"code":200}}}

pkg/webhook/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func (s *Server) Register(path string, hook http.Handler) {
100100
// TODO(directxman12): call setfields if we've already started the server
101101
s.webhooks[path] = hook
102102
s.WebhookMux.Handle(path, instrumentedHook(path, hook))
103+
log.WithName("webhooks").Info("registering webhook", "path", path)
103104
}
104105

105106
// instrumentedHook adds some instrumentation on top of the given webhook.
@@ -119,6 +120,8 @@ func (s *Server) Start(stop <-chan struct{}) error {
119120
s.defaultingOnce.Do(s.setDefaults)
120121

121122
baseHookLog := log.WithName("webhooks")
123+
baseHookLog.Info("starting webhook server")
124+
122125
// inject fields here as opposed to in Register so that we're certain to have our setFields
123126
// function available.
124127
for hookPath, webhook := range s.webhooks {
@@ -158,13 +161,16 @@ func (s *Server) Start(stop <-chan struct{}) error {
158161
return err
159162
}
160163

164+
log.Info("serving webhook server", "host", s.Host, "port", s.Port)
165+
161166
srv := &http.Server{
162167
Handler: s.WebhookMux,
163168
}
164169

165170
idleConnsClosed := make(chan struct{})
166171
go func() {
167172
<-stop
173+
log.Info("shutting down webhook server")
168174

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

0 commit comments

Comments
 (0)