Skip to content

Commit ed038a5

Browse files
committed
Add single pod metrics
1 parent 574e0f7 commit ed038a5

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/metrics.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ export interface NodeMetricsList {
5454
items: NodeMetric[];
5555
}
5656

57+
export interface SinglePodMetrics {
58+
kind: 'PodMetrics',
59+
apiVersion: 'metrics.k8s.io/v1beta1',
60+
metadata: {
61+
name: string;
62+
namespace: string;
63+
creationTimestamp: string;
64+
labels: { [key: string]: string }
65+
};
66+
timestamp: string;
67+
window: string;
68+
containers: ContainerMetric[];
69+
}
70+
5771
export class Metrics {
5872
private config: KubeConfig;
5973

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

68-
public async getPodMetrics(namespace?: string): Promise<PodMetricsList> {
82+
public async getPodMetrics(namespace?: string): Promise<PodMetricsList>
83+
public async getPodMetrics(namespace: string, name: string): Promise<SinglePodMetrics>
84+
85+
public async getPodMetrics(namespace?: string, name?: string): Promise<SinglePodMetrics | PodMetricsList> {
6986
let path: string;
7087

88+
if (namespace !== undefined && namespace.length > 0 && name !== undefined && name.length > 0) {
89+
path = `/apis/metrics.k8s.io/v1beta1/namespaces/${namespace}/pods/${name}`;
90+
return this.metricsApiRequest<SinglePodMetrics>(path);
91+
}
92+
7193
if (namespace !== undefined && namespace.length > 0) {
7294
path = `/apis/metrics.k8s.io/v1beta1/namespaces/${namespace}/pods`;
7395
} else {
@@ -77,7 +99,8 @@ export class Metrics {
7799
return this.metricsApiRequest<PodMetricsList>(path);
78100
}
79101

80-
private async metricsApiRequest<T extends PodMetricsList | NodeMetricsList>(path: string): Promise<T> {
102+
103+
private async metricsApiRequest<T extends PodMetricsList | NodeMetricsList | SinglePodMetrics>(path: string): Promise<T> {
81104
const cluster = this.config.getCurrentCluster();
82105
if (!cluster) {
83106
throw new Error('No currently active cluster');

src/metrics_test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expect } from 'chai';
33
import nock = require('nock');
44
import { KubeConfig } from './config';
55
import { V1Status, HttpError } from './gen/api';
6-
import { Metrics, NodeMetricsList, PodMetricsList } from './metrics';
6+
import { Metrics, NodeMetricsList, PodMetricsList, SinglePodMetrics } from './metrics';
77

88
const emptyPodMetrics: PodMetricsList = {
99
kind: 'PodMetricsList',
@@ -72,6 +72,25 @@ const mockedNodeMetrics: NodeMetricsList = {
7272
],
7373
};
7474

75+
const mockedSinglePodMetrics: SinglePodMetrics = {
76+
77+
kind: 'PodMetrics',
78+
apiVersion: 'metrics.k8s.io/v1beta1',
79+
metadata: {
80+
name: 'a-pod',
81+
namespace: 'default',
82+
creationTimestamp: '2021-09-26T16:01:53Z',
83+
labels: {
84+
label: 'aLabel'
85+
}
86+
},
87+
timestamp: '2021-09-26T16:01:53Z',
88+
window: '7m',
89+
containers: [
90+
{ name: 'nginx', usage: { cpu: '4414124n', memory: '123Ki' } }
91+
],
92+
};
93+
7594
const TEST_NAMESPACE = 'test-namespace';
7695

7796
const testConfigOptions: any = {
@@ -128,6 +147,7 @@ describe('Metrics', () => {
128147

129148
s.done();
130149
});
150+
131151
it('should return namespace scope pods metric list', async () => {
132152
const [metricsClient, scope] = systemUnderTest();
133153
const s = scope
@@ -138,6 +158,18 @@ describe('Metrics', () => {
138158
expect(response).to.deep.equal(mockedPodMetrics);
139159
s.done();
140160
});
161+
it('should return single pod metrics if given namespace and pod name', async () => {
162+
const podName = 'pod-name';
163+
const [metricsClient, scope] = systemUnderTest();
164+
const s = scope
165+
.get(`/apis/metrics.k8s.io/v1beta1/namespaces/${TEST_NAMESPACE}/pods/${podName}`)
166+
.reply(200, mockedSinglePodMetrics);
167+
168+
const response = await metricsClient.getPodMetrics(TEST_NAMESPACE, podName);
169+
expect(response).to.deep.equal(mockedSinglePodMetrics);
170+
171+
s.done();
172+
});
141173
it('should when connection refused', async () => {
142174
const kc = new KubeConfig();
143175
kc.loadFromOptions({

0 commit comments

Comments
 (0)