-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allegro-internal/flex-roadmap#687 Added http compression filter confi…
…guration
- Loading branch information
1 parent
1a64a5f
commit 72db37f
Showing
11 changed files
with
352 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
.../servicemesh/envoycontrol/snapshot/resource/listeners/filters/CompressionFilterFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.filters | ||
|
||
import com.google.protobuf.BoolValue | ||
import com.google.protobuf.UInt32Value | ||
import io.envoyproxy.envoy.config.core.v3.RuntimeFeatureFlag | ||
import io.envoyproxy.envoy.config.core.v3.TypedExtensionConfig | ||
import io.envoyproxy.envoy.config.filter.http.gzip.v2.Gzip.CompressionLevel.Enum.BEST_VALUE | ||
import io.envoyproxy.envoy.extensions.compression.brotli.compressor.v3.Brotli | ||
import io.envoyproxy.envoy.extensions.compression.gzip.compressor.v3.Gzip | ||
import io.envoyproxy.envoy.extensions.filters.http.compressor.v3.Compressor | ||
import io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter | ||
import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties | ||
|
||
class CompressionFilterFactory(val properties: SnapshotProperties) { | ||
|
||
private val brotliCompressionFilter: HttpFilter = compressionFilter( | ||
TypedExtensionConfig.newBuilder() | ||
.setName("envoy.compression.brotli.compressor") | ||
.setTypedConfig( | ||
com.google.protobuf.Any.pack( | ||
Brotli.newBuilder() | ||
.setQuality(UInt32Value.of(properties.compression.brotli.quality)) | ||
.build() | ||
) | ||
), | ||
properties.compression.brotli.chooseFirst | ||
) | ||
|
||
private val gzipCompressionFilter: HttpFilter = compressionFilter( | ||
TypedExtensionConfig.newBuilder() | ||
.setName("envoy.compression.gzip.compressor") | ||
.setTypedConfig( | ||
com.google.protobuf.Any.pack( | ||
Gzip.newBuilder() | ||
.setCompressionStrategy(Gzip.CompressionStrategy.DEFAULT_STRATEGY) | ||
.setCompressionLevel(Gzip.CompressionLevel.forNumber(BEST_VALUE)) | ||
.build() | ||
) | ||
), | ||
properties.compression.gzip.chooseFirst | ||
) | ||
|
||
fun gzipCompressionFilter(): HttpFilter? { | ||
return if (properties.compression.gzip.enabled) { | ||
gzipCompressionFilter | ||
} else null | ||
} | ||
|
||
fun brotliCompressionFilter(): HttpFilter? { | ||
return if (properties.compression.brotli.enabled) { | ||
brotliCompressionFilter | ||
} else null | ||
} | ||
|
||
private fun compressionFilter(library: TypedExtensionConfig.Builder, chooseFirst: Boolean) = | ||
HttpFilter.newBuilder() | ||
.setName("envoy.filters.http.compressor") | ||
.setTypedConfig( | ||
com.google.protobuf.Any.pack( | ||
Compressor.newBuilder() | ||
.setChooseFirst(chooseFirst) | ||
.setRequestDirectionConfig( | ||
Compressor.RequestDirectionConfig.newBuilder() | ||
.setCommonConfig( | ||
commonDirectionConfig( | ||
"request_compressor_enabled", | ||
properties.compression.requestCompressionEnabled | ||
) | ||
) | ||
).setResponseDirectionConfig( | ||
Compressor.ResponseDirectionConfig.newBuilder() | ||
.setCommonConfig( | ||
commonDirectionConfig( | ||
"response_compressor_enabled", | ||
properties.compression.responseCompressionEnabled | ||
) | ||
) | ||
.setDisableOnEtagHeader(properties.compression.disableOnEtagHeader) | ||
) | ||
.setCompressorLibrary(library) | ||
.build() | ||
) | ||
).build() | ||
|
||
private fun commonDirectionConfig(runtimeKey: String, defaultValue: Boolean) = | ||
Compressor.CommonDirectionConfig.newBuilder() | ||
.setEnabled( | ||
RuntimeFeatureFlag.newBuilder().setRuntimeKey(runtimeKey) | ||
.setDefaultValue(BoolValue.of(defaultValue)) | ||
) | ||
.setMinContentLength(UInt32Value.of(properties.compression.minContentLength)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...l-tests/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/CompressionFilterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package pl.allegro.tech.servicemesh.envoycontrol | ||
|
||
import okhttp3.Response | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.ObjectAssert | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
import pl.allegro.tech.servicemesh.envoycontrol.assertions.untilAsserted | ||
import pl.allegro.tech.servicemesh.envoycontrol.config.Xds | ||
import pl.allegro.tech.servicemesh.envoycontrol.config.consul.ConsulExtension | ||
import pl.allegro.tech.servicemesh.envoycontrol.config.envoy.EnvoyExtension | ||
import pl.allegro.tech.servicemesh.envoycontrol.config.envoycontrol.EnvoyControlExtension | ||
import pl.allegro.tech.servicemesh.envoycontrol.config.service.EchoContainer | ||
import pl.allegro.tech.servicemesh.envoycontrol.config.service.EchoServiceExtension | ||
import pl.allegro.tech.servicemesh.envoycontrol.config.service.GenericServiceExtension | ||
|
||
class CompressionFilterTest { | ||
|
||
companion object { | ||
|
||
@JvmField | ||
@RegisterExtension | ||
val consul = ConsulExtension() | ||
|
||
@JvmField | ||
@RegisterExtension | ||
val envoyControl = EnvoyControlExtension( | ||
consul, | ||
mapOf( | ||
"envoy-control.envoy.snapshot.compression.gzip.enabled" to true, | ||
"envoy-control.envoy.snapshot.compression.brotli.enabled" to true, | ||
"envoy-control.envoy.snapshot.compression.minContentLength" to 100, | ||
"envoy-control.envoy.snapshot.compression.responseCompressionEnabled" to true, | ||
) | ||
) | ||
private const val SERVICE_NAME = "service-1" | ||
private const val LONG_STRING = "Workshallmeantheworkofauthorship,whetherinSourceorObjectform," + | ||
"madeavailableundertheLicensesindicatedbyacopyrightnoticethatisincludedinorattachedto" + | ||
"thework(anexampleisprovidedintheAppendixbelow)." | ||
|
||
private val serviceConfig = """ | ||
node: | ||
metadata: | ||
proxy_settings: | ||
incoming: | ||
unlistedEndpointsPolicy: log | ||
endpoints: [] | ||
""".trimIndent() | ||
private val config = Xds.copy(configOverride = serviceConfig, serviceName = SERVICE_NAME) | ||
private val longText = LONG_STRING.repeat(100) | ||
|
||
@JvmField | ||
@RegisterExtension | ||
val service = | ||
GenericServiceExtension(EchoContainer(longText)) | ||
|
||
@JvmField | ||
@RegisterExtension | ||
val downstreamService = EchoServiceExtension() | ||
|
||
@JvmField | ||
@RegisterExtension | ||
val downstreamEnvoy = EnvoyExtension(envoyControl, downstreamService, config = Xds) | ||
|
||
@JvmField | ||
@RegisterExtension | ||
val serviceEnvoy = EnvoyExtension(envoyControl, config = config, localService = service) | ||
} | ||
|
||
@Test | ||
fun `should compress response with brotli`() { | ||
consul.server.operations.registerServiceWithEnvoyOnIngress(serviceEnvoy, name = SERVICE_NAME) | ||
downstreamEnvoy.waitForReadyServices(SERVICE_NAME) | ||
untilAsserted { | ||
val response = | ||
downstreamEnvoy.egressOperations.callService(SERVICE_NAME, headers = mapOf("accept-encoding" to "br")) | ||
assertThat(response).isCompressedWith("br") | ||
} | ||
} | ||
|
||
@Test | ||
fun `should compress response with gzip`() { | ||
consul.server.operations.registerServiceWithEnvoyOnIngress(serviceEnvoy, name = SERVICE_NAME) | ||
downstreamEnvoy.waitForReadyServices(SERVICE_NAME) | ||
untilAsserted { | ||
val response = | ||
downstreamEnvoy.egressOperations.callService(SERVICE_NAME, headers = mapOf("accept-encoding" to "gzip")) | ||
assertThat(response).isCompressedWith("gzip") | ||
} | ||
} | ||
|
||
@Test | ||
fun `should not compress response when accept encoding header is missing`() { | ||
consul.server.operations.registerServiceWithEnvoyOnIngress(serviceEnvoy, name = SERVICE_NAME) | ||
downstreamEnvoy.waitForReadyServices(SERVICE_NAME) | ||
untilAsserted { | ||
val response = | ||
downstreamEnvoy.egressOperations.callService(SERVICE_NAME) | ||
assertThat(response).isNotCompressed() | ||
} | ||
} | ||
|
||
private fun ObjectAssert<Response>.isCompressedWith(encoding: String): ObjectAssert<Response> { | ||
matches { it.isSuccessful && it.headers.any { x -> x.first == "content-encoding" && x.second == encoding } } | ||
return this | ||
} | ||
|
||
private fun ObjectAssert<Response>.isNotCompressed(): ObjectAssert<Response> { | ||
matches { it.isSuccessful && it.headers.none { x -> x.first == "content-encoding" } } | ||
return this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.