From 1f83855aebed477fd0118edcf6a4f70f405d81e0 Mon Sep 17 00:00:00 2001
From: Nastassia Dailidava
 <133115055+nastassia-dailidava@users.noreply.github.com>
Date: Mon, 29 Apr 2024 20:02:47 +0200
Subject: [PATCH] Added setting localityAware flag on subsetConfig for clusters
 with traffic splitting  (#416)

* allegro-internal/flex-roadmap#601 Added localitWeightAware flag
---
 CHANGELOG.md                                               | 1 +
 .../snapshot/resource/clusters/EnvoyClustersFactory.kt     | 5 +++++
 .../snapshot/resource/clusters/EnvoyClustersFactoryTest.kt | 7 +++++++
 .../ClusterCircuitBreakerDefaultSettingsTest.kt            | 2 ++
 4 files changed, 15 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3dc339e5d..7ec7c22d2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 ## [0.20.14]
 ### Changed
 - Added test to check circuit breaker metric value
+- Set "localityAware=true" for cluster subset config
 
 ## [0.20.13]
 ### Changed
diff --git a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/clusters/EnvoyClustersFactory.kt b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/clusters/EnvoyClustersFactory.kt
index 441fb51fb..86ad813c9 100644
--- a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/clusters/EnvoyClustersFactory.kt
+++ b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/clusters/EnvoyClustersFactory.kt
@@ -257,6 +257,11 @@ class EnvoyClustersFactory(
                     .setLocalityWeightedLbConfig(Cluster.CommonLbConfig.LocalityWeightedLbConfig.getDefaultInstance())
                     .build()
             )
+            .setLbSubsetConfig(
+                Cluster.LbSubsetConfig.newBuilder(cluster.lbSubsetConfig)
+                    .setLocalityWeightAware(true)
+                    .setScaleLocalityWeight(true)
+            )
             .also {
                 logger.debug("Created cluster config for traffic splitting: {}", it.toString())
             }
diff --git a/envoy-control-core/src/test/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/clusters/EnvoyClustersFactoryTest.kt b/envoy-control-core/src/test/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/clusters/EnvoyClustersFactoryTest.kt
index 8514a3841..c04ad4cc0 100644
--- a/envoy-control-core/src/test/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/clusters/EnvoyClustersFactoryTest.kt
+++ b/envoy-control-core/src/test/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/clusters/EnvoyClustersFactoryTest.kt
@@ -32,6 +32,7 @@ internal class EnvoyClustersFactoryTest {
                 DEFAULT_SERVICE_NAME to DEFAULT_CLUSTER_WEIGHTS
             )
             loadBalancing.trafficSplitting.zoneName = TRAFFIC_SPLITTING_ZONE
+            loadBalancing.trafficSplitting.zonesAllowingTrafficSplitting = listOf(CURRENT_ZONE)
         }
     }
 
@@ -112,7 +113,11 @@ internal class EnvoyClustersFactoryTest {
             .anySatisfy {
                 assertThat(it.name).isEqualTo(CLUSTER_NAME1)
                 assertThat(it.edsClusterConfig).isEqualTo(cluster1.edsClusterConfig)
+                assertThat(it.commonLbConfig).isNotNull
                 assertThat(it.commonLbConfig.localityWeightedLbConfig).isNotNull
+                assertThat(it.lbSubsetConfig).isNotNull
+                assertThat(it.lbSubsetConfig.localityWeightAware).isTrue()
+                assertThat(it.lbSubsetConfig.scaleLocalityWeight).isTrue()
             }
     }
 
@@ -133,6 +138,8 @@ internal class EnvoyClustersFactoryTest {
                 assertThat(it.name).isEqualTo(CLUSTER_NAME1)
                 assertThat(it.edsClusterConfig).isEqualTo(cluster1.edsClusterConfig)
                 assertThat(it.commonLbConfig.hasLocalityWeightedLbConfig()).isFalse()
+                assertThat(it.lbSubsetConfig.localityWeightAware).isFalse()
+                assertThat(it.lbSubsetConfig.scaleLocalityWeight).isFalse()
             }
     }
 
diff --git a/envoy-control-tests/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/ClusterCircuitBreakerDefaultSettingsTest.kt b/envoy-control-tests/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/ClusterCircuitBreakerDefaultSettingsTest.kt
index ab5d859aa..cc7db6482 100644
--- a/envoy-control-tests/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/ClusterCircuitBreakerDefaultSettingsTest.kt
+++ b/envoy-control-tests/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/ClusterCircuitBreakerDefaultSettingsTest.kt
@@ -1,6 +1,7 @@
 package pl.allegro.tech.servicemesh.envoycontrol
 
 import org.assertj.core.api.Assertions.assertThat
+import org.junit.jupiter.api.Tag
 import org.junit.jupiter.api.Test
 import org.junit.jupiter.api.extension.RegisterExtension
 import pl.allegro.tech.servicemesh.envoycontrol.assertions.isFrom
@@ -81,6 +82,7 @@ internal class ClusterCircuitBreakerDefaultSettingsTest {
         assertThat(remainingRqMetric).isNotNull()
     }
 
+    @Tag("flaky")
     @Test
     fun `should have decreased remaining pending rq`() {
         consul.server.operations.registerServiceWithEnvoyOnIngress(name = "echo", extension = envoy)