1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Introducing self-expanatory metric request functions

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-07-19 12:58:56 +03:00
parent 9c2942c333
commit 8e1bc60780
12 changed files with 198 additions and 60 deletions

View File

@ -52,6 +52,26 @@ export class ClusterApi extends KubeApi<Cluster> {
} }
} }
export function getMetricsByNodeNames(nodeNames: string[], params?: IMetricsReqParams): Promise<IClusterMetrics> {
const nodes = nodeNames.join("|");
const opts = { category: "cluster", nodes };
return metricsApi.getMetrics({
memoryUsage: opts,
memoryRequests: opts,
memoryLimits: opts,
memoryCapacity: opts,
cpuUsage: opts,
cpuRequests: opts,
cpuLimits: opts,
cpuCapacity: opts,
podUsage: opts,
podCapacity: opts,
fsSize: opts,
fsUsage: opts
}, params);
}
export enum ClusterStatus { export enum ClusterStatus {
ACTIVE = "Active", ACTIVE = "Active",
CREATING = "Creating", CREATING = "Creating",

View File

@ -20,11 +20,12 @@
*/ */
import get from "lodash/get"; import get from "lodash/get";
import type { IPodContainer } from "./pods.api";
import { IAffinity, WorkloadKubeObject } from "../workload-kube-object"; import { IAffinity, WorkloadKubeObject } from "../workload-kube-object";
import { autoBind } from "../../utils"; import { autoBind } from "../../utils";
import { KubeApi } from "../kube-api"; import { KubeApi } from "../kube-api";
import { metricsApi } from "./metrics.api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
import type { IPodContainer, IPodMetrics } from "./pods.api";
export class DaemonSet extends WorkloadKubeObject { export class DaemonSet extends WorkloadKubeObject {
static kind = "DaemonSet"; static kind = "DaemonSet";
@ -97,6 +98,24 @@ export class DaemonSet extends WorkloadKubeObject {
} }
} }
export const daemonSetApi = new KubeApi({ export class DaemonSetApi extends KubeApi<DaemonSet> {
}
export function getMetricsForDaemonSets(daemonsets: DaemonSet[], namespace: string, selector = ""): Promise<IPodMetrics> {
const podSelector = daemonsets.map(daemonset => `${daemonset.getName()}-[[:alnum:]]{5}`).join("|");
const opts = { category: "pods", pods: podSelector, namespace, selector };
return metricsApi.getMetrics({
cpuUsage: opts,
memoryUsage: opts,
fsUsage: opts,
networkReceive: opts,
networkTransmit: opts,
}, {
namespace,
});
}
export const daemonSetApi = new DaemonSetApi({
objectConstructor: DaemonSet, objectConstructor: DaemonSet,
}); });

View File

@ -24,6 +24,8 @@ import moment from "moment";
import { IAffinity, WorkloadKubeObject } from "../workload-kube-object"; import { IAffinity, WorkloadKubeObject } from "../workload-kube-object";
import { autoBind } from "../../utils"; import { autoBind } from "../../utils";
import { KubeApi } from "../kube-api"; import { KubeApi } from "../kube-api";
import { metricsApi } from "./metrics.api";
import type { IPodMetrics } from "./pods.api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
export class DeploymentApi extends KubeApi<Deployment> { export class DeploymentApi extends KubeApi<Deployment> {
@ -68,6 +70,21 @@ export class DeploymentApi extends KubeApi<Deployment> {
} }
} }
export function getMetricsForDeployments(deployments: Deployment[], namespace: string, selector = ""): Promise<IPodMetrics> {
const podSelector = deployments.map(deployment => `${deployment.getName()}-[[:alnum:]]{9,}-[[:alnum:]]{5}`).join("|");
const opts = { category: "pods", pods: podSelector, namespace, selector };
return metricsApi.getMetrics({
cpuUsage: opts,
memoryUsage: opts,
fsUsage: opts,
networkReceive: opts,
networkTransmit: opts,
}, {
namespace,
});
}
interface IContainerProbe { interface IContainerProbe {
httpGet?: { httpGet?: {
path?: string; path?: string;

View File

@ -26,18 +26,19 @@ import { KubeApi } from "../kube-api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
export class IngressApi extends KubeApi<Ingress> { export class IngressApi extends KubeApi<Ingress> {
getMetrics(ingress: string, namespace: string): Promise<IIngressMetrics> { }
const opts = { category: "ingress", ingress, namespace };
return metricsApi.getMetrics({ export function getMetricsForIngress(ingress: string, namespace: string): Promise<IIngressMetrics> {
bytesSentSuccess: opts, const opts = { category: "ingress", ingress };
bytesSentFailure: opts,
requestDurationSeconds: opts, return metricsApi.getMetrics({
responseDurationSeconds: opts bytesSentSuccess: opts,
}, { bytesSentFailure: opts,
namespace, requestDurationSeconds: opts,
}); responseDurationSeconds: opts
} }, {
namespace,
});
} }
export interface IIngressMetrics<T = IMetrics> { export interface IIngressMetrics<T = IMetrics> {

View File

@ -22,10 +22,11 @@
import get from "lodash/get"; import get from "lodash/get";
import { autoBind } from "../../utils"; import { autoBind } from "../../utils";
import { IAffinity, WorkloadKubeObject } from "../workload-kube-object"; import { IAffinity, WorkloadKubeObject } from "../workload-kube-object";
import type { IPodContainer } from "./pods.api";
import { KubeApi } from "../kube-api"; import { KubeApi } from "../kube-api";
import { metricsApi } from "./metrics.api";
import type { JsonApiParams } from "../json-api"; import type { JsonApiParams } from "../json-api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
import type { IPodContainer, IPodMetrics } from "./pods.api";
export class Job extends WorkloadKubeObject { export class Job extends WorkloadKubeObject {
static kind = "Job"; static kind = "Job";
@ -129,6 +130,24 @@ export class Job extends WorkloadKubeObject {
} }
} }
export const jobApi = new KubeApi({ export class JobApi extends KubeApi<Job> {
}
export function getMetricsForJobs(jobs: Job[], namespace: string, selector = ""): Promise<IPodMetrics> {
const podSelector = jobs.map(job => `${job.getName()}-[[:alnum:]]{5}`).join("|");
const opts = { category: "pods", pods: podSelector, namespace, selector };
return metricsApi.getMetrics({
cpuUsage: opts,
memoryUsage: opts,
fsUsage: opts,
networkReceive: opts,
networkTransmit: opts,
}, {
namespace,
});
}
export const jobApi = new JobApi({
objectConstructor: Job, objectConstructor: Job,
}); });

View File

@ -60,6 +60,15 @@ export interface IMetricsReqParams {
namespace?: string; // rbac-proxy validation param namespace?: string; // rbac-proxy validation param
} }
export interface IResourceMetrics<T = IMetrics> {
[metric: string]: T;
cpuUsage: T;
memoryUsage: T;
fsUsage: T;
networkReceive: T;
networkTransmit: T;
}
export const metricsApi = { export const metricsApi = {
async getMetrics<T = IMetricsQuery>(query: T, reqParams: IMetricsReqParams = {}): Promise<T extends object ? { [K in keyof T]: IMetrics } : IMetrics> { async getMetrics<T = IMetricsQuery>(query: T, reqParams: IMetricsReqParams = {}): Promise<T extends object ? { [K in keyof T]: IMetrics } : IMetrics> {
const { range = 3600, step = 60, namespace } = reqParams; const { range = 3600, step = 60, namespace } = reqParams;

View File

@ -22,6 +22,8 @@
import { KubeApi } from "../kube-api"; import { KubeApi } from "../kube-api";
import { KubeObject } from "../kube-object"; import { KubeObject } from "../kube-object";
import { autoBind } from "../../utils"; import { autoBind } from "../../utils";
import { metricsApi } from "./metrics.api";
import type { IPodMetrics } from "./pods.api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
export enum NamespaceStatus { export enum NamespaceStatus {
@ -50,6 +52,23 @@ export class Namespace extends KubeObject {
} }
} }
export const namespacesApi = new KubeApi({ export class NamespaceApi extends KubeApi<Namespace> {
}
export function getMetricsForNamespace(namespace: string, selector = ""): Promise<IPodMetrics> {
const opts = { category: "pods", pods: ".*", namespace, selector };
return metricsApi.getMetrics({
cpuUsage: opts,
memoryUsage: opts,
fsUsage: opts,
networkReceive: opts,
networkTransmit: opts,
}, {
namespace,
});
}
export const namespacesApi = new NamespaceApi({
objectConstructor: Namespace, objectConstructor: Namespace,
}); });

View File

@ -26,20 +26,21 @@ import { KubeApi } from "../kube-api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
export class NodesApi extends KubeApi<Node> { export class NodesApi extends KubeApi<Node> {
getMetrics(): Promise<INodeMetrics> { }
const opts = { category: "nodes" };
return metricsApi.getMetrics({ export function getMetricsForAllNodes(): Promise<INodeMetrics> {
memoryUsage: opts, const opts = { category: "nodes"};
workloadMemoryUsage: opts,
memoryCapacity: opts, return metricsApi.getMetrics({
memoryAllocatableCapacity: opts, memoryUsage: opts,
cpuUsage: opts, workloadMemoryUsage: opts,
cpuCapacity: opts, memoryCapacity: opts,
fsSize: opts, memoryAllocatableCapacity: opts,
fsUsage: opts cpuUsage: opts,
}); cpuCapacity: opts,
} fsSize: opts,
fsUsage: opts
});
} }
export interface INodeMetrics<T = IMetrics> { export interface INodeMetrics<T = IMetrics> {

View File

@ -27,14 +27,15 @@ import { KubeApi } from "../kube-api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
export class PersistentVolumeClaimsApi extends KubeApi<PersistentVolumeClaim> { export class PersistentVolumeClaimsApi extends KubeApi<PersistentVolumeClaim> {
getMetrics(pvcName: string, namespace: string): Promise<IPvcMetrics> { }
return metricsApi.getMetrics({
diskUsage: { category: "pvc", pvc: pvcName, namespace }, export function getMetricsForPvc(pvc: PersistentVolumeClaim): Promise<IPvcMetrics> {
diskCapacity: { category: "pvc", pvc: pvcName, namespace } return metricsApi.getMetrics({
}, { diskUsage: { category: "pvc", pvc: pvc.getName() },
namespace diskCapacity: { category: "pvc", pvc: pvc.getName() }
}); }, {
} namespace: pvc.getNs()
});
} }
export interface IPvcMetrics<T = IMetrics> { export interface IPvcMetrics<T = IMetrics> {

View File

@ -31,38 +31,38 @@ export class PodsApi extends KubeApi<Pod> {
return this.request.get(path, { query }); return this.request.get(path, { query });
} }
}
getMetrics(pods: Pod[], namespace: string, selector = "pod, namespace"): Promise<IPodMetrics> { export function getMetricsForPods(pods: Pod[], namespace: string, selector = "pod, namespace"): Promise<IPodMetrics> {
const podSelector = pods.map(pod => pod.getName()).join("|"); const podSelector = pods.map(pod => pod.getName()).join("|");
const opts = { category: "pods", pods: podSelector, namespace, selector }; const opts = { category: "pods", pods: podSelector, namespace, selector };
return metricsApi.getMetrics({ return metricsApi.getMetrics({
cpuUsage: opts, cpuUsage: opts,
cpuRequests: opts, cpuRequests: opts,
cpuLimits: opts, cpuLimits: opts,
memoryUsage: opts, memoryUsage: opts,
memoryRequests: opts, memoryRequests: opts,
memoryLimits: opts, memoryLimits: opts,
fsUsage: opts, fsUsage: opts,
networkReceive: opts, networkReceive: opts,
networkTransmit: opts, networkTransmit: opts,
}, { }, {
namespace, namespace,
}); });
}
} }
export interface IPodMetrics<T = IMetrics> { export interface IPodMetrics<T = IMetrics> {
[metric: string]: T; [metric: string]: T;
cpuUsage: T; cpuUsage: T;
cpuRequests: T;
cpuLimits: T;
memoryUsage: T; memoryUsage: T;
memoryRequests: T;
memoryLimits: T;
fsUsage: T; fsUsage: T;
networkReceive: T; networkReceive: T;
networkTransmit: T; networkTransmit: T;
cpuRequests?: T;
cpuLimits?: T;
memoryRequests?: T;
memoryLimits?: T;
} }
// Reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#read-log-pod-v1-core // Reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#read-log-pod-v1-core

View File

@ -22,8 +22,9 @@
import get from "lodash/get"; import get from "lodash/get";
import { autoBind } from "../../utils"; import { autoBind } from "../../utils";
import { WorkloadKubeObject } from "../workload-kube-object"; import { WorkloadKubeObject } from "../workload-kube-object";
import type { IPodContainer, Pod } from "./pods.api";
import { KubeApi } from "../kube-api"; import { KubeApi } from "../kube-api";
import { metricsApi } from "./metrics.api";
import type { IPodContainer, IPodMetrics, Pod } from "./pods.api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
export class ReplicaSetApi extends KubeApi<ReplicaSet> { export class ReplicaSetApi extends KubeApi<ReplicaSet> {
@ -49,6 +50,21 @@ export class ReplicaSetApi extends KubeApi<ReplicaSet> {
} }
} }
export function getMetricsForReplicaSets(replicasets: ReplicaSet[], namespace: string, selector = ""): Promise<IPodMetrics> {
const podSelector = replicasets.map(replicaset => `${replicaset.getName()}-[[:alnum:]]{5}`).join("|");
const opts = { category: "pods", pods: podSelector, namespace, selector };
return metricsApi.getMetrics({
cpuUsage: opts,
memoryUsage: opts,
fsUsage: opts,
networkReceive: opts,
networkTransmit: opts,
}, {
namespace,
});
}
export class ReplicaSet extends WorkloadKubeObject { export class ReplicaSet extends WorkloadKubeObject {
static kind = "ReplicaSet"; static kind = "ReplicaSet";
static namespaced = true; static namespaced = true;

View File

@ -20,10 +20,11 @@
*/ */
import get from "lodash/get"; import get from "lodash/get";
import type { IPodContainer } from "./pods.api";
import { IAffinity, WorkloadKubeObject } from "../workload-kube-object"; import { IAffinity, WorkloadKubeObject } from "../workload-kube-object";
import { autoBind } from "../../utils"; import { autoBind } from "../../utils";
import { KubeApi } from "../kube-api"; import { KubeApi } from "../kube-api";
import { metricsApi } from "./metrics.api";
import type { IPodContainer, IPodMetrics } from "./pods.api";
import type { KubeJsonApiData } from "../kube-json-api"; import type { KubeJsonApiData } from "../kube-json-api";
export class StatefulSetApi extends KubeApi<StatefulSet> { export class StatefulSetApi extends KubeApi<StatefulSet> {
@ -49,6 +50,21 @@ export class StatefulSetApi extends KubeApi<StatefulSet> {
} }
} }
export function getMetricsForStatefulSets(statefulSets: StatefulSet[], namespace: string, selector = ""): Promise<IPodMetrics> {
const podSelector = statefulSets.map(statefulset => `${statefulset.getName()}-[[:digit:]]+`).join("|");
const opts = { category: "pods", pods: podSelector, namespace, selector };
return metricsApi.getMetrics({
cpuUsage: opts,
memoryUsage: opts,
fsUsage: opts,
networkReceive: opts,
networkTransmit: opts,
}, {
namespace,
});
}
export class StatefulSet extends WorkloadKubeObject { export class StatefulSet extends WorkloadKubeObject {
static kind = "StatefulSet"; static kind = "StatefulSet";
static namespaced = true; static namespaced = true;