@@ -25,6 +25,7 @@ import (
25
25
26
26
"github.com/compose-spec/compose-go/types"
27
27
moby "github.com/docker/docker/api/types"
28
+ "github.com/docker/docker/api/types/blkiodev"
28
29
"github.com/docker/docker/api/types/container"
29
30
"github.com/docker/docker/api/types/mount"
30
31
"github.com/docker/docker/api/types/network"
@@ -377,24 +378,32 @@ func getRestartPolicy(service types.ServiceConfig) container.RestartPolicy {
377
378
}
378
379
379
380
func getDeployResources (s types.ServiceConfig ) container.Resources {
380
- resources := container.Resources {}
381
- if s .Deploy == nil {
382
- return resources
383
- }
384
-
385
- reservations := s .Deploy .Resources .Reservations
386
-
387
- if reservations == nil || len (reservations .Devices ) == 0 {
388
- return resources
389
- }
390
-
391
- for _ , device := range reservations .Devices {
392
- resources .DeviceRequests = append (resources .DeviceRequests , container.DeviceRequest {
393
- Capabilities : [][]string {device .Capabilities },
394
- Count : int (device .Count ),
395
- DeviceIDs : device .IDs ,
396
- Driver : device .Driver ,
397
- })
381
+ var swappiness * int64
382
+ if s .MemSwappiness != 0 {
383
+ val := int64 (s .MemSwappiness )
384
+ swappiness = & val
385
+ }
386
+ resources := container.Resources {
387
+ CgroupParent : s .CgroupParent ,
388
+ Memory : int64 (s .MemLimit ),
389
+ MemorySwap : int64 (s .MemSwapLimit ),
390
+ MemorySwappiness : swappiness ,
391
+ MemoryReservation : int64 (s .MemReservation ),
392
+ CPUCount : s .CPUCount ,
393
+ CPUPeriod : s .CPUPeriod ,
394
+ CPUQuota : s .CPUQuota ,
395
+ CPURealtimePeriod : s .CPURTPeriod ,
396
+ CPURealtimeRuntime : s .CPURTRuntime ,
397
+ CPUShares : s .CPUShares ,
398
+ CPUPercent : int64 (s .CPUS * 100 ),
399
+ CpusetCpus : s .CPUSet ,
400
+ }
401
+
402
+ setBlkio (s .BlkioConfig , & resources )
403
+
404
+ if s .Deploy != nil {
405
+ setLimits (s .Deploy .Resources .Limits , & resources )
406
+ setReservations (s .Deploy .Resources .Reservations , & resources )
398
407
}
399
408
400
409
for _ , device := range s .Devices {
@@ -430,6 +439,70 @@ func getDeployResources(s types.ServiceConfig) container.Resources {
430
439
return resources
431
440
}
432
441
442
+ func setReservations (reservations * types.Resource , resources * container.Resources ) {
443
+ if reservations == nil {
444
+ return
445
+ }
446
+ for _ , device := range reservations .Devices {
447
+ resources .DeviceRequests = append (resources .DeviceRequests , container.DeviceRequest {
448
+ Capabilities : [][]string {device .Capabilities },
449
+ Count : int (device .Count ),
450
+ DeviceIDs : device .IDs ,
451
+ Driver : device .Driver ,
452
+ })
453
+ }
454
+ }
455
+
456
+ func setLimits (limits * types.Resource , resources * container.Resources ) {
457
+ if limits == nil {
458
+ return
459
+ }
460
+ if limits .MemoryBytes != 0 {
461
+ resources .Memory = int64 (limits .MemoryBytes )
462
+ }
463
+ if limits .NanoCPUs != "" {
464
+ i , _ := strconv .ParseInt (limits .NanoCPUs , 10 , 64 )
465
+ resources .NanoCPUs = i
466
+ }
467
+ }
468
+
469
+ func setBlkio (blkio * types.BlkioConfig , resources * container.Resources ) {
470
+ if blkio == nil {
471
+ return
472
+ }
473
+ resources .BlkioWeight = blkio .Weight
474
+ for _ , b := range blkio .WeightDevice {
475
+ resources .BlkioWeightDevice = append (resources .BlkioWeightDevice , & blkiodev.WeightDevice {
476
+ Path : b .Path ,
477
+ Weight : b .Weight ,
478
+ })
479
+ }
480
+ for _ , b := range blkio .DeviceReadBps {
481
+ resources .BlkioDeviceReadBps = append (resources .BlkioDeviceReadBps , & blkiodev.ThrottleDevice {
482
+ Path : b .Path ,
483
+ Rate : b .Rate ,
484
+ })
485
+ }
486
+ for _ , b := range blkio .DeviceReadIOps {
487
+ resources .BlkioDeviceReadIOps = append (resources .BlkioDeviceReadIOps , & blkiodev.ThrottleDevice {
488
+ Path : b .Path ,
489
+ Rate : b .Rate ,
490
+ })
491
+ }
492
+ for _ , b := range blkio .DeviceWriteBps {
493
+ resources .BlkioDeviceWriteBps = append (resources .BlkioDeviceWriteBps , & blkiodev.ThrottleDevice {
494
+ Path : b .Path ,
495
+ Rate : b .Rate ,
496
+ })
497
+ }
498
+ for _ , b := range blkio .DeviceWriteIOps {
499
+ resources .BlkioDeviceWriteIOps = append (resources .BlkioDeviceWriteIOps , & blkiodev.ThrottleDevice {
500
+ Path : b .Path ,
501
+ Rate : b .Rate ,
502
+ })
503
+ }
504
+ }
505
+
433
506
func buildContainerPorts (s types.ServiceConfig ) nat.PortSet {
434
507
ports := nat.PortSet {}
435
508
for _ , p := range s .Ports {
0 commit comments