@@ -2,8 +2,8 @@ import { deepEqual, deepStrictEqual, strictEqual } from 'assert';
2
2
import nock from 'nock' ;
3
3
import { KubeConfig } from './config.js' ;
4
4
import { Metrics , PodMetricsList } from './metrics.js' ;
5
- import { CurrentResourceUsage , topPods } from './top.js' ;
6
- import { CoreV1Api , V1Pod } from './api.js' ;
5
+ import { CurrentResourceUsage , ResourceUsage , topNodes , topPods } from './top.js' ;
6
+ import { CoreV1Api , V1Node , V1Pod } from './api.js' ;
7
7
8
8
const emptyPodMetrics : PodMetricsList = {
9
9
kind : 'PodMetricsList' ,
@@ -83,6 +83,10 @@ const podList: V1Pod[] = [
83
83
} ,
84
84
} ,
85
85
] ,
86
+ nodeName : 'node1' ,
87
+ } ,
88
+ status : {
89
+ phase : 'Running' ,
86
90
} ,
87
91
} ,
88
92
{
@@ -118,6 +122,43 @@ const podList: V1Pod[] = [
118
122
} ,
119
123
} ,
120
124
] ,
125
+ nodeName : 'node1' ,
126
+ } ,
127
+ status : {
128
+ phase : 'Running' ,
129
+ } ,
130
+ } ,
131
+ ] ;
132
+
133
+ const nodeList : V1Node [ ] = [
134
+ {
135
+ metadata : {
136
+ name : 'node1' ,
137
+ } ,
138
+ status : {
139
+ capacity : {
140
+ cpu : '4' ,
141
+ memory : '16Gi' ,
142
+ } ,
143
+ allocatable : {
144
+ cpu : '4' ,
145
+ memory : '16Gi' ,
146
+ } ,
147
+ } ,
148
+ } ,
149
+ {
150
+ metadata : {
151
+ name : 'node2' ,
152
+ } ,
153
+ status : {
154
+ capacity : {
155
+ cpu : '8' ,
156
+ memory : '32Gi' ,
157
+ } ,
158
+ allocatable : {
159
+ cpu : '8' ,
160
+ memory : '32Gi' ,
161
+ } ,
121
162
} ,
122
163
} ,
123
164
] ;
@@ -134,22 +175,23 @@ const testConfigOptions: any = {
134
175
const systemUnderTest = (
135
176
namespace ?: string ,
136
177
options : any = testConfigOptions ,
137
- ) : [ ( ) => ReturnType < typeof topPods > , nock . Scope ] => {
178
+ ) : [ ( ) => ReturnType < typeof topPods > , ( ) => ReturnType < typeof topNodes > , nock . Scope ] => {
138
179
const kc = new KubeConfig ( ) ;
139
180
kc . loadFromOptions ( options ) ;
140
181
const metricsClient = new Metrics ( kc ) ;
141
182
const core = kc . makeApiClient ( CoreV1Api ) ;
142
183
const topPodsFunc = ( ) => topPods ( core , metricsClient , namespace ) ;
184
+ const topNodesFunc = ( ) => topNodes ( core ) ;
143
185
144
186
const scope = nock ( testConfigOptions . clusters [ 0 ] . server ) ;
145
187
146
- return [ topPodsFunc , scope ] ;
188
+ return [ topPodsFunc , topNodesFunc , scope ] ;
147
189
} ;
148
190
149
191
describe ( 'Top' , ( ) => {
150
192
describe ( 'topPods' , ( ) => {
151
193
it ( 'should return empty when no pods' , async ( ) => {
152
- const [ topPodsFunc , scope ] = systemUnderTest ( ) ;
194
+ const [ topPodsFunc , _ , scope ] = systemUnderTest ( ) ;
153
195
const podMetrics = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , emptyPodMetrics ) ;
154
196
const pods = scope . get ( '/api/v1/pods' ) . reply ( 200 , {
155
197
items : [ ] ,
@@ -160,7 +202,7 @@ describe('Top', () => {
160
202
pods . done ( ) ;
161
203
} ) ;
162
204
it ( 'should return use cluster scope when namespace empty string' , async ( ) => {
163
- const [ topPodsFunc , scope ] = systemUnderTest ( '' ) ;
205
+ const [ topPodsFunc , _ , scope ] = systemUnderTest ( '' ) ;
164
206
const podMetrics = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , emptyPodMetrics ) ;
165
207
const pods = scope . get ( '/api/v1/pods' ) . reply ( 200 , {
166
208
items : [ ] ,
@@ -171,7 +213,7 @@ describe('Top', () => {
171
213
pods . done ( ) ;
172
214
} ) ;
173
215
it ( 'should return cluster wide pod metrics' , async ( ) => {
174
- const [ topPodsFunc , scope ] = systemUnderTest ( ) ;
216
+ const [ topPodsFunc , _ , scope ] = systemUnderTest ( ) ;
175
217
const podMetrics = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , mockedPodMetrics ) ;
176
218
const pods = scope . get ( '/api/v1/pods' ) . reply ( 200 , {
177
219
items : podList ,
@@ -235,7 +277,7 @@ describe('Top', () => {
235
277
pods . done ( ) ;
236
278
} ) ;
237
279
it ( 'should return best effort pod metrics' , async ( ) => {
238
- const [ topPodsFunc , scope ] = systemUnderTest ( ) ;
280
+ const [ topPodsFunc , _ , scope ] = systemUnderTest ( ) ;
239
281
const podMetrics = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , mockedPodMetrics ) ;
240
282
const pods = scope . get ( '/api/v1/pods' ) . reply ( 200 , {
241
283
items : bestEffortPodList ,
@@ -263,7 +305,7 @@ describe('Top', () => {
263
305
pods . done ( ) ;
264
306
} ) ;
265
307
it ( 'should return 0 when pod metrics missing' , async ( ) => {
266
- const [ topPodsFunc , scope ] = systemUnderTest ( ) ;
308
+ const [ topPodsFunc , _ , scope ] = systemUnderTest ( ) ;
267
309
const podMetrics = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , emptyPodMetrics ) ;
268
310
const pods = scope . get ( '/api/v1/pods' ) . reply ( 200 , {
269
311
items : podList ,
@@ -286,7 +328,7 @@ describe('Top', () => {
286
328
pods . done ( ) ;
287
329
} ) ;
288
330
it ( 'should return empty array when pods missing' , async ( ) => {
289
- const [ topPodsFunc , scope ] = systemUnderTest ( ) ;
331
+ const [ topPodsFunc , _ , scope ] = systemUnderTest ( ) ;
290
332
const podMetrics = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , mockedPodMetrics ) ;
291
333
const pods = scope . get ( '/api/v1/pods' ) . reply ( 200 , {
292
334
items : [ ] ,
@@ -297,7 +339,7 @@ describe('Top', () => {
297
339
pods . done ( ) ;
298
340
} ) ;
299
341
it ( 'should return namespace pod metrics' , async ( ) => {
300
- const [ topPodsFunc , scope ] = systemUnderTest ( TEST_NAMESPACE ) ;
342
+ const [ topPodsFunc , _ , scope ] = systemUnderTest ( TEST_NAMESPACE ) ;
301
343
const podMetrics = scope
302
344
. get ( `/apis/metrics.k8s.io/v1beta1/namespaces/${ TEST_NAMESPACE } /pods` )
303
345
. reply ( 200 , mockedPodMetrics ) ;
@@ -363,4 +405,36 @@ describe('Top', () => {
363
405
pods . done ( ) ;
364
406
} ) ;
365
407
} ) ;
408
+ describe ( 'topNodes' , ( ) => {
409
+ it ( 'should return empty when no nodes' , async ( ) => {
410
+ const [ _ , topNodesFunc , scope ] = systemUnderTest ( ) ;
411
+ const nodes = scope . get ( '/api/v1/nodes' ) . reply ( 200 , {
412
+ items : [ ] ,
413
+ } ) ;
414
+ const result = await topNodesFunc ( ) ;
415
+ deepStrictEqual ( result , [ ] ) ;
416
+ nodes . done ( ) ;
417
+ } ) ;
418
+
419
+ it ( 'should return cluster wide node metrics' , async ( ) => {
420
+ const [ _ , topNodesFunc , scope ] = systemUnderTest ( ) ;
421
+ const pods = scope . get ( '/api/v1/pods' ) . times ( 2 ) . reply ( 200 , {
422
+ items : podList ,
423
+ } ) ;
424
+ const nodes = scope . get ( '/api/v1/nodes' ) . reply ( 200 , {
425
+ items : nodeList ,
426
+ } ) ;
427
+ const result = await topNodesFunc ( ) ;
428
+ strictEqual ( result . length , 2 ) ;
429
+ deepStrictEqual ( result [ 0 ] . CPU , new ResourceUsage ( 4 , 2.2 , 2.2 ) ) ;
430
+ deepStrictEqual (
431
+ result [ 0 ] . Memory ,
432
+ new ResourceUsage ( BigInt ( '17179869184' ) , BigInt ( '262144000' ) , BigInt ( '314572800' ) ) ,
433
+ ) ;
434
+ deepStrictEqual ( result [ 1 ] . CPU , new ResourceUsage ( 8 , 0 , 0 ) ) ;
435
+ deepStrictEqual ( result [ 1 ] . Memory , new ResourceUsage ( BigInt ( '34359738368' ) , 0 , 0 ) ) ;
436
+ pods . done ( ) ;
437
+ nodes . done ( ) ;
438
+ } ) ;
439
+ } ) ;
366
440
} ) ;
0 commit comments