diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts index 47b7388131ff..1fd022bce4dc 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts @@ -76,6 +76,12 @@ export interface ReindexService { */ findReindexOperation(indexName: string): Promise; + /** + * Delete reindex operations for completed indices with deprecations. + * @param indexNames + */ + cleanupReindexOperations(indexNames: string[]): Promise | null; + /** * Process the reindex operation through one step of the state machine and resolves * to the updated reindex operation. @@ -603,6 +609,23 @@ export const reindexServiceFactory = ( return findResponse.saved_objects[0]; }, + async cleanupReindexOperations(indexNames: string[]) { + const performCleanup = async (indexName: string) => { + const existingReindexOps = await actions.findReindexOperations(indexName); + + if (existingReindexOps && existingReindexOps.total !== 0) { + const existingOp = existingReindexOps.saved_objects[0]; + if (existingOp.attributes.status === ReindexStatus.completed) { + // Delete the existing one if its status is completed, but still contains deprecation warnings + // example scenario: index was upgraded, but then deleted and restored with an old snapshot + await actions.deleteReindexOp(existingOp); + } + } + }; + + await Promise.all(indexNames.map(performCleanup)); + }, + findAllByStatus: actions.findAllByStatus, async processNextStep(reindexOp: ReindexSavedObject) { diff --git a/x-pack/plugins/upgrade_assistant/server/routes/cluster_checkup.ts b/x-pack/plugins/upgrade_assistant/server/routes/cluster_checkup.ts index 22a121ab7868..fa4649f1c5dc 100644 --- a/x-pack/plugins/upgrade_assistant/server/routes/cluster_checkup.ts +++ b/x-pack/plugins/upgrade_assistant/server/routes/cluster_checkup.ts @@ -7,8 +7,10 @@ import { getUpgradeAssistantStatus } from '../lib/es_migration_apis'; import { versionCheckHandlerWrapper } from '../lib/es_version_precheck'; import { RouteDependencies } from '../types'; +import { reindexActionsFactory } from '../lib/reindexing/reindex_actions'; +import { reindexServiceFactory } from '../lib/reindexing'; -export function registerClusterCheckupRoutes({ cloud, router }: RouteDependencies) { +export function registerClusterCheckupRoutes({ cloud, router, licensing, log }: RouteDependencies) { const isCloudEnabled = Boolean(cloud?.isCloudEnabled); router.get( @@ -20,6 +22,7 @@ export function registerClusterCheckupRoutes({ cloud, router }: RouteDependencie async ( { core: { + savedObjects: { client: savedObjectsClient }, elasticsearch: { dataClient }, }, }, @@ -27,8 +30,24 @@ export function registerClusterCheckupRoutes({ cloud, router }: RouteDependencie response ) => { try { + const status = await getUpgradeAssistantStatus(dataClient, isCloudEnabled); + + const callAsCurrentUser = dataClient.callAsCurrentUser.bind(dataClient); + const reindexActions = reindexActionsFactory(savedObjectsClient, callAsCurrentUser); + const reindexService = reindexServiceFactory( + callAsCurrentUser, + reindexActions, + log, + licensing + ); + const indexNames = status.indices + .filter(({ index }) => typeof index !== 'undefined') + .map(({ index }) => index as string); + + await reindexService.cleanupReindexOperations(indexNames); + return response.ok({ - body: await getUpgradeAssistantStatus(dataClient, isCloudEnabled), + body: status, }); } catch (e) { if (e.status === 403) {