Skip to content

Commit

Permalink
some legacy service deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Mar 30, 2021
1 parent 510caae commit 8d5cadf
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 263 deletions.
7 changes: 0 additions & 7 deletions src/core/server/legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,3 @@ export { ensureValidConfiguration } from './config';
/** @internal */
export type { ILegacyService } from './legacy_service';
export { LegacyService } from './legacy_service';
/** @internal */
export type {
LegacyVars,
LegacyConfig,
LegacyServiceSetupDeps,
LegacyServiceSetupConfig,
} from './types';
13 changes: 2 additions & 11 deletions src/core/server/legacy/legacy_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,16 @@

import type { PublicMethodsOf } from '@kbn/utility-types';
import { LegacyService } from './legacy_service';
import { LegacyConfig, LegacyServiceSetupDeps } from './types';
import { LegacyServiceSetupDeps } from './types';

type LegacyServiceMock = jest.Mocked<PublicMethodsOf<LegacyService> & { legacyId: symbol }>;
type LegacyServiceMock = jest.Mocked<PublicMethodsOf<LegacyService>>;

const createLegacyServiceMock = (): LegacyServiceMock => ({
legacyId: Symbol(),
setup: jest.fn(),
start: jest.fn(),
stop: jest.fn(),
});

const createLegacyConfigMock = (): jest.Mocked<LegacyConfig> => ({
get: jest.fn(),
has: jest.fn(),
set: jest.fn(),
});

export const legacyServiceMock = {
create: createLegacyServiceMock,
createSetupContract: (deps: LegacyServiceSetupDeps) => createLegacyServiceMock().setup(deps),
createLegacyConfig: createLegacyConfigMock,
};
80 changes: 0 additions & 80 deletions src/core/server/legacy/legacy_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,83 +193,3 @@ describe('once LegacyService is set up with connection info', () => {
expect(loggingSystemMock.collect(logger).error).toEqual([[configError]]);
});
});

describe('once LegacyService is set up in `devClusterMaster` mode', () => {
beforeEach(() => {
configService.atPath.mockImplementation((path) => {
return new BehaviorSubject(
path === 'dev' ? { basePathProxyTargetPort: 100500 } : { basePath: '/abc' }
);
});
});

test('creates CliDevMode without base path proxy.', async () => {
const devClusterLegacyService = new LegacyService({
coreId,
env: Env.createDefault(
REPO_ROOT,
getEnvOptions({
cliArgs: { silent: true, basePath: false },
isDevCliParent: true,
})
),
logger,
configService: configService as any,
});

await devClusterLegacyService.setup(setupDeps);
await devClusterLegacyService.start();

expect(MockCliDevMode.fromCoreServices).toHaveBeenCalledTimes(1);
expect(MockCliDevMode.fromCoreServices).toHaveBeenCalledWith(
expect.objectContaining({ silent: true, basePath: false }),
expect.objectContaining({
get: expect.any(Function),
set: expect.any(Function),
}),
undefined
);
});

test('creates CliDevMode with base path proxy.', async () => {
const devClusterLegacyService = new LegacyService({
coreId,
env: Env.createDefault(
REPO_ROOT,
getEnvOptions({
cliArgs: { quiet: true, basePath: true },
isDevCliParent: true,
})
),
logger,
configService: configService as any,
});

await devClusterLegacyService.setup(setupDeps);
await devClusterLegacyService.start();

expect(MockCliDevMode.fromCoreServices).toHaveBeenCalledTimes(1);
expect(MockCliDevMode.fromCoreServices).toHaveBeenCalledWith(
expect.objectContaining({ quiet: true, basePath: true }),
expect.objectContaining({
get: expect.any(Function),
set: expect.any(Function),
}),
expect.any(BasePathProxyServer)
);
});
});

describe('start', () => {
test('Cannot start without setup phase', async () => {
const legacyService = new LegacyService({
coreId,
env,
logger,
configService: configService as any,
});
await expect(legacyService.start()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Legacy service is not setup yet."`
);
});
});
111 changes: 9 additions & 102 deletions src/core/server/legacy/legacy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Side Public License, v 1.
*/

import { combineLatest, ConnectableObservable, Observable, Subscription } from 'rxjs';
import { first, map, publishReplay, tap } from 'rxjs/operators';
import { combineLatest, Observable, Subscription } from 'rxjs';
import { first } from 'rxjs/operators';
import { Server } from '@hapi/hapi';
import {
reconfigureLogging,
Expand All @@ -16,111 +16,38 @@ import {
LegacyLoggingConfig,
} from '@kbn/legacy-logging';
import type { PublicMethodsOf } from '@kbn/utility-types';
import { PathConfigType } from '@kbn/utils';

import { CoreService } from '../../types';
import { Config } from '../config';
import { CoreContext } from '../core_context';
import { CspConfigType, config as cspConfig } from '../csp';
import { DevConfig, DevConfigType, config as devConfig } from '../dev';
import { config as loggingConfig } from '../logging';
import { opsConfig, OpsConfigType } from '../metrics';
import { BasePathProxyServer, HttpConfig, HttpConfigType, config as httpConfig } from '../http';
import { Logger } from '../logging';
import { LegacyServiceSetupDeps, LegacyConfig, LegacyVars } from './types';
import { ExternalUrlConfigType, config as externalUrlConfig } from '../external_url';
import { InternalHttpServiceSetup } from '../http';

function getLegacyRawConfig(config: Config, pathConfig: PathConfigType) {
const rawConfig = config.toRaw();

// Elasticsearch config is solely handled by the core and legacy platform
// shouldn't have direct access to it.
if (rawConfig.elasticsearch !== undefined) {
delete rawConfig.elasticsearch;
}

return {
...rawConfig,
// We rely heavily in the default value of 'path.data' in the legacy world and,
// since it has been moved to NP, it won't show up in RawConfig.
path: pathConfig,
};
interface LegacyServiceSetupDeps {
http: InternalHttpServiceSetup;
}

/** @internal */
export type ILegacyService = PublicMethodsOf<LegacyService>;

/** @internal */
export class LegacyService implements CoreService {
/** Symbol to represent the legacy platform as a fake "plugin". Used by the ContextService */
public readonly legacyId = Symbol();
export class LegacyService {
private readonly log: Logger;
private readonly httpConfig$: Observable<HttpConfig>;
private readonly opsConfig$: Observable<OpsConfigType>;
private readonly legacyLoggingConfig$: Observable<LegacyLoggingConfig>;
private configSubscription?: Subscription;
private setupDeps?: LegacyServiceSetupDeps;
private update$?: ConnectableObservable<[Config, PathConfigType]>;
private legacyRawConfig?: LegacyConfig;
private settings?: LegacyVars;

constructor(private readonly coreContext: CoreContext) {
constructor(coreContext: CoreContext) {
const { logger, configService } = coreContext;

this.log = logger.get('legacy-service');
this.httpConfig$ = combineLatest(
configService.atPath<HttpConfigType>(httpConfig.path),
configService.atPath<CspConfigType>(cspConfig.path),
configService.atPath<ExternalUrlConfigType>(externalUrlConfig.path)
).pipe(map(([http, csp, externalUrl]) => new HttpConfig(http, csp, externalUrl)));

this.legacyLoggingConfig$ = configService.atPath<LegacyLoggingConfig>(loggingConfig.path);
this.opsConfig$ = configService.atPath<OpsConfigType>(opsConfig.path);
}

public async setup(setupDeps: LegacyServiceSetupDeps) {
this.log.debug('setting up legacy service');

this.update$ = combineLatest([
this.coreContext.configService.getConfig$(),
this.coreContext.configService.atPath<PathConfigType>('path'),
]).pipe(
tap({ error: (err) => this.log.error(err) }),
publishReplay(1)
) as ConnectableObservable<[Config, PathConfigType]>;

this.configSubscription = this.update$.connect();

this.settings = await this.update$
.pipe(
first(),
map(([config, pathConfig]) => getLegacyRawConfig(config, pathConfig))
)
.toPromise();

// this.legacyRawConfig = LegacyConfigClass.withDefaultSchema(this.settings);
// propagate the instance uuid to the legacy config, as it was the legacy way to access it.
// this.legacyRawConfig!.set('server.uuid', setupDeps.core.environment.instanceUuid);

this.setupDeps = setupDeps;
await this.setupLegacyLogging(setupDeps.core.http.server);
}

public async start() {
const { setupDeps } = this;

if (!setupDeps) {
throw new Error('Legacy service is not setup yet.');
}

this.log.debug('starting legacy service');

this.kbnServer = await this.createKbnServer(
this.settings!,
this.legacyRawConfig!,
setupDeps,
startDeps
);
await this.setupLegacyLogging(setupDeps.http.server);
}

private async setupLegacyLogging(server: Server) {
Expand All @@ -130,7 +57,7 @@ export class LegacyService implements CoreService {
await setupLogging(server, legacyLoggingConfig, currentOpsConfig.interval.asMilliseconds());
await setupLoggingRotate(server, legacyLoggingConfig);

combineLatest([this.legacyLoggingConfig$, this.opsConfig$]).subscribe(
this.configSubscription = combineLatest([this.legacyLoggingConfig$, this.opsConfig$]).subscribe(
([newLoggingConfig, newOpsConfig]) => {
reconfigureLogging(server, newLoggingConfig, newOpsConfig.interval.asMilliseconds());
}
Expand All @@ -145,24 +72,4 @@ export class LegacyService implements CoreService {
this.configSubscription = undefined;
}
}

private async setupCliDevMode(config: LegacyConfig) {
const basePathProxy$ = this.coreContext.env.cliArgs.basePath
? combineLatest([this.devConfig$, this.httpConfig$]).pipe(
first(),
map(
([dev, http]) =>
new BasePathProxyServer(this.coreContext.logger.get('server'), http, dev)
)
)
: EMPTY;

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { CliDevMode } = require('./cli_dev_mode');
CliDevMode.fromCoreServices(
this.coreContext.env.cliArgs,
config,
await basePathProxy$.toPromise()
);
}
}
3 changes: 2 additions & 1 deletion src/core/server/legacy/logging/appenders/legacy_appender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import { schema } from '@kbn/config-schema';
import { LegacyLoggingServer } from '@kbn/legacy-logging';
import { DisposableAppender, LogRecord } from '@kbn/logging';
import { LegacyVars } from '../../types';

type LegacyVars = Record<string, any>;

export interface LegacyAppenderConfig {
type: 'legacy-appender';
Expand Down
54 changes: 0 additions & 54 deletions src/core/server/legacy/types.ts

This file was deleted.

9 changes: 1 addition & 8 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,7 @@ export class Server {
await ensureValidConfiguration(this.configService);

const contextServiceSetup = this.context.setup({
// We inject a fake "legacy plugin" with dependencies on every plugin so that legacy plugins:
// 1) Can access context from any KP plugin
// 2) Can register context providers that will only be available to other legacy plugins and will not leak into
// New Platform plugins.
pluginDependencies: new Map([
...pluginTree.asOpaqueIds,
[this.legacy.legacyId, [...pluginTree.asOpaqueIds.keys()]],
]),
pluginDependencies: new Map([...pluginTree.asOpaqueIds]),
});

const httpSetup = await this.http.setup({
Expand Down

0 comments on commit 8d5cadf

Please sign in to comment.