Skip to content

Commit 6ef96e6

Browse files
authored
Merge pull request #10 from appuio/change/debug-mode
Improve logging
2 parents 70599c2 + 038dc26 commit 6ef96e6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ build-docker: build-bin ## Build docker image
3232

3333
.PHONY: run
3434
run:
35-
go run . -webhook-cert-dir webhook-certs
35+
go run . -webhook-cert-dir webhook-certs -zap-devel -zap-log-level info
3636

3737
.PHONY: test
3838
test: test-go ## All-in-one test

webhooks/ratio_validator.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,55 @@ type RatioValidator struct {
2929

3030
// Handle handles the admission requests
3131
func (v *RatioValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
32+
l := log.FromContext(ctx).
33+
WithName("webhook.validate-request-ratio.appuio.io").
34+
WithValues("id", req.UID, "user", req.UserInfo.Username).
35+
WithValues("namespace", req.Namespace, "name", req.Name, "kind", req.Kind.Kind)
36+
3237
if strings.HasPrefix(req.UserInfo.Username, "system:") {
3338
// Is service account or kube system user: https://kubernetes.io/docs/reference/access-authn-authz/rbac/#referring-to-subjects
39+
l.V(1).Info("allowed: system user")
3440
return admission.Allowed("system user")
3541
}
3642

37-
l := log.FromContext(ctx).WithName("webhook.validate-request-ratio.appuio.io")
38-
l.V(3).WithValues("kind", req.Kind.Kind, "namespace", req.Namespace).Info("handling request")
39-
4043
r, err := v.getRatio(ctx, req.Namespace)
4144
if err != nil {
45+
l.Error(err, "failed to get ratio")
4246
return errored(http.StatusInternalServerError, err)
4347
}
4448

49+
l = l.WithValues("current_ratio", r.String())
4550
// If we are creating an object with resource requests, we add them to the current ratio
4651
// We cannot easily do this when updating resources.
4752
if req.Operation == admissionv1.Create {
4853
switch req.Kind.Kind {
4954
case "Pod":
5055
pod := corev1.Pod{}
5156
if err := v.decoder.Decode(req, &pod); err != nil {
57+
l.Error(err, "failed to decode pod")
5258
return errored(http.StatusBadRequest, err)
5359
}
5460
r = r.RecordPod(pod)
5561
case "Deployment":
5662
deploy := appsv1.Deployment{}
5763
if err := v.decoder.Decode(req, &deploy); err != nil {
64+
l.Error(err, "failed to decode deployment")
5865
return errored(http.StatusBadRequest, err)
5966
}
6067
r = r.RecordDeployment(deploy)
6168
case "StatefulSet":
6269
sts := appsv1.StatefulSet{}
6370
if err := v.decoder.Decode(req, &sts); err != nil {
71+
l.Error(err, "failed to decode statefulset")
6472
return errored(http.StatusBadRequest, err)
6573
}
6674
r = r.RecordStatefulSet(sts)
6775
}
6876
}
77+
l = l.WithValues("ratio", r.String())
6978

7079
if r.Below(*v.RatioLimit) {
80+
l.Info("warned: ratio too low")
7181
return admission.Response{
7282
AdmissionResponse: admissionv1.AdmissionResponse{
7383
Allowed: true,
@@ -77,6 +87,7 @@ func (v *RatioValidator) Handle(ctx context.Context, req admission.Request) admi
7787
},
7888
}}
7989
}
90+
l.V(1).Info("allowed: ratio ok")
8091
return admission.Allowed("ok")
8192
}
8293

0 commit comments

Comments
 (0)