-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
es_deprecations_status.ts
166 lines (146 loc) · 5.8 KB
/
es_deprecations_status.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* 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 type { estypes } from '@elastic/elasticsearch';
import { IScopedClusterClient } from 'src/core/server';
import { indexSettingDeprecations } from '../../common/constants';
import { EnrichedDeprecationInfo, ESUpgradeStatus } from '../../common/types';
import { esIndicesStateCheck } from './es_indices_state_check';
import {
getESSystemIndicesMigrationStatus,
convertFeaturesToIndicesArray,
} from '../lib/es_system_indices_migration';
export async function getESUpgradeStatus(
dataClient: IScopedClusterClient
): Promise<ESUpgradeStatus> {
const { body: deprecations } = await dataClient.asCurrentUser.migration.deprecations();
const getCombinedDeprecations = async () => {
const indices = await getCombinedIndexInfos(deprecations, dataClient);
const systemIndices = await getESSystemIndicesMigrationStatus(dataClient.asCurrentUser);
const systemIndicesList = convertFeaturesToIndicesArray(systemIndices.features);
return Object.keys(deprecations).reduce((combinedDeprecations, deprecationType) => {
if (deprecationType === 'index_settings') {
// We need to exclude all index related deprecations for system indices since
// they are resolved separately through the system indices upgrade section in
// the Overview page.
const withoutSystemIndices = indices.filter(
(index) => !systemIndicesList.includes(index.index!)
);
combinedDeprecations = combinedDeprecations.concat(withoutSystemIndices);
} else {
const deprecationsByType = deprecations[
deprecationType as keyof estypes.MigrationDeprecationsResponse
] as estypes.MigrationDeprecationsDeprecation[];
const enrichedDeprecationInfo = deprecationsByType.map(
({
details,
level,
message,
url,
// @ts-expect-error @elastic/elasticsearch _meta not available yet in MigrationDeprecationInfoResponse
_meta: metadata,
// @ts-expect-error @elastic/elasticsearch resolve_during_rolling_upgrade not available yet in MigrationDeprecationInfoResponse
resolve_during_rolling_upgrade: resolveDuringUpgrade,
}) => {
return {
details,
message,
url,
type: deprecationType as keyof estypes.MigrationDeprecationsResponse,
isCritical: level === 'critical',
resolveDuringUpgrade,
correctiveAction: getCorrectiveAction(message, metadata),
};
}
);
combinedDeprecations = combinedDeprecations.concat(enrichedDeprecationInfo);
}
return combinedDeprecations;
}, [] as EnrichedDeprecationInfo[]);
};
const combinedDeprecations = await getCombinedDeprecations();
const criticalWarnings = combinedDeprecations.filter(({ isCritical }) => isCritical === true);
return {
totalCriticalDeprecations: criticalWarnings.length,
deprecations: combinedDeprecations,
};
}
// Reformats the index deprecations to an array of deprecation warnings extended with an index field.
const getCombinedIndexInfos = async (
deprecations: estypes.MigrationDeprecationsResponse,
dataClient: IScopedClusterClient
) => {
const indices = Object.keys(deprecations.index_settings).reduce(
(indexDeprecations, indexName) => {
return indexDeprecations.concat(
deprecations.index_settings[indexName].map(
({
details,
message,
url,
level,
// @ts-expect-error @elastic/elasticsearch resolve_during_rolling_upgrade not available yet in MigrationDeprecationInfoResponse
resolve_during_rolling_upgrade: resolveDuringUpgrade,
}) =>
({
details,
message,
url,
index: indexName,
type: 'index_settings',
isCritical: level === 'critical',
correctiveAction: getCorrectiveAction(message),
resolveDuringUpgrade,
} as EnrichedDeprecationInfo)
)
);
},
[] as EnrichedDeprecationInfo[]
);
const indexNames = indices.map(({ index }) => index!);
// If we have found deprecation information for index/indices
// check whether the index is open or closed.
if (indexNames.length) {
const indexStates = await esIndicesStateCheck(dataClient.asCurrentUser, indexNames);
indices.forEach((indexData) => {
if (indexData.correctiveAction?.type === 'reindex') {
indexData.correctiveAction.blockerForReindexing =
indexStates[indexData.index!] === 'closed' ? 'index-closed' : undefined;
}
});
}
return indices as EnrichedDeprecationInfo[];
};
const getCorrectiveAction = (
message: string,
metadata?: { [key: string]: string }
): EnrichedDeprecationInfo['correctiveAction'] => {
const indexSettingDeprecation = Object.values(indexSettingDeprecations).find(
({ deprecationMessage }) => deprecationMessage === message
);
const requiresReindexAction = /Index created before/.test(message);
const requiresIndexSettingsAction = Boolean(indexSettingDeprecation);
const requiresMlAction = /[Mm]odel snapshot/.test(message);
if (requiresReindexAction) {
return {
type: 'reindex',
};
}
if (requiresIndexSettingsAction) {
return {
type: 'indexSetting',
deprecatedSettings: indexSettingDeprecation!.settings,
};
}
if (requiresMlAction) {
const { snapshot_id: snapshotId, job_id: jobId } = metadata!;
return {
type: 'mlSnapshot',
snapshotId,
jobId,
};
}
};