diff --git a/cache/src/main/java/io/envoyproxy/controlplane/cache/Resources.java b/cache/src/main/java/io/envoyproxy/controlplane/cache/Resources.java index 55d2b0630..b5311d8d6 100644 --- a/cache/src/main/java/io/envoyproxy/controlplane/cache/Resources.java +++ b/cache/src/main/java/io/envoyproxy/controlplane/cache/Resources.java @@ -2,7 +2,6 @@ import static com.google.common.base.Strings.isNullOrEmpty; import static io.envoyproxy.controlplane.cache.Resources.ApiVersion.V3; -import static io.envoyproxy.controlplane.cache.Resources.ResourceType.*; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -78,16 +77,21 @@ public static class V3 { } public static final List RESOURCE_TYPES_IN_ORDER = - ImmutableList.of(CLUSTER, ENDPOINT, LISTENER, ROUTE, VIRTUAL_HOST, SECRET); + ImmutableList.of(ResourceType.CLUSTER, + ResourceType.ENDPOINT, + ResourceType.LISTENER, + ResourceType.ROUTE, + ResourceType.VIRTUAL_HOST, + ResourceType.SECRET); public static final Map TYPE_URLS_TO_RESOURCE_TYPE = new ImmutableMap.Builder() - .put(Resources.V3.CLUSTER_TYPE_URL, CLUSTER) - .put(Resources.V3.ENDPOINT_TYPE_URL, ENDPOINT) - .put(Resources.V3.LISTENER_TYPE_URL, LISTENER) - .put(Resources.V3.ROUTE_TYPE_URL, ROUTE) - .put(Resources.V3.VIRTUAL_HOST_TYPE_URL, VIRTUAL_HOST) - .put(Resources.V3.SECRET_TYPE_URL, SECRET) + .put(Resources.V3.CLUSTER_TYPE_URL, ResourceType.CLUSTER) + .put(Resources.V3.ENDPOINT_TYPE_URL, ResourceType.ENDPOINT) + .put(Resources.V3.LISTENER_TYPE_URL, ResourceType.LISTENER) + .put(Resources.V3.ROUTE_TYPE_URL, ResourceType.ROUTE) + .put(Resources.V3.VIRTUAL_HOST_TYPE_URL, ResourceType.VIRTUAL_HOST) + .put(Resources.V3.SECRET_TYPE_URL, ResourceType.SECRET) .build(); public static final Map> RESOURCE_TYPE_BY_URL = diff --git a/cache/src/main/java/io/envoyproxy/controlplane/cache/SnapshotResources.java b/cache/src/main/java/io/envoyproxy/controlplane/cache/SnapshotResources.java index 374c71f77..d2c3898dd 100644 --- a/cache/src/main/java/io/envoyproxy/controlplane/cache/SnapshotResources.java +++ b/cache/src/main/java/io/envoyproxy/controlplane/cache/SnapshotResources.java @@ -2,7 +2,6 @@ import com.google.auto.value.AutoValue; import com.google.protobuf.Message; -import com.google.protobuf.MessageLiteOrBuilder; import java.util.List; import java.util.Map; @@ -23,7 +22,7 @@ public static SnapshotResources create( Iterable resources, String version) { ResourceMapBuilder resourcesMapBuilder = createResourcesMap(resources); - return new AutoValue_SnapshotResources<>( + return new io.envoyproxy.controlplane.cache.AutoValue_SnapshotResources<>( resourcesMapBuilder.getVersionedResources(), resourcesMapBuilder.getResources(), (r) -> version @@ -42,7 +41,7 @@ public static SnapshotResources create( Iterable> resources, ResourceVersionResolver versionResolver) { ResourceMapBuilder resourcesMapBuilder = createResourcesMap(resources); - return new AutoValue_SnapshotResources<>( + return new io.envoyproxy.controlplane.cache.AutoValue_SnapshotResources<>( resourcesMapBuilder.getVersionedResources(), resourcesMapBuilder.getResources(), versionResolver); diff --git a/cache/src/main/java/io/envoyproxy/controlplane/cache/TestResources.java b/cache/src/main/java/io/envoyproxy/controlplane/cache/TestResources.java index aebe5f3c0..9b1cac2df 100644 --- a/cache/src/main/java/io/envoyproxy/controlplane/cache/TestResources.java +++ b/cache/src/main/java/io/envoyproxy/controlplane/cache/TestResources.java @@ -20,18 +20,18 @@ import io.envoyproxy.envoy.config.listener.v3.Filter; import io.envoyproxy.envoy.config.listener.v3.FilterChain; import io.envoyproxy.envoy.config.listener.v3.Listener; -import io.envoyproxy.envoy.config.route.v3.*; -import io.envoyproxy.envoy.config.route.v3.VirtualHost.Builder; +import io.envoyproxy.envoy.config.route.v3.Route; +import io.envoyproxy.envoy.config.route.v3.RouteAction; +import io.envoyproxy.envoy.config.route.v3.RouteConfiguration; +import io.envoyproxy.envoy.config.route.v3.RouteMatch; +import io.envoyproxy.envoy.config.route.v3.Vhds; +import io.envoyproxy.envoy.config.route.v3.VirtualHost; import io.envoyproxy.envoy.extensions.filters.http.router.v3.Router; import io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager; import io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager.CodecType; import io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.Secret; import io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.TlsCertificate; -import io.envoyproxy.envoy.service.route.v3.VirtualHostDiscoveryService; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; /** * {@code TestResources} provides helper methods for generating resource messages for testing. It is @@ -216,7 +216,7 @@ public static Listener createListener( * * @param routeName name of the new route */ - public static RouteConfiguration createVHDSRoute(String routeName) { + public static RouteConfiguration createVhdsRoute(String routeName) { ApiVersion adsTransportVersion = ApiVersion.V3; @@ -234,11 +234,9 @@ public static RouteConfiguration createVHDSRoute(String routeName) { .setClusterName(XDS_CLUSTER)))) .build(); - RouteConfiguration routeConfigurationbuilder = RouteConfiguration.newBuilder() + return RouteConfiguration.newBuilder() .setVhds(Vhds.newBuilder().setConfigSource(edsSource).build()) .setName(routeName).build(); - - return routeConfigurationbuilder; } /** @@ -261,10 +259,16 @@ public static RouteConfiguration createRoute(String routeName, String clusterNam .build(); } - public static VirtualHost createVirtualHost(String name, int index, String domains) { + /** + * Returns a new Virtual Host. + * + * @param name Virtual host name + * @param domain domain name + */ + public static VirtualHost createVirtualHost(String name, String domain) { return VirtualHost.newBuilder() - .setName("all") - .addDomains("*") + .setName(name) + .addDomains(domain) .addRoutes( Route.newBuilder() .setMatch(RouteMatch.newBuilder().setPrefix("/"))) diff --git a/cache/src/main/java/io/envoyproxy/controlplane/cache/v3/Snapshot.java b/cache/src/main/java/io/envoyproxy/controlplane/cache/v3/Snapshot.java index 110bca875..804542c5d 100644 --- a/cache/src/main/java/io/envoyproxy/controlplane/cache/v3/Snapshot.java +++ b/cache/src/main/java/io/envoyproxy/controlplane/cache/v3/Snapshot.java @@ -15,12 +15,13 @@ import io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment; import io.envoyproxy.envoy.config.listener.v3.Listener; import io.envoyproxy.envoy.config.route.v3.RouteConfiguration; +import io.envoyproxy.envoy.config.route.v3.VirtualHost; import io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.Secret; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; -import io.envoyproxy.envoy.config.route.v3.VirtualHost; + /** * {@code Snapshot} is a data class that contains an internally consistent snapshot of v3 xDS resources. Snapshots @@ -50,7 +51,7 @@ public static Snapshot create( String version) { - return new AutoValue_Snapshot( + return new io.envoyproxy.controlplane.cache.v3.AutoValue_Snapshot( SnapshotResources .create(generateSnapshotResourceIterable(clusters), version), SnapshotResources @@ -93,7 +94,7 @@ public static Snapshot create( String secretsVersion) { // TODO(snowp): add a builder alternative - return new AutoValue_Snapshot( + return new io.envoyproxy.controlplane.cache.v3.AutoValue_Snapshot( SnapshotResources.create(generateSnapshotResourceIterable(clusters), clustersVersion), SnapshotResources.create(generateSnapshotResourceIterable(endpoints), @@ -142,6 +143,7 @@ public static Snapshot createEmpty(String version) { * Returns all secret items in the SDS payload. */ public abstract SnapshotResources secrets(); + public abstract SnapshotResources virtualHosts(); /** diff --git a/cache/src/test/java/io/envoyproxy/controlplane/cache/v3/SimpleCacheTest.java b/cache/src/test/java/io/envoyproxy/controlplane/cache/v3/SimpleCacheTest.java index b7c636931..dee15795d 100644 --- a/cache/src/test/java/io/envoyproxy/controlplane/cache/v3/SimpleCacheTest.java +++ b/cache/src/test/java/io/envoyproxy/controlplane/cache/v3/SimpleCacheTest.java @@ -7,7 +7,14 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; import com.google.protobuf.Message; -import io.envoyproxy.controlplane.cache.*; +import io.envoyproxy.controlplane.cache.NodeGroup; +import io.envoyproxy.controlplane.cache.Resources; +import io.envoyproxy.controlplane.cache.Response; +import io.envoyproxy.controlplane.cache.StatusInfo; +import io.envoyproxy.controlplane.cache.TestResources; +import io.envoyproxy.controlplane.cache.VersionedResource; +import io.envoyproxy.controlplane.cache.Watch; +import io.envoyproxy.controlplane.cache.XdsRequest; import io.envoyproxy.envoy.config.cluster.v3.Cluster; import io.envoyproxy.envoy.config.core.v3.Node; import io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment; @@ -43,7 +50,7 @@ public class SimpleCacheTest { ImmutableList.of(ClusterLoadAssignment.getDefaultInstance()), ImmutableList.of(Listener.newBuilder().setName(LISTENER_NAME).build()), ImmutableList.of(RouteConfiguration.newBuilder().setName(ROUTE_NAME).build()), - ImmutableList.of(TestResources.createVirtualHost("v1", 1, "a")), + ImmutableList.of(TestResources.createVirtualHost("v1", "a")), ImmutableList.of(Secret.newBuilder().setName(SECRET_NAME).build()), VERSION1); @@ -53,7 +60,7 @@ public class SimpleCacheTest { ImmutableList.of(ClusterLoadAssignment.getDefaultInstance()), ImmutableList.of(Listener.newBuilder().setName(LISTENER_NAME).build()), ImmutableList.of(RouteConfiguration.newBuilder().setName(ROUTE_NAME).build()), - ImmutableList.of(TestResources.createVirtualHost("v1", 1, "a")), + ImmutableList.of(TestResources.createVirtualHost("v1", "a")), ImmutableList.of(Secret.newBuilder().setName(SECRET_NAME).build()), VERSION2); @@ -65,7 +72,7 @@ public class SimpleCacheTest { ClusterLoadAssignment.newBuilder().setClusterName(SECONDARY_CLUSTER_NAME).build()), ImmutableList.of(Listener.newBuilder().setName(LISTENER_NAME).build()), ImmutableList.of(RouteConfiguration.newBuilder().setName(ROUTE_NAME).build()), - ImmutableList.of(TestResources.createVirtualHost("v1", 1, "a")), + ImmutableList.of(TestResources.createVirtualHost("v1","a")), ImmutableList.of(Secret.newBuilder().setName(SECRET_NAME).build()), VERSION2); @@ -429,7 +436,13 @@ public void watchesAreReleasedAfterCancel() { public void watchIsLeftOpenIfNotRespondedImmediately() { SimpleCache cache = new SimpleCache<>(new SingleNodeGroup()); cache.setSnapshot(SingleNodeGroup.GROUP, Snapshot.create( - ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), ImmutableList.of(),VERSION1)); + ImmutableList.of(), + ImmutableList.of(), + ImmutableList.of(), + ImmutableList.of(), + ImmutableList.of(), + ImmutableList.of(), + VERSION1)); ResponseTracker responseTracker = new ResponseTracker(); Watch watch = cache.createWatch( diff --git a/cache/src/test/java/io/envoyproxy/controlplane/cache/v3/SnapshotTest.java b/cache/src/test/java/io/envoyproxy/controlplane/cache/v3/SnapshotTest.java index dfb60dac2..0165b9617 100644 --- a/cache/src/test/java/io/envoyproxy/controlplane/cache/v3/SnapshotTest.java +++ b/cache/src/test/java/io/envoyproxy/controlplane/cache/v3/SnapshotTest.java @@ -1,6 +1,9 @@ package io.envoyproxy.controlplane.cache.v3; -import static io.envoyproxy.controlplane.cache.Resources.V3.*; +import static io.envoyproxy.controlplane.cache.Resources.V3.CLUSTER_TYPE_URL; +import static io.envoyproxy.controlplane.cache.Resources.V3.ENDPOINT_TYPE_URL; +import static io.envoyproxy.controlplane.cache.Resources.V3.LISTENER_TYPE_URL; +import static io.envoyproxy.controlplane.cache.Resources.V3.ROUTE_TYPE_URL; import static io.envoyproxy.envoy.config.core.v3.ApiVersion.V3; import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; @@ -40,7 +43,7 @@ public class SnapshotTest { private static final RouteConfiguration ROUTE = TestResources.createRoute(ROUTE_NAME, CLUSTER_NAME); private static final Secret SECRET = TestResources.createSecret(SECRET_NAME); - private static final VirtualHost VIRTUAL_HOST = TestResources.createVirtualHost(VIRTUAL_HOST_NAME, 1, "a"); + private static final VirtualHost VIRTUAL_HOST = TestResources.createVirtualHost(VIRTUAL_HOST_NAME, "a"); @Test public void createSingleVersionSetsResourcesCorrectly() { diff --git a/server/src/main/java/io/envoyproxy/controlplane/server/V3DiscoveryServer.java b/server/src/main/java/io/envoyproxy/controlplane/server/V3DiscoveryServer.java index b36ebba1f..ca8059a8f 100644 --- a/server/src/main/java/io/envoyproxy/controlplane/server/V3DiscoveryServer.java +++ b/server/src/main/java/io/envoyproxy/controlplane/server/V3DiscoveryServer.java @@ -1,5 +1,12 @@ package io.envoyproxy.controlplane.server; +import static io.envoyproxy.envoy.service.discovery.v3.AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase; +import static io.envoyproxy.envoy.service.endpoint.v3.EndpointDiscoveryServiceGrpc.EndpointDiscoveryServiceImplBase; +import static io.envoyproxy.envoy.service.listener.v3.ListenerDiscoveryServiceGrpc.ListenerDiscoveryServiceImplBase; +import static io.envoyproxy.envoy.service.route.v3.RouteDiscoveryServiceGrpc.RouteDiscoveryServiceImplBase; +import static io.envoyproxy.envoy.service.route.v3.VirtualHostDiscoveryServiceGrpc.VirtualHostDiscoveryServiceImplBase; +import static io.envoyproxy.envoy.service.secret.v3.SecretDiscoveryServiceGrpc.SecretDiscoveryServiceImplBase; + import com.google.common.base.Preconditions; import com.google.protobuf.Any; import io.envoyproxy.controlplane.cache.ConfigWatcher; @@ -8,21 +15,20 @@ import io.envoyproxy.controlplane.cache.XdsRequest; import io.envoyproxy.controlplane.server.serializer.DefaultProtoResourcesSerializer; import io.envoyproxy.controlplane.server.serializer.ProtoResourcesSerializer; + import io.envoyproxy.envoy.service.cluster.v3.ClusterDiscoveryServiceGrpc.ClusterDiscoveryServiceImplBase; -import io.envoyproxy.envoy.service.discovery.v3.*; + +import io.envoyproxy.envoy.service.discovery.v3.DeltaDiscoveryRequest; +import io.envoyproxy.envoy.service.discovery.v3.DeltaDiscoveryResponse; +import io.envoyproxy.envoy.service.discovery.v3.DiscoveryRequest; +import io.envoyproxy.envoy.service.discovery.v3.DiscoveryResponse; +import io.envoyproxy.envoy.service.discovery.v3.Resource; import io.grpc.stub.StreamObserver; import java.util.Collection; import java.util.Collections; import java.util.List; -import static io.envoyproxy.envoy.service.discovery.v3.AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase; -import static io.envoyproxy.envoy.service.endpoint.v3.EndpointDiscoveryServiceGrpc.EndpointDiscoveryServiceImplBase; -import static io.envoyproxy.envoy.service.listener.v3.ListenerDiscoveryServiceGrpc.ListenerDiscoveryServiceImplBase; -import static io.envoyproxy.envoy.service.route.v3.RouteDiscoveryServiceGrpc.RouteDiscoveryServiceImplBase; -import static io.envoyproxy.envoy.service.secret.v3.SecretDiscoveryServiceGrpc.SecretDiscoveryServiceImplBase; -import static io.envoyproxy.envoy.service.route.v3.VirtualHostDiscoveryServiceGrpc.VirtualHostDiscoveryServiceImplBase; - public class V3DiscoveryServer extends DiscoveryServer { public V3DiscoveryServer(ConfigWatcher configWatcher) { @@ -165,6 +171,11 @@ public StreamObserver streamSecrets( return createRequestHandler(responseObserver, false, Resources.V3.SECRET_TYPE_URL); } + /** + * Returns delta Secrets. + * + * @param responseObserver Stream observer + */ @Override public StreamObserver deltaSecrets( StreamObserver responseObserver) { @@ -174,6 +185,9 @@ public StreamObserver deltaSecrets( }; } + /** + * Returns VHDS implementation that uses this server. + */ public VirtualHostDiscoveryServiceImplBase getVirtualHostDiscoveryServiceImpl() { return new VirtualHostDiscoveryServiceImplBase() { diff --git a/server/src/main/java/io/envoyproxy/controlplane/server/XdsDeltaDiscoveryRequestStreamObserver.java b/server/src/main/java/io/envoyproxy/controlplane/server/XdsDeltaDiscoveryRequestStreamObserver.java index 6d410eb35..505c33f9c 100644 --- a/server/src/main/java/io/envoyproxy/controlplane/server/XdsDeltaDiscoveryRequestStreamObserver.java +++ b/server/src/main/java/io/envoyproxy/controlplane/server/XdsDeltaDiscoveryRequestStreamObserver.java @@ -36,7 +36,8 @@ public class XdsDeltaDiscoveryRequestStreamObserver extends DeltaDiscov this.pendingResources = new HashSet<>(); Resources.ResourceType resourceType = Resources.TYPE_URLS_TO_RESOURCE_TYPE.get(defaultTypeUrl); this.isWildcard = Resources.ResourceType.CLUSTER.equals(resourceType) - || Resources.ResourceType.LISTENER.equals(resourceType) || Resources.ResourceType.VIRTUAL_HOST.equals(resourceType); + || Resources.ResourceType.LISTENER.equals(resourceType) + || Resources.ResourceType.VIRTUAL_HOST.equals(resourceType); this.responses = new ConcurrentHashMap<>(); } diff --git a/server/src/test/java/io/envoyproxy/controlplane/server/TestMain.java b/server/src/test/java/io/envoyproxy/controlplane/server/TestMain.java index ecba91438..86d712eab 100644 --- a/server/src/test/java/io/envoyproxy/controlplane/server/TestMain.java +++ b/server/src/test/java/io/envoyproxy/controlplane/server/TestMain.java @@ -32,6 +32,7 @@ public static void main(String[] arg) throws IOException, InterruptedException { "cluster0", "127.0.0.1", 1234, Cluster.DiscoveryType.STATIC)), ImmutableList.of(), ImmutableList.of(), + ImmutableList.of(TestResources.createVhdsRoute("name1")), ImmutableList.of(), ImmutableList.of(), "1")); @@ -39,12 +40,13 @@ public static void main(String[] arg) throws IOException, InterruptedException { V3DiscoveryServer v3DiscoveryServer = new V3DiscoveryServer(cache); - ServerBuilder builder = + ServerBuilder builder = NettyServerBuilder.forPort(12345) .addService(v3DiscoveryServer.getAggregatedDiscoveryServiceImpl()) .addService(v3DiscoveryServer.getClusterDiscoveryServiceImpl()) .addService(v3DiscoveryServer.getEndpointDiscoveryServiceImpl()) .addService(v3DiscoveryServer.getListenerDiscoveryServiceImpl()) + .addService(v3DiscoveryServer.getVirtualHostDiscoveryServiceImpl()) .addService(v3DiscoveryServer.getRouteDiscoveryServiceImpl()); Server server = builder.build(); @@ -65,6 +67,7 @@ public static void main(String[] arg) throws IOException, InterruptedException { "cluster1", "127.0.0.1", 1235, Cluster.DiscoveryType.STATIC)), ImmutableList.of(), ImmutableList.of(), + ImmutableList.of(TestResources.createVhdsRoute("name2")), ImmutableList.of(), ImmutableList.of(), "1"));