Skip to content

Add single pod metrics #848

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

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 29 additions & 2 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ export interface NodeMetricsList {
items: NodeMetric[];
}

export interface SinglePodMetrics {
kind: 'PodMetrics';
apiVersion: 'metrics.k8s.io/v1beta1';
metadata: {
name: string;
namespace: string;
creationTimestamp: string;
labels: { [key: string]: string };
};
timestamp: string;
window: string;
containers: ContainerMetric[];
}

export class Metrics {
private config: KubeConfig;

Expand All @@ -65,9 +79,20 @@ export class Metrics {
return this.metricsApiRequest<NodeMetricsList>('/apis/metrics.k8s.io/v1beta1/nodes');
}

public async getPodMetrics(namespace?: string): Promise<PodMetricsList> {
public async getPodMetrics(namespace?: string): Promise<PodMetricsList>;
public async getPodMetrics(namespace: string, name: string): Promise<SinglePodMetrics>;

public async getPodMetrics(
namespace?: string,
name?: string,
): Promise<SinglePodMetrics | PodMetricsList> {
let path: string;

if (namespace !== undefined && namespace.length > 0 && name !== undefined && name.length > 0) {
path = `/apis/metrics.k8s.io/v1beta1/namespaces/${namespace}/pods/${name}`;
return this.metricsApiRequest<SinglePodMetrics>(path);
}

if (namespace !== undefined && namespace.length > 0) {
path = `/apis/metrics.k8s.io/v1beta1/namespaces/${namespace}/pods`;
} else {
Expand All @@ -77,7 +102,9 @@ export class Metrics {
return this.metricsApiRequest<PodMetricsList>(path);
}

private async metricsApiRequest<T extends PodMetricsList | NodeMetricsList>(path: string): Promise<T> {
private async metricsApiRequest<T extends PodMetricsList | NodeMetricsList | SinglePodMetrics>(
path: string,
): Promise<T> {
const cluster = this.config.getCurrentCluster();
if (!cluster) {
throw new Error('No currently active cluster');
Expand Down
31 changes: 30 additions & 1 deletion src/metrics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import nock = require('nock');
import { KubeConfig } from './config';
import { V1Status, HttpError } from './gen/api';
import { Metrics, NodeMetricsList, PodMetricsList } from './metrics';
import { Metrics, NodeMetricsList, PodMetricsList, SinglePodMetrics } from './metrics';

const emptyPodMetrics: PodMetricsList = {
kind: 'PodMetricsList',
Expand Down Expand Up @@ -72,6 +72,22 @@ const mockedNodeMetrics: NodeMetricsList = {
],
};

const mockedSinglePodMetrics: SinglePodMetrics = {
kind: 'PodMetrics',
apiVersion: 'metrics.k8s.io/v1beta1',
metadata: {
name: 'a-pod',
namespace: 'default',
creationTimestamp: '2021-09-26T16:01:53Z',
labels: {
label: 'aLabel',
},
},
timestamp: '2021-09-26T16:01:53Z',
window: '7m',
containers: [{ name: 'nginx', usage: { cpu: '4414124n', memory: '123Ki' } }],
};

const TEST_NAMESPACE = 'test-namespace';

const testConfigOptions: any = {
Expand Down Expand Up @@ -128,6 +144,7 @@ describe('Metrics', () => {

s.done();
});

it('should return namespace scope pods metric list', async () => {
const [metricsClient, scope] = systemUnderTest();
const s = scope
Expand All @@ -138,6 +155,18 @@ describe('Metrics', () => {
expect(response).to.deep.equal(mockedPodMetrics);
s.done();
});
it('should return single pod metrics if given namespace and pod name', async () => {
const podName = 'pod-name';
const [metricsClient, scope] = systemUnderTest();
const s = scope
.get(`/apis/metrics.k8s.io/v1beta1/namespaces/${TEST_NAMESPACE}/pods/${podName}`)
.reply(200, mockedSinglePodMetrics);

const response = await metricsClient.getPodMetrics(TEST_NAMESPACE, podName);
expect(response).to.deep.equal(mockedSinglePodMetrics);

s.done();
});
it('should when connection refused', async () => {
const kc = new KubeConfig();
kc.loadFromOptions({
Expand Down