From 12eeb6503c61669f54f23348591959f886366a74 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 5 Jul 2023 16:48:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E6=97=A0=E6=B3=95=E7=BC=96=E8=BE=91=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E7=9A=84=E9=97=AE=E9=A2=98=20(#1546)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/dto/container.go | 1 + backend/app/service/container.go | 20 +++++++++++-- frontend/src/api/interface/container.ts | 1 + .../container/container/operate/index.vue | 30 +++++++++++-------- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/backend/app/dto/container.go b/backend/app/dto/container.go index d3ee164c0..356eeb304 100644 --- a/backend/app/dto/container.go +++ b/backend/app/dto/container.go @@ -38,6 +38,7 @@ type ResourceLimit struct { } type ContainerOperate struct { + ContainerID string `json:"containerID"` Name string `json:"name"` Image string `json:"image"` PublishAllPorts bool `json:"publishAllPorts"` diff --git a/backend/app/service/container.go b/backend/app/service/container.go index 147d4918b..797ff1e9d 100644 --- a/backend/app/service/container.go +++ b/backend/app/service/container.go @@ -285,6 +285,11 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerOperate) error { if err != nil { return err } + ctx := context.Background() + newContainer, _ := client.ContainerInspect(ctx, req.Name) + if len(newContainer.ID) != 0 { + return buserr.New(constant.ErrContainerName) + } var config container.Config var hostConf container.HostConfig @@ -294,7 +299,6 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerOperate) error { global.LOG.Infof("new container info %s has been made, now start to create", req.Name) - ctx := context.Background() if !checkImageExist(client, req.Image) { if err := pullImages(ctx, client, req.Image); err != nil { return err @@ -325,6 +329,7 @@ func (u *ContainerService) ContainerInfo(req dto.OperationWithName) (*dto.Contai } var data dto.ContainerOperate + data.ContainerID = oldContainer.ID data.Name = strings.ReplaceAll(oldContainer.Name, "/", "") data.Image = oldContainer.Config.Image data.Cmd = oldContainer.Config.Cmd @@ -372,7 +377,12 @@ func (u *ContainerService) ContainerUpdate(req dto.ContainerOperate) error { return err } ctx := context.Background() - oldContainer, err := client.ContainerInspect(ctx, req.Name) + newContainer, _ := client.ContainerInspect(ctx, req.Name) + if len(newContainer.ID) != 0 && newContainer.ID != req.ContainerID { + return buserr.New(constant.ErrContainerName) + } + + oldContainer, err := client.ContainerInspect(ctx, req.ContainerID) if err != nil { return err } @@ -386,7 +396,7 @@ func (u *ContainerService) ContainerUpdate(req dto.ContainerOperate) error { if err := loadConfigInfo(req, config, hostConf); err != nil { return err } - if err := client.ContainerRemove(ctx, req.Name, types.ContainerRemoveOptions{Force: true}); err != nil { + if err := client.ContainerRemove(ctx, req.ContainerID, types.ContainerRemoveOptions{Force: true}); err != nil { return err } @@ -460,6 +470,10 @@ func (u *ContainerService) ContainerOperation(req dto.ContainerOperation) error case constant.ContainerOpUnpause: err = client.ContainerUnpause(ctx, req.Name) case constant.ContainerOpRename: + newContainer, _ := client.ContainerInspect(ctx, req.NewName) + if len(newContainer.ID) != 0 { + return buserr.New(constant.ErrContainerName) + } err = client.ContainerRename(ctx, req.Name, req.NewName) case constant.ContainerOpRemove: err = client.ContainerRemove(ctx, req.Name, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true}) diff --git a/frontend/src/api/interface/container.ts b/frontend/src/api/interface/container.ts index 42d232787..5db2daf64 100644 --- a/frontend/src/api/interface/container.ts +++ b/frontend/src/api/interface/container.ts @@ -17,6 +17,7 @@ export namespace Container { memory: number; } export interface ContainerHelper { + containerID: string; name: string; image: string; cmdStr: string; diff --git a/frontend/src/views/container/container/operate/index.vue b/frontend/src/views/container/container/operate/index.vue index 2a3ff5699..702e71d66 100644 --- a/frontend/src/views/container/container/operate/index.vue +++ b/frontend/src/views/container/container/operate/index.vue @@ -1,7 +1,7 @@