2121import com .google .common .annotations .VisibleForTesting ;
2222import com .google .common .base .Charsets ;
2323import com .google .common .base .Preconditions ;
24+ import com .google .common .base .Strings ;
2425import com .google .common .collect .ImmutableList ;
2526import com .google .common .collect .ImmutableMap ;
2627import 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
0 commit comments