Skip to content

Commit

Permalink
Added response header for traffic splitting feature (changed value to…
Browse files Browse the repository at this point in the history
… boolean)
  • Loading branch information
nastassia-dailidava committed Dec 12, 2023
1 parent 79e2879 commit ab8118b
Showing 1 changed file with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -360,7 +361,7 @@ class EnvoyEgressRoutesFactory(
.withClusterWeight(
getAggregateClusterName(routeSpec.clusterName, properties),
routeSpec.clusterWeights.secondary,
routeSpec.clusterName
true
)
)
}
Expand All @@ -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)
Expand Down

0 comments on commit ab8118b

Please sign in to comment.