@@ -10,6 +10,7 @@ import (
1010 "sort"
1111 "time"
1212
13+ corev1 "k8s.io/api/core/v1"
1314 apierrors "k8s.io/apimachinery/pkg/api/errors"
1415 "k8s.io/apimachinery/pkg/api/resource"
1516 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -697,11 +698,44 @@ func (h *objectCalculationHandler) matchGlobalCustomQuotas(
697698
698699 objLabels := labels .Set (u .GetLabels ())
699700
701+ // Fetch the namespace once so that per-GCQ namespace-selector
702+ // checks below are a pure in-memory label
703+ // evaluation rather than a repeated cache lookup.
704+ var nsLabels labels.Set
705+
706+ if req .Namespace != "" {
707+ ns := & corev1.Namespace {}
708+ if err := c .Get (ctx , types.NamespacedName {Name : req .Namespace }, ns ); err != nil {
709+ if apierrors .IsNotFound (err ) {
710+ return nil , nil
711+ }
712+
713+ return nil , fmt .Errorf ("get namespace %s: %w" , req .Namespace , err )
714+ }
715+
716+ nsLabels = labels .Set (ns .GetLabels ())
717+ }
718+
700719 out := make ([]quota.MatchedQuota , 0 )
701720
702721 for _ , gcq := range list .Items {
703- if ! gcq .Status .NamespacePresent ("*" ) && ! gcq .Status .NamespacePresent (req .Namespace ) {
704- continue
722+ // Evaluate namespace selectors directly from Spec against the live
723+ // namespace labels (informer-cached) rather than checking
724+ // Status.Namespaces, which is only updated after a controller reconcile.
725+ // This closes the window where newly-labelled namespaces bypass the quota
726+ // before the controller has had a chance to reconcile.
727+ if len (gcq .Spec .NamespaceSelectors ) > 0 {
728+ nsLabelSelectors := make ([]metav1.LabelSelector , 0 , len (gcq .Spec .NamespaceSelectors ))
729+
730+ for _ , nsSel := range gcq .Spec .NamespaceSelectors {
731+ if nsSel .LabelSelector != nil {
732+ nsLabelSelectors = append (nsLabelSelectors , * nsSel .LabelSelector )
733+ }
734+ }
735+
736+ if ! selectors .MatchesSelectors (nsLabels , nsLabelSelectors ) {
737+ continue
738+ }
705739 }
706740
707741 if ! selectors .MatchesSelectors (objLabels , gcq .Spec .ScopeSelectors ) {
0 commit comments