From 0252a30f05be7683c643cd32f1daf3bb2159f196 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 12 Oct 2023 11:50:31 +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=E5=AD=98=E5=82=A8=E5=8D=B7=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=AE=B9=E5=99=A8=E4=B8=A2=E5=A4=B1=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#2521)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/container.go | 35 +++++++++++++++++--------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/backend/app/service/container.go b/backend/app/service/container.go index bd6479584..a3f1244d6 100644 --- a/backend/app/service/container.go +++ b/backend/app/service/container.go @@ -316,15 +316,11 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerOperate) error { return buserr.New(constant.ErrContainerName) } - var config container.Config - var hostConf container.HostConfig - var networkConf network.NetworkingConfig - if err := loadConfigInfo(req, &config, &hostConf, &networkConf); err != nil { + config, hostConf, networkConf, err := loadConfigInfo(true, req, nil) + if err != nil { return err } - global.LOG.Infof("new container info %s has been made, now start to create", req.Name) - if !checkImageExist(client, req.Image) || req.ForcePull { if err := pullImages(ctx, client, req.Image); err != nil { if !req.ForcePull { @@ -333,7 +329,7 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerOperate) error { global.LOG.Errorf("force pull image %s failed, err: %v", req.Image, err) } } - container, err := client.ContainerCreate(ctx, &config, &hostConf, &networkConf, &v1.Platform{}, req.Name) + container, err := client.ContainerCreate(ctx, config, hostConf, networkConf, &v1.Platform{}, req.Name) if err != nil { _ = client.ContainerRemove(ctx, req.Name, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true}) return err @@ -431,16 +427,14 @@ func (u *ContainerService) ContainerUpdate(req dto.ContainerOperate) error { return err } - config := oldContainer.Config - hostConf := oldContainer.HostConfig - var networkConf network.NetworkingConfig - if err := loadConfigInfo(req, config, hostConf, &networkConf); err != nil { + config, hostConf, networkConf, err := loadConfigInfo(false, req, &oldContainer) + if err != nil { reCreateAfterUpdate(req.Name, client, oldContainer.Config, oldContainer.HostConfig, oldContainer.NetworkSettings) return err } global.LOG.Infof("new container info %s has been update, now start to recreate", req.Name) - container, err := client.ContainerCreate(ctx, config, hostConf, &networkConf, &v1.Platform{}, req.Name) + container, err := client.ContainerCreate(ctx, config, hostConf, networkConf, &v1.Platform{}, req.Name) if err != nil { reCreateAfterUpdate(req.Name, client, oldContainer.Config, oldContainer.HostConfig, oldContainer.NetworkSettings) return fmt.Errorf("update container failed, err: %v", err) @@ -846,10 +840,18 @@ func checkPortStats(ports []dto.PortHelper) (nat.PortMap, error) { return portMap, nil } -func loadConfigInfo(req dto.ContainerOperate, config *container.Config, hostConf *container.HostConfig, networkConf *network.NetworkingConfig) error { +func loadConfigInfo(isCreate bool, req dto.ContainerOperate, oldContainer *types.ContainerJSON) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) { + var config container.Config + var hostConf container.HostConfig + if !isCreate { + config = *oldContainer.Config + hostConf = *oldContainer.HostConfig + } + var networkConf network.NetworkingConfig + portMap, err := checkPortStats(req.ExposedPorts) if err != nil { - return err + return nil, nil, nil, err } exposed := make(nat.PortSet) for port := range portMap { @@ -867,7 +869,7 @@ func loadConfigInfo(req dto.ContainerOperate, config *container.Config, hostConf if len(req.Network) != 0 { networkConf.EndpointsConfig = map[string]*network.EndpointSettings{req.Network: {}} } else { - networkConf = &network.NetworkingConfig{} + networkConf = network.NetworkingConfig{} } hostConf.AutoRemove = req.AutoRemove @@ -886,7 +888,7 @@ func loadConfigInfo(req dto.ContainerOperate, config *container.Config, hostConf config.Volumes[volume.ContainerDir] = struct{}{} hostConf.Binds = append(hostConf.Binds, fmt.Sprintf("%s:%s:%s", volume.SourceDir, volume.ContainerDir, volume.Mode)) } - return nil + return &config, &hostConf, &networkConf, nil } func reCreateAfterUpdate(name string, client *client.Client, config *container.Config, hostConf *container.HostConfig, networkConf *types.NetworkSettings) { @@ -908,6 +910,7 @@ func reCreateAfterUpdate(name string, client *client.Client, config *container.C if err := client.ContainerStart(ctx, oldContainer.ID, types.ContainerStartOptions{}); err != nil { global.LOG.Errorf("restart after container update failed, err: %v", err) } + global.LOG.Errorf("recreate after container update successful") } func loadVolumeBinds(binds []string) []dto.VolumeHelper {