Skip to content

Commit 3af4d70

Browse files
authored
Merge pull request #848 from RCCSilva/master
Add single pod metrics
2 parents 574e0f7 + d25ff2d commit 3af4d70

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/metrics.ts

Lines changed: 29 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,20 @@ 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(
86+
namespace?: string,
87+
name?: string,
88+
): Promise<SinglePodMetrics | PodMetricsList> {
6989
let path: string;
7090

91+
if (namespace !== undefined && namespace.length > 0 && name !== undefined && name.length > 0) {
92+
path = `/apis/metrics.k8s.io/v1beta1/namespaces/${namespace}/pods/${name}`;
93+
return this.metricsApiRequest<SinglePodMetrics>(path);
94+
}
95+
7196
if (namespace !== undefined && namespace.length > 0) {
7297
path = `/apis/metrics.k8s.io/v1beta1/namespaces/${namespace}/pods`;
7398
} else {
@@ -77,7 +102,9 @@ export class Metrics {
77102
return this.metricsApiRequest<PodMetricsList>(path);
78103
}
79104

80-
private async metricsApiRequest<T extends PodMetricsList | NodeMetricsList>(path: string): Promise<T> {
105+
private async metricsApiRequest<T extends PodMetricsList | NodeMetricsList | SinglePodMetrics>(
106+
path: string,
107+
): Promise<T> {
81108
const cluster = this.config.getCurrentCluster();
82109
if (!cluster) {
83110
throw new Error('No currently active cluster');

src/metrics_test.ts

Lines changed: 30 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,22 @@ const mockedNodeMetrics: NodeMetricsList = {
7272
],
7373
};
7474

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

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

129145
s.done();
130146
});
147+
131148
it('should return namespace scope pods metric list', async () => {
132149
const [metricsClient, scope] = systemUnderTest();
133150
const s = scope
@@ -138,6 +155,18 @@ describe('Metrics', () => {
138155
expect(response).to.deep.equal(mockedPodMetrics);
139156
s.done();
140157
});
158+
it('should return single pod metrics if given namespace and pod name', async () => {
159+
const podName = 'pod-name';
160+
const [metricsClient, scope] = systemUnderTest();
161+
const s = scope
162+
.get(`/apis/metrics.k8s.io/v1beta1/namespaces/${TEST_NAMESPACE}/pods/${podName}`)
163+
.reply(200, mockedSinglePodMetrics);
164+
165+
const response = await metricsClient.getPodMetrics(TEST_NAMESPACE, podName);
166+
expect(response).to.deep.equal(mockedSinglePodMetrics);
167+
168+
s.done();
169+
});
141170
it('should when connection refused', async () => {
142171
const kc = new KubeConfig();
143172
kc.loadFromOptions({

0 commit comments

Comments
 (0)