Skip to content

Commit 2eeb8de

Browse files
authored
Merge pull request #3818 from zzzzzzzzzy9/update
reset spec if update returned error
2 parents b792f1f + ef26ad8 commit 2eeb8de

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

cmd/nerdctl/container/container_update.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"runtime"
25+
"time"
2526

2627
"github.com/docker/go-units"
2728
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
@@ -241,7 +242,7 @@ func getUpdateOption(cmd *cobra.Command, globalOptions types.GlobalCommandOption
241242
return options, nil
242243
}
243244

244-
func updateContainer(ctx context.Context, client *containerd.Client, id string, opts updateResourceOptions, cmd *cobra.Command) error {
245+
func updateContainer(ctx context.Context, client *containerd.Client, id string, opts updateResourceOptions, cmd *cobra.Command) (retErr error) {
245246
container, err := client.LoadContainer(ctx, id)
246247
if err != nil {
247248
return err
@@ -339,12 +340,18 @@ func updateContainer(ctx context.Context, client *containerd.Client, id string,
339340
}
340341

341342
if err := updateContainerSpec(ctx, container, spec); err != nil {
342-
log.G(ctx).WithError(err).Errorf("Failed to update spec %+v for container %q", spec, id)
343-
// reset spec on error.
344-
if err := updateContainerSpec(ctx, container, oldSpec); err != nil {
345-
log.G(ctx).WithError(err).Errorf("Failed to update spec %+v for container %q", oldSpec, id)
343+
return fmt.Errorf("failed to update spec %+v for container %q", spec, id)
344+
}
345+
defer func() {
346+
if retErr != nil {
347+
deferCtx, deferCancel := context.WithTimeout(ctx, 1*time.Minute)
348+
defer deferCancel()
349+
// Reset spec on error.
350+
if err := updateContainerSpec(deferCtx, container, oldSpec); err != nil {
351+
log.G(ctx).WithError(err).Errorf("Failed to update spec %+v for container %q", oldSpec, id)
352+
}
346353
}
347-
}
354+
}()
348355

349356
restart, err := cmd.Flags().GetString("restart")
350357
if err != nil {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package container
18+
19+
import (
20+
"testing"
21+
22+
"github.com/containerd/nerdctl/v2/pkg/testutil"
23+
)
24+
25+
func TestUpdateContainer(t *testing.T) {
26+
testutil.DockerIncompatible(t)
27+
testContainerName := testutil.Identifier(t)
28+
base := testutil.NewBase(t)
29+
base.Cmd("run", "-d", "--name", testContainerName, testutil.CommonImage, "sleep", "infinity").AssertOK()
30+
defer base.Cmd("rm", "-f", testContainerName).Run()
31+
base.Cmd("update", "--memory", "999999999", "--restart", "123", testContainerName).AssertFail()
32+
base.Cmd("inspect", "--mode=native", testContainerName).AssertOutNotContains(`"limit": 999999999,`)
33+
}

0 commit comments

Comments
 (0)