@@ -108,6 +108,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
108
108
@ Nullable private final Credentials credentials ;
109
109
@ Nullable private final ChannelPrimer channelPrimer ;
110
110
@ Nullable private final Boolean attemptDirectPath ;
111
+ @ Nullable private final Boolean attemptDirectPathXds ;
111
112
@ Nullable private final Boolean allowNonDefaultServiceAccount ;
112
113
@ VisibleForTesting final ImmutableMap <String , ?> directPathServiceConfig ;
113
114
@ Nullable private final MtlsProvider mtlsProvider ;
@@ -133,6 +134,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
133
134
this .credentials = builder .credentials ;
134
135
this .channelPrimer = builder .channelPrimer ;
135
136
this .attemptDirectPath = builder .attemptDirectPath ;
137
+ this .attemptDirectPathXds = builder .attemptDirectPathXds ;
136
138
this .allowNonDefaultServiceAccount = builder .allowNonDefaultServiceAccount ;
137
139
this .directPathServiceConfig =
138
140
builder .directPathServiceConfig == null
@@ -249,6 +251,21 @@ private boolean isDirectPathEnabled() {
249
251
return false ;
250
252
}
251
253
254
+ @ VisibleForTesting
255
+ boolean isDirectPathXdsEnabled () {
256
+ // Method 1: Enable DirectPath xDS by option.
257
+ if (Boolean .TRUE .equals (attemptDirectPathXds )) {
258
+ return true ;
259
+ }
260
+ // Method 2: Enable DirectPath xDS by env.
261
+ String directPathXdsEnv = envProvider .getenv (DIRECT_PATH_ENV_ENABLE_XDS );
262
+ boolean isDirectPathXdsEnv = Boolean .parseBoolean (directPathXdsEnv );
263
+ if (isDirectPathXdsEnv ) {
264
+ return true ;
265
+ }
266
+ return false ;
267
+ }
268
+
252
269
private boolean isNonDefaultServiceAccountAllowed () {
253
270
if (allowNonDefaultServiceAccount != null && allowNonDefaultServiceAccount ) {
254
271
return true ;
@@ -304,13 +321,13 @@ private ManagedChannel createSingleChannel() throws IOException {
304
321
ManagedChannelBuilder <?> builder ;
305
322
306
323
// Check DirectPath traffic.
307
- boolean isDirectPathXdsEnabled = false ;
324
+ boolean useDirectPathXds = false ;
308
325
if (isDirectPathEnabled () && isNonDefaultServiceAccountAllowed () && isOnComputeEngine ()) {
309
326
CallCredentials callCreds = MoreCallCredentials .from (credentials );
310
327
ChannelCredentials channelCreds =
311
328
GoogleDefaultChannelCredentials .newBuilder ().callCredentials (callCreds ).build ();
312
- isDirectPathXdsEnabled = Boolean . parseBoolean ( envProvider . getenv ( DIRECT_PATH_ENV_ENABLE_XDS ) );
313
- if (isDirectPathXdsEnabled ) {
329
+ useDirectPathXds = isDirectPathXdsEnabled ( );
330
+ if (useDirectPathXds ) {
314
331
// google-c2p: CloudToProd(C2P) Directpath. This scheme is defined in
315
332
// io.grpc.googleapis.GoogleCloudToProdNameResolverProvider.
316
333
// This resolver target must not have a port number.
@@ -337,7 +354,7 @@ private ManagedChannel createSingleChannel() throws IOException {
337
354
}
338
355
}
339
356
// google-c2p resolver requires service config lookup
340
- if (!isDirectPathXdsEnabled ) {
357
+ if (!useDirectPathXds ) {
341
358
// See https://github.com/googleapis/gapic-generator/issues/2816
342
359
builder .disableServiceConfigLookUp ();
343
360
}
@@ -435,6 +452,7 @@ public static final class Builder {
435
452
@ Nullable private ChannelPrimer channelPrimer ;
436
453
private ChannelPoolSettings channelPoolSettings ;
437
454
@ Nullable private Boolean attemptDirectPath ;
455
+ @ Nullable private Boolean attemptDirectPathXds ;
438
456
@ Nullable private Boolean allowNonDefaultServiceAccount ;
439
457
@ Nullable private ImmutableMap <String , ?> directPathServiceConfig ;
440
458
@@ -461,6 +479,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
461
479
this .channelPrimer = provider .channelPrimer ;
462
480
this .channelPoolSettings = provider .channelPoolSettings ;
463
481
this .attemptDirectPath = provider .attemptDirectPath ;
482
+ this .attemptDirectPathXds = provider .attemptDirectPathXds ;
464
483
this .allowNonDefaultServiceAccount = provider .allowNonDefaultServiceAccount ;
465
484
this .directPathServiceConfig = provider .directPathServiceConfig ;
466
485
this .mtlsProvider = provider .mtlsProvider ;
@@ -669,6 +688,13 @@ public Builder setAllowNonDefaultServiceAccount(boolean allowNonDefaultServiceAc
669
688
return this ;
670
689
}
671
690
691
+ /** Use DirectPath xDS. Only valid if DirectPath is attempted. */
692
+ @ InternalApi ("For internal use by google-cloud-java clients only" )
693
+ public Builder setAttemptDirectPathXds () {
694
+ this .attemptDirectPathXds = true ;
695
+ return this ;
696
+ }
697
+
672
698
/**
673
699
* Sets a service config for direct path. If direct path is not enabled, the provided service
674
700
* config will be ignored.
0 commit comments