@@ -82,8 +82,9 @@ export async function topNodes(api: CoreV1Api): Promise<NodeStatus[]> {
8282 return result ;
8383}
8484
85- // TODO describe what this method does
85+ // Returns the current pod CPU/Memory usage including the CPU/Memory usage of each container
8686export async function topPods ( api : CoreV1Api , metrics : Metrics , namespace ?: string ) : Promise < PodStatus [ ] > {
87+ // Figure out which pod list endpoint to call
8788 const getPodList = async ( ) : Promise < V1PodList > => {
8889 if ( namespace ) {
8990 return ( await api . listNamespacedPod ( namespace ) ) . body ;
@@ -105,57 +106,62 @@ export async function topPods(api: CoreV1Api, metrics: Metrics, namespace?: stri
105106 for ( const pod of podList . items ) {
106107 const podMetric = podMetricsMap . get ( pod . metadata ! . name ! ) ;
107108
108- // TODO we are calculating the contianer usages twice
109- // Once per pod, then below per container
110- // calculate the pod usage in this method instead to avoid
111- // duplicating work
112- const cpuTotal = totalCPU ( pod ) ;
113- const memTotal = totalMemory ( pod ) ;
114109 const containerStatuses : ContainerStatus [ ] = [ ] ;
115110 let currentPodCPU : number | bigint = 0 ;
116111 let currentPodMem : number | bigint = 0 ;
117-
118- if ( podMetric !== undefined ) {
119- // Create a map of container names to their resource spec
120- // to make it easier to look up when we need it later
121- const containerResourceStatuses = pod . spec ! . containers . reduce ( ( accum , next ) => {
122- const containerCpuTotal = totalCPUForContainer ( next ) ;
123- const containerMemTotal = totalMemoryForContainer ( next ) ;
124- accum . set ( next . name , [ containerCpuTotal , containerMemTotal ] ) ;
125- return accum ;
126- } , new Map < string , [ ResourceStatus , ResourceStatus ] > ( ) ) ;
127-
128- podMetric . containers . forEach ( ( containerMetrics : ContainerMetric ) => {
112+ let podRequestsCPU : number | bigint = 0 ;
113+ let podLimitsCPU : number | bigint = 0 ;
114+ let podRequestsMem : number | bigint = 0 ;
115+ let podLimitsMem : number | bigint = 0 ;
116+
117+ pod . spec ! . containers . forEach ( ( container ) => {
118+ // get the the container CPU/Memory container.resources.requests
119+ const containerCpuTotal = totalCPUForContainer ( container ) ;
120+ const containerMemTotal = totalMemoryForContainer ( container ) ;
121+
122+ // sum each container's CPU/Memory container.resources.requests
123+ // to get the pod's overall request limit
124+ podRequestsCPU = add ( podRequestsCPU , containerCpuTotal . request ) ;
125+ podLimitsCPU = add ( podLimitsCPU , containerCpuTotal . limit ) ;
126+
127+ podRequestsMem = add ( podLimitsMem , containerMemTotal . request ) ;
128+ podLimitsMem = add ( podLimitsMem , containerMemTotal . limit ) ;
129+
130+ // Find the container metrics by container.name
131+ // if both the pod and container metrics exist
132+ const containerMetrics =
133+ podMetric !== undefined
134+ ? podMetric . containers . find ( ( c ) => c . name === container . name )
135+ : undefined ;
136+
137+ // Store the current usage of each container
138+ // Sum each container to get the overall pod usage
139+ if ( containerMetrics !== undefined ) {
129140 const currentContainerCPUUsage = quantityToScalar ( containerMetrics . usage . cpu ) ;
130141 const currentContainerMemUsage = quantityToScalar ( containerMetrics . usage . memory ) ;
142+
131143 currentPodCPU = add ( currentPodCPU , currentContainerCPUUsage ) ;
132144 currentPodMem = add ( currentPodMem , currentContainerMemUsage ) ;
133145
134- const containerResourceStatus = containerResourceStatuses . get ( containerMetrics . name ) ;
135-
136- if ( containerResourceStatus !== undefined ) {
137- const [ containerCpuTotal , containerMemTotal ] = containerResourceStatus ;
138-
139- const containerCpuUsage = new CurrentResourceUsage (
140- currentContainerCPUUsage ,
141- containerCpuTotal . request ,
142- containerCpuTotal . limit ,
143- ) ;
144- const containerMemUsage = new CurrentResourceUsage (
145- currentContainerMemUsage ,
146- containerMemTotal . request ,
147- containerMemTotal . limit ,
148- ) ;
149-
150- containerStatuses . push (
151- new ContainerStatus ( containerMetrics . name , containerCpuUsage , containerMemUsage ) ,
152- ) ;
153- }
154- } ) ;
155- }
146+ const containerCpuUsage = new CurrentResourceUsage (
147+ currentContainerCPUUsage ,
148+ containerCpuTotal . request ,
149+ containerCpuTotal . limit ,
150+ ) ;
151+ const containerMemUsage = new CurrentResourceUsage (
152+ currentContainerMemUsage ,
153+ containerMemTotal . request ,
154+ containerMemTotal . limit ,
155+ ) ;
156+
157+ containerStatuses . push (
158+ new ContainerStatus ( containerMetrics . name , containerCpuUsage , containerMemUsage ) ,
159+ ) ;
160+ }
161+ } ) ;
156162
157- const podCpuUsage = new CurrentResourceUsage ( currentPodCPU , cpuTotal . request , cpuTotal . limit ) ;
158- const podMemUsage = new CurrentResourceUsage ( currentPodMem , memTotal . request , memTotal . limit ) ;
163+ const podCpuUsage = new CurrentResourceUsage ( currentPodCPU , podRequestsCPU , podLimitsCPU ) ;
164+ const podMemUsage = new CurrentResourceUsage ( currentPodMem , podRequestsMem , podLimitsMem ) ;
159165 result . push ( new PodStatus ( pod , podCpuUsage , podMemUsage , containerStatuses ) ) ;
160166 }
161167 return result ;
0 commit comments