Skip to content

Commit df62729

Browse files
[Ingest] check and create default indices during setup (#64809) (#65400)
* 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 # Conflicts: # x-pack/plugins/ingest_manager/common/constants/epm.ts Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 83a8137 commit df62729

File tree

6 files changed

+128
-23
lines changed

6 files changed

+128
-23
lines changed

x-pack/plugins/ingest_manager/common/constants/epm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
export const PACKAGES_SAVED_OBJECT_TYPE = 'epm-packages';
88
export const INDEX_PATTERN_SAVED_OBJECT_TYPE = 'index-pattern';
99
export const DEFAULT_REGISTRY_URL = 'https://epr-experimental.elastic.co';
10+
export const INDEX_PATTERN_PLACEHOLDER_SUFFIX = '-index_pattern_placeholder';

x-pack/plugins/ingest_manager/server/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export {
99
AGENT_TYPE_TEMPORARY,
1010
AGENT_POLLING_THRESHOLD_MS,
1111
AGENT_POLLING_INTERVAL,
12+
INDEX_PATTERN_PLACEHOLDER_SUFFIX,
1213
// Routes
1314
PLUGIN_ID,
1415
EPM_API_ROUTES,

x-pack/plugins/ingest_manager/server/services/epm/kibana/index_pattern/__snapshots__/install.test.ts.snap

Lines changed: 67 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugins/ingest_manager/server/services/epm/kibana/index_pattern/install.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
*/
66

77
import { SavedObjectsClientContract } from 'src/core/server';
8-
import { INDEX_PATTERN_SAVED_OBJECT_TYPE } from '../../../../constants';
8+
import {
9+
INDEX_PATTERN_SAVED_OBJECT_TYPE,
10+
INDEX_PATTERN_PLACEHOLDER_SUFFIX,
11+
} from '../../../../constants';
912
import * as Registry from '../../registry';
1013
import { loadFieldsFromYaml, Fields, Field } from '../../fields/field';
1114
import { getPackageKeysByStatus } from '../../packages/get';
12-
import { InstallationStatus, RegistryPackage } from '../../../../types';
15+
import { InstallationStatus, RegistryPackage, CallESAsCurrentUser } from '../../../../types';
1316

1417
interface FieldFormatMap {
1518
[key: string]: FieldFormatMapItem;
@@ -63,6 +66,7 @@ export interface IndexPatternField {
6366
enabled?: boolean;
6467
script?: string;
6568
lang?: string;
69+
readFromDocValues: boolean;
6670
}
6771
export enum IndexPatternType {
6872
logs = 'logs',
@@ -234,6 +238,7 @@ export const transformField = (field: Field, i: number, fields: Fields): IndexPa
234238
searchable: field.searchable ?? true,
235239
aggregatable: field.aggregatable ?? true,
236240
doc_values: field.doc_values ?? true,
241+
readFromDocValues: field.doc_values ?? true,
237242
};
238243

239244
// if type exists, check if it exists in the map
@@ -251,6 +256,7 @@ export const transformField = (field: Field, i: number, fields: Fields): IndexPa
251256
newField.aggregatable = false;
252257
newField.analyzed = false;
253258
newField.doc_values = field.doc_values ?? false;
259+
newField.readFromDocValues = field.doc_values ?? false;
254260
newField.indexed = false;
255261
newField.searchable = false;
256262
}
@@ -262,6 +268,7 @@ export const transformField = (field: Field, i: number, fields: Fields): IndexPa
262268
newField.aggregatable = false;
263269
newField.analyzed = false;
264270
newField.doc_values = false;
271+
newField.readFromDocValues = false;
265272
newField.indexed = false;
266273
newField.searchable = false;
267274
}
@@ -276,6 +283,7 @@ export const transformField = (field: Field, i: number, fields: Fields): IndexPa
276283
newField.script = field.script;
277284
newField.lang = 'painless';
278285
newField.doc_values = false;
286+
newField.readFromDocValues = false;
279287
}
280288

281289
return newField;
@@ -357,3 +365,42 @@ const getFieldFormatParams = (field: Field): FieldFormatParams => {
357365
if (field.open_link_in_current_tab) params.openLinkInCurrentTab = field.open_link_in_current_tab;
358366
return params;
359367
};
368+
369+
export const ensureDefaultIndices = async (callCluster: CallESAsCurrentUser) =>
370+
// create placeholder indices to supress errors in the kibana Dashboards app
371+
// that no matching indices exist https://github.com/elastic/kibana/issues/62343
372+
Promise.all(
373+
Object.keys(IndexPatternType).map(async indexPattern => {
374+
const defaultIndexPatternName = indexPattern + INDEX_PATTERN_PLACEHOLDER_SUFFIX;
375+
const indexExists = await doesIndexExist(defaultIndexPatternName, callCluster);
376+
if (!indexExists) {
377+
try {
378+
await callCluster('transport.request', {
379+
method: 'PUT',
380+
path: `/${defaultIndexPatternName}`,
381+
body: {
382+
mappings: {
383+
properties: {
384+
'@timestamp': { type: 'date' },
385+
},
386+
},
387+
},
388+
});
389+
} catch (putErr) {
390+
throw new Error(`${defaultIndexPatternName} could not be created`);
391+
}
392+
}
393+
})
394+
);
395+
396+
export const doesIndexExist = async (indexName: string, callCluster: CallESAsCurrentUser) => {
397+
try {
398+
await callCluster('transport.request', {
399+
method: 'HEAD',
400+
path: indexName,
401+
});
402+
return true;
403+
} catch (err) {
404+
return false;
405+
}
406+
};

x-pack/plugins/ingest_manager/server/services/epm/kibana/index_pattern/tests/test_data.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const dupeFields: IndexPatternField[] = [
1515
count: 0,
1616
indexed: true,
1717
doc_values: true,
18+
readFromDocValues: true,
1819
scripted: false,
1920
analyzed: true,
2021
},
@@ -26,6 +27,7 @@ export const dupeFields: IndexPatternField[] = [
2627
count: 0,
2728
indexed: true,
2829
doc_values: true,
30+
readFromDocValues: true,
2931
scripted: false,
3032
analyzed: true,
3133
},
@@ -37,6 +39,7 @@ export const dupeFields: IndexPatternField[] = [
3739
count: 0,
3840
indexed: true,
3941
doc_values: true,
42+
readFromDocValues: true,
4043
scripted: false,
4144
analyzed: true,
4245
},
@@ -48,6 +51,7 @@ export const dupeFields: IndexPatternField[] = [
4851
count: 2,
4952
indexed: true,
5053
doc_values: true,
54+
readFromDocValues: true,
5155
scripted: false,
5256
analyzed: true,
5357
},
@@ -59,6 +63,7 @@ export const dupeFields: IndexPatternField[] = [
5963
count: 0,
6064
indexed: true,
6165
doc_values: true,
66+
readFromDocValues: true,
6267
scripted: false,
6368
analyzed: true,
6469
},
@@ -70,6 +75,7 @@ export const dupeFields: IndexPatternField[] = [
7075
count: 0,
7176
indexed: true,
7277
doc_values: true,
78+
readFromDocValues: true,
7379
scripted: false,
7480
analyzed: true,
7581
},
@@ -81,6 +87,7 @@ export const dupeFields: IndexPatternField[] = [
8187
count: 0,
8288
indexed: true,
8389
doc_values: true,
90+
readFromDocValues: true,
8491
scripted: false,
8592
analyzed: true,
8693
},
@@ -92,6 +99,7 @@ export const dupeFields: IndexPatternField[] = [
9299
count: 1,
93100
indexed: true,
94101
doc_values: true,
102+
readFromDocValues: true,
95103
scripted: false,
96104
analyzed: false,
97105
},

x-pack/plugins/ingest_manager/server/services/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CallESAsCurrentUser } from '../types';
1111
import { agentConfigService } from './agent_config';
1212
import { outputService } from './output';
1313
import { ensureInstalledDefaultPackages } from './epm/packages/install';
14+
import { ensureDefaultIndices } from './epm/kibana/index_pattern/install';
1415
import {
1516
packageToConfigDatasource,
1617
Datasource,
@@ -38,6 +39,7 @@ export async function setupIngestManager(
3839
ensureInstalledDefaultPackages(soClient, callCluster),
3940
outputService.ensureDefaultOutput(soClient),
4041
agentConfigService.ensureDefaultAgentConfig(soClient),
42+
ensureDefaultIndices(callCluster),
4143
settingsService.getSettings(soClient).catch((e: any) => {
4244
if (e.isBoom && e.output.statusCode === 404) {
4345
const http = appContextService.getHttpSetup();

0 commit comments

Comments
 (0)