Skip to content

Commit

Permalink
Add migration support to the event log (#58010) (#58555)
Browse files Browse the repository at this point in the history
* Initial work

* Add missing file

* Add tests where missing

* Add kibana version to esNames

* Share ILM policy across versions
  • Loading branch information
mikecote authored Feb 26, 2020
1 parent 61173cd commit 8d52178
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ describe('createIndex', () => {
await clusterClientAdapter.createIndex('foo');
expect(clusterClient.callAsInternalUser).toHaveBeenCalledWith('indices.create', {
index: 'foo',
body: {},
});
});

Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ export class ClusterClientAdapter {
return result as boolean;
}

public async createIndex(name: string): Promise<void> {
public async createIndex(name: string, body: any = {}): Promise<void> {
try {
await this.callEs('indices.create', { index: name });
await this.callEs('indices.create', {
index: name,
body,
});
} catch (err) {
if (err.body?.error?.type !== 'resource_already_exists_exception') {
throw new Error(`error creating initial index: ${err.message}`);
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/event_log/server/es/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ describe('getIndexTemplate()', () => {

test('returns the correct details of the index template', () => {
const indexTemplate = getIndexTemplate(esNames);
expect(indexTemplate.index_patterns).toEqual([esNames.indexPattern]);
expect(indexTemplate.aliases[esNames.alias]).toEqual({});
expect(indexTemplate.index_patterns).toEqual([esNames.indexPatternWithVersion]);
expect(indexTemplate.settings.number_of_shards).toBeGreaterThanOrEqual(0);
expect(indexTemplate.settings.number_of_replicas).toBeGreaterThanOrEqual(0);
expect(indexTemplate.settings['index.lifecycle.name']).toBe(esNames.ilmPolicy);
Expand Down
5 changes: 1 addition & 4 deletions x-pack/plugins/event_log/server/es/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import mappings from '../../generated/mappings.json';
// returns the body of an index template used in an ES indices.putTemplate call
export function getIndexTemplate(esNames: EsNames) {
const indexTemplateBody: any = {
index_patterns: [esNames.indexPattern],
aliases: {
[esNames.alias]: {},
},
index_patterns: [esNames.indexPatternWithVersion],
settings: {
number_of_shards: 1,
number_of_replicas: 1,
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/event_log/server/es/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ class EsInitializationSteps {
async createInitialIndexIfNotExists(): Promise<void> {
const exists = await this.esContext.esAdapter.doesAliasExist(this.esContext.esNames.alias);
if (!exists) {
await this.esContext.esAdapter.createIndex(this.esContext.esNames.initialIndex);
await this.esContext.esAdapter.createIndex(this.esContext.esNames.initialIndex, {
aliases: {
[this.esContext.esNames.alias]: {
is_write_index: true,
},
},
});
}
}
}
7 changes: 4 additions & 3 deletions x-pack/plugins/event_log/server/es/names.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { EsNames } from './names';
const createNamesMock = () => {
const mock: jest.Mocked<EsNames> = {
base: '.kibana',
alias: '.kibana-event-log',
alias: '.kibana-event-log-8.0.0',
ilmPolicy: '.kibana-event-log-policy',
indexPattern: '.kibana-event-log-*',
initialIndex: '.kibana-event-log-000001',
indexTemplate: '.kibana-event-log-template',
indexPatternWithVersion: '.kibana-event-log-8.0.0-*',
initialIndex: '.kibana-event-log-8.0.0-000001',
indexTemplate: '.kibana-event-log-8.0.0-template',
};
return mock;
};
Expand Down
12 changes: 9 additions & 3 deletions x-pack/plugins/event_log/server/es/names.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@

import { getEsNames } from './names';

jest.mock('../lib/../../../../package.json', () => ({
version: '1.2.3',
}));

describe('getEsNames()', () => {
test('works as expected', () => {
const base = 'XYZ';
const version = '1.2.3';
const esNames = getEsNames(base);
expect(esNames.base).toEqual(base);
expect(esNames.alias).toEqual(`${base}-event-log`);
expect(esNames.alias).toEqual(`${base}-event-log-${version}`);
expect(esNames.ilmPolicy).toEqual(`${base}-event-log-policy`);
expect(esNames.indexPattern).toEqual(`${base}-event-log-*`);
expect(esNames.initialIndex).toEqual(`${base}-event-log-000001`);
expect(esNames.indexTemplate).toEqual(`${base}-event-log-template`);
expect(esNames.indexPatternWithVersion).toEqual(`${base}-event-log-${version}-*`);
expect(esNames.initialIndex).toEqual(`${base}-event-log-${version}-000001`);
expect(esNames.indexTemplate).toEqual(`${base}-event-log-${version}-template`);
});
});
14 changes: 10 additions & 4 deletions x-pack/plugins/event_log/server/es/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@
* you may not use this file except in compliance with the Elastic License.
*/

const EVENT_LOG_NAME_SUFFIX = '-event-log';
import xPackage from '../../../../package.json';

const EVENT_LOG_NAME_SUFFIX = `-event-log`;
const EVENT_LOG_VERSION_SUFFIX = `-${xPackage.version}`;

export interface EsNames {
base: string;
alias: string;
ilmPolicy: string;
indexPattern: string;
indexPatternWithVersion: string;
initialIndex: string;
indexTemplate: string;
}

export function getEsNames(baseName: string): EsNames {
const eventLogName = `${baseName}${EVENT_LOG_NAME_SUFFIX}`;
const eventLogNameWithVersion = `${eventLogName}${EVENT_LOG_VERSION_SUFFIX}`;
return {
base: baseName,
alias: eventLogName,
alias: eventLogNameWithVersion,
ilmPolicy: `${eventLogName}-policy`,
indexPattern: `${eventLogName}-*`,
initialIndex: `${eventLogName}-000001`,
indexTemplate: `${eventLogName}-template`,
indexPatternWithVersion: `${eventLogNameWithVersion}-*`,
initialIndex: `${eventLogNameWithVersion}-000001`,
indexTemplate: `${eventLogNameWithVersion}-template`,
};
}

0 comments on commit 8d52178

Please sign in to comment.