Skip to content

Commit

Permalink
#564 Implemented adding a header for locality weighted load balancing…
Browse files Browse the repository at this point in the history
… (fixes)
  • Loading branch information
nastassia-dailidava committed Feb 7, 2024
1 parent ed5a712 commit ef53cb2
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class ControlPlane private constructor(
val snapshotsVersions = SnapshotsVersions()
val snapshotProperties = properties.envoy.snapshot
val envoySnapshotFactory = EnvoySnapshotFactory(
ingressRoutesFactory = EnvoyIngressRoutesFactory(snapshotProperties, envoyHttpFilters),
ingressRoutesFactory = EnvoyIngressRoutesFactory(snapshotProperties, envoyHttpFilters, currentZone),
egressRoutesFactory = EnvoyEgressRoutesFactory(snapshotProperties),
clustersFactory = EnvoyClustersFactory(snapshotProperties),
endpointsFactory = EnvoyEndpointsFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties

class EnvoyDefaultFilters(
private val snapshotProperties: SnapshotProperties,
private val customLuaMetadata: LuaMetadataProperty.StructPropertyLua,
private val currentZone: String
private val customLuaMetadata: LuaMetadataProperty.StructPropertyLua
) {
private val rbacFilterFactory = RBACFilterFactory(
snapshotProperties.incomingPermissions,
Expand Down Expand Up @@ -117,7 +116,7 @@ class EnvoyDefaultFilters(
return preFilters + filters.toList() + postFilters
}

val defaultIngressMetadata = { group: Group ->
val defaultIngressMetadata = { group: Group, currentZone: String ->
luaFilterFactory.ingressScriptsMetadata(group, customLuaMetadata, currentZone)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.filters

import io.envoyproxy.envoy.config.core.v3.Metadata
import pl.allegro.tech.servicemesh.envoycontrol.groups.Group
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.HttpFilterFactory
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.routes.IngressMetadataFactory

class EnvoyHttpFilters(
val ingressFilters: List<HttpFilterFactory>,
val egressFilters: List<HttpFilterFactory>,
val ingressMetadata: IngressMetadataFactory = { _ -> Metadata.getDefaultInstance() }
val ingressMetadata: IngressMetadataFactory = { _: Group, _: String -> Metadata.getDefaultInstance() }
) {
companion object {
val emptyFilters = EnvoyHttpFilters(listOf(), listOf()) { Metadata.getDefaultInstance() }
val emptyFilters = EnvoyHttpFilters(listOf(), listOf()) { _, _ -> Metadata.getDefaultInstance() }

fun defaultFilters(
snapshotProperties: SnapshotProperties,
localDatacenter: String,
customLuaMetadata: LuaMetadataProperty.StructPropertyLua = LuaMetadataProperty.StructPropertyLua()
): EnvoyHttpFilters {
val defaultFilters = EnvoyDefaultFilters(snapshotProperties, customLuaMetadata, localDatacenter)
val defaultFilters = EnvoyDefaultFilters(snapshotProperties, customLuaMetadata)
return EnvoyHttpFilters(
defaultFilters.ingressFilters(),
defaultFilters.defaultEgressFilters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.getRuleId
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.filters.EnvoyHttpFilters

typealias IngressMetadataFactory = (node: Group) -> Metadata
typealias IngressMetadataFactory = (node: Group, currentZone: String) -> Metadata

class EnvoyIngressRoutesFactory(
private val properties: SnapshotProperties,
envoyHttpFilters: EnvoyHttpFilters = EnvoyHttpFilters.emptyFilters
envoyHttpFilters: EnvoyHttpFilters = EnvoyHttpFilters.emptyFilters,
private val currentZone: String
) {

private val allClients = setOf(
Expand Down Expand Up @@ -187,7 +188,7 @@ class EnvoyIngressRoutesFactory(
.setRoute(clusterRouteActionWithRetryPolicy(retryPolicy, localRouteAction))
}
return (retryRoutes + nonRetryRoute).map { builder ->
builder.setMetadata(filterMetadata(group)).build()
builder.setMetadata(filterMetadata(group, currentZone)).build()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.routes.EnvoyIn
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.CLUSTER_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.CURRENT_ZONE
import pl.allegro.tech.servicemesh.envoycontrol.utils.DEFAULT_CLUSTER_WEIGHTS
import pl.allegro.tech.servicemesh.envoycontrol.utils.DEFAULT_DISCOVERY_SERVICE_NAME
import pl.allegro.tech.servicemesh.envoycontrol.utils.DEFAULT_IDLE_TIMEOUT
Expand All @@ -54,6 +53,7 @@ import pl.allegro.tech.servicemesh.envoycontrol.utils.zoneWeights
class EnvoySnapshotFactoryTest {
companion object {
const val SERVICE_NAME_2 = "service-name-2"
const val CURRENT_ZONE = "dc1"
}

@Test
Expand Down Expand Up @@ -453,12 +453,13 @@ class EnvoySnapshotFactoryTest {
SnapshotProperties(),
EnvoyHttpFilters(
emptyList(), emptyList()
) { Metadata.getDefaultInstance() }
) { _, _ -> Metadata.getDefaultInstance() },
CURRENT_ZONE
)
val egressRoutesFactory = EnvoyEgressRoutesFactory(properties)
val clustersFactory = EnvoyClustersFactory(properties)
val endpointsFactory = EnvoyEndpointsFactory(properties, ServiceTagMetadataGenerator(), CURRENT_ZONE)
val envoyHttpFilters = EnvoyHttpFilters.defaultFilters(properties, "dc1")
val envoyHttpFilters = EnvoyHttpFilters.defaultFilters(properties)
val listenersFactory = EnvoyListenersFactory(properties, envoyHttpFilters)
val snapshotsVersions = SnapshotsVersions()
val meterRegistry: MeterRegistry = SimpleMeterRegistry()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class SnapshotUpdaterTest {
)

private val uninitializedSnapshot = null
private const val CURRENT_ZONE = "dc1"
}

val groupWithProxy = AllServicesGroup(
Expand Down Expand Up @@ -1309,11 +1310,11 @@ class SnapshotUpdaterTest {

private fun snapshotFactory(snapshotProperties: SnapshotProperties, meterRegistry: MeterRegistry) =
EnvoySnapshotFactory(
ingressRoutesFactory = EnvoyIngressRoutesFactory(snapshotProperties),
ingressRoutesFactory = EnvoyIngressRoutesFactory(snapshotProperties, currentZone = CURRENT_ZONE),
egressRoutesFactory = EnvoyEgressRoutesFactory(snapshotProperties),
clustersFactory = EnvoyClustersFactory(snapshotProperties),
endpointsFactory = EnvoyEndpointsFactory(
snapshotProperties, ServiceTagMetadataGenerator(snapshotProperties.routing.serviceTags), "dc1"
snapshotProperties, ServiceTagMetadataGenerator(snapshotProperties.routing.serviceTags), CURRENT_ZONE
),
listenersFactory = EnvoyListenersFactory(
snapshotProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class EnvoyDefaultFiltersTest {

private val defaultFilters = EnvoyDefaultFilters(
SnapshotProperties(),
LuaMetadataProperty.StructPropertyLua(),
"dc1"
LuaMetadataProperty.StructPropertyLua()
)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ internal class EnvoyIngressRoutesFactoryTest {
adminRoute(),
adminRedirectRoute()
)
private val currentZone = "dc1"

@Test
fun `should create route config with health check and response timeout defined`() {
Expand All @@ -94,7 +95,7 @@ internal class EnvoyIngressRoutesFactoryTest {
pathPrefix = "/config_dump"
method = "GET"
})
})
}, currentZone = currentZone)
val responseTimeout = Durations.fromSeconds(777)
val idleTimeout = Durations.fromSeconds(61)
val connectionIdleTimeout = Durations.fromSeconds(120)
Expand All @@ -107,12 +108,18 @@ internal class EnvoyIngressRoutesFactoryTest {
permissionsEnabled = true,
timeoutPolicy = TimeoutPolicy(idleTimeout, responseTimeout, connectionIdleTimeout),
rateLimitEndpoints = listOf(
IncomingRateLimitEndpoint("/hello", PathMatchingType.PATH_PREFIX, setOf("GET", "POST"),
setOf(ClientWithSelector.create("client-1", "selector")), "100/s"),
IncomingRateLimitEndpoint("/banned", PathMatchingType.PATH, setOf("GET"),
setOf(ClientWithSelector.create("*")), "0/m"),
IncomingRateLimitEndpoint("/a/.*", PathMatchingType.PATH_REGEX, emptySet(),
setOf(ClientWithSelector.create("client-2")), "0/m")
IncomingRateLimitEndpoint(
"/hello", PathMatchingType.PATH_PREFIX, setOf("GET", "POST"),
setOf(ClientWithSelector.create("client-1", "selector")), "100/s"
),
IncomingRateLimitEndpoint(
"/banned", PathMatchingType.PATH, setOf("GET"),
setOf(ClientWithSelector.create("*")), "0/m"
),
IncomingRateLimitEndpoint(
"/a/.*", PathMatchingType.PATH_REGEX, emptySet(),
setOf(ClientWithSelector.create("client-2")), "0/m"
)
)
)
)
Expand Down Expand Up @@ -185,7 +192,7 @@ internal class EnvoyIngressRoutesFactoryTest {
ingress.headersToRemove = mutableListOf("x-via-vip", "x-special-case-header")
ingress.addServiceNameHeaderToResponse = true
ingress.addRequestedAuthorityHeaderToResponse = true
})
}, currentZone = currentZone)
val proxySettingsOneEndpoint = ProxySettings(
incoming = Incoming(
healthCheck = HealthCheck(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ class ControlPlaneConfig {
@Bean
@ConditionalOnMissingBean(EnvoyHttpFilters::class)
fun envoyHttpFilters(
properties: EnvoyControlProperties,
consulProperties: ConsulProperties
properties: EnvoyControlProperties
): EnvoyHttpFilters {
return EnvoyHttpFilters.defaultFilters(properties.envoy.snapshot, localDatacenter(consulProperties))
return EnvoyHttpFilters.defaultFilters(properties.envoy.snapshot)
}

fun localDatacenter(properties: ConsulProperties) =
Expand Down

0 comments on commit ef53cb2

Please sign in to comment.