Skip to content

Commit

Permalink
xds: add support for setting bootstrap file with java system property
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
erikjoh committed Nov 16, 2020
1 parent 2c935e3 commit 6ff16a0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
8 changes: 4 additions & 4 deletions examples/example-xds/README.md
Expand Up @@ -6,7 +6,7 @@ being configured with the XDS management protocol. Out-of-the-box they behave th
as their hello-world version.

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

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

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

2. Similarly, add `--secure` on the comamnd line when you run the xDS client:
2. Similarly, add `--secure` on the command line when you run the xDS client:
```
$ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json
$ ./build/install/example-xds/bin/hello-world-client-xds xds:///yourServersName:8000 my-test-xds-client --secure
Expand Down
13 changes: 10 additions & 3 deletions xds/src/main/java/io/grpc/xds/Bootstrapper.java
Expand Up @@ -47,21 +47,28 @@ public abstract class Bootstrapper {

private static final String LOG_PREFIX = "xds-bootstrap";
private static final String BOOTSTRAP_PATH_SYS_ENV_VAR = "GRPC_XDS_BOOTSTRAP";
private static final String BOOTSTRAP_PATH_SYS_PROPERTY_VAR = "io.grpc.xds.bootstrap";
@VisibleForTesting
static final String CLIENT_FEATURE_DISABLE_OVERPROVISIONING =
"envoy.lb.does_not_support_overprovisioning";

private static final Bootstrapper DEFAULT_INSTANCE = new Bootstrapper() {
@Override
public BootstrapInfo readBootstrap() throws XdsInitializationException {
String filePath = System.getenv(BOOTSTRAP_PATH_SYS_ENV_VAR);
String filePathSource = BOOTSTRAP_PATH_SYS_ENV_VAR;
String filePath = System.getenv(filePathSource);
if (filePath == null) {
filePathSource = BOOTSTRAP_PATH_SYS_PROPERTY_VAR;
filePath = System.getProperty(filePathSource);
}
if (filePath == null) {
throw new XdsInitializationException(
"Environment variable " + BOOTSTRAP_PATH_SYS_ENV_VAR + " not defined.");
"Environment variable " + BOOTSTRAP_PATH_SYS_ENV_VAR
+ " or Java System Property " + BOOTSTRAP_PATH_SYS_PROPERTY_VAR + " not defined.");
}
XdsLogger
.withPrefix(LOG_PREFIX)
.log(XdsLogLevel.INFO, BOOTSTRAP_PATH_SYS_ENV_VAR + "={0}", filePath);
.log(XdsLogLevel.INFO, filePathSource + "={0}", filePath);
String fileContent;
try {
fileContent = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
Expand Down
Expand Up @@ -201,7 +201,9 @@ public void startXdsClient_expectException() {
} catch (IOException expected) {
assertThat(expected)
.hasMessageThat()
.contains("Environment variable GRPC_XDS_BOOTSTRAP not defined");
.contains(
"Environment variable GRPC_XDS_BOOTSTRAP"
+ " or Java System Property io.grpc.xds.bootstrap not defined.");
}
ArgumentCaptor<Status> argCaptor = ArgumentCaptor.forClass(null);
verify(mockServerWatcher).onError(argCaptor.capture());
Expand All @@ -210,7 +212,9 @@ public void startXdsClient_expectException() {
assertThat(captured.getCause()).isInstanceOf(XdsInitializationException.class);
assertThat(captured.getCause())
.hasMessageThat()
.contains("Environment variable GRPC_XDS_BOOTSTRAP not defined");
.contains(
"Environment variable GRPC_XDS_BOOTSTRAP"
+ " or Java System Property io.grpc.xds.bootstrap not defined.");
}

private DownstreamTlsContext sendListenerUpdate(
Expand Down
8 changes: 6 additions & 2 deletions xds/src/test/java/io/grpc/xds/XdsServerBuilderTest.java
Expand Up @@ -236,7 +236,9 @@ public void xdsServerWithoutMockXdsClient_startError()
} catch (IOException expected) {
assertThat(expected)
.hasMessageThat()
.contains("Environment variable GRPC_XDS_BOOTSTRAP not defined");
.contains(
"Environment variable GRPC_XDS_BOOTSTRAP"
+ " or Java System Property io.grpc.xds.bootstrap not defined.");
}
ArgumentCaptor<Status> argCaptor = ArgumentCaptor.forClass(null);
verify(mockErrorNotifier).onError(argCaptor.capture());
Expand All @@ -245,7 +247,9 @@ public void xdsServerWithoutMockXdsClient_startError()
assertThat(captured.getCause()).isInstanceOf(XdsInitializationException.class);
assertThat(captured.getCause())
.hasMessageThat()
.contains("Environment variable GRPC_XDS_BOOTSTRAP not defined");
.contains(
"Environment variable GRPC_XDS_BOOTSTRAP"
+ " or Java System Property io.grpc.xds.bootstrap not defined.");
}

@Test
Expand Down

0 comments on commit 6ff16a0

Please sign in to comment.