-
Notifications
You must be signed in to change notification settings - Fork 615
/
cluster-settings.ts
332 lines (278 loc) · 10.9 KB
/
cluster-settings.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
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
import * as _ from 'lodash-es';
import * as semver from 'semver';
import i18next from 'i18next';
import { ClusterVersionModel, MachineConfigPoolModel } from '../../models';
import { referenceForModel } from './k8s-ref';
import {
ClusterVersionCondition,
ClusterVersionConditionType,
ClusterVersionKind,
ConditionalUpdate,
k8sPatch,
K8sResourceCondition,
K8sResourceConditionStatus,
Release,
UpdateHistory,
} from '.';
import { MachineConfigPoolKind } from './types';
export enum ClusterUpdateStatus {
UpToDate = 'Up to Date',
UpdatesAvailable = 'Updates Available',
Updating = 'Updating',
Failing = 'Failing',
UpdatingAndFailing = 'Updating and Failing',
ErrorRetrieving = 'Error Retrieving',
Invalid = 'Invalid Cluster Version',
}
export const clusterVersionReference = referenceForModel(ClusterVersionModel);
const getAvailableClusterUpdates = (cv: ClusterVersionKind): Release[] => {
return cv?.status?.availableUpdates || [];
};
export const getSortedAvailableUpdates = (cv: ClusterVersionKind): Release[] => {
const available = getAvailableClusterUpdates(cv);
try {
return available.sort(({ version: left }, { version: right }) => semver.rcompare(left, right));
} catch (e) {
// eslint-disable-next-line no-console
console.error('error sorting available cluster updates', e);
return available;
}
};
const getConditionalClusterUpdates = (cv: ClusterVersionKind): ConditionalUpdate[] => {
return cv?.status?.conditionalUpdates || [];
};
export const getNotRecommendedUpdateCondition = (
conditions: K8sResourceCondition[],
): K8sResourceCondition => {
return conditions?.find(
(condition) => condition.type === 'Recommended' && condition.status !== 'True',
);
};
const getNotRecommendedUpdates = (cv: ClusterVersionKind): ConditionalUpdate[] => {
return getConditionalClusterUpdates(cv).filter((update) =>
getNotRecommendedUpdateCondition(update.conditions),
);
};
export const getSortedNotRecommendedUpdates = (cv: ClusterVersionKind): ConditionalUpdate[] => {
const notRecommended = getNotRecommendedUpdates(cv);
try {
return notRecommended.sort(({ release: { version: left } }, { release: { version: right } }) =>
semver.rcompare(left, right),
);
} catch (e) {
// eslint-disable-next-line no-console
console.error('error sorting conditional cluster updates', e);
return notRecommended;
}
};
export const getNewerMinorVersionUpdate = (currentVersion, availableUpdates) => {
const currentVersionParsed = semver.parse(currentVersion);
return availableUpdates?.find(
// find the next minor version update, which there should never be more than one
(update) => {
const updateParsed = semver.parse(update.version);
return semver.gt(
semver.coerce(`${updateParsed.major}.${updateParsed.minor}`),
semver.coerce(`${currentVersionParsed.major}.${currentVersionParsed.minor}`),
);
},
);
};
export const isMinorVersionNewer = (currentVersion, otherVersion) => {
const currentVersionParsed = semver.parse(currentVersion);
const otherVersionParsed = semver.parse(otherVersion);
return semver.gt(
semver.coerce(`${otherVersionParsed.major}.${otherVersionParsed.minor}`),
semver.coerce(`${currentVersionParsed.major}.${currentVersionParsed.minor}`),
);
};
export const getAvailableClusterChannels = (cv) => {
return cv?.status?.desired?.channels || [];
};
export const getDesiredClusterVersion = (cv: ClusterVersionKind): string => {
return _.get(cv, 'status.desired.version');
};
export const getClusterVersionChannel = (cv: ClusterVersionKind): string => cv?.spec?.channel;
export const splitClusterVersionChannel = (channel: string) => {
const parsed = /^(.+)-(\d\.\d+)$/.exec(channel);
return parsed ? { prefix: parsed[1], version: parsed[2] } : null;
};
export const getSimilarClusterVersionChannels = (cv, currentPrefix) => {
return getAvailableClusterChannels(cv).filter((channel: string) => {
return currentPrefix && splitClusterVersionChannel(channel)?.prefix === currentPrefix;
});
};
export const getNewerClusterVersionChannel = (similarChannels, currentChannel) => {
return similarChannels.find(
// find the next minor version, which there should never be more than one
(channel) => semver.gt(semver.coerce(channel).version, semver.coerce(currentChannel).version),
);
};
export const getLastCompletedUpdate = (cv: ClusterVersionKind): string => {
const history: UpdateHistory[] = _.get(cv, 'status.history', []);
const lastCompleted: UpdateHistory = history.find((update) => update.state === 'Completed');
return lastCompleted && lastCompleted.version;
};
export const getClusterVersionCondition = (
cv: ClusterVersionKind,
type: ClusterVersionConditionType,
status: K8sResourceConditionStatus = undefined,
): ClusterVersionCondition => {
const conditions: ClusterVersionCondition[] = _.get(cv, 'status.conditions');
if (status) {
return _.find(conditions, { type, status });
}
return _.find(conditions, { type });
};
export const isProgressing = (cv: ClusterVersionKind): boolean => {
return !_.isEmpty(
getClusterVersionCondition(
cv,
ClusterVersionConditionType.Progressing,
K8sResourceConditionStatus.True,
),
);
};
export const invalid = (cv: ClusterVersionKind): boolean => {
return !_.isEmpty(
getClusterVersionCondition(
cv,
ClusterVersionConditionType.Invalid,
K8sResourceConditionStatus.True,
),
);
};
export const failedToRetrieveUpdates = (cv: ClusterVersionKind): boolean => {
return !_.isEmpty(
getClusterVersionCondition(
cv,
ClusterVersionConditionType.RetrievedUpdates,
K8sResourceConditionStatus.False,
),
);
};
export const updateFailing = (cv: ClusterVersionKind): boolean => {
return !_.isEmpty(
getClusterVersionCondition(
cv,
ClusterVersionConditionType.Failing,
K8sResourceConditionStatus.True,
),
);
};
export const hasAvailableUpdates = (cv: ClusterVersionKind): boolean => {
return !_.isEmpty(getAvailableClusterUpdates(cv));
};
export const hasNotRecommendedUpdates = (cv: ClusterVersionKind): boolean => {
return !_.isEmpty(getNotRecommendedUpdates(cv));
};
export const getClusterUpdateStatus = (cv: ClusterVersionKind): ClusterUpdateStatus => {
if (invalid(cv)) {
return ClusterUpdateStatus.Invalid;
}
if (isProgressing(cv) && updateFailing(cv)) {
return ClusterUpdateStatus.UpdatingAndFailing;
}
if (updateFailing(cv)) {
return ClusterUpdateStatus.Failing;
}
if (isProgressing(cv)) {
return ClusterUpdateStatus.Updating;
}
if (failedToRetrieveUpdates(cv)) {
return ClusterUpdateStatus.ErrorRetrieving;
}
return hasAvailableUpdates(cv)
? ClusterUpdateStatus.UpdatesAvailable
: ClusterUpdateStatus.UpToDate;
};
export const getK8sGitVersion = (k8sVersionResponse): string =>
_.get(k8sVersionResponse, 'gitVersion');
export const getOpenShiftVersion = (cv: ClusterVersionKind): string => {
const lastUpdate: UpdateHistory = _.get(cv, 'status.history[0]');
if (!lastUpdate) {
return null;
}
return lastUpdate.state === 'Partial' ? `Updating to ${lastUpdate.version}` : lastUpdate.version;
};
export const getCurrentVersion = (cv: ClusterVersionKind): string => {
return _.get(cv, 'status.history[0].version') || _.get(cv, 'spec.desiredUpdate.version');
};
export const getReportBugLink = (cv: ClusterVersionKind): { label: string; href: string } => {
const version: string = getCurrentVersion(cv);
const parsed = semver.parse(version);
if (!parsed) {
return null;
}
// Show a Bugzilla link for prerelease versions and a support case link for supported versions.
const { major, minor, prerelease } = parsed;
const bugzillaVersion = major === 4 && minor <= 3 ? `${major}.${minor}.0` : `${major}.${minor}`;
const environment = encodeURIComponent(`Version: ${version}
Cluster ID: ${cv.spec.clusterID}
Browser: ${window.navigator.userAgent}
`);
return _.isEmpty(prerelease)
? {
label: i18next.t('public~Open support case with Red Hat'),
href: `https://access.redhat.com/support/cases/#/case/new?product=OpenShift%20Container%20Platform&version=${major}.${minor}&clusterId=${cv.spec.clusterID}`,
}
: {
label: i18next.t('public~Report bug to Red Hat'),
href: `https://bugzilla.redhat.com/enter_bug.cgi?product=OpenShift%20Container%20Platform&version=${bugzillaVersion}&cf_environment=${environment}`,
};
};
export const showReleaseNotes = (): boolean => {
return window.SERVER_FLAGS.branding === 'ocp';
};
// example link: https://access.redhat.com/documentation/en-us/openshift_container_platform/4.9/html/release_notes/ocp-4-9-release-notes#ocp-4-9-4
export const getReleaseNotesLink = (version: string): string => {
if (!showReleaseNotes()) {
return null;
}
const parsed = semver.parse(version);
if (!parsed) {
return null;
}
const { major, minor, patch, prerelease } = parsed;
if (major !== 4 || !_.isEmpty(prerelease)) {
return null;
}
return `https://access.redhat.com/documentation/en-us/openshift_container_platform/${major}.${minor}/html/release_notes/ocp-${major}-${minor}-release-notes#ocp-${major}-${minor}-${patch}`;
};
export const getClusterName = (): string => window.SERVER_FLAGS.kubeAPIServerURL || null;
export const getClusterID = (cv: ClusterVersionKind): string => _.get(cv, 'spec.clusterID');
export const getOCMLink = (clusterID: string): string =>
`https://console.redhat.com/openshift/details/${clusterID}`;
export const getConditionUpgradeableFalse = (resource) =>
resource.status?.conditions?.find(
(c) => c.type === 'Upgradeable' && c.status === K8sResourceConditionStatus.False,
);
export const getNotUpgradeableResources = (resources) =>
resources.filter((resource) => getConditionUpgradeableFalse(resource));
export enum NodeTypes {
master = 'control plane',
worker = 'worker',
}
/**
* Intentionally not translated as they are capitalized versions
* of the Node names for display purposes
*/
export enum NodeTypeNames {
Master = 'Control plane',
Worker = 'Worker',
}
export const isMCPMaster = (mcp: MachineConfigPoolKind) => mcp.metadata.name === NodeTypes.master;
export const isMCPWorker = (mcp: MachineConfigPoolKind) => mcp.metadata.name === NodeTypes.worker;
export const isMCPPaused = (mcp: MachineConfigPoolKind) => mcp.spec?.paused;
export const sortMCPsByCreationTimestamp = (a: MachineConfigPoolKind, b: MachineConfigPoolKind) =>
a.metadata.creationTimestamp.localeCompare(b.metadata.creationTimestamp);
export const clusterIsUpToDateOrUpdateAvailable = (status: ClusterUpdateStatus) =>
status === ClusterUpdateStatus.UpToDate || status === ClusterUpdateStatus.UpdatesAvailable;
export const getMCPsToPausePromises = (
machineConfigPools: MachineConfigPoolKind[],
paused: boolean,
) =>
machineConfigPools.map((mcp) => {
const patch = [{ op: 'add', path: '/spec/paused', value: paused }];
return k8sPatch(MachineConfigPoolModel, mcp, patch);
});