Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in NewSystemd on nil values #219

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions v2/cpuv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ func TestSystemdCgroupCpuController(t *testing.T) {
checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10))
}

func TestSystemdCgroupCpuController_NilWeight(t *testing.T) {
checkCgroupMode(t)
group := "testingCpuNilWeight.slice"
// nil weight defaults to 100
var quota int64 = 10000
var period uint64 = 8000
cpuMax := NewCPUMax(&quota, &period)
res := Resources{
CPU: &CPU{
Weight: nil,
Max: cpuMax,
},
}
_, err := NewSystemd("/", group, -1, &res)
if err != nil {
t.Fatal("failed to init new cgroup systemd manager: ", err)
}
}

func TestExtractQuotaAndPeriod(t *testing.T) {
var (
period uint64
Expand Down
4 changes: 2 additions & 2 deletions v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,12 @@ func NewSystemd(slice, group string, pid int, resources *Resources) (*Manager, e
properties = append(properties, newSystemdProperty("PIDs", []uint32{uint32(pid)}))
}

if resources.Memory != nil && *resources.Memory.Max != 0 {
if resources.Memory != nil && resources.Memory.Max != nil && *resources.Memory.Max != 0 {
properties = append(properties,
newSystemdProperty("MemoryMax", uint64(*resources.Memory.Max)))
}

if resources.CPU != nil && *resources.CPU.Weight != 0 {
if resources.CPU != nil && resources.CPU.Weight != nil && *resources.CPU.Weight != 0 {
properties = append(properties,
newSystemdProperty("CPUWeight", *resources.CPU.Weight))
}
Expand Down