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
… (set on response)
  • Loading branch information
nastassia-dailidava committed Feb 7, 2024
1 parent 6cc83a1 commit e6e9c31
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ import io.envoyproxy.envoy.config.core.v3.Metadata
import io.envoyproxy.envoy.extensions.filters.http.lua.v3.Lua
import io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter
import pl.allegro.tech.servicemesh.envoycontrol.groups.Group
import pl.allegro.tech.servicemesh.envoycontrol.logger
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.filters.LuaMetadataProperty.ListPropertyLua
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.filters.LuaMetadataProperty.StringPropertyLua
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.filters.LuaMetadataProperty.StructPropertyLua

class LuaFilterFactory(private val snapshotProperties: SnapshotProperties) {
companion object {
private val logger by logger()
}

private val ingressRbacLoggingScript: String = this::class.java.classLoader
.getResource("lua/ingress_rbac_logging.lua")!!.readText()

Expand Down Expand Up @@ -90,7 +87,6 @@ class LuaFilterFactory(private val snapshotProperties: SnapshotProperties) {
),
"current_zone" to StringPropertyLua(currentZone)
) + customLuaMetadata
logger.info("LuaFilterMetadata, currentZone: $currentZone")
return Metadata.newBuilder()
.putFilterMetadata("envoy.filters.http.lua", metadata.toValue().structValue)
.build()
Expand All @@ -99,9 +95,7 @@ class LuaFilterFactory(private val snapshotProperties: SnapshotProperties) {
fun ingressClientNameHeaderFilter(): HttpFilter? =
ingressClientNameHeaderFilter.takeIf { trustedClientIdentityHeader.isNotEmpty() }

fun ingressCurrentZoneHeaderFilter(): HttpFilter = ingressCurrentZoneHeaderFilter.also {
logger.info("Included ingressCurrentZoneHeaderFilter")
}
fun ingressCurrentZoneHeaderFilter(): HttpFilter = ingressCurrentZoneHeaderFilter
}

sealed class LuaMetadataProperty<T>(open val value: T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
function envoy_on_request(handle)
local traffic_splitting_zone_header_name = handle:metadata():get("traffic_splitting_zone_header_name") or ""
function envoy_on_response(handle)
local traffic_splitting_zone_header_name = handle:metadata():get("traffic_splitting_zone_header_name") or ""
local current_zone = handle:metadata():get("current_zone") or ""
if traffic_splitting_zone_header_name == "" then
return
end
handle:headers():add(traffic_splitting_zone_header_name, current_zone)
end

function envoy_on_response(handle)
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local function handlerMock(headers, metadata)
}
end

describe("envoy_on_request:", function()
describe("envoy_on_response:", function()
it("should set current zone header", function()
-- given
local filter_metadata = {
Expand All @@ -29,7 +29,7 @@ describe("envoy_on_request:", function()
local handle = handlerMock(headers, filter_metadata)

-- when
envoy_on_request(handle)
envoy_on_response(handle)

-- then
assert.are.equal('local-dc', headers['x-current-zone'])
Expand All @@ -45,7 +45,7 @@ describe("envoy_on_request:", function()
local handle = handlerMock(headers, filter_metadata)

-- when
envoy_on_request(handle)
envoy_on_response(handle)

-- then
assert.are.equal('local-dc-0,local-dc', headers['x-current-zone'])
Expand All @@ -60,7 +60,7 @@ describe("envoy_on_request:", function()
local handle = handlerMock(headers, filter_metadata)

-- when
envoy_on_request(handle)
envoy_on_response(handle)

-- then
assert.are.equal(nil, headers['x-current-zone'])
Expand All @@ -77,7 +77,7 @@ describe("envoy_on_request:", function()
local handle = handlerMock(headers, filter_metadata)

-- when
envoy_on_request(handle)
envoy_on_response(handle)

-- then
assert.are.equal('', headers['x-current-zone'])
Expand Down

0 comments on commit e6e9c31

Please sign in to comment.