Skip to content

Commit a89a4b1

Browse files
[Ingest Manager] Index pattern installation use requested package version (#80079)
* Install the requested package version * Add test for correct package fields * Addressing feedback
1 parent 21c3e04 commit a89a4b1

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

x-pack/plugins/ingest_manager/server/services/epm/kibana/index_pattern/install.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ export async function installIndexPatterns(
8585
savedObjectsClient,
8686
InstallationStatus.installed
8787
);
88-
8988
// TODO: move to install package
9089
// cache all installed packages if they don't exist
9190
const packagePromises = installedPackages.map((pkg) =>
@@ -95,26 +94,32 @@ export async function installIndexPatterns(
9594
);
9695
await Promise.all(packagePromises);
9796

97+
const packageVersionsToFetch = [...installedPackages];
9898
if (pkgName && pkgVersion) {
99-
// add this package to the array if it doesn't already exist
100-
const foundPkg = installedPackages.find((pkg) => pkg.pkgName === pkgName);
101-
// this may be removed if we add the packged to saved objects before installing index patterns
102-
// otherwise this is a first time install
103-
// TODO: handle update case when versions are different
104-
if (!foundPkg) {
105-
installedPackages.push({ pkgName, pkgVersion });
99+
const packageToInstall = packageVersionsToFetch.find((pkg) => pkg.pkgName === pkgName);
100+
101+
if (packageToInstall) {
102+
// set the version to the one we want to install
103+
// if we're installing for the first time the number will be the same
104+
// if this is an upgrade then we'll be modifying the version number to the upgrade version
105+
packageToInstall.pkgVersion = pkgVersion;
106+
} else {
107+
// this will likely not happen because the saved objects should already have the package we're trying
108+
// install which means that it should have been found in the case above
109+
packageVersionsToFetch.push({ pkgName, pkgVersion });
106110
}
107111
}
108112
// get each package's registry info
109-
const installedPackagesFetchInfoPromise = installedPackages.map((pkg) =>
113+
const packageVersionsFetchInfoPromise = packageVersionsToFetch.map((pkg) =>
110114
Registry.fetchInfo(pkg.pkgName, pkg.pkgVersion)
111115
);
112-
const installedPackagesInfo = await Promise.all(installedPackagesFetchInfoPromise);
116+
117+
const packageVersionsInfo = await Promise.all(packageVersionsFetchInfoPromise);
113118

114119
// for each index pattern type, create an index pattern
115120
const indexPatternTypes = [IndexPatternType.logs, IndexPatternType.metrics];
116121
indexPatternTypes.forEach(async (indexPatternType) => {
117-
// if this is an update because a package is being unisntalled (no pkgkey argument passed) and no other packages are installed, remove the index pattern
122+
// if this is an update because a package is being uninstalled (no pkgkey argument passed) and no other packages are installed, remove the index pattern
118123
if (!pkgName && installedPackages.length === 0) {
119124
try {
120125
await savedObjectsClient.delete(INDEX_PATTERN_SAVED_OBJECT_TYPE, `${indexPatternType}-*`);
@@ -125,8 +130,7 @@ export async function installIndexPatterns(
125130
}
126131

127132
// get all data stream fields from all installed packages
128-
const fields = await getAllDataStreamFieldsByType(installedPackagesInfo, indexPatternType);
129-
133+
const fields = await getAllDataStreamFieldsByType(packageVersionsInfo, indexPatternType);
130134
const kibanaIndexPattern = createIndexPattern(indexPatternType, fields);
131135
// create or overwrite the index pattern
132136
await savedObjectsClient.create(INDEX_PATTERN_SAVED_OBJECT_TYPE, kibanaIndexPattern, {

x-pack/plugins/ingest_manager/server/services/epm/packages/get.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,15 @@ export async function getPackageKeysByStatus(
8989
const allPackages = await getPackages({ savedObjectsClient });
9090
return allPackages.reduce<Array<{ pkgName: string; pkgVersion: string }>>((acc, pkg) => {
9191
if (pkg.status === status) {
92-
acc.push({ pkgName: pkg.name, pkgVersion: pkg.version });
92+
if (pkg.status === InstallationStatus.installed) {
93+
// if we're looking for installed packages grab the version from the saved object because `getPackages` will
94+
// return the latest package information from the registry
95+
acc.push({ pkgName: pkg.name, pkgVersion: pkg.savedObject.attributes.version });
96+
} else {
97+
acc.push({ pkgName: pkg.name, pkgVersion: pkg.version });
98+
}
9399
}
100+
94101
return acc;
95102
}, []);
96103
}

x-pack/test/ingest_manager_api_integration/apis/epm/install_remove_assets.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ export default function (providerContext: FtrProviderContext) {
131131
});
132132
expect(resSearch.id).equal('sample_search');
133133
});
134+
it('should create an index pattern with the package fields', async () => {
135+
const resIndexPatternLogs = await kibanaServer.savedObjects.get({
136+
type: 'index-pattern',
137+
id: 'logs-*',
138+
});
139+
const fields = JSON.parse(resIndexPatternLogs.attributes.fields);
140+
const exists = fields.find((field: { name: string }) => field.name === 'logs_test_name');
141+
expect(exists).not.to.be(undefined);
142+
const resIndexPatternMetrics = await kibanaServer.savedObjects.get({
143+
type: 'index-pattern',
144+
id: 'metrics-*',
145+
});
146+
const fieldsMetrics = JSON.parse(resIndexPatternMetrics.attributes.fields);
147+
const metricsExists = fieldsMetrics.find(
148+
(field: { name: string }) => field.name === 'metrics_test_name'
149+
);
150+
expect(metricsExists).not.to.be(undefined);
151+
});
134152
it('should have created the correct saved object', async function () {
135153
const res = await kibanaServer.savedObjects.get({
136154
type: 'epm-packages',

0 commit comments

Comments
 (0)