Browse Source

fix: 解决停止状态容器端口获取不正确的问题 (#6370)

Refs #6333
pull/6376/head
ssongliu 3 months ago committed by GitHub
parent
commit
decc7a4a1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 111
      backend/app/service/container.go

111
backend/app/service/container.go

@ -187,7 +187,7 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro
IsFromApp = true IsFromApp = true
} }
ports := loadContainerPort(item.Ports) exposePorts, _ := loadContainerPort(item.ID, client)
info := dto.ContainerInfo{ info := dto.ContainerInfo{
ContainerID: item.ID, ContainerID: item.ID,
CreateTime: time.Unix(item.Created, 0).Format(constant.DateTimeLayout), 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, ImageName: item.Image,
State: item.State, State: item.State,
RunTime: item.Status, RunTime: item.Status,
Ports: ports, Ports: transPortToStr(exposePorts),
IsFromApp: IsFromApp, IsFromApp: IsFromApp,
IsFromCompose: IsFromCompose, 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 networkSettings := oldContainer.NetworkSettings
bridgeNetworkSettings := networkSettings.Networks[data.Network] bridgeNetworkSettings := networkSettings.Networks[data.Network]
if bridgeNetworkSettings.IPAMConfig != nil { if bridgeNetworkSettings.IPAMConfig != nil {
@ -1213,7 +1214,65 @@ func loadVolumeBinds(binds []types.MountPoint) []dto.VolumeHelper {
return datas 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 ( var (
ipv4Ports []types.Port ipv4Ports []types.Port
ipv6Ports []types.Port ipv6Ports []types.Port
@ -1299,47 +1358,3 @@ func simplifyPort(ports []types.Port) []string {
} }
return datas 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
}

Loading…
Cancel
Save