@@ -52,19 +52,24 @@ type NamespaceQuotaValidator struct {
5252
5353// Handle handles the admission requests
5454func (v * NamespaceQuotaValidator ) Handle (ctx context.Context , req admission.Request ) admission.Response {
55- l := log .FromContext (ctx ).
55+ ctx = log . IntoContext ( ctx , log .FromContext (ctx ).
5656 WithName ("webhook.validate-namespace-quota.appuio.io" ).
5757 WithValues ("id" , req .UID , "user" , req .UserInfo .Username ).
5858 WithValues ("namespace" , req .Namespace , "name" , req .Name ,
59- "group" , req .Kind .Group , "version" , req .Kind .Version , "kind" , req .Kind .Kind )
59+ "group" , req .Kind .Group , "version" , req .Kind .Version , "kind" , req .Kind .Kind ))
60+
61+ return logAdmissionResponse (ctx , v .handle (ctx , req ))
62+ }
63+
64+ func (v * NamespaceQuotaValidator ) handle (ctx context.Context , req admission.Request ) admission.Response {
65+ l := log .FromContext (ctx )
6066
6167 skip , err := v .Skipper .Skip (ctx , req )
6268 if err != nil {
6369 l .Error (err , "error while checking skipper" )
6470 return admission .Errored (http .StatusInternalServerError , err )
6571 }
6672 if skip {
67- l .V (1 ).Info ("allowed: skipped" )
6873 return admission .Allowed ("skipped" )
6974 }
7075
@@ -102,7 +107,6 @@ func (v *NamespaceQuotaValidator) Handle(ctx context.Context, req admission.Requ
102107 }
103108
104109 if v .SkipValidateQuota {
105- l .V (1 ).Info ("allowed: skipped quota validation" )
106110 return admission .Allowed ("skipped quota validation" )
107111 }
108112
@@ -140,11 +144,28 @@ func (v *NamespaceQuotaValidator) Handle(ctx context.Context, req admission.Requ
140144 return admission .Errored (http .StatusInternalServerError , err )
141145 }
142146 if len (nsList .Items ) >= nsCountLimit {
143- l .V (1 ).Info ("denied: namespace count limit reached" , "limit" , nsCountLimit , "count" , len (nsList .Items ))
144147 return admission .Denied (fmt .Sprintf (
145148 "You cannot create more than %d namespaces for organization %q. Please contact support to have your quota raised." ,
146149 nsCountLimit , organizationName ))
147150 }
148151
149152 return admission .Allowed ("allowed" )
150153}
154+
155+ // logAdmissionResponse logs the admission response to the logger derived from the given context and returns it unchanged.
156+ func logAdmissionResponse (ctx context.Context , res admission.Response ) admission.Response {
157+ l := log .FromContext (ctx )
158+
159+ rmsg := "<not given>"
160+ if res .Result != nil {
161+ rmsg = res .Result .Message
162+ }
163+ msg := "denied"
164+ if res .Allowed {
165+ msg = "allowed"
166+ }
167+
168+ l .Info (msg , "admission_message" , rmsg )
169+
170+ return res
171+ }
0 commit comments