/
limits.go
56 lines (45 loc) · 1.41 KB
/
limits.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
package bundlerules
import (
spec "code.cloudfoundry.org/guardian/gardener/container-spec"
"code.cloudfoundry.org/guardian/rundmc/goci"
"github.com/opencontainers/runtime-spec/specs-go"
)
var (
CpuPeriod uint64 = 100000
MinCpuQuota uint64 = 1000
)
type Limits struct {
CpuQuotaPerShare uint64
BlockIOWeight uint16
DisableSwapLimit bool
}
func (l Limits) Apply(bndl goci.Bndl, spec spec.DesiredContainerSpec) (goci.Bndl, error) {
limit := int64(spec.Limits.Memory.LimitInBytes)
var swapLimit *int64
if !l.DisableSwapLimit {
swapLimit = &limit
}
bndl = bndl.WithMemoryLimit(specs.LinuxMemory{Limit: &limit, Swap: swapLimit})
//lint:ignore SA1019 - we still specify this to make the deprecated logic work until we get rid of the code in garden
shares := uint64(spec.Limits.CPU.LimitInShares)
if spec.Limits.CPU.Weight > 0 {
shares = uint64(spec.Limits.CPU.Weight)
}
cpuSpec := specs.LinuxCPU{Shares: &shares}
if l.CpuQuotaPerShare > 0 && shares > 0 {
cpuSpec.Period = &CpuPeriod
quota := shares * l.CpuQuotaPerShare
if quota < MinCpuQuota {
quota = MinCpuQuota
}
cpuSpec.Quota = int64PtrVal(quota)
}
bndl = bndl.WithCPUShares(cpuSpec)
bndl = bndl.WithBlockIO(specs.LinuxBlockIO{Weight: &l.BlockIOWeight})
pids := int64(spec.Limits.Pid.Max)
return bndl.WithPidLimit(specs.LinuxPids{Limit: pids}), nil
}
func int64PtrVal(n uint64) *int64 {
unsignedVal := int64(n)
return &unsignedVal
}