-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[OAS] Capture and commit serverless bundle #184915
Merged
jloleysens
merged 15 commits into
elastic:main
from
jloleysens:oas/generate-serverless-bundle
Jun 10, 2024
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
563f6a6
wip: try to generate serverless OAS
jloleysens dd7b253
wip2
jloleysens 5bb2277
wip3
jloleysens 0ef2c3a
updated test helpers so that we can pass through special settings
jloleysens a94f3bf
refactor CLI tool so that it runs for both serverless and traditional…
jloleysens 645d8aa
first serverless snapshot
jloleysens 9c0be2a
CLI flag check
jloleysens b14831b
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine dc3de51
update worker to load x-pack properly for serverless
jloleysens 0d55e13
Merge branch 'main' into oas/generate-serverless-bundle
jloleysens 698dc93
Merge branch 'main' into oas/generate-serverless-bundle
jloleysens 4c444ad
updated snapshot
jloleysens 38ee023
remove noop existent assignment
jloleysens 782381a
remove unused string
jloleysens 4c084aa
Merge branch 'main' into oas/generate-serverless-bundle
jloleysens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/kbn-capture-oas-snapshot-cli/src/capture_oas_snapshot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import fs from 'node:fs/promises'; | ||
import { encode } from 'node:querystring'; | ||
import type { ChildProcess } from 'node:child_process'; | ||
import fetch from 'node-fetch'; | ||
import * as Rx from 'rxjs'; | ||
import { startTSWorker } from '@kbn/dev-utils'; | ||
import { createTestEsCluster } from '@kbn/test'; | ||
import type { ToolingLog } from '@kbn/tooling-log'; | ||
import { createTestServerlessInstances } from '@kbn/core-test-helpers-kbn-server'; | ||
import type { Result } from './kibana_worker'; | ||
import { sortAndPrettyPrint } from './run_capture_oas_snapshot_cli'; | ||
import { buildFlavourEnvArgName } from './common'; | ||
|
||
interface CaptureOasSnapshotArgs { | ||
log: ToolingLog; | ||
buildFlavour: 'serverless' | 'traditional'; | ||
outputFile: string; | ||
update: boolean; | ||
filters?: { | ||
pathStartsWith?: string[]; | ||
excludePathsMatching?: string[]; | ||
}; | ||
} | ||
|
||
const MB = 1024 * 1024; | ||
const twoDeci = (num: number) => Math.round(num * 100) / 100; | ||
|
||
export async function captureOasSnapshot({ | ||
log, | ||
filters = {}, | ||
buildFlavour, | ||
update, | ||
outputFile, | ||
}: CaptureOasSnapshotArgs): Promise<void> { | ||
const { excludePathsMatching = [], pathStartsWith } = filters; | ||
// internal consts | ||
const port = 5622; | ||
// We are only including /api/status for now | ||
excludePathsMatching.push( | ||
'/{path*}', | ||
// Our internal asset paths | ||
'/XXXXXXXXXXXX/' | ||
jloleysens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
|
||
let esCluster: undefined | { stop(): Promise<void> }; | ||
let kbWorker: undefined | ChildProcess; | ||
|
||
try { | ||
log.info('Starting es...'); | ||
esCluster = await log.indent(4, async () => { | ||
if (buildFlavour === 'serverless') { | ||
const { startES } = createTestServerlessInstances(); | ||
return await startES(); | ||
} | ||
const cluster = createTestEsCluster({ log }); | ||
await cluster.start(); | ||
return { stop: () => cluster.cleanup() }; | ||
}); | ||
|
||
log.info('Starting Kibana...'); | ||
kbWorker = await log.indent(4, async () => { | ||
log.info('Loading core with all plugins enabled so that we can capture OAS for all...'); | ||
const { msg$, proc } = startTSWorker<Result>({ | ||
log, | ||
src: require.resolve('./kibana_worker'), | ||
env: { ...process.env, [buildFlavourEnvArgName]: buildFlavour }, | ||
}); | ||
await Rx.firstValueFrom( | ||
msg$.pipe( | ||
Rx.map((msg) => { | ||
if (msg !== 'ready') | ||
throw new Error(`received unexpected message from worker (expected "ready"): ${msg}`); | ||
}) | ||
) | ||
); | ||
return proc; | ||
}); | ||
|
||
const qs = encode({ | ||
access: 'public', | ||
version: '2023-10-31', // hard coded for now, we can make this configurable later | ||
pathStartsWith, | ||
excludePathsMatching, | ||
}); | ||
const url = `http://localhost:${port}/api/oas?${qs}`; | ||
log.info(`Fetching OAS at ${url}...`); | ||
const result = await fetch(url, { | ||
headers: { | ||
'kbn-xsrf': 'kbn-oas-snapshot', | ||
authorization: `Basic ${Buffer.from('elastic:changeme').toString('base64')}`, | ||
jloleysens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}); | ||
if (result.status !== 200) { | ||
log.error(`Failed to fetch OAS: ${JSON.stringify(result, null, 2)}`); | ||
throw new Error(`Failed to fetch OAS: ${result.status}`); | ||
} | ||
const currentOas = await result.json(); | ||
log.info(`Recieved OAS, writing to ${outputFile}...`); | ||
if (update) { | ||
await fs.writeFile(outputFile, sortAndPrettyPrint(currentOas)); | ||
const { size: sizeBytes } = await fs.stat(outputFile); | ||
log.success(`OAS written to ${outputFile}. File size ~${twoDeci(sizeBytes / MB)} MB.`); | ||
} else { | ||
log.success( | ||
`OAS recieved, not writing to file. Got OAS for ${ | ||
Object.keys(currentOas.paths).length | ||
} paths.` | ||
); | ||
} | ||
} catch (err) { | ||
log.error(`Failed to capture OAS: ${JSON.stringify(err, null, 2)}`); | ||
throw err; | ||
} finally { | ||
kbWorker?.kill('SIGILL'); | ||
await esCluster?.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export const buildFlavourEnvArgName = 'CAPTURE_OAS_SNAPSHOT_WORKER_BUILD_FLAVOR'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,6 @@ | |
"@kbn/dev-cli-runner", | ||
"@kbn/test", | ||
"@kbn/dev-utils", | ||
"@kbn/tooling-log", | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just enables passing through our special "enable all plugins" setting