Skip to content

Commit

Permalink
#362 Generic mechanism to pass custom properties (#363)
Browse files Browse the repository at this point in the history
* #362 custom data
---------
Co-authored-by: Kamil Smigielski <[email protected]>
  • Loading branch information
matb4r authored Feb 8, 2023
1 parent dc696f6 commit aeb6b3c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Lists all changes with user impact.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [0.19.29]

### Changed
- add mechanism to store custom data in group

## [0.19.28]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ data class AccessLogFilterSettings(val proto: Value?, val properties: AccessLogF

data class ProxySettings(
val incoming: Incoming = Incoming(),
val outgoing: Outgoing = Outgoing()
val outgoing: Outgoing = Outgoing(),
val customData: Map<String, Any?> = emptyMap()
) {
constructor(proto: Value?, properties: SnapshotProperties) : this(
incoming = proto?.field("incoming").toIncoming(properties),
outgoing = proto?.field("outgoing").toOutgoing(properties)
outgoing = proto?.field("outgoing").toOutgoing(properties),
customData = proto?.field("customData").toCustomData()
)

fun withIncomingPermissionsDisabled(): ProxySettings = copy(
Expand Down Expand Up @@ -482,6 +484,28 @@ fun Value.toDuration(): Duration? {
}
}

fun Value?.toCustomData(): Map<String, Any?> {
return when (this?.kindCase) {
Value.KindCase.STRUCT_VALUE -> this.toMap()
else -> emptyMap()
}
}

private fun Value.toMap(): Map<String, Any?> {
return this.structValue.fieldsMap.map { it.key to it.value.toCustomDataValue() }.toMap()
}

private fun Value?.toCustomDataValue(): Any? {
return when (this?.kindCase) {
Value.KindCase.BOOL_VALUE -> this.boolValue
Value.KindCase.LIST_VALUE -> this.listValue.valuesList.map { it.toCustomDataValue() }
Value.KindCase.STRING_VALUE -> this.stringValue
Value.KindCase.NUMBER_VALUE -> this.numberValue
Value.KindCase.STRUCT_VALUE -> this.toMap()
else -> null
}
}

data class Incoming(
val endpoints: List<IncomingEndpoint> = emptyList(),
val rateLimitEndpoints: List<IncomingRateLimitEndpoint> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package pl.allegro.tech.servicemesh.envoycontrol.groups

import com.google.protobuf.ListValue
import com.google.protobuf.NullValue
import com.google.protobuf.Struct
import com.google.protobuf.Value
import com.google.protobuf.util.Durations
import io.envoyproxy.envoy.config.accesslog.v3.ComparisonFilter
Expand Down Expand Up @@ -52,6 +55,40 @@ class NodeMetadataTest {
arguments(null, "/prefix", "/regex"),
arguments("/path", "/prefix", "/regex")
)

@JvmStatic
fun parsingCustomData() = listOf(
arguments("bool", Value.newBuilder().setBoolValue(true).build(), true),
arguments("string", Value.newBuilder().setStringValue("string").build(), "string"),
arguments("number", Value.newBuilder().setNumberValue(1.0).build(), 1.0),
arguments("not_set", Value.newBuilder().build(), null),
arguments("null", Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(), null),
arguments("list", Value.newBuilder().setListValue(
ListValue.newBuilder()
.addValues(Value.newBuilder().setBoolValue(true).build()).build())
.build(), listOf(true)
),
arguments("struct", Value.newBuilder().setStructValue(
Struct.newBuilder()
.putFields("string", Value.newBuilder().setBoolValue(true).build())
.build()
).build(), mapOf("string" to true)
)
)

@JvmStatic
fun parsingNotStructInCustomData() = listOf(
arguments(Value.newBuilder().setBoolValue(true).build(), true),
arguments(Value.newBuilder().setStringValue("string").build(), "string"),
arguments(Value.newBuilder().setNumberValue(1.0).build(), 1.0),
arguments(Value.newBuilder().build(), null),
arguments(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(), null),
arguments(Value.newBuilder().setListValue(
ListValue.newBuilder()
.addValues(Value.newBuilder().setBoolValue(true).build()).build())
.build(), listOf(true)
)
)
}

private fun snapshotProperties(
Expand Down Expand Up @@ -1217,6 +1254,49 @@ class NodeMetadataTest {
}
}

@ParameterizedTest
@MethodSource("parsingNotStructInCustomData")
fun `should return empty custom data if is not a struct`(value: Value) {
// when
val customData = value.toCustomData()

// then
assertThat(customData).isEmpty()
}

@ParameterizedTest
@MethodSource("parsingCustomData")
fun `should parse custom data if it is a struct with value`(name: String, field: Value, expected: Any?) {
// given
val value = Value.newBuilder()
.setStructValue(Struct.newBuilder()
.putFields(name, field)
.build())
.build()

// when
val customData = value.toCustomData()

// then
assertThat(customData).isEqualTo(mapOf(name to expected))
}

@Test
fun `should parse custom data if is a struct`() {
// given
val value = Value.newBuilder().setStructValue(
Struct.newBuilder()
.putFields("abc", Value.newBuilder().setBoolValue(true).build())
.build()
).build()

// when
val customData = value.toCustomData()

// then
assertThat(customData).isEqualTo(mapOf("abc" to true))
}

fun ObjectAssert<DependencySettings>.hasTimeouts(
idleTimeout: String,
connectionIdleTimeout: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ConsulContainer(
val internalPort: Int = 8500
) : GenericContainer<ConsulContainer>(
ImageFromDockerfile().withDockerfileFromBuilder {
it.from("consul:latest")
it.from("consul:1.10.12")
.run("apk", "add", "iproute2")
.cmd(consulConfig.launchCommand())
.expose(internalPort)
Expand Down

0 comments on commit aeb6b3c

Please sign in to comment.