File: update_linux.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (75 lines) | stat: -rw-r--r-- 2,076 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package daemon // import "github.com/docker/docker/daemon"

import (
	"time"

	"github.com/docker/docker/api/types/container"
	libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
	specs "github.com/opencontainers/runtime-spec/specs-go"
)

func toContainerdResources(resources container.Resources) *libcontainerdtypes.Resources {
	var r libcontainerdtypes.Resources

	if resources.BlkioWeight != 0 {
		r.BlockIO = &specs.LinuxBlockIO{
			Weight: &resources.BlkioWeight,
		}
	}

	cpu := specs.LinuxCPU{
		Cpus: resources.CpusetCpus,
		Mems: resources.CpusetMems,
	}
	if resources.CPUShares != 0 {
		shares := uint64(resources.CPUShares)
		cpu.Shares = &shares
	}

	var (
		period uint64
		quota  int64
	)
	if resources.NanoCPUs != 0 {
		period = uint64(100 * time.Millisecond / time.Microsecond)
		quota = resources.NanoCPUs * int64(period) / 1e9
	}
	if quota == 0 && resources.CPUQuota != 0 {
		quota = resources.CPUQuota
	}
	if period == 0 && resources.CPUPeriod != 0 {
		period = uint64(resources.CPUPeriod)
	}

	if period != 0 {
		cpu.Period = &period
	}
	if quota != 0 {
		cpu.Quota = &quota
	}

	if cpu != (specs.LinuxCPU{}) {
		r.CPU = &cpu
	}

	var memory specs.LinuxMemory
	if resources.Memory != 0 {
		memory.Limit = &resources.Memory
	}
	if resources.MemoryReservation != 0 {
		memory.Reservation = &resources.MemoryReservation
	}
	if resources.KernelMemory != 0 { //nolint:staticcheck // ignore SA1019: memory.Kernel is deprecated: kernel-memory limits are not supported in cgroups v2, and were obsoleted in [kernel v5.4]. This field should no longer be used, as it may be ignored by runtimes.
		memory.Kernel = &resources.KernelMemory //nolint:staticcheck // ignore SA1019: memory.Kernel is deprecated: kernel-memory limits are not supported in cgroups v2, and were obsoleted in [kernel v5.4]. This field should no longer be used, as it may be ignored by runtimes.
	}
	if resources.MemorySwap > 0 {
		memory.Swap = &resources.MemorySwap
	}

	if memory != (specs.LinuxMemory{}) {
		r.Memory = &memory
	}

	r.Pids = getPidsLimit(resources)
	return &r
}