From 36beb83177d73097e7f7574de8731f9c2f301081 Mon Sep 17 00:00:00 2001 From: Rama Chavali Date: Fri, 6 Jun 2025 15:12:11 +0530 Subject: [PATCH 1/3] remove unnecessary loop and condition evaluation Signed-off-by: Rama Chavali --- pilot/pkg/networking/core/cluster.go | 3 --- .../core/envoyfilter/cluster_patch.go | 21 +++++-------------- .../core/envoyfilter/cluster_patch_test.go | 3 +-- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/pilot/pkg/networking/core/cluster.go b/pilot/pkg/networking/core/cluster.go index f61203638c95..b31a377af542 100644 --- a/pilot/pkg/networking/core/cluster.go +++ b/pilot/pkg/networking/core/cluster.go @@ -397,9 +397,6 @@ func (p clusterPatcher) patch(hosts []host.Name, c *cluster.Cluster) *discovery. } func (p clusterPatcher) doPatch(hosts []host.Name, c *cluster.Cluster) *cluster.Cluster { - if !envoyfilter.ShouldKeepCluster(p.pctx, p.efw, c, hosts) { - return nil - } return envoyfilter.ApplyClusterMerge(p.pctx, p.efw, c, hosts) } diff --git a/pilot/pkg/networking/core/envoyfilter/cluster_patch.go b/pilot/pkg/networking/core/envoyfilter/cluster_patch.go index fe0b0a529a5c..b162660a2d04 100644 --- a/pilot/pkg/networking/core/envoyfilter/cluster_patch.go +++ b/pilot/pkg/networking/core/envoyfilter/cluster_patch.go @@ -45,6 +45,11 @@ func ApplyClusterMerge(pctx networking.EnvoyFilter_PatchContext, efw *model.Merg } for _, cp := range efw.Patches[networking.EnvoyFilter_CLUSTER] { applied := false + if cp.Operation == networking.EnvoyFilter_Patch_REMOVE && + commonConditionMatch(pctx, cp) && + clusterMatch(c, cp, hosts) { + return nil + } if cp.Operation != networking.EnvoyFilter_Patch_MERGE { IncrementEnvoyFilterMetric(cp.Key(), Cluster, applied) continue @@ -120,22 +125,6 @@ func mergeTransportSocketCluster(c *cluster.Cluster, cp *model.EnvoyFilterConfig return true, nil } -// ShouldKeepCluster checks if there is a REMOVE patch on the cluster, returns false if there is one so that it is removed. -func ShouldKeepCluster(pctx networking.EnvoyFilter_PatchContext, efw *model.MergedEnvoyFilterWrapper, c *cluster.Cluster, hosts []host.Name) bool { - if efw == nil { - return true - } - for _, cp := range efw.Patches[networking.EnvoyFilter_CLUSTER] { - if cp.Operation != networking.EnvoyFilter_Patch_REMOVE { - continue - } - if commonConditionMatch(pctx, cp) && clusterMatch(c, cp, hosts) { - return false - } - } - return true -} - // InsertedClusters collects all clusters that are added via ADD operation and match the patch context. func InsertedClusters(pctx networking.EnvoyFilter_PatchContext, efw *model.MergedEnvoyFilterWrapper) []*cluster.Cluster { if efw == nil { diff --git a/pilot/pkg/networking/core/envoyfilter/cluster_patch_test.go b/pilot/pkg/networking/core/envoyfilter/cluster_patch_test.go index 7a88c4d2d0a5..c6ab68551a32 100644 --- a/pilot/pkg/networking/core/envoyfilter/cluster_patch_test.go +++ b/pilot/pkg/networking/core/envoyfilter/cluster_patch_test.go @@ -614,8 +614,7 @@ func TestClusterPatching(t *testing.T) { efw := push.EnvoyFilters(tc.proxy) output := []*cluster.Cluster{} for _, c := range tc.input { - if ShouldKeepCluster(tc.patchContext, efw, c, []host.Name{host.Name(tc.host)}) { - pc := ApplyClusterMerge(tc.patchContext, efw, c, []host.Name{host.Name(tc.host)}) + if pc := ApplyClusterMerge(tc.patchContext, efw, c, []host.Name{host.Name(tc.host)}); pc != nil { output = append(output, pc) } } From 762b9741473b6bf4f238b229cb89a4e48f86b854 Mon Sep 17 00:00:00 2001 From: Rama Chavali Date: Fri, 6 Jun 2025 15:20:34 +0530 Subject: [PATCH 2/3] empty push Signed-off-by: Rama Chavali From 161acd0f824c24913fc263ce0262b082befb2513 Mon Sep 17 00:00:00 2001 From: Rama Chavali Date: Fri, 6 Jun 2025 15:23:08 +0530 Subject: [PATCH 3/3] add comment Signed-off-by: Rama Chavali --- pilot/pkg/networking/core/envoyfilter/cluster_patch.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pilot/pkg/networking/core/envoyfilter/cluster_patch.go b/pilot/pkg/networking/core/envoyfilter/cluster_patch.go index b162660a2d04..4212793a4e17 100644 --- a/pilot/pkg/networking/core/envoyfilter/cluster_patch.go +++ b/pilot/pkg/networking/core/envoyfilter/cluster_patch.go @@ -45,6 +45,7 @@ func ApplyClusterMerge(pctx networking.EnvoyFilter_PatchContext, efw *model.Merg } for _, cp := range efw.Patches[networking.EnvoyFilter_CLUSTER] { applied := false + // For removed patches, skip the merge if the patch matches. if cp.Operation == networking.EnvoyFilter_Patch_REMOVE && commonConditionMatch(pctx, cp) && clusterMatch(c, cp, hosts) {