From 6c98a955e3f9220b28e47145a7f3bf9b3d81b9e2 Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sat, 7 May 2022 15:23:56 -0400 Subject: [PATCH] Only set cpu limits if specified; closes pterodactyl/panel#3988 --- environment/docker/container.go | 18 ------------------ environment/settings.go | 27 +++++++++++++++++++++------ 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/environment/docker/container.go b/environment/docker/container.go index 04dca30..6973e78 100644 --- a/environment/docker/container.go +++ b/environment/docker/container.go @@ -480,21 +480,3 @@ func (e *Environment) convertMounts() []mount.Mount { return out } - -func (e *Environment) resources() container.Resources { - l := e.Configuration.Limits() - pids := l.ProcessLimit() - - return container.Resources{ - Memory: l.BoundedMemoryLimit(), - MemoryReservation: l.MemoryLimit * 1_000_000, - MemorySwap: l.ConvertedSwap(), - CPUQuota: l.ConvertedCpuLimit(), - CPUPeriod: 100_000, - CPUShares: 1024, - BlkioWeight: l.IoWeight, - OomKillDisable: &l.OOMDisabled, - CpusetCpus: l.Threads, - PidsLimit: &pids, - } -} diff --git a/environment/settings.go b/environment/settings.go index ae853cf..dcd7c3a 100644 --- a/environment/settings.go +++ b/environment/settings.go @@ -99,21 +99,36 @@ func (l Limits) ProcessLimit() int64 { return config.Get().Docker.ContainerPidLimit } +// AsContainerResources returns the available resources for a container in a format +// that Docker understands. func (l Limits) AsContainerResources() container.Resources { pids := l.ProcessLimit() - - return container.Resources{ + resources := container.Resources{ Memory: l.BoundedMemoryLimit(), MemoryReservation: l.MemoryLimit * 1_000_000, MemorySwap: l.ConvertedSwap(), - CPUQuota: l.ConvertedCpuLimit(), - CPUPeriod: 100_000, - CPUShares: 1024, BlkioWeight: l.IoWeight, OomKillDisable: &l.OOMDisabled, - CpusetCpus: l.Threads, PidsLimit: &pids, } + + // If the CPU Limit is not set, don't send any of these fields through. Providing + // them seems to break some Java services that try to read the available processors. + // + // @see https://github.com/pterodactyl/panel/issues/3988 + if l.CpuLimit > 0 { + resources.CPUQuota = l.CpuLimit * 1_000 + resources.CPUPeriod = 100_00 + resources.CPUShares = 1024 + } + + // Similar to above, don't set the specific assigned CPUs if we didn't actually limit + // the server to any of them. + if l.Threads != "" { + resources.CpusetCpus = l.Threads + } + + return resources } type Variables map[string]interface{}