mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Optimize ReplicaSet workloads status computation
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
6072e9ca65
commit
5994ef748c
@ -5,33 +5,24 @@
|
|||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { workloadInjectionToken } from "../workload-injection-token";
|
import { workloadInjectionToken } from "../workload-injection-token";
|
||||||
import { ResourceNames } from "../../../../utils/rbac";
|
import { ResourceNames } from "../../../../utils/rbac";
|
||||||
import namespaceStoreInjectable from "../../../+namespaces/store.injectable";
|
|
||||||
import replicasetsStoreInjectable from "../../../+workloads-replicasets/store.injectable";
|
|
||||||
import { computed } from "mobx";
|
|
||||||
import navigateToReplicasetsInjectable from "../../../../../common/front-end-routing/routes/cluster/workloads/replicasets/navigate-to-replicasets.injectable";
|
import navigateToReplicasetsInjectable from "../../../../../common/front-end-routing/routes/cluster/workloads/replicasets/navigate-to-replicasets.injectable";
|
||||||
|
import statusCountsForAllReplicaSetsInSelectedNamespacesInjectable from "../../../+workloads-replicasets/statuses.injectable";
|
||||||
|
import totalCountOfReplicaSetsInSelectedNamespacesInjectable from "../../../+workloads-replicasets/total-count.injectable";
|
||||||
|
|
||||||
const replicasetsWorkloadInjectable = getInjectable({
|
const replicasetsWorkloadInjectable = getInjectable({
|
||||||
id: "replicasets-workload",
|
id: "replicasets-workload",
|
||||||
|
|
||||||
instantiate: (di) => {
|
instantiate: (di) => ({
|
||||||
const navigate = di.inject(navigateToReplicasetsInjectable);
|
resource: {
|
||||||
const namespaceStore = di.inject(namespaceStoreInjectable);
|
apiName: "replicasets",
|
||||||
const store = di.inject(replicasetsStoreInjectable);
|
group: "apps",
|
||||||
|
},
|
||||||
return {
|
open: di.inject(navigateToReplicasetsInjectable),
|
||||||
resource: {
|
amountOfItems: di.inject(totalCountOfReplicaSetsInSelectedNamespacesInjectable),
|
||||||
apiName: "replicasets",
|
status: di.inject(statusCountsForAllReplicaSetsInSelectedNamespacesInjectable),
|
||||||
group: "apps",
|
title: ResourceNames.replicasets,
|
||||||
},
|
orderNumber: 50,
|
||||||
open: navigate,
|
}),
|
||||||
amountOfItems: computed(() => store.getTotalCount()),
|
|
||||||
status: computed(() =>
|
|
||||||
store.getStatuses(store.getAllByNs(namespaceStore.contextNamespaces)),
|
|
||||||
),
|
|
||||||
title: ResourceNames.replicasets,
|
|
||||||
orderNumber: 50,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
injectionToken: workloadInjectionToken,
|
injectionToken: workloadInjectionToken,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import { computed } from "mobx";
|
||||||
|
import podStoreInjectable from "../+workloads-pods/store.injectable";
|
||||||
|
import type { PodStatusPhase } from "../../../common/k8s-api/endpoints";
|
||||||
|
import { getOrInsert } from "../../utils";
|
||||||
|
import { foldPodStatusPhase } from "../../utils/fold-pod-status-phase";
|
||||||
|
import replicaSetStoreInjectable from "./store.injectable";
|
||||||
|
|
||||||
|
interface PodData {
|
||||||
|
status: PodStatusPhase;
|
||||||
|
}
|
||||||
|
|
||||||
|
const statusCountsForAllReplicaSetsInSelectedNamespacesInjectable = getInjectable({
|
||||||
|
id: "status-counts-for-all-replica-sets-in-selected-namespaces",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const podStore = di.inject(podStoreInjectable);
|
||||||
|
const replicaSetStore = di.inject(replicaSetStoreInjectable);
|
||||||
|
|
||||||
|
return computed(() => {
|
||||||
|
const podsByOwnerId = new Map<string, PodData[]>();
|
||||||
|
const statuses = { running: 0, failed: 0, pending: 0 };
|
||||||
|
|
||||||
|
for (const pod of podStore.contextItems) {
|
||||||
|
for (const ownerRef of pod.getOwnerRefs()) {
|
||||||
|
getOrInsert(podsByOwnerId, ownerRef.uid, []).push({
|
||||||
|
status: pod.getStatus(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const replicaSet of replicaSetStore.contextItems) {
|
||||||
|
const status = (podsByOwnerId.get(replicaSet.getId()) ?? [])
|
||||||
|
.map(pod => pod.status)
|
||||||
|
.reduce(foldPodStatusPhase, "running");
|
||||||
|
|
||||||
|
statuses[status]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return statuses;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default statusCountsForAllReplicaSetsInSelectedNamespacesInjectable;
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import { computed } from "mobx";
|
||||||
|
import replicaSetStoreInjectable from "./store.injectable";
|
||||||
|
|
||||||
|
const totalCountOfReplicaSetsInSelectedNamespacesInjectable = getInjectable({
|
||||||
|
id: "total-count-of-replica-sets-in-selected-namespaces",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const store = di.inject(replicaSetStoreInjectable);
|
||||||
|
|
||||||
|
return computed(() => store.getTotalCount());
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default totalCountOfReplicaSetsInSelectedNamespacesInjectable;
|
||||||
18
packages/core/src/renderer/utils/fold-pod-status-phase.ts
Normal file
18
packages/core/src/renderer/utils/fold-pod-status-phase.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { PodStatusPhase } from "../../common/k8s-api/endpoints";
|
||||||
|
|
||||||
|
export const foldPodStatusPhase = (previous: "failed" | "pending" | "running", current: PodStatusPhase): "failed" | "pending" | "running" => {
|
||||||
|
if (previous === "failed" || current === PodStatusPhase.FAILED) {
|
||||||
|
return "failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (previous === "pending" || current === PodStatusPhase.PENDING) {
|
||||||
|
return "pending";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "running";
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user