Skip to content

Commit

Permalink
Infra server NP shim + config/routing API adoption (#45299)
Browse files Browse the repository at this point in the history
* Basic cleanup before refactoring for shim work

* shim WIP

* Removes the configuration adapter

* WIP more stuff

* WIP refactoring of shimming work

* WIP continues

* Logging UI now runs on top of new platform shim

* WIP continues

* Removes unused imports and variables

* Basic infra NP server shim in place

* Reimplemented graphql http error handling for infra NP server shim

* Adds new platform infra plugin to handle NP config for legacy server shim

* Basic cleanup before refactoring for shim work

* shim WIP

* Removes the configuration adapter

* WIP more stuff

* WIP refactoring of shimming work

* WIP continues

* Logging UI now runs on top of new platform shim

* WIP continues

* Removes unused imports and variables

* Basic infra NP server shim in place

* Reimplemented graphql http error handling for infra NP server shim

* Adds new platform infra plugin to handle NP config for legacy server shim

* Adds comment about duplicating full config for NP config

* Use New Platform features plugin to registerFeature()

* Re-arranging and relying on request context as uch as possible

* Refactors KibanaRequest for RequestHandlerContext

* fixes types for callWithRequest

* Moves callWithRequest method override types directly into class to get them working, need to fix this when we understand it better

* Fixes callWithRequest framework types

* Removes a few NP_TODO comments

* Fix broken imports

* Ensure GraphQL resolvers are actually passed requestContext and not the raw request, and switch to the savedObjects client via requestContext

* Remove the legacy traces of the savedObjects plugin

* Fixes TSVB access with NP raw requests and requestContext

* Remove unused getUiSettingsService (moved to requestContext)

* Migrate to new Spaces plugin

* Fix calculateMetricInterval after merged changes

* Reinstate and migrate the infrastructure metadata route

* Fix various type check errors

* Amend InfraSources lib unit tests

Mock the savedObjects client differently

* Amend MetricsExplorer API response

Renaming of variable inadvertently broke the response

* Remove GraphQLI references from feature controls tests

* Remove other GraphiQL references

* Fix security / access issue

* Add a framework level registerRoute method which always adds access tags by default

* *Temp* disable test

* Migrate the log rate validation endpoint to the new platform

Fully migrates the [Logs UI] log rate setup index validation #50008 PR to New Platform routing etc

* Amend types

* Example of how to expose APM get indices method in NP

* Fix calls to TSVB bug caused by object mutation
This is a temp fix as the TSVB NP migration will supercede this

* Converts getApmIndices function to accept saved object client, implements usage in infra

* Fix APM setup_request tests

* Fixes some unused references for linting

* Migrate all work from #50730 to NP

* Remove duplicate declaration files for rison_node and add a single source of truth at x-pack/typings/rison_node.d.ts for x-pack uses

* Moved type file back into infra plugin to bypass strange break

* Updates apm indices method signature per feedback from @elastic/apm-ui
  • Loading branch information
jasonrhodes authored Dec 6, 2019
1 parent 3368ce0 commit ab5913d
Show file tree
Hide file tree
Showing 65 changed files with 1,263 additions and 1,236 deletions.
5 changes: 4 additions & 1 deletion x-pack/legacy/plugins/apm/server/lib/helpers/es_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ async function getParamsForSearchRequest(
) {
const { uiSettings } = context.core;
const [indices, includeFrozen] = await Promise.all([
getApmIndices(context),
getApmIndices({
savedObjectsClient: context.core.savedObjects.client,
config: context.config
}),
uiSettings.client.get('search:includeFrozen')
]);

Expand Down
10 changes: 10 additions & 0 deletions x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ function getMockRequest() {
client: {
get: jest.fn().mockResolvedValue(false)
}
},
savedObjects: {
client: {
get: jest.fn()
}
}
}
} as unknown) as APMRequestHandlerContext & {
Expand All @@ -65,6 +70,11 @@ function getMockRequest() {
get: jest.Mock<any, any>;
};
};
savedObjects: {
client: {
get: jest.Mock<any, any>;
};
};
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export async function setupRequest<TParams extends SetupRequestParams>(
const { config } = context;
const { query } = context.params;

const indices = await getApmIndices(context);
const indices = await getApmIndices({
savedObjectsClient: context.core.savedObjects.client,
config
});

const dynamicIndexPattern = await getDynamicIndexPattern({
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,25 @@ export function getApmIndicesConfig(config: APMConfig): ApmIndicesConfig {
};
}

export async function getApmIndices(context: APMRequestHandlerContext) {
// export async function getApmIndices(context: APMRequestHandlerContext) {
// return _getApmIndices(context.core, context.config);
// }

export async function getApmIndices({
config,
savedObjectsClient
}: {
config: APMConfig;
savedObjectsClient: SavedObjectsClientContract;
}) {
try {
const apmIndicesSavedObject = await getApmIndicesSavedObject(
context.core.savedObjects.client
savedObjectsClient
);
const apmIndicesConfig = getApmIndicesConfig(context.config);
const apmIndicesConfig = getApmIndicesConfig(config);
return merge({}, apmIndicesConfig, apmIndicesSavedObject);
} catch (error) {
return getApmIndicesConfig(context.config);
return getApmIndicesConfig(config);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const apmIndicesRoute = createRoute(() => ({
method: 'GET',
path: '/api/apm/settings/apm-indices',
handler: async ({ context }) => {
return await getApmIndices(context);
return await getApmIndices({
savedObjectsClient: context.core.savedObjects.client,
config: context.config
});
}
}));

Expand Down
4 changes: 1 addition & 3 deletions x-pack/legacy/plugins/infra/common/http_api/metadata_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import * as rt from 'io-ts';
import { InfraWrappableRequest } from '../../server/lib/adapters/framework';

export const InfraMetadataNodeTypeRT = rt.keyof({
host: null,
Expand Down Expand Up @@ -67,6 +66,7 @@ export const InfraMetadataInfoRT = rt.partial({
});

const InfraMetadataRequiredRT = rt.type({
id: rt.string,
name: rt.string,
features: rt.array(InfraMetadataFeatureRT),
});
Expand All @@ -81,8 +81,6 @@ export type InfraMetadata = rt.TypeOf<typeof InfraMetadataRT>;

export type InfraMetadataRequest = rt.TypeOf<typeof InfraMetadataRequestRT>;

export type InfraMetadataWrappedRequest = InfraWrappableRequest<InfraMetadataRequest>;

export type InfraMetadataFeature = rt.TypeOf<typeof InfraMetadataFeatureRT>;

export type InfraMetadataInfo = rt.TypeOf<typeof InfraMetadataInfoRT>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import * as rt from 'io-ts';
import { InventoryMetricRT, ItemTypeRT } from '../inventory_models/types';
import { InfraWrappableRequest } from '../../server/lib/adapters/framework';
import { InfraTimerangeInputRT } from './snapshot_api';

const NodeDetailsDataPointRT = rt.intersection([
Expand Down Expand Up @@ -53,6 +52,4 @@ export const NodeDetailsRequestRT = rt.intersection([
// export type NodeDetailsRequest = InfraWrappableRequest<NodesArgs & SourceArgs>;

export type NodeDetailsRequest = rt.TypeOf<typeof NodeDetailsRequestRT>;
export type NodeDetailsWrappedRequest = InfraWrappableRequest<NodeDetailsRequest>;

export type NodeDetailsMetricDataResponse = rt.TypeOf<typeof NodeDetailsMetricDataResponseRT>;
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/infra/common/http_api/snapshot_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import * as rt from 'io-ts';
import { InfraWrappableRequest } from '../../server/lib/adapters/framework';
import { SnapshotMetricTypeRT, ItemTypeRT } from '../inventory_models/types';

export const SnapshotNodePathRT = rt.intersection([
Expand Down Expand Up @@ -64,6 +63,5 @@ export const SnapshotRequestRT = rt.intersection([
]);

export type SnapshotRequest = rt.TypeOf<typeof SnapshotRequestRT>;
export type SnapshotWrappedRequest = InfraWrappableRequest<SnapshotRequest>;
export type SnapshotNode = rt.TypeOf<typeof SnapshotNodeRT>;
export type SnapshotNodeResponse = rt.TypeOf<typeof SnapshotNodeResponseRT>;
58 changes: 53 additions & 5 deletions x-pack/legacy/plugins/infra/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
import { i18n } from '@kbn/i18n';
import JoiNamespace from 'joi';
import { resolve } from 'path';

import { getConfigSchema, initServerWithKibana } from './server/kibana.index';
import { PluginInitializerContext } from 'src/core/server';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import KbnServer from 'src/legacy/server/kbn_server';
import { getConfigSchema } from './server/kibana.index';
import { savedObjectMappings } from './server/saved_objects';
import { plugin, InfraServerPluginDeps } from './server/new_platform_index';
import { InfraSetup } from '../../../plugins/infra/server';
import { APMPluginContract } from '../../../plugins/apm/server/plugin';

const APP_ID = 'infra';
const logsSampleDataLinkLabel = i18n.translate('xpack.infra.sampleDataLinkLabel', {
Expand Down Expand Up @@ -70,9 +75,52 @@ export function infra(kibana: any) {
config(Joi: typeof JoiNamespace) {
return getConfigSchema(Joi);
},
init(server: any) {
initServerWithKibana(server);
server.addAppLinksToSampleDataset('logs', [
init(legacyServer: any) {
const { newPlatform } = legacyServer as KbnServer;
const { core, plugins } = newPlatform.setup;

const infraSetup = (plugins.infra as unknown) as InfraSetup; // chef's kiss

const initContext = ({
config: infraSetup.__legacy.config,
} as unknown) as PluginInitializerContext;
// NP_TODO: Use real types from the other plugins as they are migrated
const pluginDeps: InfraServerPluginDeps = {
usageCollection: plugins.usageCollection as UsageCollectionSetup,
indexPatterns: {
indexPatternsServiceFactory: legacyServer.indexPatternsServiceFactory,
},
metrics: legacyServer.plugins.metrics,
spaces: plugins.spaces,
features: plugins.features,
// NP_NOTE: [TSVB_GROUP] Huge hack to make TSVB (getVisData()) work with raw requests that
// originate from the New Platform router (and are very different to the old request object).
// Once TSVB has migrated over to NP, and can work with the new raw requests, or ideally just
// the requestContext, this can be removed.
___legacy: {
tsvb: {
elasticsearch: legacyServer.plugins.elasticsearch,
__internals: legacyServer.newPlatform.__internals,
},
},
apm: plugins.apm as APMPluginContract,
};

const infraPluginInstance = plugin(initContext);
infraPluginInstance.setup(core, pluginDeps);

// NP_TODO: EVERYTHING BELOW HERE IS LEGACY

const libs = infraPluginInstance.getLibs();

// NP_TODO how do we replace this? Answer: return from setup function.
legacyServer.expose(
'defineInternalSourceConfiguration',
libs.sources.defineInternalSourceConfiguration.bind(libs.sources)
);

// NP_TODO: How do we move this to new platform?
legacyServer.addAppLinksToSampleDataset('logs', [
{
path: `/app/${APP_ID}#/logs`,
label: logsSampleDataLinkLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
const ROOT_ELEMENT_ID = 'react-infra-root';
const BREADCRUMBS_ELEMENT_ID = 'react-infra-breadcrumbs';

export class InfraKibanaFrameworkAdapter implements InfraFrameworkAdapter {
export class KibanaFramework implements InfraFrameworkAdapter {
public appState: object;
public kbnVersion?: string;
public timezone?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { HttpLink } from 'apollo-link-http';
import { withClientState } from 'apollo-link-state';
import { InfraFrontendLibs } from '../lib';
import introspectionQueryResultData from '../../graphql/introspection.json';
import { InfraKibanaFrameworkAdapter } from '../adapters/framework/kibana_framework_adapter';
import { KibanaFramework } from '../adapters/framework/kibana_framework_adapter';
import { InfraKibanaObservableApiAdapter } from '../adapters/observable_api/kibana_observable_api';

export function compose(): InfraFrontendLibs {
Expand Down Expand Up @@ -57,7 +57,7 @@ export function compose(): InfraFrontendLibs {

const infraModule = uiModules.get('app/infa');

const framework = new InfraKibanaFrameworkAdapter(infraModule, uiRoutes, timezoneProvider);
const framework = new KibanaFramework(infraModule, uiRoutes, timezoneProvider);

const libs: InfraFrontendLibs = {
apolloClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { InMemoryCache } from 'apollo-cache-inmemory';
import ApolloClient from 'apollo-client';
import { SchemaLink } from 'apollo-link-schema';
import { addMockFunctionsToSchema, makeExecutableSchema } from 'graphql-tools';
import { InfraKibanaFrameworkAdapter } from '../adapters/framework/kibana_framework_adapter';
import { KibanaFramework } from '../adapters/framework/kibana_framework_adapter';
import { InfraKibanaObservableApiAdapter } from '../adapters/observable_api/kibana_observable_api';
import { InfraFrontendLibs } from '../lib';

Expand All @@ -27,7 +27,7 @@ export function compose(): InfraFrontendLibs {
basePath: chrome.getBasePath(),
xsrfToken: chrome.getXsrfToken(),
});
const framework = new InfraKibanaFrameworkAdapter(infraModule, uiRoutes, timezoneProvider);
const framework = new KibanaFramework(infraModule, uiRoutes, timezoneProvider);
const typeDefs = `
Query {}
`;
Expand Down
65 changes: 65 additions & 0 deletions x-pack/legacy/plugins/infra/server/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';

export const METRICS_FEATURE = {
id: 'infrastructure',
name: i18n.translate('xpack.infra.featureRegistry.linkInfrastructureTitle', {
defaultMessage: 'Infrastructure',
}),
icon: 'infraApp',
navLinkId: 'infra:home',
app: ['infra', 'kibana'],
catalogue: ['infraops'],
privileges: {
all: {
api: ['infra'],
savedObject: {
all: ['infrastructure-ui-source'],
read: ['index-pattern'],
},
ui: ['show', 'configureSource', 'save'],
},
read: {
api: ['infra'],
savedObject: {
all: [],
read: ['infrastructure-ui-source', 'index-pattern'],
},
ui: ['show'],
},
},
};

export const LOGS_FEATURE = {
id: 'logs',
name: i18n.translate('xpack.infra.featureRegistry.linkLogsTitle', {
defaultMessage: 'Logs',
}),
icon: 'loggingApp',
navLinkId: 'infra:logs',
app: ['infra', 'kibana'],
catalogue: ['infralogging'],
privileges: {
all: {
api: ['infra'],
savedObject: {
all: ['infrastructure-ui-source'],
read: [],
},
ui: ['show', 'configureSource', 'save'],
},
read: {
api: ['infra'],
savedObject: {
all: [],
read: ['infrastructure-ui-source'],
},
ui: ['show'],
},
},
};
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/infra/server/infra_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const initInfraServer = (libs: InfraBackendLibs) => {
typeDefs: schemas,
});

libs.framework.registerGraphQLEndpoint('/api/infra/graphql', schema);
libs.framework.registerGraphQLEndpoint('/graphql', schema);

initIpToHostName(libs);
initLogAnalysisGetLogEntryRateRoute(libs);
Expand Down
Loading

0 comments on commit ab5913d

Please sign in to comment.