mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Optimize Deployment workloads status computation
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
5994ef748c
commit
1dc98a7dd9
@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* 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 { Deployment, PodStatusPhase } from "../../../common/k8s-api/endpoints";
|
||||||
|
import { byLabels } from "../../../common/k8s-api/kube-object.store";
|
||||||
|
import { getOrInsert } from "../../utils";
|
||||||
|
import { foldPodStatusPhase } from "../../utils/fold-pod-status-phase";
|
||||||
|
import deploymentStoreInjectable from "./store.injectable";
|
||||||
|
|
||||||
|
const statusCountsForAllDeploymentsInSelectedNamespacesInjectable = getInjectable({
|
||||||
|
id: "status-counts-for-all-deployments-in-selected-namespaces",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const deploymentStore = di.inject(deploymentStoreInjectable);
|
||||||
|
const podStore = di.inject(podStoreInjectable);
|
||||||
|
|
||||||
|
return computed(() => {
|
||||||
|
const statuses = { running: 0, failed: 0, pending: 0 };
|
||||||
|
const podsByNamespace = new Map<string, { metadata: { labels: Partial<Record<string, string>> }; status: PodStatusPhase }[]>();
|
||||||
|
|
||||||
|
for (const pod of podStore.items) {
|
||||||
|
getOrInsert(podsByNamespace, pod.getNs(), []).push({
|
||||||
|
metadata: {
|
||||||
|
labels: JSON.parse(JSON.stringify(pod.metadata.labels ?? {})),
|
||||||
|
},
|
||||||
|
status: pod.getStatus(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const getChildPods = (deployment: Deployment) => {
|
||||||
|
const pods = podsByNamespace.get(deployment.getNs()) ?? [];
|
||||||
|
|
||||||
|
return pods.filter(byLabels(deployment.spec.template.metadata.labels));
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const deployment of deploymentStore.contextItems) {
|
||||||
|
const status = getChildPods(deployment)
|
||||||
|
.map(pod => pod.status)
|
||||||
|
.reduce(foldPodStatusPhase, "running");
|
||||||
|
|
||||||
|
statuses[status]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return statuses;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default statusCountsForAllDeploymentsInSelectedNamespacesInjectable;
|
||||||
|
|
||||||
@ -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 deploymentStoreInjectable from "./store.injectable";
|
||||||
|
|
||||||
|
const totalCountOfDeploymentsInSelectedNamespacesInjectable = getInjectable({
|
||||||
|
id: "total-count-of-deployments-in-selected-namespaces",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const deploymentStore = di.inject(deploymentStoreInjectable);
|
||||||
|
|
||||||
|
return computed(() => deploymentStore.getTotalCount());
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default totalCountOfDeploymentsInSelectedNamespacesInjectable;
|
||||||
@ -5,38 +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 deploymentsStoreInjectable from "../../../+workloads-deployments/store.injectable";
|
|
||||||
import navigateToDeploymentsInjectable from "../../../../../common/front-end-routing/routes/cluster/workloads/deployments/navigate-to-deployments.injectable";
|
import navigateToDeploymentsInjectable from "../../../../../common/front-end-routing/routes/cluster/workloads/deployments/navigate-to-deployments.injectable";
|
||||||
import { computed } from "mobx";
|
import totalCountOfDeploymentsInSelectedNamespacesInjectable from "../../../+workloads-deployments/total-count.injectable";
|
||||||
|
import statusCountsForAllDeploymentsInSelectedNamespacesInjectable from "../../../+workloads-deployments/statuses.injectable";
|
||||||
|
|
||||||
const deploymentsWorkloadInjectable = getInjectable({
|
const deploymentsWorkloadInjectable = getInjectable({
|
||||||
id: "deployments-workload",
|
id: "deployments-workload",
|
||||||
|
|
||||||
instantiate: (di) => {
|
instantiate: (di) => ({
|
||||||
const navigate = di.inject(navigateToDeploymentsInjectable);
|
resource: {
|
||||||
const namespaceStore = di.inject(namespaceStoreInjectable);
|
apiName: "deployments",
|
||||||
const store = di.inject(deploymentsStoreInjectable);
|
group: "apps",
|
||||||
|
},
|
||||||
return {
|
open: di.inject(navigateToDeploymentsInjectable),
|
||||||
resource: {
|
amountOfItems: di.inject(totalCountOfDeploymentsInSelectedNamespacesInjectable),
|
||||||
apiName: "deployments",
|
status: di.inject(statusCountsForAllDeploymentsInSelectedNamespacesInjectable),
|
||||||
group: "apps",
|
title: ResourceNames.deployments,
|
||||||
},
|
orderNumber: 20,
|
||||||
open: navigate,
|
}),
|
||||||
|
|
||||||
amountOfItems: computed(
|
|
||||||
() => store.getAllByNs(namespaceStore.contextNamespaces).length,
|
|
||||||
),
|
|
||||||
|
|
||||||
status: computed(() =>
|
|
||||||
store.getStatuses(store.getAllByNs(namespaceStore.contextNamespaces)),
|
|
||||||
),
|
|
||||||
|
|
||||||
title: ResourceNames.deployments,
|
|
||||||
orderNumber: 20,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
injectionToken: workloadInjectionToken,
|
injectionToken: workloadInjectionToken,
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user