github.com/rawahars/moby@v24.0.4+incompatible/daemon/update_linux.go (about)

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