From 2e888f4ff6a0de0200f3ae0d13c1ceac2df57336 Mon Sep 17 00:00:00 2001 From: ningmingxiao Date: Mon, 15 Sep 2025 19:09:41 +0800 Subject: [PATCH] fix:forbid to restart/start container created by kubernetes Signed-off-by: ningmingxiao --- pkg/cmd/container/restart.go | 11 ++++++++++- pkg/containerutil/containerutil.go | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/container/restart.go b/pkg/cmd/container/restart.go index 3b376ada5a5..a3e6f0b2d49 100644 --- a/pkg/cmd/container/restart.go +++ b/pkg/cmd/container/restart.go @@ -21,10 +21,12 @@ import ( "fmt" containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/log" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/containerutil" "github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker" + "github.com/containerd/nerdctl/v2/pkg/labels/k8slabels" ) // Restart will restart one or more containers. @@ -35,13 +37,20 @@ func Restart(ctx context.Context, client *containerd.Client, containers []string if found.MatchCount > 1 { return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req) } + info, err := found.Container.Info(ctx) + if err != nil { + return fmt.Errorf("can't get container %s info ", found.Container.ID()) + } + if _, ok := info.Labels[k8slabels.ContainerType]; ok { + log.L.Warnf("nerdctl does not support restarting container %s created by Kubernetes", info.ID) + } if err := containerutil.Stop(ctx, found.Container, options.Timeout, options.Signal); err != nil { return err } if err := containerutil.Start(ctx, found.Container, false, false, client, ""); err != nil { return err } - _, err := fmt.Fprintln(options.Stdout, found.Req) + _, err = fmt.Fprintln(options.Stdout, found.Req) return err }, } diff --git a/pkg/containerutil/containerutil.go b/pkg/containerutil/containerutil.go index 31ded1a3b93..d29a4035f94 100644 --- a/pkg/containerutil/containerutil.go +++ b/pkg/containerutil/containerutil.go @@ -216,6 +216,9 @@ func Start(ctx context.Context, container containerd.Container, isAttach bool, i return err } + if _, ok := lab[k8slabels.ContainerType]; ok { + log.L.Warnf("nerdctl does not support starting container %s created by Kubernetes", container.ID()) + } if err := ReconfigNetContainer(ctx, container, client, lab); err != nil { return err }