|
| 1 | +package pl.allegro.tech.servicemesh.envoycontrol.trafficsplitting |
| 2 | + |
| 3 | +import TrafficSplitting.serviceName |
| 4 | +import TrafficSplitting.upstreamServiceName |
| 5 | +import callUpstreamServiceRepeatedly |
| 6 | +import org.junit.jupiter.api.Test |
| 7 | +import org.junit.jupiter.api.extension.RegisterExtension |
| 8 | +import pl.allegro.tech.servicemesh.envoycontrol.config.Xds |
| 9 | +import pl.allegro.tech.servicemesh.envoycontrol.config.consul.ConsulMultiClusterExtension |
| 10 | +import pl.allegro.tech.servicemesh.envoycontrol.config.envoy.EnvoyExtension |
| 11 | +import pl.allegro.tech.servicemesh.envoycontrol.config.envoycontrol.EnvoyControlClusteredExtension |
| 12 | +import pl.allegro.tech.servicemesh.envoycontrol.config.service.EchoServiceExtension |
| 13 | +import pl.allegro.tech.servicemesh.envoycontrol.logger |
| 14 | +import verifyCallsCountCloseTo |
| 15 | +import verifyCallsCountGreaterThan |
| 16 | +import verifyIsReachable |
| 17 | +import java.time.Duration |
| 18 | + |
| 19 | +class WeightedClustersServiceTagRoutingTest { |
| 20 | + companion object { |
| 21 | + val logger by logger() |
| 22 | + private const val forceTrafficZone = "dc2" |
| 23 | + |
| 24 | + private val properties = mapOf( |
| 25 | + "envoy-control.envoy.snapshot.routing.service-tags.enabled" to true, |
| 26 | + "envoy-control.envoy.snapshot.routing.service-tags.metadata-key" to "tag", |
| 27 | + "envoy-control.envoy.snapshot.routing.service-tags.auto-service-tag-enabled" to true, |
| 28 | + "envoy-control.envoy.snapshot.outgoing-permissions.services-allowed-to-use-wildcard" to setOf("echo2", "test-service"), |
| 29 | + "logging.level.pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.clusters.EnvoyClustersFactory" to "DEBUG", |
| 30 | + "logging.level.pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.routes.EnvoyEgressRoutesFactory" to "DEBUG", |
| 31 | + "logging.level.pl.allegro.tech.servicemesh.envoycontrol.snapshot.EnvoySnapshotFactory" to "DEBUG", |
| 32 | + "envoy-control.envoy.snapshot.stateSampleDuration" to Duration.ofSeconds(0), |
| 33 | + "envoy-control.sync.enabled" to true, |
| 34 | + "envoy-control.envoy.snapshot.loadBalancing.trafficSplitting.zoneName" to forceTrafficZone, |
| 35 | + "envoy-control.envoy.snapshot.loadBalancing.trafficSplitting.serviceByWeightsProperties.$serviceName.main" to 90, |
| 36 | + "envoy-control.envoy.snapshot.loadBalancing.trafficSplitting.serviceByWeightsProperties.$serviceName.secondary" to 10, |
| 37 | + "envoy-control.envoy.snapshot.loadBalancing.priorities.zonePriorities" to mapOf( |
| 38 | + "dc1" to mapOf( |
| 39 | + "dc1" to 0, |
| 40 | + "dc2" to 1 |
| 41 | + ), |
| 42 | + "dc2" to mapOf( |
| 43 | + "dc1" to 1, |
| 44 | + "dc2" to 0, |
| 45 | + ), |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + private val echo2Config = """ |
| 50 | + node: |
| 51 | + metadata: |
| 52 | + proxy_settings: |
| 53 | + outgoing: |
| 54 | + dependencies: |
| 55 | + - service: "service-1" |
| 56 | + """.trimIndent() |
| 57 | + |
| 58 | + private val config = Xds.copy(configOverride = echo2Config, serviceName = "echo2") |
| 59 | + |
| 60 | + @JvmField |
| 61 | + @RegisterExtension |
| 62 | + val consul = ConsulMultiClusterExtension() |
| 63 | + |
| 64 | + @JvmField |
| 65 | + @RegisterExtension |
| 66 | + val envoyControl = |
| 67 | + EnvoyControlClusteredExtension(consul.serverFirst, { properties }, listOf(consul)) |
| 68 | + |
| 69 | + @JvmField |
| 70 | + @RegisterExtension |
| 71 | + val envoyControl2 = |
| 72 | + EnvoyControlClusteredExtension(consul.serverSecond, { properties }, listOf(consul)) |
| 73 | + |
| 74 | + @JvmField |
| 75 | + @RegisterExtension |
| 76 | + val echoServiceDC1 = EchoServiceExtension() |
| 77 | + |
| 78 | + @JvmField |
| 79 | + @RegisterExtension |
| 80 | + val upstreamServiceDC1 = EchoServiceExtension() |
| 81 | + |
| 82 | + @JvmField |
| 83 | + @RegisterExtension |
| 84 | + val upstreamServiceDC2 = EchoServiceExtension() |
| 85 | + |
| 86 | + @JvmField |
| 87 | + @RegisterExtension |
| 88 | + val echoEnvoyDC1 = EnvoyExtension(envoyControl, localService = echoServiceDC1, config) |
| 89 | + @JvmField |
| 90 | + @RegisterExtension |
| 91 | + val echoEnvoyDC2 = EnvoyExtension(envoyControl2) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun `should route traffic according to weights with service tag`() { |
| 96 | + consul.serverFirst.operations.registerServiceWithEnvoyOnEgress(echoEnvoyDC1, name = serviceName) |
| 97 | + |
| 98 | + consul.serverFirst.operations.registerService(upstreamServiceDC1, name = upstreamServiceName, tags = listOf("global", "tag")) |
| 99 | + echoEnvoyDC1.verifyIsReachable(upstreamServiceDC1, upstreamServiceName) |
| 100 | + |
| 101 | + consul.serverSecond.operations.registerService(upstreamServiceDC2, name = upstreamServiceName, tags = listOf("global", "tag")) |
| 102 | + echoEnvoyDC1.verifyIsReachable(upstreamServiceDC2, upstreamServiceName) |
| 103 | + |
| 104 | + echoEnvoyDC1.callUpstreamServiceRepeatedly(upstreamServiceDC1, upstreamServiceDC2, tag = "tag") |
| 105 | + .verifyCallsCountCloseTo(upstreamServiceDC1, 90) |
| 106 | + .verifyCallsCountGreaterThan(upstreamServiceDC2, 1) |
| 107 | + } |
| 108 | +} |
0 commit comments