From a1d394fbc217a107ea274ba05daf2f0cf32eb204 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:07:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E8=8C=83=E5=9B=B4=E5=A4=AA=E5=A4=A7=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E7=BC=96=E8=BE=91=E6=89=93=E5=BC=80=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#6249)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #6245 --- backend/app/service/container.go | 61 ++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/backend/app/service/container.go b/backend/app/service/container.go index 08ea98e5d..55a756124 100644 --- a/backend/app/service/container.go +++ b/backend/app/service/container.go @@ -445,6 +445,7 @@ func (u *ContainerService) ContainerInfo(req dto.OperationWithName) (*dto.Contai } } + data.ExposedPorts, _ = loadContainerPortForInfo(req.Name, client) networkSettings := oldContainer.NetworkSettings bridgeNetworkSettings := networkSettings.Networks[data.Network] if bridgeNetworkSettings.IPAMConfig != nil { @@ -465,19 +466,7 @@ func (u *ContainerService) ContainerInfo(req dto.OperationWithName) (*dto.Contai for key, val := range oldContainer.Config.Labels { data.Labels = append(data.Labels, fmt.Sprintf("%s=%s", key, val)) } - for key, val := range oldContainer.HostConfig.PortBindings { - var itemPort dto.PortHelper - if !strings.Contains(string(key), "/") { - continue - } - itemPort.ContainerPort = strings.Split(string(key), "/")[0] - itemPort.Protocol = strings.Split(string(key), "/")[1] - for _, binds := range val { - itemPort.HostIP = binds.HostIP - itemPort.HostPort = binds.HostPort - data.ExposedPorts = append(data.ExposedPorts, itemPort) - } - } + data.AutoRemove = oldContainer.HostConfig.AutoRemove data.Privileged = oldContainer.HostConfig.Privileged data.PublishAllPorts = oldContainer.HostConfig.PublishAllPorts @@ -1201,7 +1190,7 @@ func reCreateAfterUpdate(name string, client *client.Client, config *container.C if err := client.ContainerStart(ctx, oldContainer.ID, container.StartOptions{}); err != nil { global.LOG.Errorf("restart after container update failed, err: %v", err) } - global.LOG.Errorf("recreate after container update successful") + global.LOG.Info("recreate after container update successful") } func loadVolumeBinds(binds []types.MountPoint) []dto.VolumeHelper { @@ -1310,3 +1299,47 @@ func simplifyPort(ports []types.Port) []string { } return datas } + +func loadContainerPortForInfo(id string, client *client.Client) ([]dto.PortHelper, error) { + var exposedPorts []dto.PortHelper + containers, err := client.ContainerList(context.Background(), container.ListOptions{}) + if err != nil { + return nil, err + } + samePortMap := make(map[string]dto.PortHelper) + for _, container := range containers { + if container.ID == id { + ports := loadContainerPort(container.Ports) + var itemPort dto.PortHelper + for _, item := range ports { + itemStr := strings.Split(item, "->") + if len(itemStr) < 2 { + continue + } + lastIndex := strings.LastIndex(itemStr[0], ":") + if lastIndex == -1 { + continue + } + itemContainer := strings.Split(itemStr[1], "/") + if len(itemContainer) != 2 { + continue + } + itemPort.HostIP = itemStr[0][0:lastIndex] + itemPort.HostPort = itemStr[0][lastIndex+1:] + itemPort.ContainerPort = itemContainer[0] + itemPort.Protocol = itemContainer[1] + keyItem := fmt.Sprintf("%s->%s/%s", itemPort.HostPort, itemPort.ContainerPort, itemPort.Protocol) + if val, ok := samePortMap[keyItem]; ok { + val.HostIP = "" + samePortMap[keyItem] = val + } else { + samePortMap[keyItem] = itemPort + } + } + } + } + for _, val := range samePortMap { + exposedPorts = append(exposedPorts, val) + } + return exposedPorts, nil +}