Skip to content

Commit f85fc10

Browse files
authored
Unified validation log for NamespaceQuotaValidator (#101)
1 parent 1cbfcec commit f85fc10

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

webhooks/namespace_quota_validator.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,24 @@ type NamespaceQuotaValidator struct {
5252

5353
// Handle handles the admission requests
5454
func (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+
}

webhooks/namespace_quota_validator_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ import (
55
"testing"
66

77
controlv1 "github.com/appuio/control-api/apis/v1"
8+
"github.com/go-logr/logr/testr"
89
projectv1 "github.com/openshift/api/project/v1"
910
userv1 "github.com/openshift/api/user/v1"
1011
"github.com/stretchr/testify/require"
1112
corev1 "k8s.io/api/core/v1"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
15+
"sigs.k8s.io/controller-runtime/pkg/log"
1416

1517
cloudagentv1 "github.com/appuio/appuio-cloud-agent/api/v1"
1618
"github.com/appuio/appuio-cloud-agent/skipper"
1719
)
1820

1921
func TestNamespaceQuotaValidator_Handle(t *testing.T) {
20-
ctx := context.Background()
22+
ctx := log.IntoContext(context.Background(), testr.New(t))
23+
2124
const orgLabel = "test.io/organization"
2225
const userDefaultOrgAnnotation = "test.io/default-organization"
2326
const nsLimit = 2

0 commit comments

Comments
 (0)