From 7d7475f5d4980209e732287fc59c8124eaf89864 Mon Sep 17 00:00:00 2001 From: Kamil Smigielski Date: Wed, 5 Oct 2022 10:30:42 +0200 Subject: [PATCH] proposal for platform feature --- .../servicemesh/envoycontrol/ControlPlane.kt | 46 ++++++++++--------- .../servicemesh/envoycontrol/groups/Groups.kt | 27 ++++++----- .../envoycontrol/groups/MetadataNodeGroup.kt | 30 +++++++++--- 3 files changed, 62 insertions(+), 41 deletions(-) diff --git a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/ControlPlane.kt b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/ControlPlane.kt index ac431e297..3a33c9b42 100644 --- a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/ControlPlane.kt +++ b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/ControlPlane.kt @@ -13,6 +13,7 @@ import io.micrometer.core.instrument.MeterRegistry import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioServerSocketChannel +import pl.allegro.tech.servicemesh.envoycontrol.groups.EmptyCustomGroupDataMapper import pl.allegro.tech.servicemesh.envoycontrol.groups.Group import pl.allegro.tech.servicemesh.envoycontrol.groups.GroupChangeWatcher import pl.allegro.tech.servicemesh.envoycontrol.groups.MetadataNodeGroup @@ -54,21 +55,21 @@ import java.util.concurrent.ThreadPoolExecutor import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicInteger -class ControlPlane private constructor( +class ControlPlane private constructor( val grpcServer: Server, val snapshotUpdater: SnapshotUpdater, - val nodeGroup: NodeGroup, - val cache: SnapshotCache, + val nodeGroup: NodeGroup>, + val cache: SnapshotCache, Snapshot>, private val changes: Flux ) : AutoCloseable { private var servicesDisposable: Disposable? = null companion object { - fun builder( + fun builder( properties: EnvoyControlProperties, meterRegistry: MeterRegistry - ) = ControlPlaneBuilder(properties, meterRegistry) + ) = ControlPlaneBuilder(properties, meterRegistry) } fun start() { @@ -84,7 +85,7 @@ class ControlPlane private constructor( grpcServer.awaitTermination() } - class ControlPlaneBuilder( + class ControlPlaneBuilder( val properties: EnvoyControlProperties, val meterRegistry: MeterRegistry ) { @@ -99,11 +100,12 @@ class ControlPlane private constructor( var envoyHttpFilters: EnvoyHttpFilters = EnvoyHttpFilters.emptyFilters var snapshotChangeAuditor: SnapshotChangeAuditor = NoopSnapshotChangeAuditor - var nodeGroup: NodeGroup = MetadataNodeGroup( - properties = properties.envoy.snapshot + var nodeGroup: NodeGroup> = MetadataNodeGroup( + properties = properties.envoy.snapshot, + EmptyCustomGroupDataMapper() ) - fun build(changes: Flux): ControlPlane { + fun build(changes: Flux): ControlPlane { if (grpcServerExecutor == null) { grpcServerExecutor = buildThreadPoolExecutor() } @@ -242,8 +244,8 @@ class ControlPlane private constructor( } private fun buildSnapshotCollectingCallback( - cache: SimpleCache - ): SnapshotCollectingCallback { + cache: SimpleCache> + ): SnapshotCollectingCallback, Snapshot> { val cleanupProperties = properties.server.snapshotCleanup return SnapshotCollectingCallback( cache, @@ -298,57 +300,57 @@ class ControlPlane private constructor( ) } - fun withNodeGroup(nodeGroup: NodeGroup): ControlPlaneBuilder { + fun withNodeGroup(nodeGroup: NodeGroup>): ControlPlaneBuilder { this.nodeGroup = nodeGroup return this } - fun withGrpcServerExecutor(executor: Executor): ControlPlaneBuilder { + fun withGrpcServerExecutor(executor: Executor): ControlPlaneBuilder { grpcServerExecutor = executor return this } - fun withNioEventLoopExecutor(executor: Executor): ControlPlaneBuilder { + fun withNioEventLoopExecutor(executor: Executor): ControlPlaneBuilder { nioEventLoopExecutor = executor return this } - fun withNioBossEventLoopExecutor(executor: Executor): ControlPlaneBuilder { + fun withNioBossEventLoopExecutor(executor: Executor): ControlPlaneBuilder { nioBossEventLoopExecutor = executor return this } - fun withExecutorGroup(executor: ExecutorGroup): ControlPlaneBuilder { + fun withExecutorGroup(executor: ExecutorGroup): ControlPlaneBuilder { executorGroup = executor return this } - fun withGlobalSnapshotExecutor(executor: Executor): ControlPlaneBuilder { + fun withGlobalSnapshotExecutor(executor: Executor): ControlPlaneBuilder { globalSnapshotExecutor = executor return this } - fun withGlobalSnapshotAuditExecutor(executor: Executor): ControlPlaneBuilder { + fun withGlobalSnapshotAuditExecutor(executor: Executor): ControlPlaneBuilder { globalSnapshotAuditExecutor = executor return this } - fun withSnapshotChangeAuditor(snapshotChangeAuditor: SnapshotChangeAuditor): ControlPlaneBuilder { + fun withSnapshotChangeAuditor(snapshotChangeAuditor: SnapshotChangeAuditor): ControlPlaneBuilder { this.snapshotChangeAuditor = snapshotChangeAuditor return this } - fun withGroupSnapshotParallelExecutor(executorSupplier: () -> Executor): ControlPlaneBuilder { + fun withGroupSnapshotParallelExecutor(executorSupplier: () -> Executor): ControlPlaneBuilder { groupSnapshotParallelExecutorSupplier = executorSupplier return this } - fun withMetrics(metrics: EnvoyControlMetrics): ControlPlaneBuilder { + fun withMetrics(metrics: EnvoyControlMetrics): ControlPlaneBuilder { this.metrics = metrics return this } - fun withEnvoyHttpFilters(envoyHttpFilters: EnvoyHttpFilters): ControlPlaneBuilder { + fun withEnvoyHttpFilters(envoyHttpFilters: EnvoyHttpFilters): ControlPlaneBuilder { this.envoyHttpFilters = envoyHttpFilters return this } diff --git a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/groups/Groups.kt b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/groups/Groups.kt index ef6eff51b..fe39b1f16 100644 --- a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/groups/Groups.kt +++ b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/groups/Groups.kt @@ -1,28 +1,31 @@ package pl.allegro.tech.servicemesh.envoycontrol.groups -sealed class Group { - abstract val communicationMode: CommunicationMode - abstract val serviceName: String - abstract val discoveryServiceName: String? - abstract val proxySettings: ProxySettings - abstract val listenersConfig: ListenersConfig? +sealed interface Group { + val communicationMode: CommunicationMode + val serviceName: String + val discoveryServiceName: String? + val proxySettings: ProxySettings + val listenersConfig: ListenersConfig? + val customData: T? } -data class ServicesGroup( +data class ServicesGroup( override val communicationMode: CommunicationMode, override val serviceName: String = "", override val discoveryServiceName: String? = null, override val proxySettings: ProxySettings = ProxySettings(), - override val listenersConfig: ListenersConfig? = null -) : Group() + override val listenersConfig: ListenersConfig? = null, + override val customData: T? +) : Group -data class AllServicesGroup( +data class AllServicesGroup( override val communicationMode: CommunicationMode, override val serviceName: String = "", override val discoveryServiceName: String? = null, override val proxySettings: ProxySettings = ProxySettings(), - override val listenersConfig: ListenersConfig? = null -) : Group() + override val listenersConfig: ListenersConfig? = null, + override val customData: T? +) : Group data class ListenersConfig( val ingressHost: String, diff --git a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/groups/MetadataNodeGroup.kt b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/groups/MetadataNodeGroup.kt index f25228335..b8056e5f9 100644 --- a/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/groups/MetadataNodeGroup.kt +++ b/envoy-control-core/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/groups/MetadataNodeGroup.kt @@ -3,16 +3,30 @@ package pl.allegro.tech.servicemesh.envoycontrol.groups import com.google.protobuf.Struct import com.google.protobuf.Value import io.envoyproxy.controlplane.cache.NodeGroup +import io.envoyproxy.envoy.config.core.v3.Node import pl.allegro.tech.servicemesh.envoycontrol.logger import pl.allegro.tech.servicemesh.envoycontrol.snapshot.SnapshotProperties import io.envoyproxy.envoy.config.core.v3.Node as NodeV3 -class MetadataNodeGroup( - val properties: SnapshotProperties -) : NodeGroup { +interface CustomGroupDataMapper { + fun map(node: NodeV3): T? + +} + +class EmptyCustomGroupDataMapper: CustomGroupDataMapper { + override fun map(node: Node): T? { + return null + } +} + + +class MetadataNodeGroup( + val properties: SnapshotProperties, + val customGroupDataMapper: CustomGroupDataMapper +) : NodeGroup> { private val logger by logger() - override fun hash(node: NodeV3): Group { + override fun hash(node: NodeV3): Group { return createV3Group(node) } @@ -128,7 +142,7 @@ class MetadataNodeGroup( ) } - private fun createV3Group(node: NodeV3): Group { + private fun createV3Group(node: NodeV3): Group { val nodeMetadata = NodeMetadata(node.metadata, properties) val serviceName = serviceName(nodeMetadata) val discoveryServiceName = nodeMetadata.discoveryServiceName @@ -142,7 +156,8 @@ class MetadataNodeGroup( serviceName, discoveryServiceName, proxySettings, - listenersConfig + listenersConfig, + customGroupDataMapper.map(node) ) else -> ServicesGroup( @@ -150,7 +165,8 @@ class MetadataNodeGroup( serviceName, discoveryServiceName, proxySettings, - listenersConfig + listenersConfig, + customGroupDataMapper.map(node) ) } }