@@ -29,45 +29,55 @@ type RatioValidator struct {
2929
3030// Handle handles the admission requests
3131func (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