diff --git a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/routes/EnvoyEgressRoutesFactory.kt b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/routes/EnvoyEgressRoutesFactory.kt index 3cfeb3d3b..021444709 100644 --- a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/routes/EnvoyEgressRoutesFactory.kt +++ b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/snapshot/resource/routes/EnvoyEgressRoutesFactory.kt @@ -36,6 +36,7 @@ import pl.allegro.tech.servicemesh.envoycontrol.snapshot.StandardRouteSpecificat import pl.allegro.tech.servicemesh.envoycontrol.snapshot.WeightRouteSpecification import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.clusters.EnvoyClustersFactory.Companion.getAggregateClusterName import pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.filters.ServiceTagFilterFactory +import java.lang.Boolean.TRUE import pl.allegro.tech.servicemesh.envoycontrol.groups.RetryPolicy as EnvoyControlRetryPolicy class EnvoyEgressRoutesFactory( @@ -360,7 +361,7 @@ class EnvoyEgressRoutesFactory( .withClusterWeight( getAggregateClusterName(routeSpec.clusterName, properties), routeSpec.clusterWeights.secondary, - routeSpec.clusterName + true ) ) } @@ -373,35 +374,33 @@ class EnvoyEgressRoutesFactory( private fun WeightedCluster.Builder.withClusterWeight( clusterName: String, weight: Int, - headerValue: String? = null + withHeader: Boolean = false ): WeightedCluster.Builder { - this.addClusters( - WeightedCluster.ClusterWeight.newBuilder() - .setName(clusterName) - .setWeight(UInt32Value.of(weight)) - .withHeader(properties.loadBalancing.trafficSplitting.headerName, headerValue) - .build() - ) - return this + val clusters = WeightedCluster.ClusterWeight.newBuilder() + .setName(clusterName) + .setWeight(UInt32Value.of(weight)) + .also { + if (withHeader) { + it.withHeader(properties.loadBalancing.trafficSplitting.headerName) + } + } + return this.addClusters(clusters) } - private fun WeightedCluster.ClusterWeight.Builder.withHeader( - key: String?, - value: String? - ): WeightedCluster.ClusterWeight.Builder { + private fun WeightedCluster.ClusterWeight.Builder.withHeader(key: String?): WeightedCluster.ClusterWeight.Builder { key?.takeIf { it.isNotBlank() } ?.let { - value?.let { this.addResponseHeadersToAdd(buildHeader(key, value)) } + this.addResponseHeadersToAdd(buildHeader(key)) } return this } - private fun buildHeader(key: String, value: String): HeaderValueOption.Builder { + private fun buildHeader(key: String): HeaderValueOption.Builder { return HeaderValueOption.newBuilder() .setHeader( HeaderValue.newBuilder() .setKey(key) - .setValue(value) + .setValue(TRUE.toString()) ) .setAppendAction(HeaderValueOption.HeaderAppendAction.OVERWRITE_IF_EXISTS_OR_ADD) .setKeepEmptyValue(false)