diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts index 7237d62dca6e2..9fc8afd356043 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts @@ -84,6 +84,30 @@ describe('KibanaMigrator', () => { const migrationResults = await new KibanaMigrator({ kbnServer }).awaitMigration(); expect(migrationResults.length).toEqual(2); }); + + it('only handles and deletes index templates once', async () => { + const { kbnServer } = mockKbnServer(); + const clusterStub = jest.fn(() => ({ status: 404 })); + const waitUntilReady = jest.fn(async () => undefined); + + kbnServer.server.plugins.elasticsearch = { + waitUntilReady, + getCluster() { + return { + callWithInternalUser: clusterStub, + }; + }, + }; + + await new KibanaMigrator({ kbnServer }).awaitMigration(); + + // callCluster with "cat.templates" is called by "deleteIndexTemplates" function + // and should only be done once + const callClusterCommands = clusterStub.mock.calls + .map(([callClusterPath]) => callClusterPath) + .filter(callClusterPath => callClusterPath === 'cat.templates'); + expect(callClusterCommands.length).toBe(1); + }); }); }); diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts index f51c15f882a4e..0c2cb768fc011 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts @@ -93,8 +93,9 @@ export class KibanaMigrator { await server.plugins.elasticsearch.waitUntilReady(); const config = server.config(); + const kibanaIndexName = config.get('kibana.index'); const indexMap = createIndexMap( - config.get('kibana.index'), + kibanaIndexName, this.kbnServer.uiExports.savedObjectSchemas, this.mappingProperties ); @@ -110,7 +111,9 @@ export class KibanaMigrator { pollInterval: config.get('migrations.pollInterval'), scrollDuration: config.get('migrations.scrollDuration'), serializer: this.serializer, - obsoleteIndexTemplatePattern: 'kibana_index_template*', + // Only necessary for the migrator of the kibana index. + obsoleteIndexTemplatePattern: + index === kibanaIndexName ? 'kibana_index_template*' : undefined, convertToAliasScript: indexMap[index].script, }); });