Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Support metrics for Pods with multiple containers #7866

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { MetricData } from "../metrics.api";
import type { Pod } from "@k8slens/kube-object";
import type { Pod, Container } from "@k8slens/kube-object";
import requestMetricsInjectable from "./request-metrics.injectable";

export interface PodMetricData {
Expand All @@ -21,16 +21,16 @@ export interface PodMetricData {
memoryLimits: MetricData;
}

export type RequestPodMetrics = (pods: Pod[], namespace: string, selector?: string) => Promise<PodMetricData>;
export type RequestPodMetrics = (pods: Pod[], namespace: string, container?: Container, selector?: string) => Promise<PodMetricData>;

const requestPodMetricsInjectable = getInjectable({
id: "request-pod-metrics",
instantiate: (di): RequestPodMetrics => {
const requestMetrics = di.inject(requestMetricsInjectable);

return (pods, namespace, selector = "pod, namespace") => {
return (pods, namespace, container, selector = "pod, namespace") => {
const podSelector = pods.map(pod => pod.getName()).join("|");
const opts = { category: "pods", pods: podSelector, namespace, selector };
const opts = { category: "pods", pods: podSelector, container: container?.name, namespace, selector };

return requestMetrics({
cpuUsage: opts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { asyncComputed } from "@ogre-tools/injectable-react";
import { now } from "mobx-utils";
import type { Pod } from "@k8slens/kube-object";
import type { Pod, Container } from "@k8slens/kube-object";
import requestPodMetricsInjectable from "../../../common/k8s-api/endpoints/metrics.api/request-pod-metrics.injectable";

interface PodContainerParams {
pod: Pod;
container: Container;
}

const podContainerMetricsInjectable = getInjectable({
id: "pod-container-metrics",
instantiate: (di, pod) => {
JohnDaly marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -17,12 +22,14 @@ const podContainerMetricsInjectable = getInjectable({
getValueFromObservedPromise: () => {
now(60 * 1000);

return requestPodMetrics([pod], pod.getNs(), "container, namespace");
return requestPodMetrics([pod], pod.getNs(), container, "pod, container, namespace");
},
});
},
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, pod: Pod) => pod.getId(),
getInstanceKey: (di, { pod, container }: PodContainerParams) => {
return `${pod.getId()}-${container.name}`;
},
}),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ const NonInjectedPodDetailsContainerMetrics = observer(({ pod, container, podCon
export const PodDetailsContainerMetrics = withInjectables<Dependencies, ContainerMetricsProps>(NonInjectedPodDetailsContainerMetrics, {
getProps: (di, props) => ({
...props,
podContainerMetrics: di.inject(podContainerMetricsInjectable, props.pod),
podContainerMetrics: di.inject(podContainerMetricsInjectable, { pod: props.pod, container: props.container }),
}),
});