Skip to content

Commit a97db60

Browse files
authored
c2p resolver: use federation if enabled via env var (#9660)
1 parent 47ddfa4 commit a97db60

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

googleapis/src/main/java/io/grpc/googleapis/GoogleCloudToProdNameResolver.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.annotations.VisibleForTesting;
2222
import com.google.common.base.Charsets;
2323
import com.google.common.base.Preconditions;
24+
import com.google.common.base.Strings;
2425
import com.google.common.collect.ImmutableList;
2526
import com.google.common.collect.ImmutableMap;
2627
import com.google.common.io.CharStreams;
@@ -54,6 +55,7 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
5455
@VisibleForTesting
5556
static final String METADATA_URL_SUPPORT_IPV6 =
5657
"http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ipv6s";
58+
static final String C2P_AUTHORITY = "traffic-director-c2p.xds.googleapis.com";
5759
@VisibleForTesting
5860
static boolean isOnGcp = InternalCheckGcpEnvironment.isOnGcp();
5961
@VisibleForTesting
@@ -62,6 +64,10 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
6264
|| System.getProperty("io.grpc.xds.bootstrap") != null
6365
|| System.getenv("GRPC_XDS_BOOTSTRAP_CONFIG") != null
6466
|| System.getProperty("io.grpc.xds.bootstrapConfig") != null;
67+
@VisibleForTesting
68+
static boolean enableFederation =
69+
!Strings.isNullOrEmpty(System.getenv("GRPC_EXPERIMENTAL_XDS_FEDERATION"))
70+
&& Boolean.parseBoolean(System.getenv("GRPC_EXPERIMENTAL_XDS_FEDERATION"));
6571

6672
private static final String serverUriOverride =
6773
System.getenv("GRPC_TEST_ONLY_GOOGLE_C2P_RESOLVER_TRAFFIC_DIRECTOR_URI");
@@ -76,7 +82,10 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
7682
private final boolean usingExecutorResource;
7783
// It's not possible to use both PSM and DirectPath C2P in the same application.
7884
// Delegate to DNS if user-provided bootstrap is found.
79-
private final String schemeOverride = !isOnGcp || xdsBootstrapProvided ? "dns" : "xds";
85+
private final String schemeOverride =
86+
!isOnGcp
87+
|| (xdsBootstrapProvided && !enableFederation)
88+
? "dns" : "xds";
8089
private Executor executor;
8190
private Listener2 listener;
8291
private boolean succeeded;
@@ -103,8 +112,12 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
103112
targetUri);
104113
authority = GrpcUtil.checkAuthority(targetPath.substring(1));
105114
syncContext = checkNotNull(args, "args").getSynchronizationContext();
115+
targetUri = overrideUriScheme(targetUri, schemeOverride);
116+
if (schemeOverride.equals("xds") && enableFederation) {
117+
targetUri = overrideUriAuthority(targetUri, C2P_AUTHORITY);
118+
}
106119
delegate = checkNotNull(nameResolverFactory, "nameResolverFactory").newNameResolver(
107-
overrideUriScheme(targetUri, schemeOverride), args);
120+
targetUri, args);
108121
executor = args.getOffloadExecutor();
109122
usingExecutorResource = executor == null;
110123
}
@@ -193,7 +206,7 @@ public void run() {
193206
serverBuilder.put("server_features", ImmutableList.of("xds_v3"));
194207
ImmutableMap.Builder<String, Object> authoritiesBuilder = ImmutableMap.builder();
195208
authoritiesBuilder.put(
196-
"traffic-director-c2p.xds.googleapis.com",
209+
C2P_AUTHORITY,
197210
ImmutableMap.of("xds_servers", ImmutableList.of(serverBuilder.buildOrThrow())));
198211
return ImmutableMap.of(
199212
"node", nodeBuilder.buildOrThrow(),
@@ -271,6 +284,16 @@ private static URI overrideUriScheme(URI uri, String scheme) {
271284
return res;
272285
}
273286

287+
private static URI overrideUriAuthority(URI uri, String authority) {
288+
URI res;
289+
try {
290+
res = new URI(uri.getScheme(), authority, uri.getPath(), uri.getQuery(), uri.getFragment());
291+
} catch (URISyntaxException ex) {
292+
throw new IllegalArgumentException("Invalid authority: " + authority, ex);
293+
}
294+
return res;
295+
}
296+
274297
private enum HttpConnectionFactory implements HttpConnectionProvider {
275298
INSTANCE;
276299

googleapis/src/test/java/io/grpc/googleapis/GoogleCloudToProdNameResolverTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class GoogleCloudToProdNameResolverTest {
6666
@Rule
6767
public final MockitoRule mocks = MockitoJUnit.rule();
6868

69-
private static final URI TARGET_URI = URI.create("google-c2p-experimental:///googleapis.com");
69+
private static final URI TARGET_URI = URI.create("google-c2p:///googleapis.com");
7070
private static final String ZONE = "us-central1-a";
7171
private static final int DEFAULT_PORT = 887;
7272

@@ -187,6 +187,40 @@ public void onGcpAndNoProvidedBootstrapDelegateToXds() {
187187
"server_uri", "directpath-pa.googleapis.com",
188188
"channel_creds", ImmutableList.of(ImmutableMap.of("type", "google_default")),
189189
"server_features", ImmutableList.of("xds_v3"));
190+
Map<String, ?> authorities = (Map<String, ?>) bootstrap.get("authorities");
191+
assertThat(authorities).containsExactly(
192+
"traffic-director-c2p.xds.googleapis.com",
193+
ImmutableMap.of("xds_servers", ImmutableList.of(server)));
194+
}
195+
196+
@SuppressWarnings("unchecked")
197+
@Test
198+
public void onGcpAndProvidedBootstrapAndFederationEnabledDelegateToXds() {
199+
GoogleCloudToProdNameResolver.isOnGcp = true;
200+
GoogleCloudToProdNameResolver.xdsBootstrapProvided = true;
201+
GoogleCloudToProdNameResolver.enableFederation = true;
202+
createResolver();
203+
resolver.start(mockListener);
204+
fakeExecutor.runDueTasks();
205+
assertThat(delegatedResolver.keySet()).containsExactly("xds");
206+
verify(Iterables.getOnlyElement(delegatedResolver.values())).start(mockListener);
207+
// check bootstrap
208+
Map<String, ?> bootstrap = fakeBootstrapSetter.bootstrapRef.get();
209+
Map<String, ?> node = (Map<String, ?>) bootstrap.get("node");
210+
assertThat(node).containsExactly(
211+
"id", "C2P-991614323",
212+
"locality", ImmutableMap.of("zone", ZONE),
213+
"metadata", ImmutableMap.of("TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE", true));
214+
Map<String, ?> server = Iterables.getOnlyElement(
215+
(List<Map<String, ?>>) bootstrap.get("xds_servers"));
216+
assertThat(server).containsExactly(
217+
"server_uri", "directpath-pa.googleapis.com",
218+
"channel_creds", ImmutableList.of(ImmutableMap.of("type", "google_default")),
219+
"server_features", ImmutableList.of("xds_v3"));
220+
Map<String, ?> authorities = (Map<String, ?>) bootstrap.get("authorities");
221+
assertThat(authorities).containsExactly(
222+
"traffic-director-c2p.xds.googleapis.com",
223+
ImmutableMap.of("xds_servers", ImmutableList.of(server)));
190224
}
191225

192226
@Test

0 commit comments

Comments
 (0)