Skip to content

Commit df40602

Browse files
authored
Merge pull request containerd#3781 from ningmingxiao/fix_update
update:fix update pids-limit=0 error
2 parents f917e5c + 75c8066 commit df40602

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

cmd/nerdctl/container/container_run_cgroup_linux_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package container
1818

1919
import (
2020
"bytes"
21+
"context"
2122
"fmt"
2223
"os"
2324
"path/filepath"
@@ -27,11 +28,14 @@ import (
2728
"gotest.tools/v3/assert"
2829

2930
"github.com/containerd/cgroups/v3"
31+
containerd "github.com/containerd/containerd/v2/client"
3032
"github.com/containerd/continuity/testutil/loopback"
3133

3234
"github.com/containerd/nerdctl/v2/pkg/cmd/container"
35+
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
3336
"github.com/containerd/nerdctl/v2/pkg/testutil"
3437
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
38+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
3539
)
3640

3741
func TestRunCgroupV2(t *testing.T) {
@@ -170,6 +174,53 @@ func TestRunCgroupV1(t *testing.T) {
170174
base.Cmd("run", "--rm", "--cpu-quota", "42000", "--cpu-period", "100000", "--cpuset-mems", "0", "--memory", "42m", "--memory-reservation", "6m", "--memory-swap", "100m", "--memory-swappiness", "0", "--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1", testutil.AlpineImage, "cat", quota, period, cpusetMems, memoryLimit, memoryReservation, memorySwap, memorySwappiness, pidsLimit, cpuShare, cpusetCpus).AssertOutExactly(expected)
171175
}
172176

177+
// TestIssue3781 tests https://github.com/containerd/nerdctl/issues/3781
178+
func TestIssue3781(t *testing.T) {
179+
t.Parallel()
180+
testCase := nerdtest.Setup()
181+
testCase.Require = test.Not(nerdtest.Docker)
182+
183+
base := testutil.NewBase(t)
184+
info := base.Info()
185+
switch info.CgroupDriver {
186+
case "none", "":
187+
t.Skip("test requires cgroup driver")
188+
}
189+
containerName := testutil.Identifier(t)
190+
base.Cmd("run", "-d", "--name", containerName, testutil.AlpineImage, "sleep", "infinity").AssertOK()
191+
defer func() {
192+
base.Cmd("rm", "-f", containerName)
193+
}()
194+
base.Cmd("update", "--cpuset-cpus", "0-1", containerName).AssertOK()
195+
addr := base.ContainerdAddress()
196+
client, err := containerd.New(addr, containerd.WithDefaultNamespace(testutil.Namespace))
197+
assert.NilError(base.T, err)
198+
ctx := context.Background()
199+
200+
// get container id by container name.
201+
var cid string
202+
var args []string
203+
args = append(args, containerName)
204+
walker := &containerwalker.ContainerWalker{
205+
Client: client,
206+
OnFound: func(ctx context.Context, found containerwalker.Found) error {
207+
if found.MatchCount > 1 {
208+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
209+
}
210+
cid = found.Container.ID()
211+
return nil
212+
},
213+
}
214+
err = walker.WalkAll(ctx, args, true)
215+
assert.NilError(base.T, err)
216+
217+
container, err := client.LoadContainer(ctx, cid)
218+
assert.NilError(base.T, err)
219+
spec, err := container.Spec(ctx)
220+
assert.NilError(base.T, err)
221+
assert.Equal(t, spec.Linux.Resources.Pids == nil, true)
222+
}
223+
173224
func TestRunDevice(t *testing.T) {
174225
if os.Geteuid() != 0 || userns.RunningInUserNS() {
175226
t.Skip("test requires the root in the initial user namespace")

cmd/nerdctl/container/container_update.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,18 @@ func updateContainer(ctx context.Context, client *containerd.Client, id string,
266266
if spec.Linux.Resources == nil {
267267
spec.Linux.Resources = &runtimespec.LinuxResources{}
268268
}
269-
if spec.Linux.Resources.BlockIO == nil {
270-
spec.Linux.Resources.BlockIO = &runtimespec.LinuxBlockIO{}
271-
}
272269
if cmd.Flags().Changed("blkio-weight") {
270+
if spec.Linux.Resources.BlockIO == nil {
271+
spec.Linux.Resources.BlockIO = &runtimespec.LinuxBlockIO{}
272+
}
273273
if spec.Linux.Resources.BlockIO.Weight != &opts.BlkioWeight {
274274
spec.Linux.Resources.BlockIO.Weight = &opts.BlkioWeight
275275
}
276276
}
277-
if spec.Linux.Resources.CPU == nil {
278-
spec.Linux.Resources.CPU = &runtimespec.LinuxCPU{}
277+
if cmd.Flags().Changed("cpu-shares") || cmd.Flags().Changed("cpu-quota") || cmd.Flags().Changed("cpu-period") || cmd.Flags().Changed("cpus") || cmd.Flags().Changed("cpuset-mems") || cmd.Flags().Changed("cpuset-cpus") {
278+
if spec.Linux.Resources.CPU == nil {
279+
spec.Linux.Resources.CPU = &runtimespec.LinuxCPU{}
280+
}
279281
}
280282
if cmd.Flags().Changed("cpu-shares") {
281283
if spec.Linux.Resources.CPU.Shares != &opts.CPUShares {
@@ -308,8 +310,10 @@ func updateContainer(ctx context.Context, client *containerd.Client, id string,
308310
spec.Linux.Resources.CPU.Cpus = opts.CpusetCpus
309311
}
310312
}
311-
if spec.Linux.Resources.Memory == nil {
312-
spec.Linux.Resources.Memory = &runtimespec.LinuxMemory{}
313+
if cmd.Flags().Changed("memory") || cmd.Flags().Changed("memory-reservation") {
314+
if spec.Linux.Resources.Memory == nil {
315+
spec.Linux.Resources.Memory = &runtimespec.LinuxMemory{}
316+
}
313317
}
314318
if cmd.Flags().Changed("memory") {
315319
if spec.Linux.Resources.Memory.Limit != &opts.MemoryLimitInBytes {
@@ -324,10 +328,10 @@ func updateContainer(ctx context.Context, client *containerd.Client, id string,
324328
spec.Linux.Resources.Memory.Reservation = &opts.MemoryReservation
325329
}
326330
}
327-
if spec.Linux.Resources.Pids == nil {
328-
spec.Linux.Resources.Pids = &runtimespec.LinuxPids{}
329-
}
330331
if cmd.Flags().Changed("pids-limit") {
332+
if spec.Linux.Resources.Pids == nil {
333+
spec.Linux.Resources.Pids = &runtimespec.LinuxPids{}
334+
}
331335
if spec.Linux.Resources.Pids.Limit != opts.PidsLimit {
332336
spec.Linux.Resources.Pids.Limit = opts.PidsLimit
333337
}

0 commit comments

Comments
 (0)