-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Metadata Server Detection Configuration #562
Changes from all commits
b5521f2
e130d04
8fcf2fe
ff78269
a376573
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,20 @@ export const HEADER_NAME = 'Metadata-Flavor'; | |
export const HEADER_VALUE = 'Google'; | ||
export const HEADERS = Object.freeze({[HEADER_NAME]: HEADER_VALUE}); | ||
|
||
/** | ||
* Metadata server detection override options. | ||
* | ||
* Available via `process.env.METADATA_SERVER_DETECTION`. | ||
*/ | ||
export const METADATA_SERVER_DETECTION = Object.freeze({ | ||
'assume-present': | ||
"don't try to ping the metadata server, but assume it's present", | ||
none: "don't try to ping the metadata server, but don't try to use it either", | ||
'bios-only': | ||
"treat the result of a BIOS probe as canonical (don't fall back to pinging)", | ||
'ping-only': 'skip the BIOS probe, and go straight to pinging', | ||
}); | ||
|
||
export interface Options { | ||
params?: {[index: string]: string}; | ||
property?: string; | ||
|
@@ -199,6 +213,30 @@ let cachedIsAvailableResponse: Promise<boolean> | undefined; | |
* Determine if the metadata server is currently available. | ||
*/ | ||
export async function isAvailable() { | ||
if (process.env.METADATA_SERVER_DETECTION) { | ||
const value = | ||
process.env.METADATA_SERVER_DETECTION.trim().toLocaleLowerCase(); | ||
|
||
if (!(value in METADATA_SERVER_DETECTION)) { | ||
throw new RangeError( | ||
`Unknown \`METADATA_SERVER_DETECTION\` env variable. Got \`${value}\`, but it should be \`${Object.keys( | ||
METADATA_SERVER_DETECTION | ||
).join('`, `')}\`, or unset` | ||
); | ||
} | ||
|
||
switch (value as keyof typeof METADATA_SERVER_DETECTION) { | ||
case 'assume-present': | ||
return true; | ||
case 'none': | ||
return false; | ||
case 'bios-only': | ||
return getGCPResidency(); | ||
case 'ping-only': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we discussed but I forgot.. we don't want to have a default where both ping and bios happen sequentially? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or default in this case is null and also ping? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, the default in this case is null and ping - I’m open to changing it. |
||
// continue, we want to ping the server | ||
} | ||
} | ||
|
||
try { | ||
// If a user is instantiating several GCP libraries at the same time, | ||
// this may result in multiple calls to isAvailable(), to detect the | ||
|
@@ -271,11 +309,26 @@ export function resetIsAvailableCache() { | |
*/ | ||
export let gcpResidencyCache: boolean | null = null; | ||
|
||
/** | ||
* Detects GCP Residency. | ||
* Caches results to reduce costs for subsequent calls. | ||
* | ||
* @see setGCPResidency for setting | ||
*/ | ||
export function getGCPResidency(): boolean { | ||
if (gcpResidencyCache === null) { | ||
setGCPResidency(); | ||
} | ||
|
||
return gcpResidencyCache!; | ||
} | ||
|
||
/** | ||
* Sets the detected GCP Residency. | ||
* Useful for forcing metadata server detection behavior. | ||
* | ||
* Set `null` to autodetect the environment (default behavior). | ||
* @see getGCPResidency for getting | ||
*/ | ||
export function setGCPResidency(value: boolean | null = null) { | ||
gcpResidencyCache = value !== null ? value : detectGCPResidency(); | ||
|
@@ -291,12 +344,7 @@ export function setGCPResidency(value: boolean | null = null) { | |
* @returns {number} a request timeout duration in milliseconds. | ||
*/ | ||
export function requestTimeout(): number { | ||
// Detecting the residency can be resource-intensive. Let's cache the result. | ||
if (gcpResidencyCache === null) { | ||
gcpResidencyCache = detectGCPResidency(); | ||
} | ||
|
||
return gcpResidencyCache ? 0 : 3000; | ||
return getGCPResidency() ? 0 : 3000; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious what timeout it is and why exactly 0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The timeout is used for availability checking; 0 (indefinite) for GCE and up to 3 secs for other platforms. |
||
} | ||
|
||
export * from './gcp-residency'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
"extends": "./node_modules/gts/tsconfig-google.json", | ||
"compilerOptions": { | ||
"lib": ["es2018", "dom"], | ||
"skipLibCheck": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will create a follow-up issue to revert this in the next major (should be soon). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Background: https://www.typescriptlang.org/tsconfig#skipLibCheck There's a broken type issue that would require a major version bump to resolve. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I too got bitten by this broken type issue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tracking issue: #564 |
||
"rootDir": ".", | ||
"outDir": "build" | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this was added but is it used someplace or did I misunderstand?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, for some reason
compdoc
wants the babel plugin now, otherwise it’ll throw and fail thedocs
check (and linkinator would also complain).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: It's a Node 14+ requirement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tracking issue: #563