Skip to content

Commit f727982

Browse files
authored
Merge pull request #271 from sunya-ch/reader-1
init podEnergy of inactive pods (e.g., nginx)
2 parents d17d44a + 60ae5ee commit f727982

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

pkg/collector/reader.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ var (
7575
systemProcessNamespace = podlister.GetSystemProcessNamespace()
7676
)
7777

78+
func init() {
79+
pods, err := podlister.Init()
80+
if err != nil {
81+
klog.V(5).Infoln(err)
82+
return
83+
}
84+
for i := 0; i < len(*pods); i++ {
85+
podName := (*pods)[i].Name
86+
podNamespace := (*pods)[i].Namespace
87+
podEnergy[podName] = NewPodEnergy(podName, podNamespace)
88+
}
89+
}
90+
7891
// readEnergy reads sensor/pkg energies in mJ
7992
func (c *Collector) readEnergy() {
8093
sensorEnergy, _ = acpiPowerMeter.GetEnergyFromHost()

pkg/podlister/resolve_container.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -65,8 +66,8 @@ var (
6566
regexReplaceContainerIDPrefix = regexp.MustCompile(`.*//`)
6667
)
6768

68-
func init() {
69-
updateListPodCache("", false)
69+
func Init() (*[]corev1.Pod, error) {
70+
return updateListPodCache("", false)
7071
}
7172

7273
func 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

167169
func GetContainerID(cGroupID, pid uint64) (string, error) {

pkg/podlister/suite_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ func TestPodLoader(t *testing.T) {
2727
RegisterFailHandler(Fail)
2828
RunSpecs(t, "PodLister Suite")
2929
}
30+
31+
var _ = BeforeSuite(func() {
32+
_, _ = Init()
33+
})

0 commit comments

Comments
 (0)