Merge pull request #19297 from backstage/philipph/replace-backend-search-module-options

make `search-backend-module-explore` & `search-backend-module-techdocs` configurable from app-config
This commit is contained in:
Patrik Oldsberg
2023-08-15 15:23:51 +02:00
committed by GitHub
16 changed files with 238 additions and 155 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-backend-module-techdocs': patch
---
**BREAKING:** Moved `schedule` & `collators` settings from module options into app-config for the new backend system. You can now pass in a `TaskScheduleDefinitionConfig` through the `search.collators.techdocs.schedule` configuration key & configure the `TechDocsCollatorFactory` with the key `search.collators.techdocs`.
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-backend-module-explore': patch
---
Breaking change for the alpha export moved `schedule` from module options into app-config for the new backend system. You can now pass in a `TaskScheduleDefinitionConfig` through the `search.collators.explore.schedule` configuration key.
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/permission-backend': patch
---
Refactor backend plugin creation parameter from callback to object.
+2 -2
View File
@@ -80,7 +80,7 @@ export const permissionModuleAllowAllPolicy = createBackendModule({
*
* @alpha
*/
export const permissionPlugin = createBackendPlugin(() => ({
export const permissionPlugin = createBackendPlugin({
pluginId: 'permission',
register(env) {
const policies = new PolicyExtensionPointImpl();
@@ -115,4 +115,4 @@ export const permissionPlugin = createBackendPlugin(() => ({
},
});
},
}));
});
@@ -4,17 +4,9 @@
```ts
import { BackendFeature } from '@backstage/backend-plugin-api';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
// @alpha
export const searchModuleExploreCollator: (
options?: SearchModuleExploreCollatorOptions | undefined,
) => BackendFeature;
// @alpha
export type SearchModuleExploreCollatorOptions = {
schedule?: TaskScheduleDefinition;
};
export const searchModuleExploreCollator: () => BackendFeature;
// (No @packageDocumentation comment for this package)
```
+27
View File
@@ -0,0 +1,27 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TaskScheduleDefinitionConfig } from '@backstage/backend-tasks';
export interface Config {
search?: {
collators?: {
explore?: {
schedule?: TaskScheduleDefinitionConfig;
};
};
};
}
@@ -58,6 +58,8 @@
"msw": "^1.2.1"
},
"files": [
"dist"
]
"dist",
"config.d.ts"
],
"configSchema": "config.d.ts"
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { startTestBackend } from '@backstage/backend-test-utils';
import { mockServices, startTestBackend } from '@backstage/backend-test-utils';
import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha';
import { searchModuleExploreCollator } from './alpha';
@@ -34,7 +34,18 @@ describe('searchModuleExploreCollator', () => {
extensionPoints: [
[searchIndexRegistryExtensionPoint, extensionPointMock],
],
features: [searchModuleExploreCollator({ schedule })],
features: [searchModuleExploreCollator()],
services: [
mockServices.rootConfig.factory({
data: {
search: {
explore: {
schedule,
},
},
},
}),
],
});
expect(extensionPointMock.addCollator).toHaveBeenCalledTimes(1);
@@ -23,66 +23,59 @@ import {
coreServices,
createBackendModule,
} from '@backstage/backend-plugin-api';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { loggerToWinstonLogger } from '@backstage/backend-common';
import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha';
import { ToolDocumentCollatorFactory } from '@backstage/plugin-search-backend-module-explore';
/**
* Options for {@link searchModuleExploreCollator}.
*
* @alpha
*/
export type SearchModuleExploreCollatorOptions = {
schedule?: TaskScheduleDefinition;
};
import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks';
/**
* Search backend module for the Explore index.
*
* @alpha
*/
export const searchModuleExploreCollator = createBackendModule(
(options?: SearchModuleExploreCollatorOptions) => ({
moduleId: 'exploreCollator',
pluginId: 'search',
register(env) {
env.registerInit({
deps: {
config: coreServices.rootConfig,
logger: coreServices.logger,
discovery: coreServices.discovery,
scheduler: coreServices.scheduler,
tokenManager: coreServices.tokenManager,
indexRegistry: searchIndexRegistryExtensionPoint,
},
async init({
config,
logger,
discovery,
scheduler,
indexRegistry,
tokenManager,
}) {
const defaultSchedule = {
frequency: { minutes: 10 },
timeout: { minutes: 15 },
initialDelay: { seconds: 3 },
};
export const searchModuleExploreCollator = createBackendModule({
moduleId: 'exploreCollator',
pluginId: 'search',
register(env) {
env.registerInit({
deps: {
config: coreServices.rootConfig,
logger: coreServices.logger,
discovery: coreServices.discovery,
scheduler: coreServices.scheduler,
tokenManager: coreServices.tokenManager,
indexRegistry: searchIndexRegistryExtensionPoint,
},
async init({
config,
logger,
discovery,
scheduler,
indexRegistry,
tokenManager,
}) {
const defaultSchedule = {
frequency: { minutes: 10 },
timeout: { minutes: 15 },
initialDelay: { seconds: 3 },
};
indexRegistry.addCollator({
schedule: scheduler.createScheduledTaskRunner(
options?.schedule ?? defaultSchedule,
),
factory: ToolDocumentCollatorFactory.fromConfig(config, {
discovery,
logger: loggerToWinstonLogger(logger),
tokenManager,
}),
});
},
});
},
}),
);
const schedule = config.has('search.collators.explore.schedule')
? readTaskScheduleDefinitionFromConfig(
config.getConfig('search.collators.explore.schedule'),
)
: defaultSchedule;
indexRegistry.addCollator({
schedule: scheduler.createScheduledTaskRunner(schedule),
factory: ToolDocumentCollatorFactory.fromConfig(config, {
discovery,
logger: loggerToWinstonLogger(logger),
tokenManager,
}),
});
},
});
},
});
@@ -4,21 +4,9 @@
```ts
import { BackendFeature } from '@backstage/backend-plugin-api';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { TechDocsCollatorFactoryOptions } from '@backstage/plugin-search-backend-module-techdocs';
// @alpha
export const searchModuleTechDocsCollator: (
options?: SearchModuleTechDocsCollatorOptions | undefined,
) => BackendFeature;
// @alpha
export type SearchModuleTechDocsCollatorOptions = Omit<
TechDocsCollatorFactoryOptions,
'logger' | 'discovery' | 'tokenManager' | 'catalogClient'
> & {
schedule?: TaskScheduleDefinition;
};
export const searchModuleTechDocsCollator: () => BackendFeature;
// (No @packageDocumentation comment for this package)
```
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TaskScheduleDefinitionConfig } from '@backstage/backend-tasks';
export interface Config {
search?: {
collators?: {
techdocs?: {
schedule?: TaskScheduleDefinitionConfig;
locationTemplate?: string;
parallelismLimit?: number;
legacyPathCasing?: boolean;
};
};
};
}
@@ -65,6 +65,8 @@
"msw": "^1.0.0"
},
"files": [
"dist"
]
"dist",
"config.d.ts"
],
"configSchema": "config.d.ts"
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { startTestBackend } from '@backstage/backend-test-utils';
import { mockServices, startTestBackend } from '@backstage/backend-test-utils';
import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha';
import { searchModuleTechDocsCollator } from './alpha';
@@ -34,7 +34,18 @@ describe('searchModuleTechDocsCollator', () => {
extensionPoints: [
[searchIndexRegistryExtensionPoint, extensionPointMock],
],
features: [searchModuleTechDocsCollator({ schedule })],
features: [searchModuleTechDocsCollator()],
services: [
mockServices.rootConfig.factory({
data: {
search: {
techdocs: {
schedule,
},
},
},
}),
],
});
expect(extensionPointMock.addCollator).toHaveBeenCalledTimes(1);
@@ -19,79 +19,65 @@
* A module for the search backend that exports TechDocs modules.
*/
import { loggerToWinstonLogger } from '@backstage/backend-common';
import {
coreServices,
createBackendModule,
} from '@backstage/backend-plugin-api';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { loggerToWinstonLogger } from '@backstage/backend-common';
import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha';
import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks';
import { catalogServiceRef } from '@backstage/plugin-catalog-node/alpha';
import {
DefaultTechDocsCollatorFactory,
TechDocsCollatorFactoryOptions,
} from '@backstage/plugin-search-backend-module-techdocs';
/**
* @alpha
* Options for {@link searchModuleTechDocsCollator}.
*/
export type SearchModuleTechDocsCollatorOptions = Omit<
TechDocsCollatorFactoryOptions,
'logger' | 'discovery' | 'tokenManager' | 'catalogClient'
> & {
schedule?: TaskScheduleDefinition;
};
import { DefaultTechDocsCollatorFactory } from '@backstage/plugin-search-backend-module-techdocs';
import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha';
/**
* @alpha
* Search backend module for the TechDocs index.
*/
export const searchModuleTechDocsCollator = createBackendModule(
(options?: SearchModuleTechDocsCollatorOptions) => ({
moduleId: 'techDocsCollator',
pluginId: 'search',
register(env) {
env.registerInit({
deps: {
config: coreServices.rootConfig,
logger: coreServices.logger,
discovery: coreServices.discovery,
tokenManager: coreServices.tokenManager,
scheduler: coreServices.scheduler,
catalog: catalogServiceRef,
indexRegistry: searchIndexRegistryExtensionPoint,
},
async init({
config,
logger,
discovery,
tokenManager,
scheduler,
catalog,
indexRegistry,
}) {
const defaultSchedule = {
frequency: { minutes: 10 },
timeout: { minutes: 15 },
initialDelay: { seconds: 3 },
};
export const searchModuleTechDocsCollator = createBackendModule({
moduleId: 'techDocsCollator',
pluginId: 'search',
register(env) {
env.registerInit({
deps: {
config: coreServices.rootConfig,
logger: coreServices.logger,
discovery: coreServices.discovery,
tokenManager: coreServices.tokenManager,
scheduler: coreServices.scheduler,
catalog: catalogServiceRef,
indexRegistry: searchIndexRegistryExtensionPoint,
},
async init({
config,
logger,
discovery,
tokenManager,
scheduler,
catalog,
indexRegistry,
}) {
const defaultSchedule = {
frequency: { minutes: 10 },
timeout: { minutes: 15 },
initialDelay: { seconds: 3 },
};
indexRegistry.addCollator({
schedule: scheduler.createScheduledTaskRunner(
options?.schedule ?? defaultSchedule,
),
factory: DefaultTechDocsCollatorFactory.fromConfig(config, {
...options,
discovery,
tokenManager,
logger: loggerToWinstonLogger(logger),
catalogClient: catalog,
}),
});
},
});
},
}),
);
const schedule = config.has('search.collators.techdocs.schedule')
? readTaskScheduleDefinitionFromConfig(
config.getConfig('search.collators.techdocs.schedule'),
)
: defaultSchedule;
indexRegistry.addCollator({
schedule: scheduler.createScheduledTaskRunner(schedule),
factory: DefaultTechDocsCollatorFactory.fromConfig(config, {
discovery,
tokenManager,
logger: loggerToWinstonLogger(logger),
catalogClient: catalog,
}),
});
},
});
},
});
@@ -174,10 +174,15 @@ describe('DefaultTechDocsCollatorFactory', () => {
it('maps a returned entity with a custom locationTemplate', async () => {
// Provide an alternate location template.
factory = DefaultTechDocsCollatorFactory.fromConfig(config, {
const _config = new ConfigReader({
...config.get(),
search: {
collators: { techdocs: { locationTemplate: '/software/:name' } },
},
});
factory = DefaultTechDocsCollatorFactory.fromConfig(_config, {
discovery: mockDiscoveryApi,
tokenManager: mockTokenManager,
locationTemplate: '/software/:name',
logger,
});
collator = await factory.getCollator();
@@ -194,10 +199,17 @@ describe('DefaultTechDocsCollatorFactory', () => {
// A parallelismLimit of 1 is a catalog limit of 50 per request. Code
// above in the /entities handler ensures valid entities are only
// returned on the second page.
factory = DefaultTechDocsCollatorFactory.fromConfig(config, {
...options,
parallelismLimit: 1,
const _config = new ConfigReader({
...config.get(),
search: {
collators: {
techdocs: {
parallelismLimit: 1,
},
},
},
});
factory = DefaultTechDocsCollatorFactory.fromConfig(_config, options);
collator = await factory.getCollator();
const pipeline = TestPipeline.fromCollator(collator);
@@ -205,6 +217,9 @@ describe('DefaultTechDocsCollatorFactory', () => {
// Only 1 entity with TechDocs configured multiplied by 3 pages.
expect(documents).toHaveLength(3);
expect(_config.get('search.collators.techdocs.parallelismLimit')).toEqual(
1,
);
});
describe('with legacyPathCasing configuration', () => {
@@ -104,7 +104,18 @@ export class DefaultTechDocsCollatorFactory implements DocumentCollatorFactory {
config.getOptionalBoolean(
'techdocs.legacyUseCaseSensitiveTripletPaths',
) || false;
return new DefaultTechDocsCollatorFactory({ ...options, legacyPathCasing });
const locationTemplate = config.getOptionalString(
'search.collators.techdocs.locationTemplate',
);
const parallelismLimit = config.getOptionalNumber(
'search.collators.techdocs.parallelismLimit',
);
return new DefaultTechDocsCollatorFactory({
...options,
locationTemplate,
parallelismLimit,
legacyPathCasing,
});
}
async getCollator(): Promise<Readable> {