From ee7e6dc277cddf5615b454be3c19bd4a4a90ca63 Mon Sep 17 00:00:00 2001 From: Mikhail Shustov Date: Thu, 7 Mar 2019 21:53:21 +0100 Subject: [PATCH] [New platform] Expose NP core services and plugins contracts to the legacy 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 --- src/core/server/config/config_service.test.ts | 2 +- src/core/server/config/env.test.ts | 2 +- src/core/server/config/env.ts | 4 +++- src/core/{types => server}/core_context.ts | 4 ++-- .../elasticsearch_service.test.ts | 2 +- .../elasticsearch/elasticsearch_service.ts | 3 ++- src/core/server/elasticsearch/index.ts | 2 +- src/core/server/legacy/index.ts | 2 +- src/core/server/legacy/legacy_service.ts | 3 ++- .../discovery/plugin_discovery.test.ts | 2 +- .../plugins/discovery/plugins_discovery.ts | 2 +- src/core/server/plugins/index.ts | 2 +- src/core/server/plugins/plugin.test.ts | 3 +-- src/core/server/plugins/plugin_context.ts | 2 +- src/core/server/plugins/plugins_service.ts | 3 ++- .../server/plugins/plugins_system.test.ts | 2 +- src/core/server/plugins/plugins_system.ts | 2 +- src/core/types/index.ts | 10 ++++++++-- src/legacy/core_plugins/console/index.js | 2 +- .../core_plugins/elasticsearch/index.js | 8 ++++---- .../__tests__/helpers/get_es_shard_timeout.js | 8 ++++++-- .../vis_data/helpers/get_es_shard_timeout.js | 2 +- .../__tests__/fixtures/tlConfig.js | 10 +++++++--- .../server/series_functions/es/index.js | 2 +- src/legacy/server/config/complete.js | 2 +- src/legacy/server/config/complete.test.js | 4 +++- src/legacy/server/core/index.ts | 5 +++-- src/legacy/server/http/index.js | 2 +- src/legacy/server/kbn_server.d.ts | 20 +++++++++++++++++-- src/legacy/server/kbn_server.js | 16 ++++++++++++++- 30 files changed, 92 insertions(+), 41 deletions(-) rename src/core/{types => server}/core_context.ts (90%) diff --git a/src/core/server/config/config_service.test.ts b/src/core/server/config/config_service.test.ts index dbb205518e4a0..7a10d4992e2c2 100644 --- a/src/core/server/config/config_service.test.ts +++ b/src/core/server/config/config_service.test.ts @@ -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'; diff --git a/src/core/server/config/env.test.ts b/src/core/server/config/env.test.ts index 34d37509db456..3c251b7339e49 100644 --- a/src/core/server/config/env.test.ts +++ b/src/core/server/config/env.test.ts @@ -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'; diff --git a/src/core/server/config/env.ts b/src/core/server/config/env.ts index 48843e5bc50dd..4ee7a778e9c8f 100644 --- a/src/core/server/config/env.ts +++ b/src/core/server/config/env.ts @@ -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; diff --git a/src/core/types/core_context.ts b/src/core/server/core_context.ts similarity index 90% rename from src/core/types/core_context.ts rename to src/core/server/core_context.ts index 1488826e7e2da..a08bd03f148cb 100644 --- a/src/core/types/core_context.ts +++ b/src/core/server/core_context.ts @@ -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 diff --git a/src/core/server/elasticsearch/elasticsearch_service.test.ts b/src/core/server/elasticsearch/elasticsearch_service.test.ts index 4e7dd7faba214..286d815fe5c2d 100644 --- a/src/core/server/elasticsearch/elasticsearch_service.test.ts +++ b/src/core/server/elasticsearch/elasticsearch_service.test.ts @@ -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'; diff --git a/src/core/server/elasticsearch/elasticsearch_service.ts b/src/core/server/elasticsearch/elasticsearch_service.ts index 7a1b7e717e49b..493dc8577fb31 100644 --- a/src/core/server/elasticsearch/elasticsearch_service.ts +++ b/src/core/server/elasticsearch/elasticsearch_service.ts @@ -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'; diff --git a/src/core/server/elasticsearch/index.ts b/src/core/server/elasticsearch/index.ts index 47242678ec736..a0e0a51ddc16f 100644 --- a/src/core/server/elasticsearch/index.ts +++ b/src/core/server/elasticsearch/index.ts @@ -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 */ diff --git a/src/core/server/legacy/index.ts b/src/core/server/legacy/index.ts index 187e808ce3ebe..429e54ef673d4 100644 --- a/src/core/server/legacy/index.ts +++ b/src/core/server/legacy/index.ts @@ -17,7 +17,7 @@ * under the License. */ -import { CoreContext } from '../../types'; +import { CoreContext } from '../core_context'; import { LegacyService } from './legacy_service'; /** @internal */ diff --git a/src/core/server/legacy/legacy_service.ts b/src/core/server/legacy/legacy_service.ts index 6bd4ff79d2439..26dd003fd43c6 100644 --- a/src/core/server/legacy/legacy_service.ts +++ b/src/core/server/legacy/legacy_service.ts @@ -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'; diff --git a/src/core/server/plugins/discovery/plugin_discovery.test.ts b/src/core/server/plugins/discovery/plugin_discovery.test.ts index 7a67f332de40f..266ad1780d47f 100644 --- a/src/core/server/plugins/discovery/plugin_discovery.test.ts +++ b/src/core/server/plugins/discovery/plugin_discovery.test.ts @@ -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'; diff --git a/src/core/server/plugins/discovery/plugins_discovery.ts b/src/core/server/plugins/discovery/plugins_discovery.ts index e7ec311c5823f..bbd1150712157 100644 --- a/src/core/server/plugins/discovery/plugins_discovery.ts +++ b/src/core/server/plugins/discovery/plugins_discovery.ts @@ -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'; diff --git a/src/core/server/plugins/index.ts b/src/core/server/plugins/index.ts index 92b783a148218..a66f24c0790a6 100644 --- a/src/core/server/plugins/index.ts +++ b/src/core/server/plugins/index.ts @@ -17,7 +17,7 @@ * under the License. */ -import { CoreContext } from '../../types'; +import { CoreContext } from '../core_context'; import { PluginsService } from './plugins_service'; /** @internal */ diff --git a/src/core/server/plugins/plugin.test.ts b/src/core/server/plugins/plugin.test.ts index bbbaf462d86ea..fa3c89f6ee918 100644 --- a/src/core/server/plugins/plugin.test.ts +++ b/src/core/server/plugins/plugin.test.ts @@ -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'; diff --git a/src/core/server/plugins/plugin_context.ts b/src/core/server/plugins/plugin_context.ts index 1bf2a71f0dcf4..55a152b4fe239 100644 --- a/src/core/server/plugins/plugin_context.ts +++ b/src/core/server/plugins/plugin_context.ts @@ -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'; diff --git a/src/core/server/plugins/plugins_service.ts b/src/core/server/plugins/plugins_service.ts index e63f733caa900..3b571b0b34dce 100644 --- a/src/core/server/plugins/plugins_service.ts +++ b/src/core/server/plugins/plugins_service.ts @@ -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'; diff --git a/src/core/server/plugins/plugins_system.test.ts b/src/core/server/plugins/plugins_system.test.ts index a820ffaa2d6c0..b2aa20e541b7b 100644 --- a/src/core/server/plugins/plugins_system.test.ts +++ b/src/core/server/plugins/plugins_system.test.ts @@ -17,7 +17,7 @@ * under the License. */ -import { CoreContext } from '../../types'; +import { CoreContext } from '../core_context'; const mockCreatePluginStartContext = jest.fn(); jest.mock('./plugin_context', () => ({ diff --git a/src/core/server/plugins/plugins_system.ts b/src/core/server/plugins/plugins_system.ts index 49dcfc6055cd9..229f3955d1e65 100644 --- a/src/core/server/plugins/plugins_system.ts +++ b/src/core/server/plugins/plugins_system.ts @@ -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'; diff --git a/src/core/types/index.ts b/src/core/types/index.ts index 2d8ed7a64885d..dfed05f32c049 100644 --- a/src/core/types/index.ts +++ b/src/core/types/index.ts @@ -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'; diff --git a/src/legacy/core_plugins/console/index.js b/src/legacy/core_plugins/console/index.js index 2a314c873de50..64d3c04db242c 100644 --- a/src/legacy/core_plugins/console/index.js +++ b/src/legacy/core_plugins/console/index.js @@ -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)); diff --git a/src/legacy/core_plugins/elasticsearch/index.js b/src/legacy/core_plugins/elasticsearch/index.js index 7eeea23b5473c..6abeceb300a0e 100644 --- a/src/legacy/core_plugins/elasticsearch/index.js +++ b/src/legacy/core_plugins/elasticsearch/index.js @@ -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]) => [ @@ -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, })); diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/__tests__/helpers/get_es_shard_timeout.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/__tests__/helpers/get_es_shard_timeout.js index b10005c35368d..a3174d0b2c6cf 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/__tests__/helpers/get_es_shard_timeout.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/__tests__/helpers/get_es_shard_timeout.js @@ -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) }) } } + } + } } } }; diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/get_es_shard_timeout.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/get_es_shard_timeout.js index da4ad46a563f3..7f3b8c157cda2 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/get_es_shard_timeout.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/get_es_shard_timeout.js @@ -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(); diff --git a/src/legacy/core_plugins/timelion/server/series_functions/__tests__/fixtures/tlConfig.js b/src/legacy/core_plugins/timelion/server/series_functions/__tests__/fixtures/tlConfig.js index e0f95eca2adf3..aaabfc420031a 100644 --- a/src/legacy/core_plugins/timelion/server/series_functions/__tests__/fixtures/tlConfig.js +++ b/src/legacy/core_plugins/timelion/server/series_functions/__tests__/fixtures/tlConfig.js @@ -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) }) } + } + } } }, }; diff --git a/src/legacy/core_plugins/timelion/server/series_functions/es/index.js b/src/legacy/core_plugins/timelion/server/series_functions/es/index.js index e3ede24ff7e48..c38d1c2a4af44 100644 --- a/src/legacy/core_plugins/timelion/server/series_functions/es/index.js +++ b/src/legacy/core_plugins/timelion/server/series_functions/es/index.js @@ -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(); diff --git a/src/legacy/server/config/complete.js b/src/legacy/server/config/complete.js index db9f1b888e250..fad8bcbe657cd 100644 --- a/src/legacy/server/config/complete.js +++ b/src/legacy/server/config/complete.js @@ -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, diff --git a/src/legacy/server/config/complete.test.js b/src/legacy/server/config/complete.test.js index a628a1e3f3c35..9d43567e98fcb 100644 --- a/src/legacy/server/config/complete.test.js +++ b/src/legacy/server/config/complete.test.js @@ -46,7 +46,9 @@ describe('server/config completeMixin()', function () { }; const kbnServer = { - core: { handledConfigPaths: [] }, + newPlatform: { + params: { handledConfigPaths: [] } + }, settings, server, config, diff --git a/src/legacy/server/core/index.ts b/src/legacy/server/core/index.ts index bb00c47b46a9d..de5391d33cada 100644 --- a/src/legacy/server/core/index.ts +++ b/src/legacy/server/core/index.ts @@ -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); } diff --git a/src/legacy/server/http/index.js b/src/legacy/server/http/index.js index d6247021ab069..5feab9c89493f 100644 --- a/src/legacy/server/http/index.js +++ b/src/legacy/server/http/index.js @@ -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); diff --git a/src/legacy/server/kbn_server.d.ts b/src/legacy/server/kbn_server.d.ts index 7f814f68f184d..5460e6ab5b53a 100644 --- a/src/legacy/server/kbn_server.d.ts +++ b/src/legacy/server/kbn_server.d.ts @@ -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'; @@ -55,9 +59,21 @@ declare module 'hapi' { } type KbnMixinFunc = (kbnServer: KbnServer, server: Server, config: any) => Promise | void; - +type Unpromise = T extends Promise ? 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>; + }; + }; public server: Server; public inject: Server['inject']; diff --git a/src/legacy/server/kbn_server.js b/src/legacy/server/kbn_server.js index 002cd9771ef25..a0426869d736e 100644 --- a/src/legacy/server/kbn_server.js +++ b/src/legacy/server/kbn_server.js @@ -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,