-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
86 lines (78 loc) · 2.01 KB
/
options.go
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
76
77
78
79
80
81
82
83
84
85
86
package cgroup
import (
"github.com/containerd/cgroups/v3/cgroup2"
"github.com/opencontainers/runtime-spec/specs-go"
)
type Option func(cgroup ICgroup)
// for cgroup v1
func WithName(name string) Option {
return func(cgroup ICgroup) {
if c, ok := cgroup.Instance().(*cgroupImplV1); ok {
c.name = name
}
}
}
// for cgroup v2
func WithSlice(slice string) Option {
return func(cgroup ICgroup) {
if c, ok := cgroup.Instance().(*cgroupImplV2); ok {
c.slice = slice
}
}
}
// for cgroup v2
func WithGroup(group string) Option {
return func(cgroup ICgroup) {
if c, ok := cgroup.Instance().(*cgroupImplV2); ok {
c.group = group
}
}
}
// for cgroup v1,v2
func WithCPULimit(cpuLimit Percent) Option {
return func(cgroup ICgroup) {
if c, ok := cgroup.Instance().(*cgroupImplV1); ok {
if c.res.CPU == nil {
c.res.CPU = &specs.LinuxCPU{}
}
if c.res.CPU.Period == nil {
c.res.CPU.Period = new(uint64)
*c.res.CPU.Period = 100000
}
c.res.CPU.Quota = new(int64)
*c.res.CPU.Quota = int64(*c.res.CPU.Period * uint64(cpuLimit) / 100)
} else if c, ok := cgroup.Instance().(*cgroupImplV2); ok {
var period *uint64 = new(uint64)
*period = 100000
var quota *int64 = new(int64)
*quota = int64(*period * uint64(cpuLimit) / 100)
c.res.CPU.Max = cgroup2.NewCPUMax(quota, period)
}
}
}
// for cgroup v1,v2
func WithMemoryLimit(memory Memory) Option {
return func(cgroup ICgroup) {
if c, ok := cgroup.Instance().(*cgroupImplV1); ok {
if c.res.Memory == nil {
c.res.Memory = &specs.LinuxMemory{}
}
c.res.Memory.Limit = new(int64)
*c.res.Memory.Limit = int64(memory)
} else if c, ok := cgroup.Instance().(*cgroupImplV2); ok {
var mem *int64 = new(int64)
*mem = int64(memory)
c.res.Memory.Max = mem
}
}
}
// for cgroup v1,v2
func WithDisableOOMKiller() Option {
return func(cgroup ICgroup) {
if c, ok := cgroup.Instance().(*cgroupImplV1); ok {
c.disableOOMKiller()
} else if c, ok := cgroup.Instance().(*cgroupImplV2); ok {
c.disableOOMKiller()
}
}
}