Skip to content

Commit

Permalink
[Ingest] check and create default indices during setup (#64809)
Browse files Browse the repository at this point in the history
* check and create default indices during setup

* fix typos

* add kibana issue

* add date mapping

* add removeDocValues to each field to stop apps from creating a new index pattern

* update snapshot

* add property to test data
  • Loading branch information
neptunian committed May 5, 2020
1 parent a1f8ad0 commit daceebb
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 23 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/ingest_manager/common/constants/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
export const PACKAGES_SAVED_OBJECT_TYPE = 'epm-packages';
export const INDEX_PATTERN_SAVED_OBJECT_TYPE = 'index-pattern';
export const DEFAULT_REGISTRY_URL = 'https://epr.elastic.co';
export const INDEX_PATTERN_PLACEHOLDER_SUFFIX = '-index_pattern_placeholder';
1 change: 1 addition & 0 deletions x-pack/plugins/ingest_manager/server/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
AGENT_TYPE_TEMPORARY,
AGENT_POLLING_THRESHOLD_MS,
AGENT_POLLING_INTERVAL,
INDEX_PATTERN_PLACEHOLDER_SUFFIX,
// Routes
PLUGIN_ID,
EPM_API_ROUTES,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
*/

import { SavedObjectsClientContract } from 'src/core/server';
import { INDEX_PATTERN_SAVED_OBJECT_TYPE } from '../../../../constants';
import {
INDEX_PATTERN_SAVED_OBJECT_TYPE,
INDEX_PATTERN_PLACEHOLDER_SUFFIX,
} from '../../../../constants';
import * as Registry from '../../registry';
import { loadFieldsFromYaml, Fields, Field } from '../../fields/field';
import { getPackageKeysByStatus } from '../../packages/get';
import { InstallationStatus, RegistryPackage } from '../../../../types';
import { InstallationStatus, RegistryPackage, CallESAsCurrentUser } from '../../../../types';

interface FieldFormatMap {
[key: string]: FieldFormatMapItem;
Expand Down Expand Up @@ -63,6 +66,7 @@ export interface IndexPatternField {
enabled?: boolean;
script?: string;
lang?: string;
readFromDocValues: boolean;
}
export enum IndexPatternType {
logs = 'logs',
Expand Down Expand Up @@ -234,6 +238,7 @@ export const transformField = (field: Field, i: number, fields: Fields): IndexPa
searchable: field.searchable ?? true,
aggregatable: field.aggregatable ?? true,
doc_values: field.doc_values ?? true,
readFromDocValues: field.doc_values ?? true,
};

// if type exists, check if it exists in the map
Expand All @@ -251,6 +256,7 @@ export const transformField = (field: Field, i: number, fields: Fields): IndexPa
newField.aggregatable = false;
newField.analyzed = false;
newField.doc_values = field.doc_values ?? false;
newField.readFromDocValues = field.doc_values ?? false;
newField.indexed = false;
newField.searchable = false;
}
Expand All @@ -262,6 +268,7 @@ export const transformField = (field: Field, i: number, fields: Fields): IndexPa
newField.aggregatable = false;
newField.analyzed = false;
newField.doc_values = false;
newField.readFromDocValues = false;
newField.indexed = false;
newField.searchable = false;
}
Expand All @@ -276,6 +283,7 @@ export const transformField = (field: Field, i: number, fields: Fields): IndexPa
newField.script = field.script;
newField.lang = 'painless';
newField.doc_values = false;
newField.readFromDocValues = false;
}

return newField;
Expand Down Expand Up @@ -357,3 +365,42 @@ const getFieldFormatParams = (field: Field): FieldFormatParams => {
if (field.open_link_in_current_tab) params.openLinkInCurrentTab = field.open_link_in_current_tab;
return params;
};

export const ensureDefaultIndices = async (callCluster: CallESAsCurrentUser) =>
// create placeholder indices to supress errors in the kibana Dashboards app
// that no matching indices exist https://github.com/elastic/kibana/issues/62343
Promise.all(
Object.keys(IndexPatternType).map(async indexPattern => {
const defaultIndexPatternName = indexPattern + INDEX_PATTERN_PLACEHOLDER_SUFFIX;
const indexExists = await doesIndexExist(defaultIndexPatternName, callCluster);
if (!indexExists) {
try {
await callCluster('transport.request', {
method: 'PUT',
path: `/${defaultIndexPatternName}`,
body: {
mappings: {
properties: {
'@timestamp': { type: 'date' },
},
},
},
});
} catch (putErr) {
throw new Error(`${defaultIndexPatternName} could not be created`);
}
}
})
);

export const doesIndexExist = async (indexName: string, callCluster: CallESAsCurrentUser) => {
try {
await callCluster('transport.request', {
method: 'HEAD',
path: indexName,
});
return true;
} catch (err) {
return false;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const dupeFields: IndexPatternField[] = [
count: 0,
indexed: true,
doc_values: true,
readFromDocValues: true,
scripted: false,
analyzed: true,
},
Expand All @@ -26,6 +27,7 @@ export const dupeFields: IndexPatternField[] = [
count: 0,
indexed: true,
doc_values: true,
readFromDocValues: true,
scripted: false,
analyzed: true,
},
Expand All @@ -37,6 +39,7 @@ export const dupeFields: IndexPatternField[] = [
count: 0,
indexed: true,
doc_values: true,
readFromDocValues: true,
scripted: false,
analyzed: true,
},
Expand All @@ -48,6 +51,7 @@ export const dupeFields: IndexPatternField[] = [
count: 2,
indexed: true,
doc_values: true,
readFromDocValues: true,
scripted: false,
analyzed: true,
},
Expand All @@ -59,6 +63,7 @@ export const dupeFields: IndexPatternField[] = [
count: 0,
indexed: true,
doc_values: true,
readFromDocValues: true,
scripted: false,
analyzed: true,
},
Expand All @@ -70,6 +75,7 @@ export const dupeFields: IndexPatternField[] = [
count: 0,
indexed: true,
doc_values: true,
readFromDocValues: true,
scripted: false,
analyzed: true,
},
Expand All @@ -81,6 +87,7 @@ export const dupeFields: IndexPatternField[] = [
count: 0,
indexed: true,
doc_values: true,
readFromDocValues: true,
scripted: false,
analyzed: true,
},
Expand All @@ -92,6 +99,7 @@ export const dupeFields: IndexPatternField[] = [
count: 1,
indexed: true,
doc_values: true,
readFromDocValues: true,
scripted: false,
analyzed: false,
},
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/ingest_manager/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CallESAsCurrentUser } from '../types';
import { agentConfigService } from './agent_config';
import { outputService } from './output';
import { ensureInstalledDefaultPackages } from './epm/packages/install';
import { ensureDefaultIndices } from './epm/kibana/index_pattern/install';
import {
packageToConfigDatasource,
Datasource,
Expand Down Expand Up @@ -38,6 +39,7 @@ export async function setupIngestManager(
ensureInstalledDefaultPackages(soClient, callCluster),
outputService.ensureDefaultOutput(soClient),
agentConfigService.ensureDefaultAgentConfig(soClient),
ensureDefaultIndices(callCluster),
settingsService.getSettings(soClient).catch((e: any) => {
if (e.isBoom && e.output.statusCode === 404) {
const http = appContextService.getHttpSetup();
Expand Down

0 comments on commit daceebb

Please sign in to comment.