From decc7a4a1e93d50353766d2cd92085d4d3e2cffb Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 5 Sep 2024 11:45:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=AE=B9=E5=99=A8=E7=AB=AF=E5=8F=A3=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#6370)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #6333 --- backend/app/service/container.go | 111 ++++++++++++++++++------------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/backend/app/service/container.go b/backend/app/service/container.go index 55a756124..431162b9b 100644 --- a/backend/app/service/container.go +++ b/backend/app/service/container.go @@ -187,7 +187,7 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro IsFromApp = true } - ports := loadContainerPort(item.Ports) + exposePorts, _ := loadContainerPort(item.ID, client) info := dto.ContainerInfo{ ContainerID: item.ID, CreateTime: time.Unix(item.Created, 0).Format(constant.DateTimeLayout), @@ -196,7 +196,7 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro ImageName: item.Image, State: item.State, RunTime: item.Status, - Ports: ports, + Ports: transPortToStr(exposePorts), IsFromApp: IsFromApp, IsFromCompose: IsFromCompose, } @@ -445,7 +445,8 @@ func (u *ContainerService) ContainerInfo(req dto.OperationWithName) (*dto.Contai } } - data.ExposedPorts, _ = loadContainerPortForInfo(req.Name, client) + exposePorts, _ := loadContainerPort(oldContainer.ID, client) + data.ExposedPorts = loadContainerPortForInfo(exposePorts) networkSettings := oldContainer.NetworkSettings bridgeNetworkSettings := networkSettings.Networks[data.Network] if bridgeNetworkSettings.IPAMConfig != nil { @@ -1213,7 +1214,65 @@ func loadVolumeBinds(binds []types.MountPoint) []dto.VolumeHelper { return datas } -func loadContainerPort(ports []types.Port) []string { +func loadContainerPort(id string, client *client.Client) ([]types.Port, error) { + container, err := client.ContainerInspect(context.Background(), id) + if err != nil { + return nil, err + } + var itemPorts []types.Port + for key, val := range container.ContainerJSONBase.HostConfig.PortBindings { + if !strings.Contains(string(key), "/") { + continue + } + item := strings.Split(string(key), "/") + itemPort, _ := strconv.ParseUint(item[0], 10, 16) + + for _, itemVal := range val { + publicPort, _ := strconv.ParseUint(itemVal.HostPort, 10, 16) + itemPorts = append(itemPorts, types.Port{PrivatePort: uint16(itemPort), Type: item[1], PublicPort: uint16(publicPort), IP: itemVal.HostIP}) + } + } + return itemPorts, nil +} + +func loadContainerPortForInfo(itemPorts []types.Port) []dto.PortHelper { + var exposedPorts []dto.PortHelper + samePortMap := make(map[string]dto.PortHelper) + ports := transPortToStr(itemPorts) + 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 { + itemPort.HostPort = itemStr[0] + } else { + itemPort.HostIP = itemStr[0][0:lastIndex] + itemPort.HostPort = itemStr[0][lastIndex+1:] + } + itemContainer := strings.Split(itemStr[1], "/") + if len(itemContainer) != 2 { + continue + } + 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 +} + +func transPortToStr(ports []types.Port) []string { var ( ipv4Ports []types.Port ipv6Ports []types.Port @@ -1299,47 +1358,3 @@ 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 -}