Skip to content

Commit d47d438

Browse files
authored
[Ingest Manager] Remove package cache on package uninstall (#76873) (#76921)
* On uninstall, remove package from cache. * Really remove package archive and contents from cache. * Refactor * move deletePackageCache to registry/index.ts * clean up imports
1 parent 9f737ef commit d47d438

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getInstallation, savedObjectTypes } from './index';
1313
import { deletePipeline } from '../elasticsearch/ingest_pipeline/';
1414
import { installIndexPatterns } from '../kibana/index_pattern/install';
1515
import { packagePolicyService, appContextService } from '../..';
16-
import { splitPkgKey } from '../registry';
16+
import { splitPkgKey, deletePackageCache, getArchiveInfo } from '../registry';
1717

1818
export async function removeInstallation(options: {
1919
savedObjectsClient: SavedObjectsClientContract;
@@ -22,7 +22,7 @@ export async function removeInstallation(options: {
2222
}): Promise<AssetReference[]> {
2323
const { savedObjectsClient, pkgkey, callCluster } = options;
2424
// TODO: the epm api should change to /name/version so we don't need to do this
25-
const { pkgName } = splitPkgKey(pkgkey);
25+
const { pkgName, pkgVersion } = splitPkgKey(pkgkey);
2626
const installation = await getInstallation({ savedObjectsClient, pkgName });
2727
if (!installation) throw Boom.badRequest(`${pkgName} is not installed`);
2828
if (installation.removable === false)
@@ -50,6 +50,11 @@ export async function removeInstallation(options: {
5050
// could also update with [] or some other state
5151
await savedObjectsClient.delete(PACKAGES_SAVED_OBJECT_TYPE, pkgName);
5252

53+
// remove the package archive and its contents from the cache so that a reinstall fetches
54+
// a fresh copy from the registry
55+
const paths = await getArchiveInfo(pkgName, pkgVersion);
56+
deletePackageCache(pkgName, pkgVersion, paths);
57+
5358
// successful delete's in SO client return {}. return something more useful
5459
return installedAssets;
5560
}

x-pack/plugins/ingest_manager/server/services/epm/registry/cache.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ export const getArchiveLocation = (name: string, version: string) =>
1818

1919
export const setArchiveLocation = (name: string, version: string, location: string) =>
2020
archiveLocationCache.set(pkgToPkgKey({ name, version }), location);
21+
22+
export const deleteArchiveLocation = (name: string, version: string) =>
23+
archiveLocationCache.delete(pkgToPkgKey({ name, version }));

x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ import {
1717
RegistrySearchResults,
1818
RegistrySearchResult,
1919
} from '../../../types';
20-
import { cacheGet, cacheSet, cacheHas, getArchiveLocation, setArchiveLocation } from './cache';
20+
import {
21+
cacheGet,
22+
cacheSet,
23+
cacheDelete,
24+
cacheHas,
25+
getArchiveLocation,
26+
setArchiveLocation,
27+
deleteArchiveLocation,
28+
} from './cache';
2129
import { ArchiveEntry, untarBuffer, unzipBuffer } from './extract';
2230
import { fetchUrl, getResponse, getResponseStream } from './requests';
2331
import { streamToBuffer } from './streams';
@@ -241,3 +249,17 @@ export function groupPathsByService(paths: string[]): AssetsGroupedByServiceByTy
241249
// elasticsearch: assets.elasticsearch,
242250
};
243251
}
252+
253+
export const deletePackageCache = (name: string, version: string, paths: string[]) => {
254+
const archiveLocation = getArchiveLocation(name, version);
255+
if (archiveLocation) {
256+
// delete cached archive
257+
cacheDelete(archiveLocation);
258+
259+
// delete cached archive location
260+
deleteArchiveLocation(name, version);
261+
}
262+
// delete cached archive contents
263+
// this has been populated in Registry.getArchiveInfo()
264+
paths.forEach((path) => cacheDelete(path));
265+
};

0 commit comments

Comments
 (0)