Skip to content

Commit

Permalink
[8.x] [UII] Make constant keyword backfill optional (#192921) (#192940)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[UII] Make constant keyword backfill optional
(#192921)](#192921)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Jen
Huang","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-14T02:34:46Z","message":"[UII]
Make constant keyword backfill optional (#192921)\n\n##
Summary\r\n\r\nFollow up to #188145. In some edge
cases\r\n(elastic/sdh-beats#5156), users could
override\r\nthe index template used by integration data streams. It is
possible to\r\ncreate an index template without mappings, this
causes\r\n`fillConstantKeywordValues` to receive an undefined object
when\r\nupgrading the integration, and the upgrade then
fails.\r\n\r\nThis PR makes the backfill operation here more
fail-safe.","sha":"b5abc4ec7e308815b0f338f4c836a9caf3ee48a3","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-major"],"title":"[UII]
Make constant keyword backfill
optional","number":192921,"url":"https://github.com/elastic/kibana/pull/192921","mergeCommit":{"message":"[UII]
Make constant keyword backfill optional (#192921)\n\n##
Summary\r\n\r\nFollow up to #188145. In some edge
cases\r\n(elastic/sdh-beats#5156), users could
override\r\nthe index template used by integration data streams. It is
possible to\r\ncreate an index template without mappings, this
causes\r\n`fillConstantKeywordValues` to receive an undefined object
when\r\nupgrading the integration, and the upgrade then
fails.\r\n\r\nThis PR makes the backfill operation here more
fail-safe.","sha":"b5abc4ec7e308815b0f338f4c836a9caf3ee48a3"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192921","number":192921,"mergeCommit":{"message":"[UII]
Make constant keyword backfill optional (#192921)\n\n##
Summary\r\n\r\nFollow up to #188145. In some edge
cases\r\n(elastic/sdh-beats#5156), users could
override\r\nthe index template used by integration data streams. It is
possible to\r\ncreate an index template without mappings, this
causes\r\n`fillConstantKeywordValues` to receive an undefined object
when\r\nupgrading the integration, and the upgrade then
fails.\r\n\r\nThis PR makes the backfill operation here more
fail-safe.","sha":"b5abc4ec7e308815b0f338f4c836a9caf3ee48a3"}}]}]
BACKPORT-->

Co-authored-by: Jen Huang <[email protected]>
  • Loading branch information
kibanamachine and jen-huang authored Sep 14, 2024
1 parent 0c66ca8 commit 4773f52
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,110 @@ describe('EPM template', () => {
},
});
});

it('should fill constant keywords from previous mappings', async () => {
const esClient = elasticsearchServiceMock.createElasticsearchClient();
esClient.indices.getDataStream.mockResponse({
data_streams: [{ name: 'test-constant.keyword-default' }],
} as any);
esClient.indices.get.mockResponse({
'test-constant.keyword-default': {
mappings: {
properties: {
some_keyword_field: {
type: 'constant_keyword',
},
},
},
},
} as any);
esClient.indices.simulateTemplate.mockResponse({
template: {
settings: { index: {} },
mappings: {
properties: {
some_keyword_field: {
type: 'constant_keyword',
value: 'some_value',
},
},
},
},
} as any);
const logger = loggerMock.create();
await updateCurrentWriteIndices(esClient, logger, [
{
templateName: 'test',
indexTemplate: {
index_patterns: ['test-constant.keyword-*'],
template: {
template: {
settings: { index: {} },
mappings: { properties: {} },
},
},
} as any,
},
]);
const putMappingsCalls = esClient.indices.putMapping.mock.calls;
expect(putMappingsCalls).toHaveLength(1);
expect(putMappingsCalls[0][0]).toEqual({
index: 'test-constant.keyword-default',
body: {
properties: {
some_keyword_field: {
type: 'constant_keyword',
value: 'some_value',
},
},
},
write_index_only: true,
});
});

it('should not error when previous mappings are not found', async () => {
const esClient = elasticsearchServiceMock.createElasticsearchClient();
esClient.indices.getDataStream.mockResponse({
data_streams: [{ name: 'test-constant.keyword-default' }],
} as any);
esClient.indices.get.mockResponse({
'test-constant.keyword-default': {
mappings: {
properties: {
some_keyword_field: {
type: 'constant_keyword',
},
},
},
},
} as any);
esClient.indices.simulateTemplate.mockResponse({
template: {},
} as any);
const logger = loggerMock.create();
await updateCurrentWriteIndices(esClient, logger, [
{
templateName: 'test',
indexTemplate: {
index_patterns: ['test-constant.keyword-*'],
template: {
template: {
settings: { index: {} },
mappings: { properties: {} },
},
},
} as any,
},
]);
const putMappingsCalls = esClient.indices.putMapping.mock.calls;
expect(putMappingsCalls).toHaveLength(1);
expect(putMappingsCalls[0][0]).toEqual({
index: 'test-constant.keyword-default',
body: {},
write_index_only: true,
});
});

it('should rollover on expected error', async () => {
const esClient = elasticsearchServiceMock.createElasticsearchClient();
esClient.indices.getDataStream.mockResponse({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,16 @@ const updateExistingDataStream = async ({
);

settings = simulateResult.template.settings;
mappings = fillConstantKeywordValues(
currentBackingIndexConfig?.mappings || {},
simulateResult.template.mappings
);

try {
mappings = fillConstantKeywordValues(
currentBackingIndexConfig?.mappings || {},
simulateResult.template.mappings || {}
);
} catch (err) {
logger.error(`Error filling constant keyword values: ${err}`);
mappings = simulateResult.template.mappings;
}

lifecycle = simulateResult.template.lifecycle;

Expand Down

0 comments on commit 4773f52

Please sign in to comment.