From b18144f629afd9c32071609796d116aa5762b4b5 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 26 May 2022 11:03:48 +0800 Subject: [PATCH] optmize the logic of listNamespacesForDistributor func (#952) Signed-off-by: hantmac exclude redundant list Signed-off-by: hantmac Signed-off-by: Liu Zhenwei --- pkg/controller/resourcedistribution/utils.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/controller/resourcedistribution/utils.go b/pkg/controller/resourcedistribution/utils.go index 5de7c4a871..15c4ddafeb 100644 --- a/pkg/controller/resourcedistribution/utils.go +++ b/pkg/controller/resourcedistribution/utils.go @@ -293,8 +293,15 @@ func listNamespacesForDistributor(handlerClient client.Client, targets *appsv1al for _, namespace := range namespacesList.Items { matchedSet.Insert(namespace.Name) } - } else if len(targets.NamespaceLabelSelector.MatchLabels) != 0 || len(targets.NamespaceLabelSelector.MatchExpressions) != 0 { - // 2. select the namespaces via targets.NamespaceLabelSelector + } else { + // 2. select the namespaces via targets.IncludedNamespaces + for _, namespace := range targets.IncludedNamespaces.List { + matchedSet.Insert(namespace.Name) + } + } + + if !targets.AllNamespaces && (len(targets.NamespaceLabelSelector.MatchLabels) != 0 || len(targets.NamespaceLabelSelector.MatchExpressions) != 0) { + // 3. select the namespaces via targets.NamespaceLabelSelector selectors, err := util.GetFastLabelSelector(&targets.NamespaceLabelSelector) if err != nil { return nil, nil, err @@ -308,11 +315,6 @@ func listNamespacesForDistributor(handlerClient client.Client, targets *appsv1al } } - // 3. select the namespaces via targets.IncludedNamespaces - for _, namespace := range targets.IncludedNamespaces.List { - matchedSet.Insert(namespace.Name) - } - // 4. exclude the namespaces via target.ExcludedNamespaces for _, namespace := range targets.ExcludedNamespaces.List { matchedSet.Delete(namespace.Name)