forked from grpc/grpc-java
/
Bootstrapper.java
314 lines (283 loc) · 11.2 KB
/
Bootstrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright 2019 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.xds;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.Internal;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.GrpcUtil.GrpcBuildVersion;
import io.grpc.internal.JsonParser;
import io.grpc.internal.JsonUtil;
import io.grpc.xds.EnvoyProtoData.Locality;
import io.grpc.xds.EnvoyProtoData.Node;
import io.grpc.xds.XdsLogger.XdsLogLevel;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* Loads configuration information to bootstrap gRPC's integration of xDS protocol.
*/
@Internal
public abstract class Bootstrapper {
private static final String LOG_PREFIX = "xds-bootstrap";
private static final String BOOTSTRAP_PATH_SYS_ENV_VAR = "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 IOException {
String filePath = System.getenv(BOOTSTRAP_PATH_SYS_ENV_VAR);
if (filePath == null) {
throw
new IOException("Environment variable " + BOOTSTRAP_PATH_SYS_ENV_VAR + " not defined.");
}
XdsLogger
.withPrefix(LOG_PREFIX)
.log(XdsLogLevel.INFO, BOOTSTRAP_PATH_SYS_ENV_VAR + "={0}", filePath);
return parseConfig(
new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8));
}
};
public static Bootstrapper getInstance() {
return DEFAULT_INSTANCE;
}
/**
* Returns configurations from bootstrap.
*/
public abstract BootstrapInfo readBootstrap() throws IOException;
/** Parses a raw string into {@link BootstrapInfo}. */
@VisibleForTesting
@SuppressWarnings("deprecation")
public static BootstrapInfo parseConfig(String rawData) throws IOException {
XdsLogger logger = XdsLogger.withPrefix(LOG_PREFIX);
logger.log(XdsLogLevel.INFO, "Reading bootstrap information");
@SuppressWarnings("unchecked")
Map<String, ?> rawBootstrap = (Map<String, ?>) JsonParser.parse(rawData);
logger.log(XdsLogLevel.DEBUG, "Bootstrap configuration:\n{0}", rawBootstrap);
List<ServerInfo> servers = new ArrayList<>();
List<?> rawServerConfigs = JsonUtil.getList(rawBootstrap, "xds_servers");
if (rawServerConfigs == null) {
throw new IOException("Invalid bootstrap: 'xds_servers' does not exist.");
}
logger.log(XdsLogLevel.INFO, "Configured with {0} xDS servers", rawServerConfigs.size());
List<Map<String, ?>> serverConfigList = JsonUtil.checkObjectList(rawServerConfigs);
for (Map<String, ?> serverConfig : serverConfigList) {
String serverUri = JsonUtil.getString(serverConfig, "server_uri");
if (serverUri == null) {
throw new IOException("Invalid bootstrap: 'xds_servers' contains unknown server.");
}
logger.log(XdsLogLevel.INFO, "xDS server URI: {0}", serverUri);
List<ChannelCreds> channelCredsOptions = new ArrayList<>();
List<?> rawChannelCredsList = JsonUtil.getList(serverConfig, "channel_creds");
if (rawChannelCredsList == null) {
throw new IOException("Invalid bootstrap: 'channel_creds' required");
}
List<Map<String, ?>> channelCredsList = JsonUtil.checkObjectList(rawChannelCredsList);
for (Map<String, ?> channelCreds : channelCredsList) {
String type = JsonUtil.getString(channelCreds, "type");
if (type == null) {
throw new IOException("Invalid bootstrap: 'xds_servers' contains server with "
+ "unknown type 'channel_creds'.");
}
logger.log(XdsLogLevel.INFO, "Channel credentials option: {0}", type);
ChannelCreds creds = new ChannelCreds(type, JsonUtil.getObject(channelCreds, "config"));
channelCredsOptions.add(creds);
}
List<String> serverFeatures = JsonUtil.getListOfStrings(serverConfig, "server_features");
if (serverFeatures != null) {
logger.log(XdsLogLevel.INFO, "Server features: {0}", serverFeatures);
}
servers.add(new ServerInfo(serverUri, channelCredsOptions, serverFeatures));
}
Node.Builder nodeBuilder = Node.newBuilder();
Map<String, ?> rawNode = JsonUtil.getObject(rawBootstrap, "node");
if (rawNode != null) {
String id = JsonUtil.getString(rawNode, "id");
if (id != null) {
logger.log(XdsLogLevel.INFO, "Node id: {0}", id);
nodeBuilder.setId(id);
}
String cluster = JsonUtil.getString(rawNode, "cluster");
if (cluster != null) {
logger.log(XdsLogLevel.INFO, "Node cluster: {0}", cluster);
nodeBuilder.setCluster(cluster);
}
Map<String, ?> metadata = JsonUtil.getObject(rawNode, "metadata");
if (metadata != null) {
nodeBuilder.setMetadata(metadata);
}
Map<String, ?> rawLocality = JsonUtil.getObject(rawNode, "locality");
if (rawLocality != null) {
String region = JsonUtil.getString(rawLocality, "region");
String zone = JsonUtil.getString(rawLocality, "zone");
String subZone = JsonUtil.getString(rawLocality, "sub_zone");
if (region != null) {
logger.log(XdsLogLevel.INFO, "Locality region: {0}", region);
}
if (rawLocality.containsKey("zone")) {
logger.log(XdsLogLevel.INFO, "Locality zone: {0}", zone);
}
if (rawLocality.containsKey("sub_zone")) {
logger.log(XdsLogLevel.INFO, "Locality sub_zone: {0}", subZone);
}
Locality locality = new Locality(region, zone, subZone);
nodeBuilder.setLocality(locality);
}
}
GrpcBuildVersion buildVersion = GrpcUtil.getGrpcBuildVersion();
logger.log(XdsLogLevel.INFO, "Build version: {0}", buildVersion);
nodeBuilder.setBuildVersion(buildVersion.toString());
nodeBuilder.setUserAgentName(buildVersion.getUserAgent());
nodeBuilder.setUserAgentVersion(buildVersion.getImplementationVersion());
nodeBuilder.addClientFeatures(CLIENT_FEATURE_DISABLE_OVERPROVISIONING);
Map<String, ?> certProvidersBlob = JsonUtil.getObject(rawBootstrap, "certificate_providers");
Map<String, CertificateProviderInfo> certProviders = null;
if (certProvidersBlob != null) {
certProviders = new HashMap<>(certProvidersBlob.size());
for (String name : certProvidersBlob.keySet()) {
Map<String, ?> valueMap = JsonUtil.getObject(certProvidersBlob, name);
String pluginName =
checkForNull(JsonUtil.getString(valueMap, "plugin_name"), "plugin_name");
Map<String, ?> config = checkForNull(JsonUtil.getObject(valueMap, "config"), "config");
CertificateProviderInfo certificateProviderInfo =
new CertificateProviderInfo(pluginName, config);
certProviders.put(name, certificateProviderInfo);
}
}
return new BootstrapInfo(servers, nodeBuilder.build(), certProviders);
}
static <T> T checkForNull(T value, String fieldName) throws IOException {
if (value == null) {
throw new IOException("Invalid bootstrap: '" + fieldName + "' does not exist.");
}
return value;
}
/**
* Data class containing channel credentials configurations for xDS protocol communication.
*/
// TODO(chengyuanzhang): May need more complex structure for channel creds config representation.
@Immutable
static class ChannelCreds {
private final String type;
@Nullable
private final Map<String, ?> config;
@VisibleForTesting
ChannelCreds(String type, @Nullable Map<String, ?> config) {
this.type = type;
this.config = config;
}
String getType() {
return type;
}
@Nullable
Map<String, ?> getConfig() {
if (config != null) {
return Collections.unmodifiableMap(config);
}
return null;
}
}
/**
* Data class containing xDS server information, such as server URI and channel credential
* options to be used for communication.
*/
@Immutable
static class ServerInfo {
private final String serverUri;
private final List<ChannelCreds> channelCredsList;
@Nullable
private final List<String> serverFeatures;
@VisibleForTesting
ServerInfo(String serverUri, List<ChannelCreds> channelCredsList, List<String> serverFeatures) {
this.serverUri = serverUri;
this.channelCredsList = channelCredsList;
this.serverFeatures = serverFeatures;
}
String getServerUri() {
return serverUri;
}
List<ChannelCreds> getChannelCredentials() {
return Collections.unmodifiableList(channelCredsList);
}
List<String> getServerFeatures() {
return serverFeatures == null
? Collections.<String>emptyList()
: Collections.unmodifiableList(serverFeatures);
}
}
/**
* Data class containing Certificate provider information: the plugin-name and an opaque
* Map that represents the config for that plugin.
*/
@Internal
@Immutable
public static class CertificateProviderInfo {
private final String pluginName;
private final Map<String, ?> config;
CertificateProviderInfo(String pluginName, Map<String, ?> config) {
this.pluginName = checkNotNull(pluginName, "pluginName");
this.config = checkNotNull(config, "config");
}
public String getPluginName() {
return pluginName;
}
public Map<String, ?> getConfig() {
return config;
}
}
/**
* Data class containing the results of reading bootstrap.
*/
@Internal
@Immutable
public static class BootstrapInfo {
private List<ServerInfo> servers;
private final Node node;
@Nullable private final Map<String, CertificateProviderInfo> certProviders;
@VisibleForTesting
BootstrapInfo(
List<ServerInfo> servers, Node node, Map<String, CertificateProviderInfo> certProviders) {
this.servers = servers;
this.node = node;
this.certProviders = certProviders;
}
/**
* Returns the list of xDS servers to be connected to.
*/
List<ServerInfo> getServers() {
return Collections.unmodifiableList(servers);
}
/**
* Returns the node identifier to be included in xDS requests.
*/
public Node getNode() {
return node;
}
/** Returns the cert-providers config map. */
public Map<String, CertificateProviderInfo> getCertProviders() {
return Collections.unmodifiableMap(certProviders);
}
}
}