update_linux.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "time"
  4. "github.com/docker/docker/api/types/container"
  5. libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
  6. specs "github.com/opencontainers/runtime-spec/specs-go"
  7. )
  8. func toContainerdResources(resources container.Resources) *libcontainerdtypes.Resources {
  9. var r libcontainerdtypes.Resources
  10. if resources.BlkioWeight != 0 {
  11. r.BlockIO = &specs.LinuxBlockIO{
  12. Weight: &resources.BlkioWeight,
  13. }
  14. }
  15. cpu := specs.LinuxCPU{
  16. Cpus: resources.CpusetCpus,
  17. Mems: resources.CpusetMems,
  18. }
  19. if resources.CPUShares != 0 {
  20. shares := uint64(resources.CPUShares)
  21. cpu.Shares = &shares
  22. }
  23. var (
  24. period uint64
  25. quota int64
  26. )
  27. if resources.NanoCPUs != 0 {
  28. period = uint64(100 * time.Millisecond / time.Microsecond)
  29. quota = resources.NanoCPUs * int64(period) / 1e9
  30. }
  31. if quota == 0 && resources.CPUQuota != 0 {
  32. quota = resources.CPUQuota
  33. }
  34. if period == 0 && resources.CPUPeriod != 0 {
  35. period = uint64(resources.CPUPeriod)
  36. }
  37. if period != 0 {
  38. cpu.Period = &period
  39. }
  40. if quota != 0 {
  41. cpu.Quota = &quota
  42. }
  43. if cpu != (specs.LinuxCPU{}) {
  44. r.CPU = &cpu
  45. }
  46. var memory specs.LinuxMemory
  47. if resources.Memory != 0 {
  48. memory.Limit = &resources.Memory
  49. }
  50. if resources.MemoryReservation != 0 {
  51. memory.Reservation = &resources.MemoryReservation
  52. }
  53. if resources.KernelMemory != 0 {
  54. memory.Kernel = &resources.KernelMemory
  55. }
  56. if resources.MemorySwap > 0 {
  57. memory.Swap = &resources.MemorySwap
  58. }
  59. if memory != (specs.LinuxMemory{}) {
  60. r.Memory = &memory
  61. }
  62. r.Pids = getPidsLimit(resources)
  63. return &r
  64. }