/
cluster-update-modal.tsx
353 lines (346 loc) · 12.8 KB
/
cluster-update-modal.tsx
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import * as _ from 'lodash-es';
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import { Alert, Radio, Text, TextContent, TextVariants } from '@patternfly/react-core';
import { useK8sWatchResource } from '@console/internal/components/utils/k8s-watch-hook';
import {
documentationURLs,
ExternalLink,
getDocumentationURL,
} from '@console/internal/components/utils';
import { DropdownWithSwitch } from '@console/shared/src/components/dropdown';
import { ClusterVersionModel, MachineConfigPoolModel, NodeModel } from '../../models';
import {
FieldLevelHelp,
HandlePromiseProps,
LinkifyExternal,
isManaged,
withHandlePromise,
} from '../utils';
import {
ClusterVersionKind,
getConditionUpgradeableFalse,
getDesiredClusterVersion,
getMCPsToPausePromises,
getNotRecommendedUpdateCondition,
getSortedAvailableUpdates,
getSortedNotRecommendedUpdates,
isMCPMaster,
isMCPPaused,
isMinorVersionNewer,
k8sPatch,
MachineConfigPoolKind,
NodeTypeNames,
referenceForModel,
sortMCPsByCreationTimestamp,
} from '../../module/k8s';
import {
createModalLauncher,
ModalBody,
ModalComponentProps,
ModalSubmitFooter,
ModalTitle,
} from '../factory/modal';
import {
ClusterNotUpgradeableAlert,
UpdateBlockedLabel,
} from '../cluster-settings/cluster-settings';
import { MachineConfigPoolsSelector } from '../machine-config-pools-selector';
enum upgradeTypes {
Full = 'Full',
Partial = 'Partial',
}
const ClusterUpdateModal = withHandlePromise((props: ClusterUpdateModalProps) => {
const { cancel, close, cv, errorMessage, handlePromise, inProgress } = props;
const clusterUpgradeableFalse = !!getConditionUpgradeableFalse(cv);
const availableSortedUpdates = getSortedAvailableUpdates(cv);
const notRecommendedSortedUpdates = getSortedNotRecommendedUpdates(cv);
const currentVersion = getDesiredClusterVersion(cv);
const currentMinorVersionPatchUpdate = availableSortedUpdates?.find(
(update) => !isMinorVersionNewer(currentVersion, update.version),
);
const [desiredVersion, setDesiredVersion] = React.useState(
(clusterUpgradeableFalse
? currentMinorVersionPatchUpdate?.version
: availableSortedUpdates[0]?.version) || '',
);
const [machineConfigPools, machineConfigPoolsLoaded] = useK8sWatchResource<
MachineConfigPoolKind[]
>({
isList: true,
kind: referenceForModel(MachineConfigPoolModel),
});
const [error, setError] = React.useState('');
const [machineConfigPoolsToPause, setMachineConfigPoolsToPause] = React.useState<string[]>([]);
const [upgradeType, setUpgradeType] = React.useState<upgradeTypes>(upgradeTypes.Full);
const [includeNotRecommended, setIncludeNotRecommended] = React.useState(false);
const { t } = useTranslation();
React.useEffect(() => {
const initialMCPPausedValues = machineConfigPools
.filter((mcp) => !isMCPMaster(mcp) && isMCPPaused(mcp))
.map((mcp) => mcp.metadata.name);
setMachineConfigPoolsToPause(initialMCPPausedValues);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []); // only run the effect once so changes don't affect user input
const onSelectVersion = (event, selection) => {
event.preventDefault();
setDesiredVersion(selection);
};
const handleUpgradeTypeChange = (value: typeof upgradeType) => {
setUpgradeType(value);
};
const handleMCPSelectionChange = (checked: boolean, event: React.FormEvent<HTMLInputElement>) => {
const checkedItems = [...machineConfigPoolsToPause];
checked
? checkedItems.push(event.currentTarget.id)
: _.pull(checkedItems, event.currentTarget.id);
setMachineConfigPoolsToPause(checkedItems);
};
const pauseableMCPs = machineConfigPools
.filter((mcp) => !isMCPMaster(mcp))
.sort(sortMCPsByCreationTimestamp);
const pausedMCPs = pauseableMCPs.filter((mcp) => isMCPPaused(mcp));
const desiredRecommendedUpdate = _.find(availableSortedUpdates, { version: desiredVersion });
const desiredNotRecommendedUpdate = _.find(notRecommendedSortedUpdates, {
release: { version: desiredVersion },
});
const desiredNotRecommendedUpdateConditions = getNotRecommendedUpdateCondition(
desiredNotRecommendedUpdate?.conditions,
);
const submit: React.FormEventHandler<HTMLFormElement> = (e) => {
e.preventDefault();
if (!desiredRecommendedUpdate && !desiredNotRecommendedUpdate) {
setError(
t(
'public~Version {{desiredVersion}} not found among the supported updates. Select another version.',
{ desiredVersion },
),
);
return;
}
// Clear any previous error message.
setError('');
let MCPsToResumePromises;
let MCPsToPausePromises;
if (upgradeType === upgradeTypes.Full) {
MCPsToResumePromises = getMCPsToPausePromises(pausedMCPs, false);
MCPsToPausePromises = [];
} else {
const MCPsToResume = pausedMCPs.filter((mcp) =>
machineConfigPoolsToPause.find((m) => m !== mcp.metadata.name),
);
const MCPsToPause = pauseableMCPs.filter((mcp) =>
machineConfigPoolsToPause.find((m) => m === mcp.metadata.name),
);
MCPsToResumePromises = getMCPsToPausePromises(MCPsToResume, false);
MCPsToPausePromises = getMCPsToPausePromises(MCPsToPause, true);
}
const patch = [
{
op: 'add',
path: '/spec/desiredUpdate',
value: desiredNotRecommendedUpdate
? desiredNotRecommendedUpdate.release
: desiredRecommendedUpdate,
},
];
return handlePromise(
Promise.all([
k8sPatch(ClusterVersionModel, cv, patch),
...MCPsToResumePromises,
...MCPsToPausePromises,
]),
close,
);
};
const dropdownItem = (version) => {
const isDisabled = clusterUpgradeableFalse && isMinorVersionNewer(currentVersion, version);
return {
isDisabled,
key: version,
title: isDisabled ? (
<>
{version} <UpdateBlockedLabel />
</>
) : (
version
),
};
};
const recommendedOptions = availableSortedUpdates.map(({ version }) => {
return dropdownItem(version);
});
const notRecommendedOptions = notRecommendedSortedUpdates.map(({ release: { version } }) => {
return dropdownItem(version);
});
const options = [
{
items: recommendedOptions,
key: 'recommended',
label: t('public~Recommended'),
},
];
if (includeNotRecommended) {
options.unshift({
items: notRecommendedOptions,
key: 'notRecommended',
label: t('public~Supported but not recommended'),
});
}
const helpURL = getDocumentationURL(documentationURLs.updateUsingCustomMachineConfigPools);
return (
<form onSubmit={submit} name="form" className="modal-content">
<ModalTitle>{t('public~Update cluster')}</ModalTitle>
<ModalBody>
{clusterUpgradeableFalse && <ClusterNotUpgradeableAlert onCancel={cancel} cv={cv} />}
<div className="form-group">
<label>{t('public~Current version')}</label>
<p>{currentVersion}</p>
</div>
<div className="form-group">
<label id="version-label">{t('public~Select new version')}</label>
<DropdownWithSwitch
isFullWidth
onSelect={onSelectVersion}
options={options}
selected={desiredVersion}
switchIsChecked={includeNotRecommended}
switchIsDisabled={notRecommendedOptions.length === 0}
switchLabel={
<>
{t('public~Include supported but not recommended versions')}
<FieldLevelHelp>
{t(
'public~These versions are supported, but not recommended. Review the known risks before updating.',
)}
</FieldLevelHelp>
</>
}
switchLabelClassName="co-switch-label"
switchLabelIsReversed
switchOnChange={(val) => setIncludeNotRecommended(val)}
toggleLabel={desiredVersion}
/>
{desiredNotRecommendedUpdate && desiredNotRecommendedUpdateConditions?.message && (
<Alert
className="pf-u-mt-sm"
isExpandable
isInline
title={t(
'public~Updating this cluster to {{desiredVersion}} is supported, but not recommended as it might not be optimized for some components in this cluster.',
{ desiredVersion: desiredNotRecommendedUpdate.release.version },
)}
variant="info"
>
<TextContent>
<Text component={TextVariants.p}>
<LinkifyExternal>
{desiredNotRecommendedUpdateConditions.message.split('\n').map((item) => (
<React.Fragment key={item}>
{item}
<br />
</React.Fragment>
))}
</LinkifyExternal>
</Text>
</TextContent>
</Alert>
)}
</div>
<div className="form-group">
<label>
{t('public~Update options')}
<FieldLevelHelp>
{t(
"public~Full cluster update allows you to update all your Nodes, but takes longer. Partial cluster update allows you to pause worker and custom pool Nodes to accommodate your maintenance schedule, but you'll need to resume the non-control plane Node updates within 60 days to avoid failure.",
)}
</FieldLevelHelp>
</label>
<Radio
isChecked={upgradeType === upgradeTypes.Full}
name={upgradeTypes.Full}
onChange={() => handleUpgradeTypeChange(upgradeTypes.Full)}
label={t('public~Full cluster update')}
id={upgradeTypes.Full}
value={upgradeTypes.Full}
description={t(
'public~{{master}}, {{worker}}, and custom pool {{resource}} are updated concurrently. This might take longer, so make sure to allocate enough time for maintenance.',
{
master: NodeTypeNames.Master,
worker: NodeTypeNames.Worker,
resource: NodeModel.labelPlural,
},
)}
className="pf-u-mb-sm"
body={
machineConfigPoolsLoaded &&
pausedMCPs.length > 0 &&
upgradeType === upgradeTypes.Full && (
<Alert
variant="warning"
isInline
isPlain
title={t(
'public~Paused {{worker}} or custom pool {{resource}} updates will be resumed. If you want to update only the control plane, select "Partial cluster update" below.',
{ worker: NodeTypeNames.Worker, resource: NodeModel.label },
)}
/>
)
}
/>
<Radio
isChecked={upgradeType === upgradeTypes.Partial}
name={upgradeTypes.Partial}
onChange={() => handleUpgradeTypeChange(upgradeTypes.Partial)}
label={t('public~Partial cluster update')}
id={upgradeTypes.Partial}
value={upgradeTypes.Partial}
description={t(
'public~Pause {{worker}} or custom pool {{resource}} updates to accommodate your maintenance schedule.',
{ worker: NodeTypeNames.Worker, resource: NodeModel.label },
)}
className="pf-u-mb-md"
body={
upgradeType === upgradeTypes.Partial && (
<>
<MachineConfigPoolsSelector
machineConfigPools={pauseableMCPs}
selected={machineConfigPoolsToPause}
onChange={handleMCPSelectionChange}
/>
<Alert
variant="warning"
isInline
isPlain
title={t('public~You must resume updates within 60 days to avoid failures.')}
className="pf-u-mb-md"
>
{!isManaged() && (
<ExternalLink href={helpURL}>{t('public~Learn more')}</ExternalLink>
)}
</Alert>
</>
)
}
/>
</div>
</ModalBody>
<ModalSubmitFooter
errorMessage={errorMessage || error}
inProgress={inProgress}
submitText={t('public~Update')}
cancelText={t('public~Cancel')}
cancel={cancel}
submitDisabled={
!desiredVersion ||
(upgradeType === upgradeTypes.Partial && machineConfigPoolsToPause.length === 0)
}
/>
</form>
);
});
export const clusterUpdateModal = createModalLauncher(ClusterUpdateModal);
type ClusterUpdateModalProps = {
cv: ClusterVersionKind;
} & ModalComponentProps &
HandlePromiseProps;