From 016e25fb96689bf75558d1498b99e7c2fcccb24a Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 30 Jan 2023 11:19:35 -0600 Subject: [PATCH 1/4] [serverless] add support for loading serverless specific config locally --- .gitignore | 1 + config/kibana.serverless.yml | 9 ++++++ package.json | 1 + src/cli/serve/serve.js | 56 +++++++++++++++++++++++++++++------- 4 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 config/kibana.serverless.yml diff --git a/.gitignore b/.gitignore index c417069940161..545a2f8112019 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,7 @@ disabledPlugins webpackstats.json /config/* !/config/kibana.yml +!/config/kibana.serverless.yml !/config/node.options coverage selenium diff --git a/config/kibana.serverless.yml b/config/kibana.serverless.yml new file mode 100644 index 0000000000000..e5c235f1ab872 --- /dev/null +++ b/config/kibana.serverless.yml @@ -0,0 +1,9 @@ +# as work on serverless picks up we will add config values to this file that +# define how Kibana will run in "serverless" mode. To start Kibana locally with +# this configuration, pass `--serverless` or run `yarn start-serverless` + +# configuration is applied in the following order, later values override +# 1. kibana.yml +# 2. kibana.serverless.yml (when --serverless is passed) +# 3. kibana.dev.yml +# 4. kibana.serverless.dev.yml (when --serverless is passed) diff --git a/package.json b/package.json index 124c77a6829d7..58ea281dcf7a2 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "makelogs": "node scripts/makelogs", "spec_to_console": "node scripts/spec_to_console", "start": "node scripts/kibana --dev", + "start-serverless": "node scripts/kibana --dev --serverless", "storybook": "node scripts/storybook", "test:ftr": "node scripts/functional_tests", "test:ftr:runner": "node scripts/functional_test_runner", diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index 61e482e5d6d07..01ff08bf16ee2 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -55,6 +55,37 @@ const pathCollector = function () { const configPathCollector = pathCollector(); const pluginPathCollector = pathCollector(); +/** + * @param {string} repoRel + * @param {string[]} configs + * @param {'push' | 'unshift'} method + */ +function maybeAddConfig(repoRel, configs, method) { + const path = fromRoot(repoRel) + try { + if (statSync(path).isFile()) { + configs[method](path) + } + } catch (err) { + if (err.code === 'ENOENT') { + return; + } + + throw err + } +} + +/** + * @returns {string[]} + */ +function getEnvConfigs() { + const val = process.env.KBN_CONFIG_PATHS + if (typeof val === 'string') { + return val.split(',').map(p => p.trim()) + } + return [] +} + function applyConfigOverrides(rawConfig, opts, extraCliOptions) { const set = _.partial(lodashSet, rawConfig); const get = _.partial(_.get, rawConfig); @@ -177,7 +208,8 @@ export default function (program) { .option( '--run-examples', 'Adds plugin paths for all the Kibana example plugins and runs with no base path' - ); + ) + .option('--serverless', 'Start Kibana with serverless configuration overrides'); } if (DEV_MODE_SUPPORTED) { @@ -200,19 +232,22 @@ export default function (program) { } command.action(async function (opts) { + const unknownOptions = this.getUnknownOptions(); + const configs = [].concat(getEnvConfigs(), opts.config || []); + + // we "unshift" .serverless. config so that it only overrides defaults + if (opts.serverless) { + maybeAddConfig('config/kibana.serverless.yml', configs, 'unshift'); + } + + // .dev. configs are "pushed" so that they override all other config files if (opts.dev && opts.devConfig !== false) { - try { - const kbnDevConfig = fromRoot('config/kibana.dev.yml'); - if (statSync(kbnDevConfig).isFile()) { - opts.config.push(kbnDevConfig); - } - } catch (err) { - // ignore, kibana.dev.yml does not exist + maybeAddConfig('config/kibana.dev.yml', configs, 'push'); + if (opts.serverless) { + maybeAddConfig('config/kibana.serverless.dev.yml', configs, 'push'); } } - const unknownOptions = this.getUnknownOptions(); - const configs = [].concat(opts.config || []); const cliArgs = { dev: !!opts.dev, envName: unknownOptions.env ? unknownOptions.env.name : undefined, @@ -231,6 +266,7 @@ export default function (program) { oss: !!opts.oss, cache: !!opts.cache, dist: !!opts.dist, + serverless: !!opts.serverless, }; // In development mode, the main process uses the @kbn/dev-cli-mode From 8085bf6c1c2c0fb02e45bca17a5aba3c575c4a63 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 30 Jan 2023 13:13:18 -0600 Subject: [PATCH 2/4] base config paths on KBN_PATH_CONFIG --- src/cli/serve/serve.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index 01ff08bf16ee2..6a834faa977a9 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -12,7 +12,7 @@ import { statSync } from 'fs'; import { resolve } from 'path'; import url from 'url'; -import { getConfigPath } from '@kbn/utils'; +import { getConfigPath, getConfigDirectory } from '@kbn/utils'; import { fromRoot, isKibanaDistributable } from '@kbn/repo-info'; import { readKeystore } from '../keystore/read_keystore'; @@ -56,12 +56,12 @@ const configPathCollector = pathCollector(); const pluginPathCollector = pathCollector(); /** - * @param {string} repoRel + * @param {string} name * @param {string[]} configs * @param {'push' | 'unshift'} method */ -function maybeAddConfig(repoRel, configs, method) { - const path = fromRoot(repoRel) +function maybeAddConfig(name, configs, method) { + const path = resolve(getConfigDirectory(), name) try { if (statSync(path).isFile()) { configs[method](path) @@ -81,7 +81,7 @@ function maybeAddConfig(repoRel, configs, method) { function getEnvConfigs() { const val = process.env.KBN_CONFIG_PATHS if (typeof val === 'string') { - return val.split(',').map(p => p.trim()) + return val.split(',').map(p => resolve(p.trim())) } return [] } @@ -183,7 +183,7 @@ export default function (program) { '-c, --config ', 'Path to the config file, use multiple --config args to include multiple config files', configPathCollector, - [getConfigPath()] + [] ) .option('-p, --port ', 'The port to bind to', parseInt) .option('-Q, --silent', 'Set the root logger level to off') @@ -233,18 +233,22 @@ export default function (program) { command.action(async function (opts) { const unknownOptions = this.getUnknownOptions(); - const configs = [].concat(getEnvConfigs(), opts.config || []); + const configs = [ + ...getConfigPath(), + ...getEnvConfigs(), + ...(opts.config || []) + ]; // we "unshift" .serverless. config so that it only overrides defaults if (opts.serverless) { - maybeAddConfig('config/kibana.serverless.yml', configs, 'unshift'); + maybeAddConfig('kibana.serverless.yml', configs, 'unshift'); } // .dev. configs are "pushed" so that they override all other config files if (opts.dev && opts.devConfig !== false) { - maybeAddConfig('config/kibana.dev.yml', configs, 'push'); + maybeAddConfig('kibana.dev.yml', configs, 'push'); if (opts.serverless) { - maybeAddConfig('config/kibana.serverless.dev.yml', configs, 'push'); + maybeAddConfig('kibana.serverless.dev.yml', configs, 'push'); } } From 2b922c29c149558315ec5ef7dfdca6f11206e32b Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 30 Jan 2023 19:19:42 +0000 Subject: [PATCH 3/4] [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' --- src/cli/serve/serve.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index 6a834faa977a9..7dedf06706731 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -13,7 +13,7 @@ import { resolve } from 'path'; import url from 'url'; import { getConfigPath, getConfigDirectory } from '@kbn/utils'; -import { fromRoot, isKibanaDistributable } from '@kbn/repo-info'; +import { isKibanaDistributable } from '@kbn/repo-info'; import { readKeystore } from '../keystore/read_keystore'; function canRequire(path) { @@ -61,17 +61,17 @@ const pluginPathCollector = pathCollector(); * @param {'push' | 'unshift'} method */ function maybeAddConfig(name, configs, method) { - const path = resolve(getConfigDirectory(), name) + const path = resolve(getConfigDirectory(), name); try { if (statSync(path).isFile()) { - configs[method](path) + configs[method](path); } } catch (err) { if (err.code === 'ENOENT') { return; } - throw err + throw err; } } @@ -79,11 +79,11 @@ function maybeAddConfig(name, configs, method) { * @returns {string[]} */ function getEnvConfigs() { - const val = process.env.KBN_CONFIG_PATHS + const val = process.env.KBN_CONFIG_PATHS; if (typeof val === 'string') { - return val.split(',').map(p => resolve(p.trim())) + return val.split(',').map((p) => resolve(p.trim())); } - return [] + return []; } function applyConfigOverrides(rawConfig, opts, extraCliOptions) { @@ -233,11 +233,7 @@ export default function (program) { command.action(async function (opts) { const unknownOptions = this.getUnknownOptions(); - const configs = [ - ...getConfigPath(), - ...getEnvConfigs(), - ...(opts.config || []) - ]; + const configs = [...getConfigPath(), ...getEnvConfigs(), ...(opts.config || [])]; // we "unshift" .serverless. config so that it only overrides defaults if (opts.serverless) { From 6eec315f717f85dee6ad27716b9251412ad460c9 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 30 Jan 2023 13:23:59 -0600 Subject: [PATCH 4/4] stop spreading a string into an array --- src/cli/serve/serve.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index 6a834faa977a9..98df4737b0964 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -81,7 +81,7 @@ function maybeAddConfig(name, configs, method) { function getEnvConfigs() { const val = process.env.KBN_CONFIG_PATHS if (typeof val === 'string') { - return val.split(',').map(p => resolve(p.trim())) + return val.split(',').filter(v => !!v).map(p => resolve(p.trim())) } return [] } @@ -234,7 +234,7 @@ export default function (program) { command.action(async function (opts) { const unknownOptions = this.getUnknownOptions(); const configs = [ - ...getConfigPath(), + getConfigPath(), ...getEnvConfigs(), ...(opts.config || []) ];