Skip to content

Commit 895a9eb

Browse files
authored
Merge pull request #4516 from ningmingxiao/forbid_restart_k8s
fix:forbid to restart/start container created by k8s
2 parents 8f6860a + 2e888f4 commit 895a9eb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pkg/cmd/container/restart.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import (
2121
"fmt"
2222

2323
containerd "github.com/containerd/containerd/v2/client"
24+
"github.com/containerd/log"
2425

2526
"github.com/containerd/nerdctl/v2/pkg/api/types"
2627
"github.com/containerd/nerdctl/v2/pkg/config"
2728
"github.com/containerd/nerdctl/v2/pkg/containerutil"
2829
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
30+
"github.com/containerd/nerdctl/v2/pkg/labels/k8slabels"
2931
)
3032

3133
// Restart will restart one or more containers.
@@ -36,13 +38,20 @@ func Restart(ctx context.Context, client *containerd.Client, containers []string
3638
if found.MatchCount > 1 {
3739
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
3840
}
41+
info, err := found.Container.Info(ctx)
42+
if err != nil {
43+
return fmt.Errorf("can't get container %s info ", found.Container.ID())
44+
}
45+
if _, ok := info.Labels[k8slabels.ContainerType]; ok {
46+
log.L.Warnf("nerdctl does not support restarting container %s created by Kubernetes", info.ID)
47+
}
3948
if err := containerutil.Stop(ctx, found.Container, options.Timeout, options.Signal); err != nil {
4049
return err
4150
}
4251
if err := containerutil.Start(ctx, found.Container, false, false, client, "", (*config.Config)(&options.GOption)); err != nil {
4352
return err
4453
}
45-
_, err := fmt.Fprintln(options.Stdout, found.Req)
54+
_, err = fmt.Fprintln(options.Stdout, found.Req)
4655
return err
4756
},
4857
}

pkg/containerutil/containerutil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ func Start(ctx context.Context, container containerd.Container, isAttach bool, i
218218
return err
219219
}
220220

221+
if _, ok := lab[k8slabels.ContainerType]; ok {
222+
log.L.Warnf("nerdctl does not support starting container %s created by Kubernetes", container.ID())
223+
}
221224
if err := ReconfigNetContainer(ctx, container, client, lab); err != nil {
222225
return err
223226
}

0 commit comments

Comments
 (0)