use of io.grpc.xds.Bootstrapper.BootstrapInfo in project grpc-java by grpc.
the class SharedXdsClientPoolProvider method getOrCreate.
@Override
public ObjectPool<XdsClient> getOrCreate() throws XdsInitializationException {
ObjectPool<XdsClient> ref = xdsClientPool;
if (ref == null) {
synchronized (lock) {
ref = xdsClientPool;
if (ref == null) {
BootstrapInfo bootstrapInfo;
Map<String, ?> rawBootstrap = bootstrapOverride.get();
if (rawBootstrap != null) {
bootstrapInfo = bootstrapper.bootstrap(rawBootstrap);
} else {
bootstrapInfo = bootstrapper.bootstrap();
}
if (bootstrapInfo.servers().isEmpty()) {
throw new XdsInitializationException("No xDS server provided");
}
ref = xdsClientPool = new RefCountedXdsClientObjectPool(bootstrapInfo);
}
}
}
return ref;
}
use of io.grpc.xds.Bootstrapper.BootstrapInfo in project grpc-java by grpc.
the class XdsNameResolver method start.
@Override
public void start(Listener2 listener) {
this.listener = checkNotNull(listener, "listener");
try {
xdsClientPool = xdsClientPoolFactory.getOrCreate();
} catch (Exception e) {
listener.onError(Status.UNAVAILABLE.withDescription("Failed to initialize xDS").withCause(e));
return;
}
xdsClient = xdsClientPool.getObject();
BootstrapInfo bootstrapInfo = xdsClient.getBootstrapInfo();
String listenerNameTemplate;
if (targetAuthority == null) {
listenerNameTemplate = bootstrapInfo.clientDefaultListenerResourceNameTemplate();
} else {
AuthorityInfo authorityInfo = bootstrapInfo.authorities().get(targetAuthority);
if (authorityInfo == null) {
listener.onError(Status.INVALID_ARGUMENT.withDescription("invalid target URI: target authority not found in the bootstrap"));
return;
}
listenerNameTemplate = authorityInfo.clientListenerResourceNameTemplate();
}
String replacement = serviceAuthority;
if (listenerNameTemplate.startsWith(XDSTP_SCHEME)) {
replacement = XdsClient.percentEncodePath(replacement);
}
String ldsResourceName = expandPercentS(listenerNameTemplate, replacement);
if (!XdsClient.isResourceNameValid(ldsResourceName, ResourceType.LDS.typeUrl()) && !XdsClient.isResourceNameValid(ldsResourceName, ResourceType.LDS.typeUrlV2())) {
listener.onError(Status.INVALID_ARGUMENT.withDescription("invalid listener resource URI for service authority: " + serviceAuthority));
return;
}
ldsResourceName = XdsClient.canonifyResourceName(ldsResourceName);
callCounterProvider = SharedCallCounterMap.getInstance();
resolveState = new ResolveState(ldsResourceName);
resolveState.start();
}
use of io.grpc.xds.Bootstrapper.BootstrapInfo in project grpc-java by grpc.
the class BootstrapperImplTest method parseClientDefaultListenerResourceNameTemplate.
@Test
public void parseClientDefaultListenerResourceNameTemplate() throws Exception {
BootstrapperImpl.enableFederation = true;
String rawData = "{\n" + " \"xds_servers\": [\n" + " ]\n" + "}";
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
BootstrapInfo info = bootstrapper.bootstrap();
assertThat(info.clientDefaultListenerResourceNameTemplate()).isEqualTo("%s");
rawData = "{\n" + " \"client_default_listener_resource_name_template\": \"xdstp://a.com/faketype/%s\",\n" + " \"xds_servers\": [\n" + " ]\n" + "}";
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
info = bootstrapper.bootstrap();
assertThat(info.clientDefaultListenerResourceNameTemplate()).isEqualTo("xdstp://a.com/faketype/%s");
}
use of io.grpc.xds.Bootstrapper.BootstrapInfo in project grpc-java by grpc.
the class BootstrapperImplTest method useV3ProtocolIfV3FeaturePresent.
@Test
public void useV3ProtocolIfV3FeaturePresent() throws XdsInitializationException {
String rawData = "{\n" + " \"xds_servers\": [\n" + " {\n" + " \"server_uri\": \"" + SERVER_URI + "\",\n" + " \"channel_creds\": [\n" + " {\"type\": \"insecure\"}\n" + " ],\n" + " \"server_features\": [\"xds_v3\"]\n" + " }\n" + " ]\n" + "}";
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
BootstrapInfo info = bootstrapper.bootstrap();
ServerInfo serverInfo = Iterables.getOnlyElement(info.servers());
assertThat(serverInfo.target()).isEqualTo(SERVER_URI);
assertThat(serverInfo.channelCredentials()).isInstanceOf(InsecureChannelCredentials.class);
assertThat(serverInfo.useProtocolV3()).isTrue();
}
use of io.grpc.xds.Bootstrapper.BootstrapInfo in project grpc-java by grpc.
the class BootstrapperImplTest method parseBootstrap_IgnoreIrrelevantFields.
@Test
public void parseBootstrap_IgnoreIrrelevantFields() throws XdsInitializationException {
String rawData = "{\n" + " \"node\": {\n" + " \"id\": \"ENVOY_NODE_ID\",\n" + " \"cluster\": \"ENVOY_CLUSTER\",\n" + " \"locality\": {},\n" + " \"metadata\": {\n" + " \"TRAFFICDIRECTOR_INTERCEPTION_PORT\": \"ENVOY_PORT\",\n" + " \"TRAFFICDIRECTOR_NETWORK_NAME\": \"VPC_NETWORK_NAME\"\n" + " }\n" + " },\n" + " \"xds_servers\": [\n" + " {\n" + " \"server_uri\": \"" + SERVER_URI + "\",\n" + " \"ignore\": \"something irrelevant\"," + " \"channel_creds\": [\n" + " {\"type\": \"insecure\"}\n" + " ]\n" + " }\n" + " ],\n" + " \"ignore\": \"something irrelevant\"\n" + "}";
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
BootstrapInfo info = bootstrapper.bootstrap();
assertThat(info.servers()).hasSize(1);
ServerInfo serverInfo = Iterables.getOnlyElement(info.servers());
assertThat(serverInfo.target()).isEqualTo(SERVER_URI);
assertThat(serverInfo.channelCredentials()).isInstanceOf(InsecureChannelCredentials.class);
assertThat(info.node()).isEqualTo(getNodeBuilder().setId("ENVOY_NODE_ID").setCluster("ENVOY_CLUSTER").setLocality(Locality.create("", "", "")).setMetadata(ImmutableMap.of("TRAFFICDIRECTOR_INTERCEPTION_PORT", "ENVOY_PORT", "TRAFFICDIRECTOR_NETWORK_NAME", "VPC_NETWORK_NAME")).build());
}
Aggregations