Skip to content

Commit

Permalink
Implemented possibility for configuring traffic splitting, and fallba…
Browse files Browse the repository at this point in the history
…ck using aggregate cluster| Added tests and fixed review issues #292
  • Loading branch information
nastassia-dailidava committed Aug 25, 2023
1 parent a579901 commit 919cff0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import pl.allegro.tech.servicemesh.envoycontrol.snapshot.RouteSpecification
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.clusters.EnvoyClustersFactory.Companion.getSecondaryClusterName
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.filters.ServiceTagFilterFactory
import pl.allegro.tech.servicemesh.envoycontrol.utils.withClusterWeight
import pl.allegro.tech.servicemesh.envoycontrol.groups.RetryPolicy as EnvoyControlRetryPolicy

class EnvoyEgressRoutesFactory(
Expand Down Expand Up @@ -367,6 +366,16 @@ class EnvoyEgressRoutesFactory(
)
}
}

private fun WeightedCluster.Builder.withClusterWeight(clusterName: String, weight: Int): WeightedCluster.Builder {
this.addClusters(
WeightedCluster.ClusterWeight.newBuilder()
.setName(clusterName)
.setWeight(UInt32Value.of(weight))
.build()
)
return this
}
}

class RequestPolicyMapper private constructor() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package pl.allegro.tech.servicemesh.envoycontrol

import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.AGGREGATE_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.CLUSTER_NAME1
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.CLUSTER_NAME2
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.DEFAULT_DISCOVERY_SERVICE_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.DEFAULT_SERVICE_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.EGRESS_HOST
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.EGRESS_PORT
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.INGRESS_HOST
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.INGRESS_PORT
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.MAIN_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.SECONDARY_CLUSTER_NAME
import io.envoyproxy.controlplane.cache.SnapshotResources
import io.envoyproxy.envoy.config.cluster.v3.Cluster
import io.envoyproxy.envoy.config.core.v3.Metadata
Expand All @@ -31,7 +20,6 @@ import pl.allegro.tech.servicemesh.envoycontrol.groups.Outgoing
import pl.allegro.tech.servicemesh.envoycontrol.groups.ProxySettings
import pl.allegro.tech.servicemesh.envoycontrol.groups.ServicesGroup
import pl.allegro.tech.servicemesh.envoycontrol.groups.with
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.ClusterWeights
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.EnvoySnapshotFactory
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.GlobalSnapshot
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties
Expand All @@ -45,6 +33,18 @@ import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.routes.EnvoyEg
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.routes.EnvoyIngressRoutesFactory
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.routes.ServiceTagMetadataGenerator
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.serviceDependencies
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.AGGREGATE_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.CLUSTER_NAME1
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.CLUSTER_NAME2
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.DEFAULT_CLUSTER_WEIGHTS
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.DEFAULT_DISCOVERY_SERVICE_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.DEFAULT_SERVICE_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.EGRESS_HOST
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.EGRESS_PORT
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.INGRESS_HOST
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.INGRESS_PORT
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.MAIN_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.SECONDARY_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.createCluster
import pl.allegro.tech.servicemesh.envoycontrol.utils.createClusterConfigurations
import pl.allegro.tech.servicemesh.envoycontrol.utils.createLoadAssignments
Expand All @@ -57,15 +57,9 @@ class EnvoySnapshotFactoryTest {
const val FORCE_TRAFFIC_ZONE = "dc2"
}

private val defaultClusterWeights = ClusterWeights().apply {
mainClusterWeight = 50
secondaryClusterWeight = 50
}

private val snapshotPropertiesWithWeights = SnapshotProperties().also {
it.dynamicListeners.enabled = false
it.loadBalancing.trafficSplitting.serviceByWeightsProperties = mapOf(
DEFAULT_SERVICE_NAME to defaultClusterWeights
DEFAULT_SERVICE_NAME to DEFAULT_CLUSTER_WEIGHTS
)
it.loadBalancing.trafficSplitting.zoneName = FORCE_TRAFFIC_ZONE
}
Expand Down Expand Up @@ -273,9 +267,8 @@ class EnvoySnapshotFactoryTest {
fun `should not create traffic splitting configuration when zone condition isn't complied`() {
// given
val defaultProperties = SnapshotProperties().also {
it.dynamicListeners.enabled = false
it.loadBalancing.trafficSplitting.serviceByWeightsProperties = mapOf(
DEFAULT_SERVICE_NAME to defaultClusterWeights
DEFAULT_SERVICE_NAME to DEFAULT_CLUSTER_WEIGHTS
)
it.loadBalancing.trafficSplitting.zoneName = "not-matching-dc"
}
Expand Down Expand Up @@ -318,7 +311,8 @@ class EnvoySnapshotFactoryTest {

// then
assertThat(snapshot.clusters().resources())
.containsKeys(MAIN_CLUSTER_NAME,
.containsKeys(
MAIN_CLUSTER_NAME,
SECONDARY_CLUSTER_NAME,
AGGREGATE_CLUSTER_NAME,
CLUSTER_NAME2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.clusters

import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.AGGREGATE_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.CLUSTER_NAME1
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.CLUSTER_NAME2
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.DEFAULT_SERVICE_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.MAIN_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.SECONDARY_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.TRAFFIC_SPLITTING_FORCE_TRAFFIC_ZONE
import io.envoyproxy.controlplane.cache.SnapshotResources
import io.envoyproxy.envoy.config.cluster.v3.Cluster
import io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import pl.allegro.tech.servicemesh.envoycontrol.groups.DependencySettings
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.ClusterWeights
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.GlobalSnapshot
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.AGGREGATE_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.CLUSTER_NAME1
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.CLUSTER_NAME2
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.DEFAULT_CLUSTER_WEIGHTS
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.DEFAULT_SERVICE_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.MAIN_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.SECONDARY_CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.TestData.TRAFFIC_SPLITTING_FORCE_TRAFFIC_ZONE
import pl.allegro.tech.servicemesh.envoycontrol.utils.createAllServicesGroup
import pl.allegro.tech.servicemesh.envoycontrol.utils.createCluster
import pl.allegro.tech.servicemesh.envoycontrol.utils.createClusterConfigurations
Expand All @@ -29,10 +29,7 @@ internal class EnvoyClustersFactoryTest {
private val factory = EnvoyClustersFactory(SnapshotProperties())
private val snapshotPropertiesWithWeights = SnapshotProperties().apply {
loadBalancing.trafficSplitting.serviceByWeightsProperties = mapOf(
DEFAULT_SERVICE_NAME to ClusterWeights().apply {
mainClusterWeight = 50
secondaryClusterWeight = 50
}
DEFAULT_SERVICE_NAME to DEFAULT_CLUSTER_WEIGHTS
)
loadBalancing.trafficSplitting.zoneName = TRAFFIC_SPLITTING_FORCE_TRAFFIC_ZONE
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pl.allegro.tech.servicemesh.envoycontrol.utils

object TestData {
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.ClusterWeights

internal object TestData {
const val INGRESS_HOST = "ingress-host"
const val INGRESS_PORT = 3380
const val EGRESS_HOST = "egress-host"
Expand All @@ -13,4 +15,9 @@ object TestData {
const val SECONDARY_CLUSTER_NAME = "cluster-1-secondary"
const val AGGREGATE_CLUSTER_NAME = "cluster-1-aggregate"
const val TRAFFIC_SPLITTING_FORCE_TRAFFIC_ZONE = "dc2"

val DEFAULT_CLUSTER_WEIGHTS = ClusterWeights().apply {
mainClusterWeight = 50
secondaryClusterWeight = 50
}
}

0 comments on commit 919cff0

Please sign in to comment.