Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xds: fail to create xDS channel if no server with supported channel creds found #7400

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
100 changes: 100 additions & 0 deletions xds/src/main/java/io/grpc/xds/XdsChannelFactory.java
@@ -0,0 +1,100 @@
/*
* Copyright 2020 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.xds;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.annotations.VisibleForTesting;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.alts.GoogleDefaultChannelBuilder;
import io.grpc.xds.Bootstrapper.ChannelCreds;
import io.grpc.xds.Bootstrapper.ServerInfo;
import io.grpc.xds.XdsClient.XdsChannel;
import io.grpc.xds.XdsLogger.XdsLogLevel;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Factory for creating channels to xDS severs.
*/
abstract class XdsChannelFactory {
@VisibleForTesting
static boolean experimentalV3SupportEnvVar = Boolean.parseBoolean(
System.getenv("GRPC_XDS_EXPERIMENTAL_V3_SUPPORT"));

private static final String XDS_V3_SERVER_FEATURE = "xds_v3";
private static final XdsChannelFactory DEFAULT_INSTANCE = new XdsChannelFactory() {
/**
* Creates a channel to the first server in the given list.
*/
@Override
XdsChannel createChannel(List<ServerInfo> servers) throws XdsInitializationException {
checkArgument(!servers.isEmpty(), "No management server provided.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well throw XdsInitializationException?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Fixed.

XdsLogger logger = XdsLogger.withPrefix("xds-client-channel-factory");
ServerInfo serverInfo = servers.get(0);
String serverUri = serverInfo.getServerUri();
logger.log(XdsLogLevel.INFO, "Creating channel to {0}", serverUri);
List<ChannelCreds> channelCredsList = serverInfo.getChannelCredentials();
ManagedChannelBuilder<?> channelBuilder = null;
// Use the first supported channel credentials configuration.
for (ChannelCreds creds : channelCredsList) {
switch (creds.getType()) {
case "google_default":
logger.log(XdsLogLevel.INFO, "Using channel credentials: google_default");
channelBuilder = GoogleDefaultChannelBuilder.forTarget(serverUri);
break;
case "insecure":
logger.log(XdsLogLevel.INFO, "Using channel credentials: insecure");
channelBuilder = ManagedChannelBuilder.forTarget(serverUri).usePlaintext();
break;
case "tls":
logger.log(XdsLogLevel.INFO, "Using channel credentials: tls");
channelBuilder = ManagedChannelBuilder.forTarget(serverUri);
break;
default:
}
if (channelBuilder != null) {
break;
}
}
if (channelBuilder == null) {
throw new XdsInitializationException("No server with supported channel creds found");
}

ManagedChannel channel = channelBuilder
.keepAliveTime(5, TimeUnit.MINUTES)
.build();
boolean useProtocolV3 = experimentalV3SupportEnvVar
&& serverInfo.getServerFeatures().contains(XDS_V3_SERVER_FEATURE);

return new XdsChannel(channel, useProtocolV3);
}
};

static XdsChannelFactory getInstance() {
return DEFAULT_INSTANCE;
}

/**
* Creates a channel to one of the provided management servers.
*
* @throws XdsInitializationException if failed to create a channel with the given list of
* servers.
*/
abstract XdsChannel createChannel(List<ServerInfo> servers) throws XdsInitializationException;
}
68 changes: 0 additions & 68 deletions xds/src/main/java/io/grpc/xds/XdsClient.java
Expand Up @@ -16,7 +16,6 @@

package io.grpc.xds;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.annotations.VisibleForTesting;
Expand All @@ -25,29 +24,22 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Status;
import io.grpc.alts.GoogleDefaultChannelBuilder;
import io.grpc.internal.ObjectPool;
import io.grpc.xds.Bootstrapper.BootstrapInfo;
import io.grpc.xds.Bootstrapper.ChannelCreds;
import io.grpc.xds.Bootstrapper.ServerInfo;
import io.grpc.xds.EnvoyProtoData.DropOverload;
import io.grpc.xds.EnvoyProtoData.Locality;
import io.grpc.xds.EnvoyProtoData.LocalityLbEndpoints;
import io.grpc.xds.EnvoyProtoData.Route;
import io.grpc.xds.EnvoyServerProtoData.Listener;
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
import io.grpc.xds.LoadStatsManager.LoadStatsStore;
import io.grpc.xds.XdsLogger.XdsLogLevel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -613,62 +605,6 @@ public synchronized XdsClient returnObject(Object object) {
}
}

/**
* Factory for creating channels to xDS severs.
*/
abstract static class XdsChannelFactory {
@VisibleForTesting
static boolean experimentalV3SupportEnvVar = Boolean.parseBoolean(
System.getenv("GRPC_XDS_EXPERIMENTAL_V3_SUPPORT"));

private static final String XDS_V3_SERVER_FEATURE = "xds_v3";
private static final XdsChannelFactory DEFAULT_INSTANCE = new XdsChannelFactory() {
/**
* Creates a channel to the first server in the given list.
*/
@Override
XdsChannel createChannel(List<ServerInfo> servers) {
checkArgument(!servers.isEmpty(), "No management server provided.");
XdsLogger logger = XdsLogger.withPrefix("xds-client-channel-factory");
ServerInfo serverInfo = servers.get(0);
String serverUri = serverInfo.getServerUri();
logger.log(XdsLogLevel.INFO, "Creating channel to {0}", serverUri);
List<ChannelCreds> channelCredsList = serverInfo.getChannelCredentials();
ManagedChannelBuilder<?> channelBuilder = null;
// Use the first supported channel credentials configuration.
// Currently, only "google_default" is supported.
for (ChannelCreds creds : channelCredsList) {
if (creds.getType().equals("google_default")) {
logger.log(XdsLogLevel.INFO, "Using channel credentials: google_default");
channelBuilder = GoogleDefaultChannelBuilder.forTarget(serverUri);
break;
}
}
if (channelBuilder == null) {
logger.log(XdsLogLevel.INFO, "Using default channel credentials");
channelBuilder = ManagedChannelBuilder.forTarget(serverUri);
}

ManagedChannel channel = channelBuilder
.keepAliveTime(5, TimeUnit.MINUTES)
.build();
boolean useProtocolV3 = experimentalV3SupportEnvVar
&& serverInfo.getServerFeatures().contains(XDS_V3_SERVER_FEATURE);

return new XdsChannel(channel, useProtocolV3);
}
};

static XdsChannelFactory getInstance() {
return DEFAULT_INSTANCE;
}

/**
* Creates a channel to one of the provided management servers.
*/
abstract XdsChannel createChannel(List<ServerInfo> servers);
}

static final class XdsChannel {
private final ManagedChannel managedChannel;
private final boolean useProtocolV3;
Expand All @@ -687,8 +623,4 @@ boolean isUseProtocolV3() {
return useProtocolV3;
}
}

interface XdsClientPoolFactory {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More cleanup to be done to put RefCountedXdsClientObjectPool into its own file.

ObjectPool<XdsClient> newXdsClientObjectPool(BootstrapInfo bootstrapInfo);
}
}
99 changes: 99 additions & 0 deletions xds/src/test/java/io/grpc/xds/XdsChannelFactoryTest.java
@@ -0,0 +1,99 @@
/*
* Copyright 2020 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.xds;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import io.grpc.xds.Bootstrapper.ChannelCreds;
import io.grpc.xds.Bootstrapper.ServerInfo;
import io.grpc.xds.XdsClient.XdsChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests for {@link XdsChannelFactory}.
*/
@RunWith(JUnit4.class)
public class XdsChannelFactoryTest {

private final XdsChannelFactory channelFactory = XdsChannelFactory.getInstance();
private final List<XdsChannel> channels = new ArrayList<>();
private ServerInfo server1; // google_default
private ServerInfo server2; // plaintext, v3
private ServerInfo server3; // unsupported

@Before
public void setUp() {
ChannelCreds googleDefault = new ChannelCreds("google_default", null);
ChannelCreds insecure = new ChannelCreds("insecure", null);
ChannelCreds unsupported = new ChannelCreds("unsupported", null);
server1 = new ServerInfo("server1.com", Collections.singletonList(googleDefault),
Collections.<String>emptyList());
server2 = new ServerInfo("server2.com", Collections.singletonList(insecure),
Collections.singletonList("xds_v3"));
server3 = new ServerInfo("server4.com", Collections.singletonList(unsupported),
Collections.<String>emptyList());
}

@After
public void tearDown() {
for (XdsChannel channel : channels) {
channel.getManagedChannel().shutdown();
}
}

@Test
public void failToCreateChannel_unsupportedChannelCreds() {
try {
createChannel(server3);
fail("Should have thrown");
} catch (XdsInitializationException expected) {
}
}

@Test
public void defaultUseV2ProtocolL() throws XdsInitializationException {
XdsChannel channel = createChannel(server1);
assertThat(channel.isUseProtocolV3()).isFalse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also check
server1 with experimentalV3SupportEnvVar = true
and
server2 with experimentalV3SupportEnvVar = false
still do not support v3.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... Doesn't need to be that throughout, right? It makes tests noisy to test trivial things deeply.

server1 with experimentalV3SupportEnvVar = true and server2 with experimentalV3SupportEnvVar = false

This really just tests the functionality of the environment variable, which is a temporary gate. IMO, we shouldn't bother testing it. Unit tests can also introduces bugs, so if something is trivial in implementation, we won't bother write tests for it.

}

@Test
public void supportServerFeature_v3Protocol() throws XdsInitializationException {
boolean originalV3SupportEnvVar = XdsChannelFactory.experimentalV3SupportEnvVar;
XdsChannelFactory.experimentalV3SupportEnvVar = true;
try {
XdsChannel channel = createChannel(server2);
assertThat(channel.isUseProtocolV3()).isTrue();
} finally {
XdsChannelFactory.experimentalV3SupportEnvVar = originalV3SupportEnvVar;
}
}

private XdsChannel createChannel(ServerInfo... servers) throws XdsInitializationException {
XdsChannel channel = channelFactory.createChannel(Arrays.asList(servers));
channels.add(channel);
return channel;
}
}
59 changes: 0 additions & 59 deletions xds/src/test/java/io/grpc/xds/XdsClientTest.java
Expand Up @@ -21,12 +21,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import com.google.common.collect.ImmutableList;
import io.grpc.xds.Bootstrapper.ChannelCreds;
import io.grpc.xds.Bootstrapper.ServerInfo;
import io.grpc.xds.XdsClient.RefCountedXdsClientObjectPool;
import io.grpc.xds.XdsClient.XdsChannel;
import io.grpc.xds.XdsClient.XdsChannelFactory;
import io.grpc.xds.XdsClient.XdsClientFactory;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -107,58 +102,4 @@ XdsClient createXdsClient() {
XdsClient xdsClient2 = xdsClientPool.getObject();
assertThat(xdsClient2).isNotSameInstanceAs(xdsClient1);
}

@Test
public void channelFactorySupportsV3() {
boolean originalV3SupportEnvVar = XdsChannelFactory.experimentalV3SupportEnvVar;
try {
XdsChannelFactory xdsChannelFactory = XdsChannelFactory.getInstance();
XdsChannelFactory.experimentalV3SupportEnvVar = true;
XdsChannel xdsChannel =
xdsChannelFactory.createChannel(
ImmutableList.of(
new ServerInfo(
"xdsserver.com",
ImmutableList.<ChannelCreds>of(),
ImmutableList.<String>of()),
new ServerInfo(
"xdsserver2.com",
ImmutableList.<ChannelCreds>of(),
ImmutableList.of("xds_v3"))));
xdsChannel.getManagedChannel().shutdown();
assertThat(xdsChannel.isUseProtocolV3()).isFalse();

XdsChannelFactory.experimentalV3SupportEnvVar = false;
xdsChannel =
xdsChannelFactory.createChannel(
ImmutableList.of(
new ServerInfo(
"xdsserver.com",
ImmutableList.<ChannelCreds>of(),
ImmutableList.of("xds_v3")),
new ServerInfo(
"xdsserver2.com",
ImmutableList.<ChannelCreds>of(),
ImmutableList.of("baz"))));
xdsChannel.getManagedChannel().shutdown();
assertThat(xdsChannel.isUseProtocolV3()).isFalse();

XdsChannelFactory.experimentalV3SupportEnvVar = true;
xdsChannel =
xdsChannelFactory.createChannel(
ImmutableList.of(
new ServerInfo(
"xdsserver.com",
ImmutableList.<ChannelCreds>of(),
ImmutableList.of("xds_v3")),
new ServerInfo(
"xdsserver2.com",
ImmutableList.<ChannelCreds>of(),
ImmutableList.of("baz"))));
xdsChannel.getManagedChannel().shutdown();
assertThat(xdsChannel.isUseProtocolV3()).isTrue();
} finally {
XdsChannelFactory.experimentalV3SupportEnvVar = originalV3SupportEnvVar;
}
}
}