Skip to content
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
8 changes: 7 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export default tseslint.config(
'@typescript-eslint/no-empty-object-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],
'@typescript-eslint/no-unused-vars': [
'error',
{
args: 'none',
destructuredArrayIgnorePattern: '^_',
Copy link
Contributor

@cjihrig cjihrig Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish JavaScript had this built in. Is the intent to ignore anything that starts with _, or strictly _? If it is the latter, we might need a $ on the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think? I'm ok with both _ and _ignore etc, but if you feel strongly I'm happy to adapt.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also OK with both, I just wanted to verify.

},
],
},
},
);
96 changes: 85 additions & 11 deletions src/top_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { deepEqual, deepStrictEqual, strictEqual } from 'assert';
import nock from 'nock';
import { KubeConfig } from './config.js';
import { Metrics, PodMetricsList } from './metrics.js';
import { CurrentResourceUsage, topPods } from './top.js';
import { CoreV1Api, V1Pod } from './api.js';
import { CurrentResourceUsage, ResourceUsage, topNodes, topPods } from './top.js';
import { CoreV1Api, V1Node, V1Pod } from './api.js';

const emptyPodMetrics: PodMetricsList = {
kind: 'PodMetricsList',
Expand Down Expand Up @@ -83,6 +83,10 @@ const podList: V1Pod[] = [
},
},
],
nodeName: 'node1',
},
status: {
phase: 'Running',
},
},
{
Expand Down Expand Up @@ -118,6 +122,43 @@ const podList: V1Pod[] = [
},
},
],
nodeName: 'node1',
},
status: {
phase: 'Running',
},
},
];

const nodeList: V1Node[] = [
{
metadata: {
name: 'node1',
},
status: {
capacity: {
cpu: '4',
memory: '16Gi',
},
allocatable: {
cpu: '4',
memory: '16Gi',
},
},
},
{
metadata: {
name: 'node2',
},
status: {
capacity: {
cpu: '8',
memory: '32Gi',
},
allocatable: {
cpu: '8',
memory: '32Gi',
},
},
},
];
Expand All @@ -134,22 +175,23 @@ const testConfigOptions: any = {
const systemUnderTest = (
namespace?: string,
options: any = testConfigOptions,
): [() => ReturnType<typeof topPods>, nock.Scope] => {
): [() => ReturnType<typeof topPods>, () => ReturnType<typeof topNodes>, nock.Scope] => {
const kc = new KubeConfig();
kc.loadFromOptions(options);
const metricsClient = new Metrics(kc);
const core = kc.makeApiClient(CoreV1Api);
const topPodsFunc = () => topPods(core, metricsClient, namespace);
const topNodesFunc = () => topNodes(core);

const scope = nock(testConfigOptions.clusters[0].server);

return [topPodsFunc, scope];
return [topPodsFunc, topNodesFunc, scope];
};

describe('Top', () => {
describe('topPods', () => {
it('should return empty when no pods', async () => {
const [topPodsFunc, scope] = systemUnderTest();
const [topPodsFunc, _, scope] = systemUnderTest();
const podMetrics = scope.get('/apis/metrics.k8s.io/v1beta1/pods').reply(200, emptyPodMetrics);
const pods = scope.get('/api/v1/pods').reply(200, {
items: [],
Expand All @@ -160,7 +202,7 @@ describe('Top', () => {
pods.done();
});
it('should return use cluster scope when namespace empty string', async () => {
const [topPodsFunc, scope] = systemUnderTest('');
const [topPodsFunc, _, scope] = systemUnderTest('');
const podMetrics = scope.get('/apis/metrics.k8s.io/v1beta1/pods').reply(200, emptyPodMetrics);
const pods = scope.get('/api/v1/pods').reply(200, {
items: [],
Expand All @@ -171,7 +213,7 @@ describe('Top', () => {
pods.done();
});
it('should return cluster wide pod metrics', async () => {
const [topPodsFunc, scope] = systemUnderTest();
const [topPodsFunc, _, scope] = systemUnderTest();
const podMetrics = scope.get('/apis/metrics.k8s.io/v1beta1/pods').reply(200, mockedPodMetrics);
const pods = scope.get('/api/v1/pods').reply(200, {
items: podList,
Expand Down Expand Up @@ -235,7 +277,7 @@ describe('Top', () => {
pods.done();
});
it('should return best effort pod metrics', async () => {
const [topPodsFunc, scope] = systemUnderTest();
const [topPodsFunc, _, scope] = systemUnderTest();
const podMetrics = scope.get('/apis/metrics.k8s.io/v1beta1/pods').reply(200, mockedPodMetrics);
const pods = scope.get('/api/v1/pods').reply(200, {
items: bestEffortPodList,
Expand Down Expand Up @@ -263,7 +305,7 @@ describe('Top', () => {
pods.done();
});
it('should return 0 when pod metrics missing', async () => {
const [topPodsFunc, scope] = systemUnderTest();
const [topPodsFunc, _, scope] = systemUnderTest();
const podMetrics = scope.get('/apis/metrics.k8s.io/v1beta1/pods').reply(200, emptyPodMetrics);
const pods = scope.get('/api/v1/pods').reply(200, {
items: podList,
Expand All @@ -286,7 +328,7 @@ describe('Top', () => {
pods.done();
});
it('should return empty array when pods missing', async () => {
const [topPodsFunc, scope] = systemUnderTest();
const [topPodsFunc, _, scope] = systemUnderTest();
const podMetrics = scope.get('/apis/metrics.k8s.io/v1beta1/pods').reply(200, mockedPodMetrics);
const pods = scope.get('/api/v1/pods').reply(200, {
items: [],
Expand All @@ -297,7 +339,7 @@ describe('Top', () => {
pods.done();
});
it('should return namespace pod metrics', async () => {
const [topPodsFunc, scope] = systemUnderTest(TEST_NAMESPACE);
const [topPodsFunc, _, scope] = systemUnderTest(TEST_NAMESPACE);
const podMetrics = scope
.get(`/apis/metrics.k8s.io/v1beta1/namespaces/${TEST_NAMESPACE}/pods`)
.reply(200, mockedPodMetrics);
Expand Down Expand Up @@ -363,4 +405,36 @@ describe('Top', () => {
pods.done();
});
});
describe('topNodes', () => {
it('should return empty when no nodes', async () => {
const [_, topNodesFunc, scope] = systemUnderTest();
const nodes = scope.get('/api/v1/nodes').reply(200, {
items: [],
});
const result = await topNodesFunc();
deepStrictEqual(result, []);
nodes.done();
});

it('should return cluster wide node metrics', async () => {
const [_, topNodesFunc, scope] = systemUnderTest();
const pods = scope.get('/api/v1/pods').times(2).reply(200, {
items: podList,
});
const nodes = scope.get('/api/v1/nodes').reply(200, {
items: nodeList,
});
const result = await topNodesFunc();
strictEqual(result.length, 2);
deepStrictEqual(result[0].CPU, new ResourceUsage(4, 2.2, 2.2));
deepStrictEqual(
result[0].Memory,
new ResourceUsage(BigInt('17179869184'), BigInt('262144000'), BigInt('314572800')),
);
deepStrictEqual(result[1].CPU, new ResourceUsage(8, 0, 0));
deepStrictEqual(result[1].Memory, new ResourceUsage(BigInt('34359738368'), 0, 0));
pods.done();
nodes.done();
});
});
});
3 changes: 3 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { CoreV1Api, V1Container, V1Pod } from './gen/index.js';

export async function podsForNode(api: CoreV1Api, nodeName: string): Promise<V1Pod[]> {
const allPods = await api.listPodForAllNamespaces();
if (!allPods.items) {
return [];
}
return allPods.items.filter((pod: V1Pod) => pod.spec!.nodeName === nodeName);
}

Expand Down