From 776180b7403de070415291362ceaea77a673148f Mon Sep 17 00:00:00 2001 From: Praveen Ranjan Keshri Date: Thu, 25 Nov 2021 01:59:54 +0530 Subject: [PATCH 1/4] Fixed bug in backend-common to allow passing of remote option in order to enable passing remote url in --config option. The remote option should be passed along with reloadIntervalSeconds from packages/backend/src/index.ts (Updated the file as well) Signed-off-by: Praveen Ranjan Keshri --- .changeset/brave-impalas-switch.md | 24 ++++++++++++++++++++++++ docs/conf/writing.md | 22 ++++++++++++++++++++-- packages/backend-common/src/config.ts | 3 +++ packages/backend/src/index.ts | 4 ++++ packages/config-loader/src/loader.ts | 14 +++++++++++--- 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 .changeset/brave-impalas-switch.md diff --git a/.changeset/brave-impalas-switch.md b/.changeset/brave-impalas-switch.md new file mode 100644 index 0000000000..058e2dbf8d --- /dev/null +++ b/.changeset/brave-impalas-switch.md @@ -0,0 +1,24 @@ +--- +'example-backend': patch +'@backstage/backend-common': patch +'@backstage/config-loader': patch +--- + +Fixed bug in backend-common to allow passing of remote option in order to enable passing remote url in --config option. The remote option should be passed along with reloadIntervalSeconds from packages/backend/src/index.ts (Updated the file as well) + +These changes are needed in `packages/backend/src/index.ts` if remote urls are desired to be passed in --config option and read and watch remote files for config. + +```diff +@@ -86,7 +86,11 @@ async function main() { + const config = await loadBackendConfig({ + argv: process.argv, + logger, ++ remote: { ++ reloadIntervalSeconds: 60 * 60 * 12 // Check remote config changes every 12 hours. Change to your desired interval in seconds ++ } + }); ++ + const createEnv = makeCreateEnv(config); + + const healthcheckEnv = useHotMemoize(module, () => createEnv('healthcheck')); +``` diff --git a/docs/conf/writing.md b/docs/conf/writing.md index 7945d6c980..afd86955fa 100644 --- a/docs/conf/writing.md +++ b/docs/conf/writing.md @@ -67,7 +67,7 @@ production build. ## Configuration Files -It is possible to have multiple configuration files (bundled and/or remote), +It is possible to have multiple configuration files (bundled and/or remote\*), both to support different environments, but also to define configuration that is local to specific packages. The configuration files to load are selected using a `--config ` flag, and it is possible to load any number of @@ -75,7 +75,25 @@ files. Paths are relative to the working directory of the executed process, for example `package/backend`. This means that to select a config file in the repo root when running the backend, you would use `--config ../../my-config.yaml`, and for config file on a config server you would use -`--config https://some.domain.io/app-config.yaml` +`--config https://some.domain.io/app-config.yaml`
+ +**\*Note**: In order to use remote urls, ensure that the option 'remote' is +passed in `loadBackendConfig(...)` call (See below) inside +`packages/backend/src/index.ts`, with the option of +reloadIntervalSeconds(required) as given below. This will allow the usage of +remote configs and also, will ensure that this config is checked for any changes +every 12 hours. (This can be any desired value in seconds, here 60 _ 60 _ 12 = +12 hours!): + +```ts +const config = await loadBackendConfig({ + argv: process.argv, + logger, + remote: { + reloadIntervalSeconds: 60 * 60 * 12, // Check remote config changes every 12 hours. Change to your desired interval in seconds + }, +}); +``` If no `config` flags are specified, the default behavior is to load `app-config.yaml` and, if it exists, `app-config.local.yaml` from the repo root. diff --git a/packages/backend-common/src/config.ts b/packages/backend-common/src/config.ts index 941ad26e65..1ede91dbe5 100644 --- a/packages/backend-common/src/config.ts +++ b/packages/backend-common/src/config.ts @@ -23,6 +23,7 @@ import { loadConfig, ConfigSchema, ConfigTarget, + LoadConfigOptionsRemote, } from '@backstage/config-loader'; import { AppConfig, Config, ConfigReader } from '@backstage/config'; import { JsonValue } from '@backstage/types'; @@ -178,6 +179,7 @@ let currentCancelFunc: () => void; export async function loadBackendConfig(options: { logger: Logger; // process.argv or any other overrides + remote?: LoadConfigOptionsRemote; argv: string[]; }): Promise { const args = parseArgs(options.argv); @@ -204,6 +206,7 @@ export async function loadBackendConfig(options: { configRoot: paths.targetRoot, configPaths: [], configTargets: configTargets, + remote: options.remote, watch: { onChange(newConfigs) { options.logger.info( diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index f978e84da9..ea1d643187 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -86,7 +86,11 @@ async function main() { const config = await loadBackendConfig({ argv: process.argv, logger, + remote: { + reloadIntervalSeconds: 60 * 60 * 12, // Check remote config changes every 12 hours. Change to your desired interval in seconds + }, }); + const createEnv = makeCreateEnv(config); const healthcheckEnv = useHotMemoize(module, () => createEnv('healthcheck')); diff --git a/packages/config-loader/src/loader.ts b/packages/config-loader/src/loader.ts index 6a92ed519d..388017318f 100644 --- a/packages/config-loader/src/loader.ts +++ b/packages/config-loader/src/loader.ts @@ -45,7 +45,7 @@ export type LoadConfigOptionsWatch = { export type LoadConfigOptionsRemote = { /** - * An optional remote config reloading period, in seconds + * A remote config reloading period, in seconds */ reloadIntervalSeconds: number; }; @@ -126,8 +126,16 @@ export async function loadConfig( .filter((e): e is { url: string } => e.hasOwnProperty('url')) .map(configTarget => configTarget.url); - if (remote === undefined && configUrls.length > 0) { - throw new Error(`Remote config detected but this feature is turned off`); + if (remote === undefined) { + if (configUrls.length > 0) { + throw new Error( + `Remote config detected but this feature is turned off. Please enable by passing remote option in loadBackendConfig() call inside packages/backend/src/index.ts. See https://backstage.io/docs/conf/writing#configuration-files for detailed info.`, + ); + } + } else if (remote.reloadIntervalSeconds === undefined) { + throw new Error( + `Remote config must be contain reloadIntervalSeconds: value`, + ); } // If no paths are provided, we default to reading From 1e7070443decf3004eaec10c8ba30e8fa15fd591 Mon Sep 17 00:00:00 2001 From: Praveen Ranjan Keshri Date: Wed, 8 Dec 2021 00:21:53 +0530 Subject: [PATCH 2/4] Fixes post review by Rugvip: 1. Ensuring that reloadIntervals, if present, is a valid positive number 2. Remote config error message fix 3. Removed remote config from example backend 4. Lessened the writing.md 5. Multiple changesets Signed-off-by: Praveen Ranjan Keshri --- .changeset/brave-impalas-switch.md | 2 -- .changeset/cuddly-cooks-enjoy.md | 5 +++++ docs/conf/writing.md | 21 +++------------------ packages/backend/src/index.ts | 3 --- packages/config-loader/src/loader.ts | 6 +++--- 5 files changed, 11 insertions(+), 26 deletions(-) create mode 100644 .changeset/cuddly-cooks-enjoy.md diff --git a/.changeset/brave-impalas-switch.md b/.changeset/brave-impalas-switch.md index 058e2dbf8d..de7e2ee528 100644 --- a/.changeset/brave-impalas-switch.md +++ b/.changeset/brave-impalas-switch.md @@ -1,7 +1,5 @@ --- -'example-backend': patch '@backstage/backend-common': patch -'@backstage/config-loader': patch --- Fixed bug in backend-common to allow passing of remote option in order to enable passing remote url in --config option. The remote option should be passed along with reloadIntervalSeconds from packages/backend/src/index.ts (Updated the file as well) diff --git a/.changeset/cuddly-cooks-enjoy.md b/.changeset/cuddly-cooks-enjoy.md new file mode 100644 index 0000000000..d39844e7a9 --- /dev/null +++ b/.changeset/cuddly-cooks-enjoy.md @@ -0,0 +1,5 @@ +--- +'@backstage/config-loader': patch +--- + +In case remote.reloadIntervalSeconds is passed, it must be a valid positive value diff --git a/docs/conf/writing.md b/docs/conf/writing.md index afd86955fa..a4da1e3df0 100644 --- a/docs/conf/writing.md +++ b/docs/conf/writing.md @@ -75,25 +75,10 @@ files. Paths are relative to the working directory of the executed process, for example `package/backend`. This means that to select a config file in the repo root when running the backend, you would use `--config ../../my-config.yaml`, and for config file on a config server you would use -`--config https://some.domain.io/app-config.yaml`
+`--config https://some.domain.io/app-config.yaml` -**\*Note**: In order to use remote urls, ensure that the option 'remote' is -passed in `loadBackendConfig(...)` call (See below) inside -`packages/backend/src/index.ts`, with the option of -reloadIntervalSeconds(required) as given below. This will allow the usage of -remote configs and also, will ensure that this config is checked for any changes -every 12 hours. (This can be any desired value in seconds, here 60 _ 60 _ 12 = -12 hours!): - -```ts -const config = await loadBackendConfig({ - argv: process.argv, - logger, - remote: { - reloadIntervalSeconds: 60 * 60 * 12, // Check remote config changes every 12 hours. Change to your desired interval in seconds - }, -}); -``` +**Note**: In case URLs are passed, it is also needed to set the remote option in +the loadBackendConfig call. If no `config` flags are specified, the default behavior is to load `app-config.yaml` and, if it exists, `app-config.local.yaml` from the repo root. diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index e3c15b95b6..7a79c278ff 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -88,9 +88,6 @@ async function main() { const config = await loadBackendConfig({ argv: process.argv, logger, - remote: { - reloadIntervalSeconds: 60 * 60 * 12, // Check remote config changes every 12 hours. Change to your desired interval in seconds - }, }); const createEnv = makeCreateEnv(config); diff --git a/packages/config-loader/src/loader.ts b/packages/config-loader/src/loader.ts index 388017318f..e7aa3d4c10 100644 --- a/packages/config-loader/src/loader.ts +++ b/packages/config-loader/src/loader.ts @@ -129,12 +129,12 @@ export async function loadConfig( if (remote === undefined) { if (configUrls.length > 0) { throw new Error( - `Remote config detected but this feature is turned off. Please enable by passing remote option in loadBackendConfig() call inside packages/backend/src/index.ts. See https://backstage.io/docs/conf/writing#configuration-files for detailed info.`, + `Please make sure you are passing the remote option when loading the configuration. See https://backstage.io/docs/conf/writing#configuration-files for detailed info.`, ); } - } else if (remote.reloadIntervalSeconds === undefined) { + } else if (remote.reloadIntervalSeconds <= 0) { throw new Error( - `Remote config must be contain reloadIntervalSeconds: value`, + `Remote config must be contain a non zero reloadIntervalSeconds: value`, ); } From d614e1cd9a35d3289dbf8e8164a4b4c1283d11b3 Mon Sep 17 00:00:00 2001 From: Praveen Ranjan Keshri Date: Fri, 10 Dec 2021 22:53:31 +0530 Subject: [PATCH 3/4] Updates post review by @Rugvip: 1. Updated reloadIntervalSeconds example to 10 minutes 2. Updated config load error message 3. Fixed vocab error Signed-off-by: Praveen Ranjan Keshri --- .changeset/brave-impalas-switch.md | 4 ++-- packages/config-loader/src/loader.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.changeset/brave-impalas-switch.md b/.changeset/brave-impalas-switch.md index de7e2ee528..729a4ec96a 100644 --- a/.changeset/brave-impalas-switch.md +++ b/.changeset/brave-impalas-switch.md @@ -4,7 +4,7 @@ Fixed bug in backend-common to allow passing of remote option in order to enable passing remote url in --config option. The remote option should be passed along with reloadIntervalSeconds from packages/backend/src/index.ts (Updated the file as well) -These changes are needed in `packages/backend/src/index.ts` if remote urls are desired to be passed in --config option and read and watch remote files for config. +These changes are needed in `packages/backend/src/index.ts` if remote URLs are desired to be passed in --config option and read and watch remote files for config. ```diff @@ -86,7 +86,11 @@ async function main() { @@ -12,7 +12,7 @@ These changes are needed in `packages/backend/src/index.ts` if remote urls are d argv: process.argv, logger, + remote: { -+ reloadIntervalSeconds: 60 * 60 * 12 // Check remote config changes every 12 hours. Change to your desired interval in seconds ++ reloadIntervalSeconds: 60 * 10 // Check remote config changes every 10 minutes. Change to your desired interval in seconds + } }); + diff --git a/packages/config-loader/src/loader.ts b/packages/config-loader/src/loader.ts index e7aa3d4c10..d19cd691be 100644 --- a/packages/config-loader/src/loader.ts +++ b/packages/config-loader/src/loader.ts @@ -129,7 +129,7 @@ export async function loadConfig( if (remote === undefined) { if (configUrls.length > 0) { throw new Error( - `Please make sure you are passing the remote option when loading the configuration. See https://backstage.io/docs/conf/writing#configuration-files for detailed info.`, + `Please make sure you are passing the remote option when loading remote configurations. See https://backstage.io/docs/conf/writing#configuration-files for detailed info.`, ); } } else if (remote.reloadIntervalSeconds <= 0) { From 85b844585c83e8d29b45d1f0f6272b7b471d7bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 16 Dec 2021 21:27:21 +0100 Subject: [PATCH 4/4] fixup api report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- packages/backend-common/api-report.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/backend-common/api-report.md b/packages/backend-common/api-report.md index e623f656ab..fae460c856 100644 --- a/packages/backend-common/api-report.md +++ b/packages/backend-common/api-report.md @@ -21,6 +21,7 @@ import { GitLabIntegration } from '@backstage/integration'; import { isChildPath } from '@backstage/cli-common'; import { JsonValue } from '@backstage/types'; import { Knex } from 'knex'; +import { LoadConfigOptionsRemote } from '@backstage/config-loader'; import { Logger as Logger_2 } from 'winston'; import { MergeResult } from 'isomorphic-git'; import { PushResult } from 'isomorphic-git'; @@ -389,6 +390,7 @@ export function isDatabaseConflictError(e: unknown): boolean; // @public export function loadBackendConfig(options: { logger: Logger_2; + remote?: LoadConfigOptionsRemote; argv: string[]; }): Promise;