Skip to content

Commit

Permalink
feat(ocm)!: migrate to the new backend and remove deprecations (#2104)
Browse files Browse the repository at this point in the history
* feat(ocm)!: migrate to the new backend and remove deprecation

Signed-off-by: Paul Schultz <pschultz@pobox.com>

* run prettier

Signed-off-by: Paul Schultz <pschultz@pobox.com>

* fix config.d.ts

Signed-off-by: Paul Schultz <pschultz@pobox.com>

* update test

Signed-off-by: Paul Schultz <pschultz@pobox.com>

* update exports

Signed-off-by: Paul Schultz <pschultz@pobox.com>

* update dist-dynamic

Signed-off-by: Paul Schultz <pschultz@pobox.com>

* apply requested changes

Signed-off-by: Paul Schultz <pschultz@pobox.com>

* Merge branch '1.30-deprecations' of https://github.com/schultzp2020/backstage-plugins into 1.30-deprecations

Signed-off-by: Paul Schultz <pschultz@pobox.com>

---------

Signed-off-by: Paul Schultz <pschultz@pobox.com>
  • Loading branch information
schultzp2020 authored Sep 26, 2024
1 parent 40dd140 commit 1ea2d5a
Show file tree
Hide file tree
Showing 23 changed files with 615 additions and 818 deletions.
4 changes: 2 additions & 2 deletions plugins/ocm-backend/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TaskScheduleDefinitionConfig } from '@backstage/backend-tasks';
import { SchedulerServiceTaskScheduleDefinitionConfig } from '@backstage/backend-plugin-api';

export interface Config {
catalog?: {
Expand Down Expand Up @@ -48,7 +48,7 @@ export interface Config {
* Owner reference to created cluster entities in the catalog
*/
owner?: string;
schedule?: TaskScheduleDefinitionConfig;
schedule?: SchedulerServiceTaskScheduleDefinitionConfig;
};
};
};
Expand Down
148 changes: 8 additions & 140 deletions plugins/ocm-backend/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,144 +1,12 @@
import {
CacheManager,
createServiceBuilder,
DatabaseManager,
getRootLogger,
HostDiscovery,
ServerTokenManager,
UrlReaders,
useHotMemoize,
} from '@backstage/backend-common';
import { LoggerService } from '@backstage/backend-plugin-api';
import { TaskScheduler } from '@backstage/backend-tasks';
import { Config, ConfigReader } from '@backstage/config';
import { DefaultIdentityClient } from '@backstage/plugin-auth-node';
import { CatalogBuilder } from '@backstage/plugin-catalog-backend';
import { ServerPermissionClient } from '@backstage/plugin-permission-node';
import { createBackend } from '@backstage/backend-defaults';

import { Router } from 'express';
import { catalogModuleOCMEntityProvider, ocmPlugin } from '../src/';

import { Server } from 'http';
const backend = createBackend();

import { ManagedClusterProvider } from '../src/';
import { createRouter, RouterOptions } from '../src/service/router';
import { PluginEnvironment } from './types';
// api endpoints from here: https://github.com/backstage/backstage/blob/master/plugins/catalog-backend/src/service/createRouter.ts
backend.add(import('@backstage/plugin-catalog-backend/alpha'));
backend.add(catalogModuleOCMEntityProvider);
backend.add(ocmPlugin);

export interface ServerOptions {
port: number;
logger: LoggerService;
}

function makeCreateEnv(config: Config) {
const root = getRootLogger();
const reader = UrlReaders.default({ logger: root, config });
const discovery = HostDiscovery.fromConfig(config);
const cacheManager = CacheManager.fromConfig(config);
const databaseManager = DatabaseManager.fromConfig(config, { logger: root });
const tokenManager = ServerTokenManager.noop();
const taskScheduler = TaskScheduler.fromConfig(config);

const identity = DefaultIdentityClient.create({
discovery,
});
const permissions = ServerPermissionClient.fromConfig(config, {
discovery,
tokenManager,
});

root.info(`Created UrlReader ${reader}`);

return (plugin: string): PluginEnvironment => {
const logger = root.child({ type: 'plugin', plugin });
const database = databaseManager.forPlugin(plugin);
const cache = cacheManager.forPlugin(plugin);
const scheduler = taskScheduler.forPlugin(plugin);
return {
logger,
database,
cache,
config,
reader,
discovery,
tokenManager,
scheduler,
permissions,
identity,
};
};
}
const catalog = async (env: PluginEnvironment): Promise<Router> => {
const builder = await CatalogBuilder.create(env);
const ocm = ManagedClusterProvider.fromConfig(env.config, {
logger: env.logger,
schedule: env.scheduler.createScheduledTaskRunner({
frequency: { minutes: 1 },
timeout: { minutes: 1 },
}),
});
builder.addEntityProvider(ocm);
const { processingEngine, router } = await builder.build();
await processingEngine.start();
return router;
};

export async function startStandaloneServer(
options: ServerOptions,
): Promise<Server> {
const logger = options.logger.child({
service: 'ocm-backend',
});

logger.info('Starting application server...');
const config = new ConfigReader({
backend: {
baseUrl: 'http://localhost:7007',
database: {
client: 'better-sqlite3',
connection: ':memory:',
},
},
catalog: {
providers: {
ocm: {
hub: {
name: 'foo',
url: 'http://localhost:5000',
serviceAccountToken: 'TOKEN',
},
},
},
},
});

const createEnv = makeCreateEnv(config);
const catalogEnv = useHotMemoize(module, () => createEnv('catalog'));
const discovery = HostDiscovery.fromConfig(config);
const tokenManager = ServerTokenManager.fromConfig(config, {
logger,
});
const permissions = ServerPermissionClient.fromConfig(config, {
discovery,
tokenManager,
});

const ocmRouterOptions: RouterOptions = {
logger,
config,
permissions,
discovery,
};
const service = createServiceBuilder(module)
.setPort(options.port)
.enableCors({
origin: '*',
})
.addRouter('/api/ocm', await createRouter(ocmRouterOptions))
.addRouter('/catalog', await catalog(catalogEnv));

return await service.start().catch(err => {
logger.error('Dev server failed:', err);
process.exit(1);
});
}

module.hot?.accept();
backend.start();
25 changes: 0 additions & 25 deletions plugins/ocm-backend/dev/types.ts

This file was deleted.

13 changes: 1 addition & 12 deletions plugins/ocm-backend/dist-dynamic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@
"require": "./dist/index.cjs.js",
"default": "./dist/index.cjs.js"
},
"./alpha": {
"require": "./dist/alpha.cjs.js",
"default": "./dist/alpha.cjs.js"
},
"./package.json": "./package.json"
},
"scripts": {},
"configSchema": "config.d.ts",
"dependencies": {
"@kubernetes/client-node": "^0.20.0",
"express": "^4.18.2",
"express-promise-router": "^4.1.1",
"semver": "^7.5.4",
"kubernetes-models": "^4.3.1",
"lodash": "^4.17.21",
Expand All @@ -43,8 +38,7 @@
"files": [
"dist",
"config.d.ts",
"app-config.janus-idp.yaml",
"alpha"
"app-config.janus-idp.yaml"
],
"repository": {
"type": "git",
Expand All @@ -65,15 +59,10 @@
"author": "Red Hat",
"bundleDependencies": true,
"peerDependencies": {
"@backstage/backend-common": "^0.23.3",
"@backstage/backend-defaults": "^0.4.1",
"@backstage/backend-dynamic-feature-service": "^0.2.15",
"@backstage/backend-openapi-utils": "^0.1.15",
"@backstage/backend-plugin-api": "^0.7.0",
"@backstage/backend-tasks": "^0.5.27",
"@backstage/catalog-client": "^1.6.5",
"@backstage/catalog-model": "^1.5.0",
"@backstage/config": "^1.2.0",
"@backstage/errors": "^1.2.4",
"@backstage/plugin-catalog-node": "^1.12.4",
"@backstage/plugin-permission-common": "^0.8.0",
Expand Down
49 changes: 15 additions & 34 deletions plugins/ocm-backend/dist-dynamic/yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1ea2d5a

Please sign in to comment.