-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Fleet] Replace call to registry when deleting kibana assets for custom packages #224886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
280302b
[Fleet] Replace call to registry when deleting kibana assets for cust…
criamico aaead01
fix linter
criamico 167b759
Merge branch 'main' into 224191_delete_assets_bug
elasticmachine 471b201
Merge branch 'main' into 224191_delete_assets_bug
elasticmachine b60dea2
Add some logging and tests
criamico 3ab6218
conditional logging
criamico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,8 @@ | |
| * 2.0. | ||
| */ | ||
|
|
||
| import type { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server'; | ||
| import { differenceBy } from 'lodash'; | ||
| import type { ElasticsearchClient, SavedObjectsClientContract, Logger } from '@kbn/core/server'; | ||
| import { differenceBy, chunk } from 'lodash'; | ||
|
|
||
| import type { SavedObject } from '@kbn/core/server'; | ||
|
|
||
|
|
@@ -17,7 +17,6 @@ import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common/constants'; | |
| import { SavedObjectsUtils, SavedObjectsErrorHelpers } from '@kbn/core/server'; | ||
| import minVersion from 'semver/ranges/min-version'; | ||
|
|
||
| import { chunk } from 'lodash'; | ||
| import pMap from 'p-map'; | ||
|
|
||
| import { updateIndexSettings } from '../elasticsearch/index/update_settings'; | ||
|
|
@@ -36,8 +35,7 @@ import type { | |
| EsAssetReference, | ||
| KibanaAssetReference, | ||
| Installation, | ||
| ArchivePackage, | ||
| RegistryPackage, | ||
| InstallSource, | ||
| } from '../../../types'; | ||
| import { deletePipeline } from '../elasticsearch/ingest_pipeline'; | ||
| import { removeUnusedIndexPatterns } from '../kibana/index_pattern/install'; | ||
|
|
@@ -52,9 +50,10 @@ import { auditLoggingService } from '../../audit_logging'; | |
| import { FleetError, PackageRemovalError } from '../../../errors'; | ||
|
|
||
| import { populatePackagePolicyAssignedAgentsCount } from '../../package_policies/populate_package_policy_assigned_agents_count'; | ||
| import * as Registry from '../registry'; | ||
|
|
||
| import { getInstallation, kibanaSavedObjectTypes } from '.'; | ||
| import type { PackageSpecConditions } from '../../../../common'; | ||
|
|
||
| import { getInstallation, getPackageInfo, kibanaSavedObjectTypes } from '.'; | ||
| import { updateUninstallFailedAttempts } from './uninstall_errors_helpers'; | ||
|
|
||
| const MAX_ASSETS_TO_DELETE = 1000; | ||
|
|
@@ -65,6 +64,7 @@ export async function removeInstallation(options: { | |
| pkgVersion?: string; | ||
| esClient: ElasticsearchClient; | ||
| force?: boolean; | ||
| installSource?: InstallSource; | ||
| }): Promise<AssetReference[]> { | ||
| const { savedObjectsClient, pkgName, pkgVersion, esClient } = options; | ||
| const installation = await getInstallation({ savedObjectsClient, pkgName }); | ||
|
|
@@ -103,7 +103,7 @@ export async function removeInstallation(options: { | |
|
|
||
| // Delete the installed assets. Don't include installation.package_assets. Those are irrelevant to users | ||
| const installedAssets = [...installation.installed_kibana, ...installation.installed_es]; | ||
| await deleteAssets(installation, esClient); | ||
| await deleteAssets(savedObjectsClient, installation, esClient); | ||
|
|
||
| // Delete the manager saved object with references to the asset objects | ||
| // could also update with [] or some other state | ||
|
|
@@ -144,30 +144,34 @@ export async function removeInstallation(options: { | |
| */ | ||
| export async function deleteKibanaAssets({ | ||
| installedObjects, | ||
| packageInfo, | ||
| packageSpecConditions, | ||
| logger, | ||
| spaceId = DEFAULT_SPACE_ID, | ||
| }: { | ||
| installedObjects: KibanaAssetReference[]; | ||
| logger: Logger; | ||
| packageSpecConditions?: PackageSpecConditions; | ||
| spaceId?: string; | ||
| packageInfo: RegistryPackage | ArchivePackage; | ||
| }) { | ||
| const savedObjectsClient = new SavedObjectsClient( | ||
| appContextService.getSavedObjects().createInternalRepository() | ||
| ); | ||
|
|
||
| const namespace = SavedObjectsUtils.namespaceStringToId(spaceId); | ||
| if (namespace) { | ||
| logger.debug(`Deleting Kibana assets in namespace: ${namespace}`); | ||
| } | ||
|
|
||
| // TODO this should be the installed package info, not the package that is being installed | ||
| const minKibana = packageInfo.conditions?.kibana?.version | ||
| ? minVersion(packageInfo.conditions.kibana.version) | ||
| const minKibana = packageSpecConditions?.kibana?.version | ||
| ? minVersion(packageSpecConditions.kibana.version) | ||
| : null; | ||
|
|
||
| // Compare Kibana versions to determine if the package could been installed | ||
| // only in 8.x or later. If so, we can skip SO resolution step altogether | ||
| // and delete the assets directly. Otherwise, we need to resolve the assets | ||
| // which might create high memory pressure if a package has a lot of assets. | ||
| if (minKibana && minKibana.major >= 8) { | ||
| await bulkDeleteSavedObjects(installedObjects, namespace, savedObjectsClient); | ||
| await bulkDeleteSavedObjects(installedObjects, namespace, savedObjectsClient, logger); | ||
| } else { | ||
| const { resolved_objects: resolvedObjects } = await savedObjectsClient.bulkResolve( | ||
| installedObjects, | ||
|
|
@@ -190,23 +194,25 @@ export async function deleteKibanaAssets({ | |
| // we filter these out before calling delete | ||
| const assetsToDelete = foundObjects.map(({ saved_object: { id, type } }) => ({ id, type })); | ||
|
|
||
| await bulkDeleteSavedObjects(assetsToDelete, namespace, savedObjectsClient); | ||
| await bulkDeleteSavedObjects(assetsToDelete, namespace, savedObjectsClient, logger); | ||
| } | ||
| } | ||
|
|
||
| async function bulkDeleteSavedObjects( | ||
| assetsToDelete: Array<{ id: string; type: string }>, | ||
| namespace: string | undefined, | ||
| savedObjectsClient: SavedObjectsClientContract | ||
| savedObjectsClient: SavedObjectsClientContract, | ||
| logger: Logger | ||
| ) { | ||
| logger.debug(`Starting bulk deletion of assets and saved objects`); | ||
| for (const asset of assetsToDelete) { | ||
| logger.debug(`Delete asset - id: ${asset?.id}, type: ${asset?.type},`); | ||
| auditLoggingService.writeCustomSoAuditLog({ | ||
| action: 'delete', | ||
| id: asset.id, | ||
| savedObjectType: asset.type, | ||
| }); | ||
| } | ||
|
|
||
| // Delete assets in chunks to avoid high memory pressure. This is mostly | ||
| // relevant for packages containing many assets, as large payload and response | ||
| // objects are created in memory during the delete operation. While chunking | ||
|
|
@@ -333,13 +339,15 @@ export async function deletePrerequisiteAssets( | |
| } | ||
|
|
||
| async function deleteAssets( | ||
| savedObjectsClient: SavedObjectsClientContract, | ||
| { | ||
| installed_es: installedEs, | ||
| installed_kibana: installedKibana, | ||
| installed_kibana_space_id: spaceId = DEFAULT_SPACE_ID, | ||
| additional_spaces_installed_kibana: installedInAdditionalSpacesKibana = {}, | ||
| name, | ||
| version, | ||
| install_source: installSource, | ||
| }: Installation, | ||
| esClient: ElasticsearchClient | ||
| ) { | ||
|
|
@@ -357,17 +365,29 @@ async function deleteAssets( | |
| esClient | ||
| ); | ||
|
|
||
| // delete the other asset types | ||
| try { | ||
| const packageInfo = await Registry.fetchInfo(name, version); | ||
| const packageInfo = await getPackageInfo({ | ||
| savedObjectsClient, | ||
| pkgName: name, | ||
| pkgVersion: version, | ||
| skipArchive: installSource !== 'registry', | ||
| }); | ||
|
|
||
| // delete the other asset types | ||
| await Promise.all([ | ||
| ...deleteESAssets(otherAssets, esClient), | ||
| deleteKibanaAssets({ installedObjects: installedKibana, spaceId, packageInfo }), | ||
| deleteKibanaAssets({ | ||
| installedObjects: installedKibana, | ||
| spaceId, | ||
| packageSpecConditions: packageInfo?.conditions, | ||
| logger, | ||
| }), | ||
| Object.entries(installedInAdditionalSpacesKibana).map(([additionalSpaceId, kibanaAssets]) => | ||
| deleteKibanaAssets({ | ||
| installedObjects: kibanaAssets, | ||
| spaceId: additionalSpaceId, | ||
| packageInfo, | ||
| logger, | ||
| packageSpecConditions: packageInfo?.conditions, | ||
| }) | ||
| ), | ||
| ]); | ||
|
|
@@ -402,9 +422,11 @@ async function deleteComponentTemplate(esClient: ElasticsearchClient, name: stri | |
| } | ||
|
|
||
| export async function deleteKibanaSavedObjectsAssets({ | ||
| savedObjectsClient, | ||
| installedPkg, | ||
| spaceId, | ||
| }: { | ||
| savedObjectsClient: SavedObjectsClientContract; | ||
| installedPkg: SavedObject<Installation>; | ||
| spaceId?: string; | ||
| }) { | ||
|
|
@@ -427,15 +449,18 @@ export async function deleteKibanaSavedObjectsAssets({ | |
| .map(({ id, type }) => ({ id, type } as KibanaAssetReference)); | ||
|
|
||
| try { | ||
| const packageInfo = await Registry.fetchInfo( | ||
| installedPkg.attributes.name, | ||
| installedPkg.attributes.version | ||
| ); | ||
| const packageInfo = await getPackageInfo({ | ||
| savedObjectsClient, | ||
| pkgName: installedPkg.attributes.name, | ||
| pkgVersion: installedPkg.attributes.version, | ||
| skipArchive: installedPkg.attributes.install_source !== 'registry', | ||
| }); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replacing it also here, it is a different code path but this way we ensure that packages that it doesn't error for packages not installed from registry. |
||
|
|
||
| await deleteKibanaAssets({ | ||
| installedObjects: assetsToDelete, | ||
| spaceId: spaceIdToDelete, | ||
| packageInfo, | ||
| packageSpecConditions: packageInfo?.conditions, | ||
| logger, | ||
| }); | ||
| } catch (err) { | ||
| // in the rollback case, partial installs are likely, so missing assets are not an error | ||
|
|
@@ -502,7 +527,7 @@ export async function cleanupAssets( | |
| esClient: ElasticsearchClient, | ||
| soClient: SavedObjectsClientContract | ||
| ) { | ||
| await deleteAssets(installationToDelete, esClient); | ||
| await deleteAssets(soClient, installationToDelete, esClient); | ||
|
|
||
| const { | ||
| installed_es: installedEs, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main fix: used getPackageInfo instead of calling the registry.