Skip to content
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

Add request UID to webhook logs. #150

Merged
merged 2 commits into from
Jun 4, 2024
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
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