-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ocm)!: migrate to the new backend and remove deprecations (#2104)
* 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
1 parent
40dd140
commit 1ea2d5a
Showing
23 changed files
with
615 additions
and
818 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.