Skip to content

Commit

Permalink
Add request UID to webhook logs. (#150)
Browse files Browse the repository at this point in the history
For better debuggability when there are concurrent webhook calls.
  • Loading branch information
seizethedave authored Jun 4, 2024
1 parent 54b71c1 commit 3b0040a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [CHANGE] The docker base images are now based off distroless images rather than Alpine. #149
* The standard base image is now `gcr.io/distroless/static-debian12:nonroot`.
* The boringcrypto base image is now `gcr.io/distroless/base-nossl-debian12:nonroot` (for glibc).
* [ENHANCEMENT] Include unique IDs of webhook requests in logs for easier debugging. #150

## v0.16.0

Expand Down
1 change: 1 addition & 0 deletions pkg/admission/prep_downscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func prepareDownscale(ctx context.Context, l log.Logger, ar v1.AdmissionReview,
logger.SetSpanAndLogTag("object.resource", ar.Request.Resource.Resource)
logger.SetSpanAndLogTag("object.namespace", ar.Request.Namespace)
logger.SetSpanAndLogTag("request.dry_run", *ar.Request.DryRun)
logger.SetSpanAndLogTag("request.uid", ar.Request.UID)

if *ar.Request.DryRun {
return &v1.AdmissionResponse{Allowed: true}
Expand Down
23 changes: 20 additions & 3 deletions pkg/admission/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
v1 "k8s.io/api/admission/v1"
"k8s.io/api/admission/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -61,6 +62,8 @@ func Serve(admit AdmitV1Func, logger log.Logger, api *kubernetes.Clientset) http
return
}

var requestUid types.UID

var responseObj runtime.Object
switch *gvk {
case v1beta1.SchemeGroupVersion.WithKind("AdmissionReview"):
Expand All @@ -69,32 +72,46 @@ func Serve(admit AdmitV1Func, logger log.Logger, api *kubernetes.Clientset) http
level.Error(logger).Log("msg", "unexpected type", "type", fmt.Sprintf("%T", obj), "expected", "*v1beta1.AdmissionReview")
return
}
level.Debug(logger).Log("msg", "handling request", "kind", requestedAdmissionReview.Request.Kind, "namespace", requestedAdmissionReview.Request.Namespace, "name", requestedAdmissionReview.Request.Name)
level.Debug(logger).Log(
"msg", "handling request",
"kind", requestedAdmissionReview.Request.Kind,
"namespace", requestedAdmissionReview.Request.Namespace,
"name", requestedAdmissionReview.Request.Name,
"request.uid", requestedAdmissionReview.Request.UID,
)
responseAdmissionReview := &v1beta1.AdmissionReview{}
responseAdmissionReview.SetGroupVersionKind(*gvk)
responseAdmissionReview.Response = delegateV1beta1AdmitToV1(admit)(r.Context(), logger, *requestedAdmissionReview, api)
responseAdmissionReview.Response.UID = requestedAdmissionReview.Request.UID
responseObj = responseAdmissionReview
requestUid = requestedAdmissionReview.Request.UID
case v1.SchemeGroupVersion.WithKind("AdmissionReview"):
requestedAdmissionReview, ok := obj.(*v1.AdmissionReview)
if !ok {
level.Error(logger).Log("msg", "unexpected type", "type", fmt.Sprintf("%T", obj), "expected", "*v1.AdmissionReview")
return
}
level.Debug(logger).Log("msg", "handling request", "kind", requestedAdmissionReview.Request.Kind, "namespace", requestedAdmissionReview.Request.Namespace, "name", requestedAdmissionReview.Request.Name)
level.Debug(logger).Log(
"msg", "handling request",
"kind", requestedAdmissionReview.Request.Kind,
"namespace", requestedAdmissionReview.Request.Namespace,
"name", requestedAdmissionReview.Request.Name,
"request.uid", requestedAdmissionReview.Request.UID,
)
responseAdmissionReview := &v1.AdmissionReview{}
responseAdmissionReview.SetGroupVersionKind(*gvk)
responseAdmissionReview.Response = admit(r.Context(), logger, *requestedAdmissionReview, api)
responseAdmissionReview.Response.UID = requestedAdmissionReview.Request.UID
responseObj = responseAdmissionReview
requestUid = requestedAdmissionReview.Request.UID
default:
msg := fmt.Sprintf("Unsupported group version kind: %v", gvk)
level.Error(logger).Log("msg", "unsupported group version kind", "gvk", gvk)
http.Error(w, msg, http.StatusBadRequest)
return
}

level.Debug(logger).Log("msg", "sending response", "response", responseObj)
level.Debug(logger).Log("msg", "sending response", "request.uid", requestUid, "response", responseObj)
respBytes, err := json.Marshal(responseObj)
if err != nil {
level.Error(logger).Log("msg", "error marshaling response", "err", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/admission/zone_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (zt *zoneTracker) prepareDownscale(ctx context.Context, l log.Logger, ar v1
logger.SetSpanAndLogTag("object.resource", ar.Request.Resource.Resource)
logger.SetSpanAndLogTag("object.namespace", ar.Request.Namespace)
logger.SetSpanAndLogTag("request.dry_run", *ar.Request.DryRun)
logger.SetSpanAndLogTag("request.uid", ar.Request.UID)

if *ar.Request.DryRun {
return &v1.AdmissionResponse{Allowed: true}
Expand Down

0 comments on commit 3b0040a

Please sign in to comment.