-
Notifications
You must be signed in to change notification settings - Fork 593
/
pod-ring-utils.ts
261 lines (240 loc) · 8.15 KB
/
pod-ring-utils.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
import * as React from 'react';
import { ChartLabel } from '@patternfly/react-charts';
import * as classNames from 'classnames';
import i18next, { TFunction } from 'i18next';
import * as _ from 'lodash';
import { ImpersonateKind, useSafetyFirst } from '@console/dynamic-plugin-sdk';
import { DaemonSetModel, PodModel, JobModel, CronJobModel } from '@console/internal/models';
import {
K8sResourceKind,
K8sKind,
SelfSubjectAccessReviewKind,
HorizontalPodAutoscalerKind,
} from '@console/internal/module/k8s';
import { RevisionModel } from '@console/knative-plugin';
import { AllPodStatus } from '../constants';
import { ExtPodKind } from '../types';
import { checkPodEditAccess, getPodStatus } from './pod-utils';
import './pod-ring-text.scss';
type PodRingLabelType = {
subTitle: string;
title: string;
titleComponent: React.ReactElement;
};
type PodRingLabelData = {
title: string;
longTitle: boolean;
subTitle: string;
longSubtitle: boolean;
reversed: boolean;
};
const podKindString = (count: number) =>
count === 1 ? i18next.t('console-shared~Pod') : i18next.t('console-shared~Pods');
const isPendingPods = (
pods: ExtPodKind[],
currentPodCount: number,
desiredPodCount: number,
): boolean =>
(pods?.length === 1 && pods[0].status?.phase === 'Pending') ||
(!currentPodCount && !!desiredPodCount);
export const getFailedPods = (pods: ExtPodKind[]): number => {
if (!pods?.length) {
return 0;
}
return pods.reduce((acc, currValue) => {
if ([AllPodStatus.CrashLoopBackOff, AllPodStatus.Failed].includes(getPodStatus(currValue))) {
return acc + 1;
}
return acc;
}, 0);
};
const getTitleAndSubtitle = (
isPending: boolean,
currentPodCount: number,
desiredPodCount: number,
t: TFunction,
) => {
let titlePhrase;
let subTitlePhrase = '';
let longTitle = false;
let longSubtitle = false;
// handles the initial state when the first pod is coming up and the state for no pods(scaled to zero)
if (!currentPodCount) {
titlePhrase = isPending ? '0' : t('console-shared~Scaled to 0');
longTitle = !isPending;
if (desiredPodCount) {
subTitlePhrase = t('console-shared~Scaling to {{podSubTitle}}', {
podSubTitle: desiredPodCount,
});
longSubtitle = true;
}
}
// handles the idle state or scaling to desired no. of pods
if (currentPodCount) {
titlePhrase = currentPodCount.toString();
if (currentPodCount === desiredPodCount) {
subTitlePhrase = podKindString(currentPodCount);
} else {
subTitlePhrase = t('console-shared~Scaling to {{podSubTitle}}', {
podSubTitle: desiredPodCount,
});
longSubtitle = true;
}
}
return { title: titlePhrase, longTitle, subTitle: subTitlePhrase, longSubtitle };
};
const getTitleComponent = (
longTitle: boolean = false,
longSubtitle: boolean = false,
reversed: boolean = false,
) => {
const labelClasses = classNames('pf-v5-chart-donut-title', {
'pod-ring__center-text--reversed': reversed,
'pod-ring__center-text': !reversed,
'pod-ring__long-text': longTitle,
});
return React.createElement(ChartLabel, {
dy: longSubtitle ? -5 : 0,
style: { lineHeight: '11px' },
className: labelClasses,
});
};
export const podRingLabel = (
obj: K8sResourceKind,
ownerKind: string,
pods: ExtPodKind[],
t: TFunction,
): PodRingLabelData => {
let currentPodCount;
let desiredPodCount;
let isPending;
let titleData;
const podRingLabelData: PodRingLabelData = {
title: '',
subTitle: '',
longTitle: false,
longSubtitle: false,
reversed: false,
};
const failedPodCount = getFailedPods(pods);
switch (ownerKind) {
case DaemonSetModel.kind:
currentPodCount = (obj.status?.currentNumberScheduled || 0) + failedPodCount;
desiredPodCount = obj.status?.desiredNumberScheduled;
desiredPodCount = obj.status?.desiredNumberScheduled;
isPending = isPendingPods(pods, currentPodCount, desiredPodCount);
titleData = getTitleAndSubtitle(isPending, currentPodCount, desiredPodCount, t);
podRingLabelData.title = titleData.title;
podRingLabelData.subTitle = titleData.subTitle;
podRingLabelData.longSubtitle = titleData.longSubtitle;
break;
case RevisionModel.kind:
currentPodCount = (obj.status?.readyReplicas || 0) + failedPodCount;
desiredPodCount = obj.spec?.replicas;
isPending = isPendingPods(pods, currentPodCount, desiredPodCount);
if (!isPending && !desiredPodCount) {
podRingLabelData.title = t('console-shared~Autoscaled');
podRingLabelData.subTitle = t('console-shared~to 0');
podRingLabelData.reversed = true;
break;
}
if (isPending) {
podRingLabelData.title = '0';
podRingLabelData.subTitle = t('console-shared~Scaling to {{podSubTitle}}', {
podSubTitle: desiredPodCount,
});
} else {
podRingLabelData.title = currentPodCount;
podRingLabelData.subTitle = podKindString(currentPodCount);
}
break;
case PodModel.kind:
case JobModel.kind:
podRingLabelData.title = '1';
podRingLabelData.subTitle = PodModel.label;
break;
case CronJobModel.kind:
podRingLabelData.title = `${pods.length}`;
podRingLabelData.subTitle = podKindString(currentPodCount);
break;
default:
currentPodCount = (obj.status?.readyReplicas || 0) + failedPodCount;
desiredPodCount = obj.spec?.replicas;
isPending = isPendingPods(pods, currentPodCount, desiredPodCount);
titleData = getTitleAndSubtitle(isPending, currentPodCount, desiredPodCount, t);
podRingLabelData.title = titleData.title;
podRingLabelData.subTitle = titleData.subTitle;
podRingLabelData.longTitle = titleData.longTitle;
podRingLabelData.longSubtitle = titleData.longSubtitle;
break;
}
return podRingLabelData;
};
export const hpaPodRingLabel = (
obj: K8sResourceKind,
hpa: HorizontalPodAutoscalerKind,
pods: ExtPodKind[],
t: TFunction,
): PodRingLabelData => {
const desiredPodCount = obj.spec?.replicas;
const desiredPods = hpa.status?.desiredReplicas || desiredPodCount;
const currentPods = hpa.status?.currentReplicas;
const scaling =
(!currentPods && !!desiredPods) || !pods?.every((p) => p.status?.phase === 'Running');
return {
title: scaling ? t('console-shared~Autoscaling') : t('console-shared~Autoscaled'),
subTitle: t('console-shared~to {{count}} Pod', { count: desiredPods }),
longTitle: false,
longSubtitle: false,
reversed: true,
};
};
export const usePodRingLabel = (
obj: K8sResourceKind,
ownerKind: string,
pods: ExtPodKind[],
hpaControlledScaling: boolean = false,
t: TFunction,
hpa?: HorizontalPodAutoscalerKind,
): PodRingLabelType => {
const podRingLabelData = hpaControlledScaling
? hpaPodRingLabel(obj, hpa, pods, t)
: podRingLabel(obj, ownerKind, pods, t);
const { title, subTitle, longTitle, longSubtitle, reversed } = podRingLabelData;
return React.useMemo(
() => ({
title,
subTitle,
titleComponent: getTitleComponent(longTitle, longSubtitle, reversed),
}),
[longSubtitle, longTitle, reversed, subTitle, title],
);
};
export const usePodScalingAccessStatus = (
obj: K8sResourceKind,
resourceKind: K8sKind,
pods: ExtPodKind[],
enableScaling?: boolean,
impersonate?: ImpersonateKind,
) => {
const isKnativeRevision = obj.kind === 'Revision';
const isPod = obj.kind === 'Pod';
const isScalingAllowed = !isKnativeRevision && !isPod && enableScaling;
const [editable, setEditable] = useSafetyFirst(false);
React.useEffect(() => {
if (isScalingAllowed) {
checkPodEditAccess(obj, resourceKind, impersonate, 'scale')
.then((resp: SelfSubjectAccessReviewKind) =>
setEditable(_.get(resp, 'status.allowed', false)),
)
.catch((error) => {
// console.log is used here instead of throw error
// throw error will break the thread and likely end-up in a white screen
// eslint-disable-next-line
console.log('Checking pod edit acceess failed:', error);
setEditable(false);
});
}
}, [pods, obj, resourceKind, impersonate, setEditable, isScalingAllowed]);
return editable;
};