Skip to content

Commit 6ff16a0

Browse files
committed
xds: add support for setting bootstrap file with java system property
While most languages support setting environment variables during runtime, Java does not. In Java, the preferred approach is to use Java System Properties in order so specify configuration options. By checking for the existence of the io.grpc.xds.bootstrap property if GRPC_XDS_BOOTSTRAP is not found, it is possible to either supply the bootstrap location during runtime or as a Java argument. The environment variable still takes precedence in order to not break any existing documentation.
1 parent 2c935e3 commit 6ff16a0

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

examples/example-xds/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ being configured with the XDS management protocol. Out-of-the-box they behave th
66
as their hello-world version.
77

88
__XDS support is incomplete and experimental, with limited compatibility. It
9-
will be very hard to produce a working enviornment just by this example. Please
9+
will be very hard to produce a working environment just by this example. Please
1010
refer to documentation specific for your XDS management server and
1111
environment.__
1212

@@ -24,8 +24,8 @@ This creates the scripts `build/install/example-xds/bin/hello-world-client-xds`
2424
### Run the example without using XDS Credentials
2525

2626
To use XDS, you should first deploy the XDS management server in your deployment environment
27-
and know its name. You need to set the `GRPC_XDS_BOOTSTRAP` environment variable to point to the
28-
gRPC XDS bootstrap file (see
27+
and know its name. You need to set the `GRPC_XDS_BOOTSTRAP` environment variable (preferred) or if that is not set then
28+
the `io.grpc.xds.bootstrap` java system property to point to the gRPC XDS bootstrap file (see
2929
[gRFC A27](https://github.com/grpc/proposal/blob/master/A27-xds-global-load-balancing.md#xdsclient-and-bootstrap-file) for the
3030
bootstrap format). This is needed by both `build/install/example-xds/bin/hello-world-client-xds`
3131
and `build/install/example-xds/bin/hello-world-server-xds`.
@@ -61,7 +61,7 @@ $ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json
6161
$ ./build/install/example-xds/bin/hello-world-server-xds 8000 my-test-xds-server --secure
6262
```
6363

64-
2. Similarly, add `--secure` on the comamnd line when you run the xDS client:
64+
2. Similarly, add `--secure` on the command line when you run the xDS client:
6565
```
6666
$ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json
6767
$ ./build/install/example-xds/bin/hello-world-client-xds xds:///yourServersName:8000 my-test-xds-client --secure

xds/src/main/java/io/grpc/xds/Bootstrapper.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,28 @@ public abstract class Bootstrapper {
4747

4848
private static final String LOG_PREFIX = "xds-bootstrap";
4949
private static final String BOOTSTRAP_PATH_SYS_ENV_VAR = "GRPC_XDS_BOOTSTRAP";
50+
private static final String BOOTSTRAP_PATH_SYS_PROPERTY_VAR = "io.grpc.xds.bootstrap";
5051
@VisibleForTesting
5152
static final String CLIENT_FEATURE_DISABLE_OVERPROVISIONING =
5253
"envoy.lb.does_not_support_overprovisioning";
5354

5455
private static final Bootstrapper DEFAULT_INSTANCE = new Bootstrapper() {
5556
@Override
5657
public BootstrapInfo readBootstrap() throws XdsInitializationException {
57-
String filePath = System.getenv(BOOTSTRAP_PATH_SYS_ENV_VAR);
58+
String filePathSource = BOOTSTRAP_PATH_SYS_ENV_VAR;
59+
String filePath = System.getenv(filePathSource);
60+
if (filePath == null) {
61+
filePathSource = BOOTSTRAP_PATH_SYS_PROPERTY_VAR;
62+
filePath = System.getProperty(filePathSource);
63+
}
5864
if (filePath == null) {
5965
throw new XdsInitializationException(
60-
"Environment variable " + BOOTSTRAP_PATH_SYS_ENV_VAR + " not defined.");
66+
"Environment variable " + BOOTSTRAP_PATH_SYS_ENV_VAR
67+
+ " or Java System Property " + BOOTSTRAP_PATH_SYS_PROPERTY_VAR + " not defined.");
6168
}
6269
XdsLogger
6370
.withPrefix(LOG_PREFIX)
64-
.log(XdsLogLevel.INFO, BOOTSTRAP_PATH_SYS_ENV_VAR + "={0}", filePath);
71+
.log(XdsLogLevel.INFO, filePathSource + "={0}", filePath);
6572
String fileContent;
6673
try {
6774
fileContent = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);

xds/src/test/java/io/grpc/xds/XdsClientWrapperForServerSdsTestMisc.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ public void startXdsClient_expectException() {
201201
} catch (IOException expected) {
202202
assertThat(expected)
203203
.hasMessageThat()
204-
.contains("Environment variable GRPC_XDS_BOOTSTRAP not defined");
204+
.contains(
205+
"Environment variable GRPC_XDS_BOOTSTRAP"
206+
+ " or Java System Property io.grpc.xds.bootstrap not defined.");
205207
}
206208
ArgumentCaptor<Status> argCaptor = ArgumentCaptor.forClass(null);
207209
verify(mockServerWatcher).onError(argCaptor.capture());
@@ -210,7 +212,9 @@ public void startXdsClient_expectException() {
210212
assertThat(captured.getCause()).isInstanceOf(XdsInitializationException.class);
211213
assertThat(captured.getCause())
212214
.hasMessageThat()
213-
.contains("Environment variable GRPC_XDS_BOOTSTRAP not defined");
215+
.contains(
216+
"Environment variable GRPC_XDS_BOOTSTRAP"
217+
+ " or Java System Property io.grpc.xds.bootstrap not defined.");
214218
}
215219

216220
private DownstreamTlsContext sendListenerUpdate(

xds/src/test/java/io/grpc/xds/XdsServerBuilderTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ public void xdsServerWithoutMockXdsClient_startError()
236236
} catch (IOException expected) {
237237
assertThat(expected)
238238
.hasMessageThat()
239-
.contains("Environment variable GRPC_XDS_BOOTSTRAP not defined");
239+
.contains(
240+
"Environment variable GRPC_XDS_BOOTSTRAP"
241+
+ " or Java System Property io.grpc.xds.bootstrap not defined.");
240242
}
241243
ArgumentCaptor<Status> argCaptor = ArgumentCaptor.forClass(null);
242244
verify(mockErrorNotifier).onError(argCaptor.capture());
@@ -245,7 +247,9 @@ public void xdsServerWithoutMockXdsClient_startError()
245247
assertThat(captured.getCause()).isInstanceOf(XdsInitializationException.class);
246248
assertThat(captured.getCause())
247249
.hasMessageThat()
248-
.contains("Environment variable GRPC_XDS_BOOTSTRAP not defined");
250+
.contains(
251+
"Environment variable GRPC_XDS_BOOTSTRAP"
252+
+ " or Java System Property io.grpc.xds.bootstrap not defined.");
249253
}
250254

251255
@Test

0 commit comments

Comments
 (0)