Skip to content

Commit 0f508ce

Browse files
authored
Check API version before deploying to dogfood (#3449)
If we accidentally yolo a version of the console to dogfood that has a too-new API client, it won't work at all. Someone how it never occurred to me before we can just check this at upload time. With oxidecomputer/omicron#10250 we'll have an endpoint we can call to find the current version deployed, but in the meantime we can get it (without auth) through binary search, making requests against the API with various `api-version` values and seeing which are rejected with a version error. If the check fails, we get output like this: ``` Resolved fe921de to fe921de Checking which API versions dogfood accepts... Dogfood API version: 2026110500.0.0 Console API version: 2026111000.0.0 Error: dogfood would reject API requests from this console build. Pick a commit with app/api/__generated__/API_VERSION at or below dogfood's. ```
1 parent fe921de commit 0f508ce

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

‎tools/deploy-dogfood.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,44 @@ import $ from 'dax'
2626
// user <your actual username>
2727
// ForwardAgent yes
2828

29+
const DOGFOOD_HOST = 'https://oxide.sys.rack2.eng.oxide.computer'
30+
31+
/**
32+
* Nexus validates `api-version` before auth, so an unauthenticated request
33+
* suffices to check whether `major.0.0` is newer than dogfood supports.
34+
* Supported versions proceed to auth and return 401.
35+
*/
36+
async function isTooNew(major: number) {
37+
const res = await fetch(`${DOGFOOD_HOST}/v1/me`, {
38+
headers: { 'api-version': `${major}.0.0` },
39+
})
40+
const body = await res.text()
41+
if (res.status === 400 && body.includes('server does not support this API version')) {
42+
return true
43+
}
44+
// other 400s are for retired versions ("no longer supports"), below dogfood's newest
45+
if (res.status === 401 || res.status === 400) return false
46+
throw new Error(`Unexpected response checking dogfood API version: ${res.status} ${body}`)
47+
}
48+
49+
/** As `<YYYYMMDDNN>`. A week out leaves room for time zones and dates picked a little ahead. */
50+
const weekFromNow = () =>
51+
Number(
52+
new Date(Date.now() + 7 * 86_400_000).toISOString().slice(0, 10).replaceAll('-', '')
53+
) * 100
54+
55+
/**
56+
* Bisects to find dogfood's newest API version until Nexus has a version
57+
* endpoint. Versions have the form `<YYYYMMDDNN>.0.0`, so only the major
58+
* number varies. `lo` is accepted and `hi` rejected. `lo` starts at the first
59+
* versioned Nexus API, and no version can be dated much after today.
60+
*/
61+
async function getDogfoodApiMajor(lo = 2025_11_20_00, hi = weekFromNow()): Promise<number> {
62+
if (hi - lo <= 1) return lo
63+
const mid = Math.floor((lo + hi) / 2)
64+
return (await isTooNew(mid)) ? getDogfoodApiMajor(lo, mid) : getDogfoodApiMajor(mid, hi)
65+
}
66+
2967
const USAGE = `
3068
Usage:
3169
@@ -51,6 +89,23 @@ if (consoleRevision !== fullCommit) {
5189
console.info(`Resolved ${consoleRevision} to ${fullCommit}`)
5290
}
5391

92+
const consoleApiVersion = (
93+
await $`git show ${fullCommit}:app/api/__generated__/API_VERSION`.text()
94+
).trim()
95+
const consoleApiMajor = Number(consoleApiVersion.split('.')[0])
96+
97+
console.info('Checking which API versions dogfood accepts...')
98+
const dogfoodApiMajor = await getDogfoodApiMajor()
99+
console.info(`Dogfood API version: ${dogfoodApiMajor}.0.0`)
100+
console.info(`Console API version: ${consoleApiVersion}\n`)
101+
102+
if (consoleApiMajor > dogfoodApiMajor) {
103+
console.error(
104+
"Error: dogfood would reject API requests from this console build. Pick a commit with app/api/__generated__/API_VERSION at or below dogfood's."
105+
)
106+
process.exit(1)
107+
}
108+
54109
console.info('Finding nexus zones...')
55110
const zones: string = await $`./tools/dogfood/find-zone.sh nexus`.text()
56111
const gimletNums = zones

0 commit comments

Comments
 (0)