Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ingest] check and create default indices during setup #64809

Merged
merged 7 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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;
}
};
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 @@ -10,6 +10,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 All @@ -36,6 +37,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) {
return settingsService.saveSettings(soClient, {
Expand Down