@@ -28,6 +28,7 @@ import (
2828
2929 "github.com/sustainable-computing-io/kepler/pkg/config"
3030 "golang.org/x/sys/unix"
31+ corev1 "k8s.io/api/core/v1"
3132 "k8s.io/klog/v2"
3233)
3334
6566 regexReplaceContainerIDPrefix = regexp .MustCompile (`.*//` )
6667)
6768
68- func init ( ) {
69- updateListPodCache ("" , false )
69+ func Init () ( * []corev1. Pod , error ) {
70+ return updateListPodCache ("" , false )
7071}
7172
7273func GetSystemProcessName () string {
@@ -117,7 +118,7 @@ func getContainerInfo(cGroupID, pid uint64) (*ContainerInfo, error) {
117118 }
118119
119120 // update cache info and stop loop if container id found
120- updateListPodCache (containerID , true )
121+ _ , _ = updateListPodCache (containerID , true )
121122 if i , ok := containerIDToContainerInfo [containerID ]; ok {
122123 return i , nil
123124 }
@@ -128,11 +129,11 @@ func getContainerInfo(cGroupID, pid uint64) (*ContainerInfo, error) {
128129
129130// updateListPodCache updates cache info with all pods and optionally
130131// stops the loop when a given container ID is found
131- func updateListPodCache (targetContainerID string , stopWhenFound bool ) {
132+ func updateListPodCache (targetContainerID string , stopWhenFound bool ) ( * []corev1. Pod , error ) {
132133 pods , err := podLister .ListPods ()
133134 if err != nil {
134135 klog .V (4 ).Infof ("%v" , err )
135- return
136+ return pods , err
136137 }
137138 for i := 0 ; i < len (* pods ); i ++ {
138139 statuses := (* pods )[i ].Status .ContainerStatuses
@@ -145,7 +146,7 @@ func updateListPodCache(targetContainerID string, stopWhenFound bool) {
145146 containerID := regexReplaceContainerIDPrefix .ReplaceAllString (statuses [j ].ContainerID , "" )
146147 containerIDToContainerInfo [containerID ] = info
147148 if stopWhenFound && statuses [j ].ContainerID == targetContainerID {
148- return
149+ return pods , err
149150 }
150151 }
151152 statuses = (* pods )[i ].Status .InitContainerStatuses
@@ -158,10 +159,11 @@ func updateListPodCache(targetContainerID string, stopWhenFound bool) {
158159 containerID := regexReplaceContainerIDPrefix .ReplaceAllString (statuses [j ].ContainerID , "" )
159160 containerIDToContainerInfo [containerID ] = info
160161 if stopWhenFound && statuses [j ].ContainerID == targetContainerID {
161- return
162+ return pods , err
162163 }
163164 }
164165 }
166+ return pods , err
165167}
166168
167169func GetContainerID (cGroupID , pid uint64 ) (string , error ) {
0 commit comments