refactor(compose): convert blkio throttle devices with one helper

The four throttle device lists of the blkio configuration were converted by four identical loops.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2026-10-29 10:48:11 +01:00 • committed by Nicolas De loof
parent ee84ed3549
commit cc04eb384f
2 changed files with 29 additions and 20 deletions

View file

@ -935,30 +935,21 @@ func setBlkio(blkio *types.BlkioConfig, resources *container.Resources) {
Weight: b.Weight,
})
}
for _, b := range blkio.DeviceReadBps {
resources.BlkioDeviceReadBps = append(resources.BlkioDeviceReadBps, &blkiodev.ThrottleDevice{
Path: b.Path,
Rate: uint64(b.Rate),
})
}
for _, b := range blkio.DeviceReadIOps {
resources.BlkioDeviceReadIOps = append(resources.BlkioDeviceReadIOps, &blkiodev.ThrottleDevice{
Path: b.Path,
Rate: uint64(b.Rate),
})
}
for _, b := range blkio.DeviceWriteBps {
resources.BlkioDeviceWriteBps = append(resources.BlkioDeviceWriteBps, &blkiodev.ThrottleDevice{
Path: b.Path,
Rate: uint64(b.Rate),
})
}
for _, b := range blkio.DeviceWriteIOps {
resources.BlkioDeviceWriteIOps = append(resources.BlkioDeviceWriteIOps, &blkiodev.ThrottleDevice{
resources.BlkioDeviceReadBps = append(resources.BlkioDeviceReadBps, toThrottleDevices(blkio.DeviceReadBps)...)
resources.BlkioDeviceReadIOps = append(resources.BlkioDeviceReadIOps, toThrottleDevices(blkio.DeviceReadIOps)...)
resources.BlkioDeviceWriteBps = append(resources.BlkioDeviceWriteBps, toThrottleDevices(blkio.DeviceWriteBps)...)
resources.BlkioDeviceWriteIOps = append(resources.BlkioDeviceWriteIOps, toThrottleDevices(blkio.DeviceWriteIOps)...)
}
func toThrottleDevices(devices []types.ThrottleDevice) []*blkiodev.ThrottleDevice {
var out []*blkiodev.ThrottleDevice
for _, b := range devices {
out = append(out, &blkiodev.ThrottleDevice{
Path: b.Path,
Rate: uint64(b.Rate),
})
}
return out
}
func buildContainerPorts(s types.ServiceConfig) (network.PortSet, error) {

View file

@ -526,3 +526,21 @@ volumes:
})
}
}
func TestSetBlkio(t *testing.T) {
resources := &container.Resources{}
setBlkio(&composetypes.BlkioConfig{
Weight: 300,
WeightDevice: []composetypes.WeightDevice{{Path: "/dev/sda", Weight: 400}},
DeviceReadBps: []composetypes.ThrottleDevice{{Path: "/dev/sda", Rate: 1}},
DeviceReadIOps: []composetypes.ThrottleDevice{{Path: "/dev/sdb", Rate: 2}},
DeviceWriteBps: []composetypes.ThrottleDevice{{Path: "/dev/sdc", Rate: 3}},
}, resources)
assert.Equal(t, resources.BlkioWeight, uint16(300))
assert.Equal(t, len(resources.BlkioWeightDevice), 1)
assert.Equal(t, resources.BlkioDeviceReadBps[0].Path, "/dev/sda")
assert.Equal(t, resources.BlkioDeviceReadBps[0].Rate, uint64(1))
assert.Equal(t, resources.BlkioDeviceReadIOps[0].Rate, uint64(2))
assert.Equal(t, resources.BlkioDeviceWriteBps[0].Rate, uint64(3))
assert.Assert(t, resources.BlkioDeviceWriteIOps == nil)
}