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
10 changes: 7 additions & 3 deletions pkg/monitor/nvidia/cudevshr.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,13 @@ func (l *ContainerLister) Update() error {
if !entry.IsDir() {
continue
}
parts := strings.SplitN(entry.Name(), "_", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
klog.Warningf("Skipping dir with unexpected name format: %s", entry.Name())
continue
}
dirName := filepath.Join(l.containerPath, entry.Name())
podUID := strings.Split(entry.Name(), "_")[0]
podUID := parts[0]
if !podUIDs[podUID] {
dirInfo, err := os.Stat(dirName)
if err == nil && dirInfo.ModTime().Add(resyncInterval).After(time.Now()) {
Expand All @@ -209,11 +214,10 @@ func (l *ContainerLister) Update() error {
continue
}
if usage == nil {
// no cuInit in container
continue
}
usage.PodUID = podUID
usage.ContainerName = strings.Split(entry.Name(), "_")[1]
usage.ContainerName = parts[1]
l.containers[entry.Name()] = usage
klog.Infof("Adding ctr dirname %s in monitorpath", dirName)
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/monitor/nvidia/cudevshr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,34 @@ func Test_ContainerLister_Update(t *testing.T) {
assert.Equal(t, got.ContainerName, "mycontainer")
defer func() { _ = syscall.Munmap(got.data) }()
})

t.Run("dir without underscore in name is skipped", func(t *testing.T) {
dir := t.TempDir()
pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: "default", UID: "nodashes"}}
ctrDir := filepath.Join(dir, "nodashes")
assert.NilError(t, os.Mkdir(ctrDir, 0755))
writeCacheFile(t, ctrDir, "x.cache", headerBytes(v1CacheFileSize, SharedRegionMagicFlag, 1, 0))
l := &ContainerLister{
containerPath: dir,
containers: map[string]*ContainerUsage{},
podLister: newTestPodLister(pod),
}
assert.NilError(t, l.Update())
assert.Equal(t, len(l.containers), 0)
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.

t.Run("dir with trailing underscore is skipped", func(t *testing.T) {
dir := t.TempDir()
pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: "default", UID: "uid"}}
ctrDir := filepath.Join(dir, "uid_")
assert.NilError(t, os.Mkdir(ctrDir, 0755))
writeCacheFile(t, ctrDir, "x.cache", headerBytes(v1CacheFileSize, SharedRegionMagicFlag, 1, 0))
l := &ContainerLister{
containerPath: dir,
containers: map[string]*ContainerUsage{},
podLister: newTestPodLister(pod),
}
assert.NilError(t, l.Update())
assert.Equal(t, len(l.containers), 0)
})
}
Loading