Skip to content

✨ Improve webhook loggings #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/webhook/admission/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
wh.writeResponse(w, reviewResponse)
return
}
wh.log.V(1).Info("received request", "UID", req.UID, "kind", req.Kind, "resource", req.Resource)

// TODO: add panic-recovery for Handle
reviewResponse = wh.Handle(r.Context(), req)
Expand All @@ -96,5 +97,8 @@ func (wh *Webhook) writeResponse(w io.Writer, response Response) {
if err != nil {
wh.log.Error(err, "unable to encode the response")
wh.writeResponse(w, Errored(http.StatusInternalServerError, err))
} else {
res := responseAdmissionReview.Response
wh.log.V(1).Info("wrote response", "UID", res.UID, "allowed", res.Allowed, "result", res.Result)
}
}
3 changes: 3 additions & 0 deletions pkg/webhook/admission/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"

admissionv1beta1 "k8s.io/api/admission/v1beta1"
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
)

var _ = Describe("Admission Webhooks", func() {
Expand Down Expand Up @@ -88,6 +89,7 @@ var _ = Describe("Admission Webhooks", func() {
}
webhook := &Webhook{
Handler: &fakeHandler{},
log: logf.RuntimeLog.WithName("webhook"),
}

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

expected := []byte(fmt.Sprintf(`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"reason":%q,"code":200}}}
Expand Down
6 changes: 6 additions & 0 deletions pkg/webhook/admission/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
machinerytypes "k8s.io/apimachinery/pkg/types"

logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
)

Expand All @@ -45,6 +46,7 @@ var _ = Describe("Admission Webhooks", func() {
}
webhook := &Webhook{
Handler: handler,
log: logf.RuntimeLog.WithName("webhook"),
}

return webhook
Expand Down Expand Up @@ -94,6 +96,7 @@ var _ = Describe("Admission Webhooks", func() {
},
}
}),
log: logf.RuntimeLog.WithName("webhook"),
}

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

By("invoking the webhook")
Expand All @@ -135,6 +139,7 @@ var _ = Describe("Admission Webhooks", func() {
handler := &fakeHandler{}
webhook := &Webhook{
Handler: handler,
log: logf.RuntimeLog.WithName("webhook"),
}
Expect(setFields(webhook)).To(Succeed())
Expect(inject.InjectorInto(setFields, webhook)).To(BeTrue())
Expand All @@ -154,6 +159,7 @@ var _ = Describe("Admission Webhooks", func() {
handler := &fakeHandler{}
webhook := &Webhook{
Handler: handler,
log: logf.RuntimeLog.WithName("webhook"),
}
Expect(setFields(webhook)).To(Succeed())
Expect(inject.InjectorInto(setFields, webhook)).To(BeTrue())
Expand Down
6 changes: 6 additions & 0 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (s *Server) Register(path string, hook http.Handler) {
// TODO(directxman12): call setfields if we've already started the server
s.webhooks[path] = hook
s.WebhookMux.Handle(path, instrumentedHook(path, hook))
log.Info("registering webhook", "path", path)
}

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

baseHookLog := log.WithName("webhooks")
baseHookLog.Info("starting webhook server")

// inject fields here as opposed to in Register so that we're certain to have our setFields
// function available.
for hookPath, webhook := range s.webhooks {
Expand Down Expand Up @@ -164,13 +167,16 @@ func (s *Server) Start(stop <-chan struct{}) error {
return err
}

log.Info("serving webhook server", "host", s.Host, "port", s.Port)

srv := &http.Server{
Handler: s.WebhookMux,
}

idleConnsClosed := make(chan struct{})
go func() {
<-stop
log.Info("shutting down webhook server")

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