Skip to content

Commit 2e888f4

Browse files
committed
fix:forbid to restart/start container created by kubernetes
Signed-off-by: ningmingxiao <[email protected]>
1 parent ae7c0e5 commit 2e888f4

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,10 +21,12 @@ 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/containerutil"
2728
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
29+
"github.com/containerd/nerdctl/v2/pkg/labels/k8slabels"
2830
)
2931

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

pkg/containerutil/containerutil.go

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

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

0 commit comments

Comments
 (0)