-
Notifications
You must be signed in to change notification settings - Fork 153
/
KubeStatusIndicator.tsx
217 lines (187 loc) · 4.93 KB
/
KubeStatusIndicator.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
import _ from "lodash";
import * as React from "react";
import styled from "styled-components";
import { Condition } from "../lib/api/core/types.pb";
import { colors } from "../typedefs/styled.d";
import Flex from "./Flex";
import Icon, { IconType } from "./Icon";
import Text from "./Text";
type Props = {
className?: string;
conditions: Condition[];
short?: boolean;
suspended?: boolean;
noText?: boolean;
};
export enum ReadyType {
Ready = "Ready",
NotReady = "Not Ready",
Reconciling = "Reconciling",
PendingAction = "PendingAction",
Suspended = "Suspended",
None = "None",
}
export enum ReadyStatusValue {
True = "True",
False = "False",
Unknown = "Unknown",
None = "None",
}
export function computeReady(conditions: Condition[]): ReadyType {
if (!conditions?.length) return undefined;
const readyCondition = _.find(
conditions,
(c) => c.type === "Ready" || c.type === "Available"
);
if (readyCondition) {
if (readyCondition.status === ReadyStatusValue.True) {
return ReadyType.Ready;
}
if (readyCondition.status === ReadyStatusValue.Unknown) {
if (readyCondition.reason === "Progressing") return ReadyType.Reconciling;
if (readyCondition.reason === "TerraformPlannedWithChanges")
return ReadyType.PendingAction;
}
if (readyCondition.status === ReadyStatusValue.None) return ReadyType.None;
return ReadyType.NotReady;
}
if (_.find(conditions, (c) => c.status === ReadyStatusValue.False)) {
return ReadyType.NotReady;
}
return ReadyType.Ready;
}
export function computeMessage(conditions: Condition[]) {
if (!conditions?.length) {
return undefined;
}
const readyCondition = _.find(
conditions,
(c) => c.type === "Ready" || c.type === "Available"
);
if (readyCondition) {
return readyCondition.message;
}
const falseCondition = _.find(
conditions,
(c) => c.status === ReadyStatusValue.False
);
if (falseCondition) {
return falseCondition.message;
}
return conditions[0].message;
}
type IndicatorInfo = {
icon: IconType;
color: keyof typeof colors;
type: ReadyType;
};
export const getIndicatorInfo = (
suspended: boolean,
conditions: Condition[]
): IndicatorInfo => {
if (suspended)
return {
icon: IconType.SuspendedIcon,
color: "feedbackOriginal",
type: ReadyType.Suspended,
};
const ready = computeReady(conditions);
if (ready === ReadyType.Reconciling)
return {
type: ReadyType.Reconciling,
icon: IconType.ReconcileIcon,
color: "primary",
};
if (ready === ReadyType.PendingAction)
return {
type: ReadyType.PendingAction,
icon: IconType.PendingActionIcon,
color: "feedbackOriginal",
};
if (ready === ReadyType.Ready)
return {
type: ReadyType.Ready,
icon: IconType.CheckCircleIcon,
color: "successOriginal",
};
if (ready === ReadyType.None)
return {
type: ReadyType.None,
icon: IconType.RemoveCircleIcon,
color: "neutral20",
};
return {
type: ReadyType.NotReady,
icon: IconType.FailedIcon,
color: "alertOriginal",
};
};
export type SpecialObject = "DaemonSet";
interface DaemonSetStatus {
currentNumberScheduled: number;
desiredNumberScheduled: number;
numberMisscheduled: number;
numberReady: number;
numberUnavailable: number;
observedGeneration: number;
updatedNumberScheduled: number;
}
const NotReady: Condition = {
type: ReadyType.Ready,
status: ReadyStatusValue.False,
message: "Not Ready",
};
const Ready: Condition = {
type: ReadyType.Ready,
status: ReadyStatusValue.True,
message: "Ready",
};
const Unknown: Condition = {
type: ReadyType.Ready,
status: ReadyStatusValue.Unknown,
message: "Unknown",
};
// Certain objects to not have a status.conditions key, so we generate those conditions
// and feed it into the `KubeStatusIndicator` to keep the public API consistent.
export function createSyntheticCondition(
kind: SpecialObject,
// This will eventually be a union type when we add another special object.
// Example: DaemonSetStatus | CoolObjectStatus | ...
status: DaemonSetStatus
): Condition {
switch (kind) {
case "DaemonSet":
if (status.numberReady === status.desiredNumberScheduled) {
return Ready;
}
return NotReady;
default:
return Unknown;
}
}
function KubeStatusIndicator({
className,
conditions,
short,
suspended,
noText,
}: Props) {
const { type, color, icon } = getIndicatorInfo(suspended, conditions);
let text;
if (noText) text = null;
else if (short || suspended) text = type;
else text = computeMessage(conditions);
return (
<Flex start className={className} align>
<Icon size="base" type={icon} color={color} text={text} />
</Flex>
);
}
export default styled(KubeStatusIndicator).attrs({
className: KubeStatusIndicator.name,
})`
${Icon} ${Text} {
color: ${(props) => props.theme.colors.neutral40};
font-weight: 400;
}
`;