Skip to content

Commit 5c68447

Browse files
fix(api): address review feedback on TenantNamespace watch path
- Hoist user identity extraction out of the Watch goroutine; reuse a cached username and groups map across events instead of re-fetching them per event. Watch now returns Unauthorized up front when no user is present in the context, rather than failing silently per event. - Switch the per-event access-check error log to structured klog.ErrorS to comply with the project Go style guide. - Strengthen TestGet_WithAccess to assert the concrete *TenantNamespace type plus Name, Kind, and APIVersion, so type or metadata regressions fail fast. Assisted-By: Claude <[email protected]> Signed-off-by: Myasnikov Daniil <[email protected]> (cherry picked from commit 61ed7ad)
1 parent e1f6692 commit 5c68447

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

‎pkg/registry/core/tenantnamespace/rest.go‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ func (r *REST) Get(
156156
// -----------------------------------------------------------------------------
157157

158158
func (r *REST) Watch(ctx context.Context, opts *metainternal.ListOptions) (watch.Interface, error) {
159+
// Extract user identity once for the lifetime of the watch — it does not
160+
// change between events and rebuilding it per event is wasteful.
161+
u, ok := request.UserFrom(ctx)
162+
if !ok {
163+
return nil, apierrors.NewUnauthorized("user missing in context")
164+
}
165+
username := u.GetName()
166+
groups := make(map[string]struct{})
167+
for _, group := range u.GetGroups() {
168+
groups[group] = struct{}{}
169+
}
170+
159171
nsList := &corev1.NamespaceList{}
160172

161173
// Build upstream watch options with field and label selectors
@@ -224,10 +236,11 @@ func (r *REST) Watch(ctx context.Context, opts *metainternal.ListOptions) (watch
224236
}
225237
}
226238

227-
// Check if user has access to this namespace
228-
hasAccess, err := r.hasAccessToNamespace(ctx, ns.Name)
239+
// Check if user has access to this namespace using the cached
240+
// identity — avoids re-extracting user/groups on every event.
241+
hasAccess, err := r.hasAccessToNamespaceForUser(ctx, ns.Name, username, groups)
229242
if err != nil {
230-
klog.Errorf("Failed to check access for namespace %s in watch: %v", ns.Name, err)
243+
klog.ErrorS(err, "Failed to check access for namespace in watch", "namespace", ns.Name)
231244
continue
232245
}
233246
if !hasAccess {
@@ -437,12 +450,22 @@ func (r *REST) hasAccessToNamespace(
437450
if !ok {
438451
return false, fmt.Errorf("user missing in context")
439452
}
440-
441-
// Check privileged groups
442453
groups := make(map[string]struct{})
443454
for _, group := range u.GetGroups() {
444455
groups[group] = struct{}{}
445456
}
457+
return r.hasAccessToNamespaceForUser(ctx, namespace, u.GetName(), groups)
458+
}
459+
460+
// hasAccessToNamespaceForUser is the inner check that does not re-extract user
461+
// identity from context. Use this in hot paths (e.g. the Watch loop) where the
462+
// caller has already cached the user name and groups.
463+
func (r *REST) hasAccessToNamespaceForUser(
464+
ctx context.Context,
465+
namespace, username string,
466+
groups map[string]struct{},
467+
) (bool, error) {
468+
// Check privileged groups
446469
if _, ok := groups["system:masters"]; ok {
447470
return true, nil
448471
}
@@ -461,7 +484,7 @@ func (r *REST) hasAccessToNamespace(
461484
for i := range rbs.Items {
462485
for j := range rbs.Items[i].Subjects {
463486
subj := rbs.Items[i].Subjects[j]
464-
if matchesSubject(subj, rbs.Items[i].Namespace, u.GetName(), groups) {
487+
if matchesSubject(subj, rbs.Items[i].Namespace, username, groups) {
465488
return true, nil
466489
}
467490
}

‎pkg/registry/core/tenantnamespace/rest_test.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"k8s.io/apiserver/pkg/authentication/user"
1616
"k8s.io/apiserver/pkg/endpoints/request"
1717
"sigs.k8s.io/controller-runtime/pkg/client/fake"
18+
19+
corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1"
1820
)
1921

2022
func TestMakeListSortsAlphabetically(t *testing.T) {
@@ -408,8 +410,18 @@ func TestGet_WithAccess(t *testing.T) {
408410
if err != nil {
409411
t.Fatalf("unexpected error: %v", err)
410412
}
411-
if obj == nil {
412-
t.Fatal("expected object, got nil")
413+
tn, ok := obj.(*corev1alpha1.TenantNamespace)
414+
if !ok {
415+
t.Fatalf("expected *TenantNamespace, got %T", obj)
416+
}
417+
if tn.Name != "tenant-test" {
418+
t.Errorf("expected name %q, got %q", "tenant-test", tn.Name)
419+
}
420+
if tn.Kind != "TenantNamespace" {
421+
t.Errorf("expected Kind=TenantNamespace, got %q", tn.Kind)
422+
}
423+
if tn.APIVersion != corev1alpha1.SchemeGroupVersion.String() {
424+
t.Errorf("expected APIVersion=%q, got %q", corev1alpha1.SchemeGroupVersion.String(), tn.APIVersion)
413425
}
414426
}
415427

0 commit comments

Comments
 (0)