Skip to content

Commit d04a54c

Browse files
committed
optimize code, filter podUID is empty string
Signed-off-by: rongfu.leng <[email protected]>
1 parent 359b9ba commit d04a54c

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

pkg/kubelet/cm/devicemanager/manager.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,20 @@ func (m *ManagerImpl) genericDeviceUpdateCallback(resourceName string, devices [
280280

281281
if utilfeature.DefaultFeatureGate.Enabled(features.ResourceHealthStatus) {
282282
// compare with old device's health and send update to the channel if needed
283+
updatePodUIDFn := func(deviceID string) {
284+
podUID, _ := m.podDevices.getPodAndContainerForDevice(deviceID)
285+
if podUID != "" {
286+
podsToUpdate.Insert(podUID)
287+
}
288+
}
283289
if oldDev, ok := oldDevices[dev.ID]; ok {
284290
if oldDev.Health != dev.Health {
285-
podUID, _ := m.podDevices.getPodAndContainerForDevice(dev.ID)
286-
podsToUpdate.Insert(podUID)
291+
updatePodUIDFn(dev.ID)
287292
}
288293
} else {
289294
// if this is a new device, it might have existed before and disappeared for a while
290295
// but still be assigned to a Pod. In this case, we need to send an update to the channel
291-
podUID, _ := m.podDevices.getPodAndContainerForDevice(dev.ID)
292-
podsToUpdate.Insert(podUID)
296+
updatePodUIDFn(dev.ID)
293297
}
294298
}
295299

pkg/kubelet/cm/devicemanager/manager_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ import (
3838
"k8s.io/apimachinery/pkg/util/sets"
3939
"k8s.io/apimachinery/pkg/util/uuid"
4040
"k8s.io/apimachinery/pkg/util/wait"
41+
utilfeature "k8s.io/apiserver/pkg/util/feature"
4142
"k8s.io/client-go/tools/record"
43+
featuregatetesting "k8s.io/component-base/featuregate/testing"
4244
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
4345
watcherapi "k8s.io/kubelet/pkg/apis/pluginregistration/v1"
46+
"k8s.io/kubernetes/pkg/features"
4447
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
4548
"k8s.io/kubernetes/pkg/kubelet/cm/containermap"
4649
"k8s.io/kubernetes/pkg/kubelet/cm/devicemanager/checkpoint"
4750
plugin "k8s.io/kubernetes/pkg/kubelet/cm/devicemanager/plugin/v1beta1"
51+
"k8s.io/kubernetes/pkg/kubelet/cm/resourceupdates"
4852
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
4953
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask"
5054
"k8s.io/kubernetes/pkg/kubelet/config"
@@ -1953,3 +1957,76 @@ func sortContainerStatuses(statuses []v1.ContainerStatus) {
19531957
}
19541958
}
19551959
}
1960+
1961+
func TestFeatureGateResourceHealthStatus(t *testing.T) {
1962+
tmpDir, err := os.MkdirTemp("", "checkpoint")
1963+
require.NoError(t, err, "err should be nil")
1964+
defer func() {
1965+
err = os.RemoveAll(tmpDir)
1966+
if err != nil {
1967+
t.Fatalf("Fail to remove tmpdir: %v", err)
1968+
}
1969+
}()
1970+
ckm, err := checkpointmanager.NewCheckpointManager(tmpDir)
1971+
require.NoError(t, err, "err should be nil")
1972+
resourceName := "domain1.com/resource1"
1973+
existDevices := map[string]DeviceInstances{
1974+
resourceName: map[string]pluginapi.Device{
1975+
"dev1": {
1976+
ID: "dev1",
1977+
Health: pluginapi.Healthy,
1978+
},
1979+
"dev2": {
1980+
ID: "dev2",
1981+
Health: pluginapi.Unhealthy,
1982+
},
1983+
},
1984+
}
1985+
testManager := &ManagerImpl{
1986+
allDevices: ResourceDeviceInstances(existDevices),
1987+
endpoints: make(map[string]endpointInfo),
1988+
healthyDevices: make(map[string]sets.Set[string]),
1989+
unhealthyDevices: make(map[string]sets.Set[string]),
1990+
allocatedDevices: make(map[string]sets.Set[string]),
1991+
podDevices: newPodDevices(),
1992+
checkpointManager: ckm,
1993+
update: make(chan resourceupdates.Update),
1994+
}
1995+
1996+
podID := "pod1"
1997+
contID := "con1"
1998+
devices := checkpoint.DevicesPerNUMA{0: []string{"dev1"}, 1: []string{"dev1"}}
1999+
2000+
testManager.podDevices.insert(podID, contID, resourceName,
2001+
devices,
2002+
newContainerAllocateResponse(
2003+
withDevices(map[string]string{"/dev/r1dev1": "/dev/r1dev1", "/dev/r1dev2": "/dev/r1dev2"}),
2004+
withMounts(map[string]string{"/home/r1lib1": "/usr/r1lib1"}),
2005+
),
2006+
)
2007+
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ResourceHealthStatus, true)
2008+
2009+
testManager.genericDeviceUpdateCallback(resourceName, []pluginapi.Device{
2010+
{ID: "dev1", Health: pluginapi.Healthy},
2011+
{ID: "dev2", Health: pluginapi.Unhealthy},
2012+
})
2013+
// update chan no data
2014+
assert.Empty(t, len(testManager.update), 0)
2015+
2016+
// update chan receive pod1
2017+
var wg sync.WaitGroup
2018+
go func() {
2019+
defer wg.Done()
2020+
u := <-testManager.update
2021+
assert.Equal(t, resourceupdates.Update{
2022+
PodUIDs: []string{"pod1"},
2023+
}, u)
2024+
}()
2025+
wg.Add(1)
2026+
testManager.genericDeviceUpdateCallback(resourceName, []pluginapi.Device{
2027+
{ID: "dev1", Health: pluginapi.Unhealthy},
2028+
{ID: "dev2", Health: pluginapi.Healthy},
2029+
})
2030+
wg.Wait()
2031+
2032+
}

pkg/kubelet/cm/devicemanager/pod_devices_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,27 @@ func TestDeviceRunContainerOptions(t *testing.T) {
251251
})
252252
}
253253
}
254+
255+
func TestGetPodAndContainerForDevice(t *testing.T) {
256+
podDevices := newPodDevices()
257+
resourceName1 := "domain1.com/resource1"
258+
podID := "pod1"
259+
contID := "con1"
260+
devices := checkpoint.DevicesPerNUMA{0: []string{"dev1"}, 1: []string{"dev1"}}
261+
262+
podDevices.insert(podID, contID, resourceName1,
263+
devices,
264+
newContainerAllocateResponse(
265+
withDevices(map[string]string{"/dev/r1dev1": "/dev/r1dev1", "/dev/r1dev2": "/dev/r1dev2"}),
266+
withMounts(map[string]string{"/home/r1lib1": "/usr/r1lib1"}),
267+
),
268+
)
269+
270+
// dev2 is a new device
271+
podUID, _ := podDevices.getPodAndContainerForDevice("dev2")
272+
assert.Equal(t, "", podUID)
273+
274+
// dev1 is a exist device
275+
podUID, _ = podDevices.getPodAndContainerForDevice("dev1")
276+
assert.Equal(t, "pod1", podUID)
277+
}

0 commit comments

Comments
 (0)