Skip to content

Commit 0025df4

Browse files
authored
Merge pull request #1869 from huan/master
add xds support reading bootstrap config directly from env var (#1868)
2 parents ba24f18 + d9bbd01 commit 0025df4

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

packages/grpc-js-xds/src/xds-bootstrap.ts

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -244,34 +244,66 @@ export async function loadBootstrapInfo(): Promise<BootstrapInfo> {
244244
if (loadedBootstrapInfo !== null) {
245245
return loadedBootstrapInfo;
246246
}
247+
248+
/**
249+
* If GRPC_XDS_BOOTSTRAP exists
250+
* then use its value as the name of the bootstrap file.
251+
*
252+
* If the file is missing or the contents of the file are malformed,
253+
* return an error.
254+
*/
247255
const bootstrapPath = process.env.GRPC_XDS_BOOTSTRAP;
248-
if (bootstrapPath === undefined) {
249-
return Promise.reject(
250-
new Error(
251-
'The GRPC_XDS_BOOTSTRAP environment variable needs to be set to the path to the bootstrap file to use xDS'
252-
)
253-
);
254-
}
255-
loadedBootstrapInfo = new Promise((resolve, reject) => {
256-
fs.readFile(bootstrapPath, { encoding: 'utf8' }, (err, data) => {
257-
if (err) {
258-
reject(
259-
new Error(
260-
`Failed to read xDS bootstrap file from path ${bootstrapPath} with error ${err.message}`
261-
)
262-
);
263-
}
264-
try {
265-
const parsedFile = JSON.parse(data);
266-
resolve(validateBootstrapFile(parsedFile));
267-
} catch (e) {
268-
reject(
269-
new Error(
270-
`Failed to parse xDS bootstrap file at path ${bootstrapPath} with error ${e.message}`
271-
)
272-
);
273-
}
256+
if (bootstrapPath) {
257+
loadedBootstrapInfo = new Promise((resolve, reject) => {
258+
fs.readFile(bootstrapPath, { encoding: 'utf8' }, (err, data) => {
259+
if (err) {
260+
reject(
261+
new Error(
262+
`Failed to read xDS bootstrap file from path ${bootstrapPath} with error ${err.message}`
263+
)
264+
);
265+
}
266+
try {
267+
const parsedFile = JSON.parse(data);
268+
resolve(validateBootstrapFile(parsedFile));
269+
} catch (e) {
270+
reject(
271+
new Error(
272+
`Failed to parse xDS bootstrap file at path ${bootstrapPath} with error ${e.message}`
273+
)
274+
);
275+
}
276+
});
274277
});
275-
});
276-
return loadedBootstrapInfo;
278+
return loadedBootstrapInfo;
279+
}
280+
281+
/**
282+
* Else, if GRPC_XDS_BOOTSTRAP_CONFIG exists
283+
* then use its value as the bootstrap config.
284+
*
285+
* If the value is malformed, return an error.
286+
*
287+
* See: https://github.com/grpc/grpc-node/issues/1868
288+
*/
289+
const bootstrapConfig = process.env.GRPC_XDS_BOOTSTRAP_CONFIG;
290+
if (bootstrapConfig) {
291+
try {
292+
const parsedConfig = JSON.parse(bootstrapConfig);
293+
const loadedBootstrapInfoValue = validateBootstrapFile(parsedConfig);
294+
loadedBootstrapInfo = Promise.resolve(loadedBootstrapInfoValue);
295+
} catch (e) {
296+
throw new Error(
297+
`Failed to parse xDS bootstrap config from environment variable GRPC_XDS_BOOTSTRAP_CONFIG with error ${e.message}`
298+
);
299+
}
300+
301+
return loadedBootstrapInfo;
302+
}
303+
304+
return Promise.reject(
305+
new Error(
306+
'The GRPC_XDS_BOOTSTRAP or GRPC_XDS_BOOTSTRAP_CONFIG environment variables need to be set to the path to the bootstrap file to use xDS'
307+
)
308+
);
277309
}

0 commit comments

Comments
 (0)