From cc04eb384f25c0ba71bf0a8d8522d11d0881bb3e Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 29 Oct 2026 10:48:11 +0100 Subject: [PATCH] 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 --- pkg/compose/create.go | 31 +++++++++++-------------------- pkg/compose/create_test.go | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 75cc906d4..7656e9192 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -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) { diff --git a/pkg/compose/create_test.go b/pkg/compose/create_test.go index 41090d940..350de6ecf 100644 --- a/pkg/compose/create_test.go +++ b/pkg/compose/create_test.go @@ -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) +}