Skip to content

Commit

Permalink
[Fleet] Don't overwrite logs-* and metrics-* data views on every …
Browse files Browse the repository at this point in the history
…integration install (#170188)

Replaces #169409

- Import data views (index patterns) separately with `overwrite: false`

---------

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
kpollich and kibanamachine authored Nov 1, 2023
1 parent 878df86 commit 9051d76
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
14 changes: 10 additions & 4 deletions x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,21 @@ export async function installKibanaAssets(options: {
return [];
}

// As we use `import` to create our saved objects, we have to install
// their references (the index patterns) at the same time
// to prevent a reference error
// Create index patterns separately with `overwrite: false` to prevent blowing away users' runtime fields.
// These don't get retried on conflict, because we expect that they exist once an integration has been installed.
const indexPatternSavedObjects = getIndexPatternSavedObjects() as ArchiveAsset[];
await savedObjectsImporter.import({
overwrite: false,
readStream: createListStream(indexPatternSavedObjects),
createNewCopies: false,
refresh: false,
managed: true,
});

const installedAssets = await installKibanaSavedObjects({
logger,
savedObjectsImporter,
kibanaAssets: [...indexPatternSavedObjects, ...assetsToInstall],
kibanaAssets: assetsToInstall,
});

return installedAssets;
Expand Down
77 changes: 77 additions & 0 deletions x-pack/test/fleet_api_integration/apis/epm/data_views.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 testPkgs = [
{
name: 'apache',
version: '0.1.4',
},
{
name: 'nginx',
version: '1.2.1',
},
];

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

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

const listDataViews = async () => {
const response = await supertest.get('/api/data_views');

return response.body.data_view;
};

describe('EPM - data views', () => {
skipIfNoDockerRegistry(providerContext);
setupFleetAndAgents(providerContext);

afterEach(async () => {
await Promise.all(testPkgs.map((pkg) => uninstallPackage(pkg.name, pkg.version)));
});

describe('with subsequent integration installation', async () => {
it('does not recreate managed data views', async () => {
await installPackage(testPkgs[0].name, testPkgs[0].version);
const initialDataViews: any[] = await listDataViews();
const initialLogsDataView = initialDataViews.find(({ title }) => title === 'logs-*');
const initialMetricsDataView = initialDataViews.find(({ title }) => title === 'metrics-*');

expect(initialLogsDataView).to.be.ok();
expect(initialMetricsDataView).to.be.ok();

await installPackage(testPkgs[1].name, testPkgs[1].version);
const subsequentDataViews: any[] = await listDataViews();
const subsequentLogsDataView = subsequentDataViews.find(({ title }) => title === 'logs-*');
const subsequentMetricsDataView = subsequentDataViews.find(
({ title }) => title === 'metrics-*'
);

// ID's should not have changed as the data views should not have been recreated
expect(initialLogsDataView.id).to.eql(subsequentLogsDataView.id);
expect(initialMetricsDataView.id).to.eql(subsequentMetricsDataView.id);
});
});
});
}
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 @@ -47,5 +47,6 @@ export default function loadTests({ loadTestFile, getService }) {
loadTestFile(require.resolve('./routing_rules'));
loadTestFile(require.resolve('./install_runtime_field'));
loadTestFile(require.resolve('./get_templates_inputs'));
loadTestFile(require.resolve('./data_views'));
});
}

0 comments on commit 9051d76

Please sign in to comment.