fix(kubelet):resolve loop variable capture bug in WaitForAllPodsUnmount

This commit is contained in:
qiuxue
2026-01-16 21:55:52 +08:00
parent f4ed7b3e28
commit aeb9002d14
2 changed files with 9 additions and 0 deletions

View File

@@ -516,6 +516,7 @@ func (vm *volumeManager) WaitForAllPodsUnmount(ctx context.Context, pods []*v1.P
funcs := make([]func() error, 0, len(pods))
for _, pod := range pods {
pod := pod
funcs = append(funcs, func() error {
return vm.WaitForUnmount(ctx, pod)
})

View File

@@ -743,6 +743,14 @@ func TestWaitForAllPodsUnmount(t *testing.T) {
require.ErrorAs(t, err, &aggErr, "Expected error to be an Aggregate error")
errs := aggErr.Errors()
require.Len(t, errs, test.numPods, "Expected %d errors but got %d", test.numPods, len(errs))
// Verify that each pod's volume name appears in the error messages,
// which proves different pods are being processed
errString := err.Error()
for i := 0; i < test.numPods; i++ {
volumeName := fmt.Sprintf("fake/fake-device-%d", i)
require.Contains(t, errString, volumeName, "Expected error to contain volume name %s for pod-%d", volumeName, i)
}
} else {
require.NoError(t, err, "Expected no error")
}