Skip to content

Commit

Permalink
[Fleet] fix for upgrade package with tsds removed (elastic#157395)
Browse files Browse the repository at this point in the history
## Summary

Fixes elastic#157345

To test:

Install `nginx-1.12.0-beta` which has `index.mode:time_series`.

```
POST http://elastic:changeme@localhost:5601/api/fleet/epm/packages/nginx-1.12.0-beta
kbn-xsrf: kibana

{
   "force": true
 }
```

Upgrade to `nginx-1.12.1-beta` which has `index.mode:time_series`
removed.

Upload this package built manually:

[nginx-1.12.1-beta.zip](https://github.com/elastic/kibana/files/11452945/nginx-1.12.1-beta.zip)

```
curl -XPOST -H 'content-type: application/zip' -H 'kbn-xsrf: true' http://localhost:5601/api/fleet/epm/packages -u elastic:changeme --data-binary @nginx-1.12.1-beta.zip
```

The package should install successfully and time_series should be
removed from the index template `metrics-nginx.stubstatus`

WIP: update tests

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

(cherry picked from commit a0ee7b6)
  • Loading branch information
juliaElastic committed May 11, 2023
1 parent 31ff66e commit 872d169
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,46 @@ export async function installComponentAndIndexTemplateForDataStream({
componentTemplates: TemplateMap;
indexTemplate: IndexTemplateEntry;
}) {
// update index template first in case TSDS was removed, so that it does not become invalid
await updateIndexTemplateIfTsdsDisabled({ esClient, logger, indexTemplate });

await installDataStreamComponentTemplates({ esClient, logger, componentTemplates });
await installTemplate({ esClient, logger, template: indexTemplate });
}

async function updateIndexTemplateIfTsdsDisabled({
esClient,
logger,
indexTemplate,
}: {
esClient: ElasticsearchClient;
logger: Logger;
indexTemplate: IndexTemplateEntry;
}) {
try {
const existingIndexTemplate = await esClient.indices.getIndexTemplate({
name: indexTemplate.templateName,
});
if (
existingIndexTemplate.index_templates?.[0]?.index_template.template?.settings?.index?.mode ===
'time_series' &&
indexTemplate.indexTemplate.template.settings.index.mode !== 'time_series'
) {
await installTemplate({ esClient, logger, template: indexTemplate });
}
} catch (e) {
if (e.statusCode === 404) {
logger.debug(
`Index template ${indexTemplate.templateName} does not exist, skipping time_series check`
);
} else {
logger.warn(
`Error while trying to install index template before component template: ${e.message}`
);
}
}
}

function putComponentTemplate(
esClient: ElasticsearchClient,
logger: Logger,
Expand Down
1 change: 1 addition & 0 deletions x-pack/test/fleet_api_integration/apis/epm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function loadTests({ loadTestFile, getService }) {
loadTestFile(require.resolve('./install_remove_kbn_assets_in_space'));
loadTestFile(require.resolve('./install_remove_multiple'));
loadTestFile(require.resolve('./install_update'));
loadTestFile(require.resolve('./install_tsds_disable'));
loadTestFile(require.resolve('./install_tag_assets'));
loadTestFile(require.resolve('./bulk_upgrade'));
loadTestFile(require.resolve('./update_assets'));
Expand Down
63 changes: 63 additions & 0 deletions x-pack/test/fleet_api_integration/apis/epm/install_tsds_disable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { skipIfNoDockerRegistry } from '../../helpers';
import { setupFleetAndAgents } from '../agents/services';

export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const supertest = getService('supertest');
const es = getService('es');

const deletePackage = async (name: string, version: string) => {
await supertest.delete(`/api/fleet/epm/packages/${name}/${version}`).set('kbn-xsrf', 'xxxx');
};

describe('installing with tsds disabled', async () => {
skipIfNoDockerRegistry(providerContext);
setupFleetAndAgents(providerContext);

after(async () => {
await deletePackage('nginx', '1.12.1-beta');
});

it('should upgrade with tsds disabled if nginx exists with tsds', async function () {
const templateName = 'metrics-nginx.stubstatus';

await supertest
.post(`/api/fleet/epm/packages/nginx/1.12.0-beta`)
.set('kbn-xsrf', 'xxxx')
.send({ force: true })
.expect(200);

expect(await getIndexMode(templateName)).to.eql('time_series');

await supertest
.post(`/api/fleet/epm/packages/nginx/1.12.1-beta`)
.set('kbn-xsrf', 'xxxx')
.send({ force: true })
.expect(200);

expect(await getIndexMode(templateName)).to.be(undefined);
});

async function getIndexMode(templateName: string) {
const { body: indexTemplateResponse } = await es.transport.request<any>(
{
method: 'GET',
path: `/_index_template/${templateName}`,
},
{ meta: true }
);

const indexTemplate = indexTemplateResponse.index_templates[0].index_template;
return indexTemplate.template.settings.index?.mode;
}
});
}
Binary file not shown.
Binary file not shown.

0 comments on commit 872d169

Please sign in to comment.