Use Catalog REST API instead of assuming local process.

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2021-03-18 13:28:04 +01:00
parent 3560405cab
commit 44ec2571b6
4 changed files with 30 additions and 28 deletions
+15 -8
View File
@@ -13,22 +13,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { useHotCleanup } from '@backstage/backend-common';
import { createRouter } from '@backstage/plugin-search-backend';
import { IndexBuilder } from '@backstage/plugin-search-backend-node';
import { PluginEnvironment } from '../types';
// import { DefaultCatalogCollator } from '@backstage/plugin-catalog-backend';
import { DefaultCatalogCollator } from '@backstage/plugin-catalog-backend';
export default async function createPlugin({ logger }: PluginEnvironment) {
export default async function createPlugin({
logger,
discovery,
}: PluginEnvironment) {
const indexBuilder = new IndexBuilder();
// TODO: Within this PR, update to use REST API instead of Catalog Builder.
/* indexRegistry.addCollator({
indexBuilder.addCollator({
type: 'software-catalog',
defaultRefreshIntervalSeconds: 600,
collator: new DefaultCatalogCollator(entitiesCatalog),
});*/
collator: new DefaultCatalogCollator(discovery),
});
// TODO: Make this a more proper refresh loop.
indexBuilder.build();
// TODO: Move refresh loop logic into the builder.
const timerId = setInterval(() => {
indexBuilder.build();
}, 60000);
useHotCleanup(module, () => clearInterval(timerId));
return await createRouter({
logger,
-10
View File
@@ -49,16 +49,6 @@ export interface IndexableDocument {
* is clicked).
*/
location: string;
/**
* (Optional) The owner of the document (e.g. spec.owner on a catalog entity).
*/
owner?: string;
/**
* (Optional) The lifecycle of the document (e.g. spec.lifecycle on a catalog entity).
*/
lifecycle?: string;
}
/**
@@ -14,35 +14,40 @@
* limitations under the License.
*/
import { PluginEndpointDiscovery } from '@backstage/backend-common';
import { Entity } from '@backstage/catalog-model';
import { IndexableDocument, DocumentCollator } from '@backstage/search-common';
import { EntitiesCatalog } from '../catalog';
import fetch from 'cross-fetch';
export interface CatalogEntityDocument extends IndexableDocument {
componentType: string;
namespace: string;
kind: string;
}
export class DefaultCatalogCollator implements DocumentCollator {
protected entitiesCatalog: EntitiesCatalog;
protected discovery: PluginEndpointDiscovery;
constructor(entitiesCatalog: EntitiesCatalog) {
this.entitiesCatalog = entitiesCatalog;
constructor(discovery: PluginEndpointDiscovery) {
this.discovery = discovery;
}
async execute() {
const entities = await this.entitiesCatalog.entities();
const baseUrl = await this.discovery.getBaseUrl('catalog');
const res = await fetch(`${baseUrl}/entities`);
const entities: Entity[] = await res.json();
return entities.map(
(entity): CatalogEntityDocument => {
return {
title: entity.metadata.name,
// TODO: Use a config-based template approach for entity location.
location: `/catalog/${
entity.metadata.namespace || 'default'
}/component/${entity.metadata.name}`,
text: entity.metadata.description || '',
componentType: entity.spec?.type?.toString() || 'other',
...(entity.spec?.owner && { owner: entity.spec.owner.toString() }),
...(entity.metadata.lifecycle && {
lifecycle: entity.metadata.lifecycle.toString(),
}),
namespace: entity.metadata.namespace || 'default',
kind: entity.kind,
};
},
);
+1 -1
View File
@@ -14,4 +14,4 @@
* limitations under the License.
*/
export * from './Collator';
export * from './DefaultCatalogCollator';