Skip to content

Commit

Permalink
[New platform] Expose NP core services and plugins contracts to the l…
Browse files Browse the repository at this point in the history
…egacy server (#32468) (#32666)

* expose NewPlatform core and plugins to legacy server

* get rid of .core in favor of .newPlatform

* decalre start/stop contract explicitly

* expose newPlatform typings

* Remove dependency on legacy package_json util from core

* Remove dependencies on core/server in core/types
  • Loading branch information
mshustov authored and epixa committed Mar 7, 2019
1 parent d356b08 commit ee7e6dc
Show file tree
Hide file tree
Showing 30 changed files with 92 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/core/server/config/config_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { BehaviorSubject } from 'rxjs';
import { first } from 'rxjs/operators';

const mockPackage = new Proxy({ raw: {} as any }, { get: (obj, prop) => obj.raw[prop] });
jest.mock('../../../legacy/utils/package_json', () => ({ pkg: mockPackage }));
jest.mock('../../../../package.json', () => mockPackage);

import { schema, Type, TypeOf } from '@kbn/config-schema';

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/config/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jest.mock('path', () => ({
}));

const mockPackage = new Proxy({ raw: {} as any }, { get: (obj, prop) => obj.raw[prop] });
jest.mock('../../../legacy/utils/package_json', () => ({ pkg: mockPackage }));
jest.mock('../../../../package.json', () => mockPackage);

import { Env } from '.';
import { getEnvOptions } from './__mocks__/env';
Expand Down
4 changes: 3 additions & 1 deletion src/core/server/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import { resolve } from 'path';
import process from 'process';

import { pkg } from '../../../legacy/utils/package_json';
// `require` is necessary for this to work inside x-pack code as well
// tslint:disable no-var-requires
const pkg = require('../../../../package.json');

export interface PackageInfo {
version: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import { ConfigService, Env } from '../server/config';
import { LoggerFactory } from '../server/logging';
import { ConfigService, Env } from './config';
import { LoggerFactory } from './logging';

/**
* Groups all main Kibana's core modules/systems/services that are consumed in a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const MockClusterClient = jest.fn();
jest.mock('./cluster_client', () => ({ ClusterClient: MockClusterClient }));

import { BehaviorSubject, combineLatest } from 'rxjs';
import { CoreContext } from '../../types';
import { Config, ConfigService, Env, ObjectToConfigAdapter } from '../config';
import { getEnvOptions } from '../config/__mocks__/env';
import { CoreContext } from '../core_context';
import { loggingServiceMock } from '../logging/logging_service.mock';
import { ElasticsearchConfig } from './elasticsearch_config';
import { ElasticsearchService } from './elasticsearch_service';
Expand Down
3 changes: 2 additions & 1 deletion src/core/server/elasticsearch/elasticsearch_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import { ConnectableObservable, Observable, Subscription } from 'rxjs';
import { filter, map, publishReplay, switchMap } from 'rxjs/operators';
import { CoreContext, CoreService } from '../../types';
import { CoreService } from '../../types';
import { CoreContext } from '../core_context';
import { Logger } from '../logging';
import { ClusterClient } from './cluster_client';
import { ElasticsearchClientConfig } from './elasticsearch_client_config';
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/elasticsearch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
export { ElasticsearchServiceStart } from './elasticsearch_service';
export { CallAPIOptions, ClusterClient } from './cluster_client';

import { CoreContext } from '../../types';
import { CoreContext } from '../core_context';
import { ElasticsearchService } from './elasticsearch_service';

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { CoreContext } from '../../types';
import { CoreContext } from '../core_context';
import { LegacyService } from './legacy_service';

/** @internal */
Expand Down
3 changes: 2 additions & 1 deletion src/core/server/legacy/legacy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import { Server as HapiServer } from 'hapi';
import { combineLatest, ConnectableObservable, EMPTY, Subscription } from 'rxjs';
import { first, map, mergeMap, publishReplay, tap } from 'rxjs/operators';
import { CoreContext, CoreService } from '../../types';
import { CoreService } from '../../types';
import { Config } from '../config';
import { CoreContext } from '../core_context';
import { DevConfig } from '../dev';
import { ElasticsearchServiceStart } from '../elasticsearch';
import { BasePathProxyServer, HttpConfig, HttpServiceStart } from '../http';
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/plugins/discovery/plugin_discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jest.mock('fs', () => ({
}));

const mockPackage = new Proxy({ raw: {} as any }, { get: (obj, prop) => obj.raw[prop] });
jest.mock('../../../../legacy/utils/package_json', () => ({ pkg: mockPackage }));
jest.mock('../../../../../package.json', () => mockPackage);

import { resolve } from 'path';
import { BehaviorSubject } from 'rxjs';
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/plugins/discovery/plugins_discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { readdir, stat } from 'fs';
import { resolve } from 'path';
import { bindNodeCallback, from } from 'rxjs';
import { catchError, filter, map, mergeMap, shareReplay } from 'rxjs/operators';
import { CoreContext } from '../../../types';
import { CoreContext } from '../../core_context';
import { Logger } from '../../logging';
import { Plugin } from '../plugin';
import { createPluginInitializerContext } from '../plugin_context';
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { CoreContext } from '../../types';
import { CoreContext } from '../core_context';
import { PluginsService } from './plugins_service';

/** @internal */
Expand Down
3 changes: 1 addition & 2 deletions src/core/server/plugins/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@

import { join } from 'path';
import { BehaviorSubject } from 'rxjs';
import { CoreContext } from '../../types';
import { Config, ConfigService, Env, ObjectToConfigAdapter } from '../config';
import { getEnvOptions } from '../config/__mocks__/env';
import { CoreContext } from '../core_context';
import { elasticsearchServiceMock } from '../elasticsearch/elasticsearch_service.mock';
import { loggingServiceMock } from '../logging/logging_service.mock';

import { Plugin, PluginManifest } from './plugin';
import { createPluginInitializerContext, createPluginStartContext } from './plugin_context';

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import { Type } from '@kbn/config-schema';
import { Observable } from 'rxjs';
import { CoreContext } from '../../types';
import { ConfigWithSchema, EnvironmentMode } from '../config';
import { CoreContext } from '../core_context';
import { ClusterClient } from '../elasticsearch';
import { LoggerFactory } from '../logging';
import { Plugin, PluginManifest } from './plugin';
Expand Down
3 changes: 2 additions & 1 deletion src/core/server/plugins/plugins_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import { Observable } from 'rxjs';
import { filter, first, mergeMap, tap, toArray } from 'rxjs/operators';
import { CoreContext, CoreService } from '../../types';
import { CoreService } from '../../types';
import { CoreContext } from '../core_context';
import { ElasticsearchServiceStart } from '../elasticsearch';
import { Logger } from '../logging';
import { discover, PluginDiscoveryError, PluginDiscoveryErrorType } from './discovery';
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/plugins/plugins_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { CoreContext } from '../../types';
import { CoreContext } from '../core_context';

const mockCreatePluginStartContext = jest.fn();
jest.mock('./plugin_context', () => ({
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/plugins/plugins_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { CoreContext } from '../../types';
import { CoreContext } from '../core_context';
import { Logger } from '../logging';
import { Plugin, PluginName } from './plugin';
import { createPluginStartContext } from './plugin_context';
Expand Down
10 changes: 8 additions & 2 deletions src/core/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@
* under the License.
*/

export { CoreContext } from './core_context';
export { CoreService } from './core_service';
/**
* Use * syntax so that these exports do not break when internal
* types are stripped.
*
* No imports in this directory can import from ./server or ./public
* or else builds will not work correctly for both NodeJS and Webpack.
*/
export * from './core_service';
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/console/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function (kibana) {
}

const config = server.config();
const legacyEsConfig = await server.core.elasticsearch.legacy.config$.pipe(first()).toPromise();
const legacyEsConfig = await server.newPlatform.start.core.elasticsearch.legacy.config$.pipe(first()).toPromise();
const proxyConfigCollection = new ProxyConfigCollection(options.proxyConfig);
const proxyPathFilters = options.proxyFilter.map(str => new RegExp(str));

Expand Down
8 changes: 4 additions & 4 deletions src/legacy/core_plugins/elasticsearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export default function (kibana) {
// value from all observables here to be able to synchronously return and create
// cluster clients afterwards.
const [esConfig, adminCluster, dataCluster] = await combineLatest(
server.core.elasticsearch.legacy.config$,
server.core.elasticsearch.adminClient$,
server.core.elasticsearch.dataClient$
server.newPlatform.start.core.elasticsearch.legacy.config$,
server.newPlatform.start.core.elasticsearch.adminClient$,
server.newPlatform.start.core.elasticsearch.dataClient$
).pipe(
first(),
map(([config, adminClusterClient, dataClusterClient]) => [
Expand Down Expand Up @@ -80,7 +80,7 @@ export default function (kibana) {
// We fill all the missing properties in the `clientConfig` using the default
// Elasticsearch config so that we don't depend on default values set and
// controlled by underlying Elasticsearch JS client.
const cluster = new Cluster(server.core.elasticsearch.createClient(name, {
const cluster = new Cluster(server.newPlatform.start.core.elasticsearch.createClient(name, {
...esConfig,
...clientConfig,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ describe('getEsShardTimeout', () => {
it('should return the elasticsearch.shardTimeout', async () => {
const req = {
server: {
core: {
elasticsearch: { legacy: { config$: of({ shardTimeout: moment.duration(12345) }) } }
newPlatform: {
start: {
core: {
elasticsearch: { legacy: { config$: of({ shardTimeout: moment.duration(12345) }) } }
}
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { first, map } from 'rxjs/operators';

export async function getEsShardTimeout(req) {
return await req.server.core.elasticsearch.legacy.config$.pipe(
return await req.server.newPlatform.start.core.elasticsearch.legacy.config$.pipe(
first(),
map(config => config.shardTimeout.asMilliseconds())
).toPromise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ export default function () {
})
}
},
core: {
elasticsearch: {
legacy: { config$: of({ shardTimeout: moment.duration(30000) }) }
newPlatform: {
start: {
core: {
elasticsearch: {
legacy: { config$: of({ shardTimeout: moment.duration(30000) }) }
}
}
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default new Datasource('es', {
});
}

const esShardTimeout = await tlConfig.server.core.elasticsearch.legacy.config$.pipe(
const esShardTimeout = await tlConfig.server.newPlatform.start.core.elasticsearch.legacy.config$.pipe(
first(),
map(config => config.shardTimeout.asMilliseconds())
).toPromise();
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/server/config/complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default async function (kbnServer, server, config) {
});

const unusedKeys = await getUnusedConfigKeys(
kbnServer.core.handledConfigPaths,
kbnServer.newPlatform.params.handledConfigPaths,
kbnServer.plugins,
kbnServer.disabledPluginSpecs,
kbnServer.settings,
Expand Down
4 changes: 3 additions & 1 deletion src/legacy/server/config/complete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ describe('server/config completeMixin()', function () {
};

const kbnServer = {
core: { handledConfigPaths: [] },
newPlatform: {
params: { handledConfigPaths: [] }
},
settings,
server,
config,
Expand Down
5 changes: 3 additions & 2 deletions src/legacy/server/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import { Server } from 'hapi';
import KbnServer from '../kbn_server';

/**
* Exposes `kbnServer.core` through Hapi API.
* Exposes `kbnServer.newPlatform` through Hapi API.
* @param kbnServer KbnServer singleton instance.
* @param server Hapi server instance to expose `core` on.
*/
export function coreMixin(kbnServer: KbnServer, server: Server) {
server.decorate('server', 'core', kbnServer.core);
// we suppress type error because hapi expect a function here not an object
server.decorate('server', 'newPlatform', kbnServer.newPlatform as any);
}
2 changes: 1 addition & 1 deletion src/legacy/server/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { setupBasePathProvider } from './setup_base_path_provider';
import { setupXsrf } from './xsrf';

export default async function (kbnServer, server, config) {
kbnServer.server = new Hapi.Server(kbnServer.core.serverOptions);
kbnServer.server = new Hapi.Server(kbnServer.newPlatform.params.serverOptions);
server = kbnServer.server;

setupBasePathProvider(server, config);
Expand Down
20 changes: 18 additions & 2 deletions src/legacy/server/kbn_server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import { Server } from 'hapi';

import { ConfigService } from '../../core/server/config';
import { ElasticsearchServiceStart } from '../../core/server/elasticsearch';
import { HttpServerInfo } from '../../core/server/http/';
import { PluginsServiceStart } from '../../core/server/plugins/plugins_service';
import { ApmOssPlugin } from '../core_plugins/apm_oss';
import { CallClusterWithRequest, ElasticsearchPlugin } from '../core_plugins/elasticsearch';

Expand Down Expand Up @@ -55,9 +59,21 @@ declare module 'hapi' {
}

type KbnMixinFunc = (kbnServer: KbnServer, server: Server, config: any) => Promise<any> | void;

type Unpromise<T> = T extends Promise<infer U> ? U : T;
export default class KbnServer {
public readonly core: any;
public readonly newPlatform: {
start: {
core: {
elasticsearch: ElasticsearchServiceStart;
};
plugins: PluginsServiceStart;
};
stop: null;
params: {
serverOptions: HttpServerInfo;
handledConfigPaths: Unpromise<ReturnType<ConfigService['getUsedPaths']>>;
};
};
public server: Server;
public inject: Server['inject'];

Expand Down
16 changes: 15 additions & 1 deletion src/legacy/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,21 @@ export default class KbnServer {
this.rootDir = rootDir;
this.settings = settings || {};

this.core = core;
const { plugins, elasticsearch, serverOptions, handledConfigPaths } = core;

this.newPlatform = {
start: {
core: {
elasticsearch,
},
plugins,
},
stop: null,
params: {
serverOptions,
handledConfigPaths,
},
};

this.ready = constant(this.mixin(
Plugins.waitForInitSetupMixin,
Expand Down

0 comments on commit ee7e6dc

Please sign in to comment.