mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Improve correctness and efficiency in some of pods api helper functions (#2067)
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
8eb8fb6234
commit
c7e1ab27fd
2 files changed
+349
-67
No files matched your search
@@ -270,60 +270,55 @@ export class Pod extends WorkloadKubeObject {
|
||||
}
|
||||
|
||||
getAllContainers() {
|
||||
return this.getContainers().concat(this.getInitContainers());
|
||||
return [...this.getContainers(), ...this.getInitContainers()];
|
||||
}
|
||||
|
||||
getRunningContainers() {
|
||||
const statuses = this.getContainerStatuses();
|
||||
|
||||
return this.getAllContainers().filter(container => {
|
||||
return statuses.find(status => status.name === container.name && !!status.state["running"]);
|
||||
}
|
||||
const runningContainerNames = new Set(
|
||||
this.getContainerStatuses()
|
||||
.filter(({ state }) => state.running)
|
||||
.map(({ name }) => name)
|
||||
);
|
||||
|
||||
return this.getAllContainers()
|
||||
.filter(({ name }) => runningContainerNames.has(name));
|
||||
}
|
||||
|
||||
getContainerStatuses(includeInitContainers = true) {
|
||||
const statuses: IPodContainerStatus[] = [];
|
||||
const { containerStatuses, initContainerStatuses } = this.status;
|
||||
const { containerStatuses = [], initContainerStatuses = [] } = this.status ?? {};
|
||||
|
||||
if (containerStatuses) {
|
||||
statuses.push(...containerStatuses);
|
||||
if (includeInitContainers) {
|
||||
return [...containerStatuses, ...initContainerStatuses];
|
||||
}
|
||||
|
||||
if (includeInitContainers && initContainerStatuses) {
|
||||
statuses.push(...initContainerStatuses);
|
||||
}
|
||||
|
||||
return statuses;
|
||||
return [...containerStatuses];
|
||||
}
|
||||
|
||||
getRestartsCount(): number {
|
||||
const { containerStatuses } = this.status;
|
||||
const { containerStatuses = [] } = this.status ?? {};
|
||||
|
||||
if (!containerStatuses) return 0;
|
||||
|
||||
return containerStatuses.reduce((count, item) => count + item.restartCount, 0);
|
||||
return containerStatuses.reduce((totalCount, { restartCount }) => totalCount + restartCount, 0);
|
||||
}
|
||||
|
||||
getQosClass() {
|
||||
return this.status.qosClass || "";
|
||||
return this.status?.qosClass || "";
|
||||
}
|
||||
|
||||
getReason() {
|
||||
return this.status.reason || "";
|
||||
return this.status?.reason || "";
|
||||
}
|
||||
|
||||
getPriorityClassName() {
|
||||
return this.spec.priorityClassName || "";
|
||||
}
|
||||
|
||||
// Returns one of 5 statuses: Running, Succeeded, Pending, Failed, Evicted
|
||||
getStatus() {
|
||||
getStatus(): PodStatus {
|
||||
const phase = this.getStatusPhase();
|
||||
const reason = this.getReason();
|
||||
const goodConditions = ["Initialized", "Ready"].every(condition =>
|
||||
!!this.getConditions().find(item => item.type === condition && item.status === "True")
|
||||
);
|
||||
const trueConditionTypes = new Set(this.getConditions()
|
||||
.filter(({ status }) => status === "True")
|
||||
.map(({ type }) => type));
|
||||
const isInGoodCondition = ["Initialized", "Ready"].every(condition => trueConditionTypes.has(condition));
|
||||
|
||||
if (reason === PodStatus.EVICTED) {
|
||||
return PodStatus.EVICTED;
|
||||
@@ -337,7 +332,7 @@ export class Pod extends WorkloadKubeObject {
|
||||
return PodStatus.SUCCEEDED;
|
||||
}
|
||||
|
||||
if (phase === PodStatus.RUNNING && goodConditions) {
|
||||
if (phase === PodStatus.RUNNING && isInGoodCondition) {
|
||||
return PodStatus.RUNNING;
|
||||
}
|
||||
|
||||
@@ -349,37 +344,27 @@ export class Pod extends WorkloadKubeObject {
|
||||
if (this.getReason() === PodStatus.EVICTED) return "Evicted";
|
||||
if (this.getStatus() === PodStatus.RUNNING && this.metadata.deletionTimestamp) return "Terminating";
|
||||
|
||||
let message = "";
|
||||
const statuses = this.getContainerStatuses(false); // not including initContainers
|
||||
|
||||
if (statuses.length) {
|
||||
statuses.forEach(status => {
|
||||
const { state } = status;
|
||||
for (const { state } of statuses.reverse()) {
|
||||
if (state.waiting) {
|
||||
return state.waiting.reason || "Waiting";
|
||||
}
|
||||
|
||||
if (state.waiting) {
|
||||
const { reason } = state.waiting;
|
||||
|
||||
message = reason ? reason : "Waiting";
|
||||
}
|
||||
|
||||
if (state.terminated) {
|
||||
const { reason } = state.terminated;
|
||||
|
||||
message = reason ? reason : "Terminated";
|
||||
}
|
||||
});
|
||||
if (state.terminated) {
|
||||
return state.terminated.reason || "Terminated";
|
||||
}
|
||||
}
|
||||
if (message) return message;
|
||||
|
||||
return this.getStatusPhase();
|
||||
}
|
||||
|
||||
getStatusPhase() {
|
||||
return this.status.phase;
|
||||
return this.status?.phase;
|
||||
}
|
||||
|
||||
getConditions() {
|
||||
return this.status.conditions || [];
|
||||
return this.status?.conditions || [];
|
||||
}
|
||||
|
||||
getVolumes() {
|
||||
@@ -393,9 +378,7 @@ export class Pod extends WorkloadKubeObject {
|
||||
}
|
||||
|
||||
getNodeSelectors(): string[] {
|
||||
const { nodeSelector } = this.spec;
|
||||
|
||||
if (!nodeSelector) return [];
|
||||
const { nodeSelector = {} } = this.spec;
|
||||
|
||||
return Object.entries(nodeSelector).map(values => values.join(": "));
|
||||
}
|
||||
@@ -409,20 +392,19 @@ export class Pod extends WorkloadKubeObject {
|
||||
}
|
||||
|
||||
hasIssues() {
|
||||
const notReady = !!this.getConditions().find(condition => {
|
||||
return condition.type == "Ready" && condition.status !== "True";
|
||||
});
|
||||
const crashLoop = !!this.getContainerStatuses().find(condition => {
|
||||
const waiting = condition.state.waiting;
|
||||
for (const { type, status } of this.getConditions()) {
|
||||
if (type === "Ready" && status !== "True") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return (waiting && waiting.reason == "CrashLoopBackOff");
|
||||
});
|
||||
for (const { state } of this.getContainerStatuses()) {
|
||||
if (state?.waiting?.reason === "CrashLookBackOff") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
notReady ||
|
||||
crashLoop ||
|
||||
this.getStatusPhase() !== "Running"
|
||||
);
|
||||
return this.getStatusPhase() !== "Running";
|
||||
}
|
||||
|
||||
getLivenessProbe(container: IPodContainer) {
|
||||
@@ -476,14 +458,11 @@ export class Pod extends WorkloadKubeObject {
|
||||
}
|
||||
|
||||
getNodeName() {
|
||||
return this.spec?.nodeName;
|
||||
return this.spec.nodeName;
|
||||
}
|
||||
|
||||
getSelectedNodeOs() {
|
||||
if (!this.spec.nodeSelector) return;
|
||||
if (!this.spec.nodeSelector["kubernetes.io/os"] && !this.spec.nodeSelector["beta.kubernetes.io/os"]) return;
|
||||
|
||||
return this.spec.nodeSelector["kubernetes.io/os"] || this.spec.nodeSelector["beta.kubernetes.io/os"];
|
||||
getSelectedNodeOs(): string | undefined {
|
||||
return this.spec.nodeSelector?.["kubernetes.io/os"] || this.spec.nodeSelector?.["beta.kubernetes.io/os"];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user