mirror of
https://github.com/containers/podman.git
synced 2025-07-01 16:17:06 +08:00
libpod: Move ocicniPortsToNetTypesPorts and compareOCICNIPorts to networking_common.go
[NO NEW TESTS NEEDED] Signed-off-by: Doug Rabson <dfr@rabson.org>
This commit is contained in:
@ -652,3 +652,68 @@ func (r *Runtime) normalizeNetworkName(nameOrID string) (string, error) {
|
||||
}
|
||||
return net.Name, nil
|
||||
}
|
||||
|
||||
// ocicniPortsToNetTypesPorts convert the old port format to the new one
|
||||
// while deduplicating ports into ranges
|
||||
func ocicniPortsToNetTypesPorts(ports []types.OCICNIPortMapping) []types.PortMapping {
|
||||
if len(ports) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
newPorts := make([]types.PortMapping, 0, len(ports))
|
||||
|
||||
// first sort the ports
|
||||
sort.Slice(ports, func(i, j int) bool {
|
||||
return compareOCICNIPorts(ports[i], ports[j])
|
||||
})
|
||||
|
||||
// we already check if the slice is empty so we can use the first element
|
||||
currentPort := types.PortMapping{
|
||||
HostIP: ports[0].HostIP,
|
||||
HostPort: uint16(ports[0].HostPort),
|
||||
ContainerPort: uint16(ports[0].ContainerPort),
|
||||
Protocol: ports[0].Protocol,
|
||||
Range: 1,
|
||||
}
|
||||
|
||||
for i := 1; i < len(ports); i++ {
|
||||
if ports[i].HostIP == currentPort.HostIP &&
|
||||
ports[i].Protocol == currentPort.Protocol &&
|
||||
ports[i].HostPort-int32(currentPort.Range) == int32(currentPort.HostPort) &&
|
||||
ports[i].ContainerPort-int32(currentPort.Range) == int32(currentPort.ContainerPort) {
|
||||
currentPort.Range++
|
||||
} else {
|
||||
newPorts = append(newPorts, currentPort)
|
||||
currentPort = types.PortMapping{
|
||||
HostIP: ports[i].HostIP,
|
||||
HostPort: uint16(ports[i].HostPort),
|
||||
ContainerPort: uint16(ports[i].ContainerPort),
|
||||
Protocol: ports[i].Protocol,
|
||||
Range: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
newPorts = append(newPorts, currentPort)
|
||||
return newPorts
|
||||
}
|
||||
|
||||
// compareOCICNIPorts will sort the ocicni ports by
|
||||
// 1) host ip
|
||||
// 2) protocol
|
||||
// 3) hostPort
|
||||
// 4) container port
|
||||
func compareOCICNIPorts(i, j types.OCICNIPortMapping) bool {
|
||||
if i.HostIP != j.HostIP {
|
||||
return i.HostIP < j.HostIP
|
||||
}
|
||||
|
||||
if i.Protocol != j.Protocol {
|
||||
return i.Protocol < j.Protocol
|
||||
}
|
||||
|
||||
if i.HostPort != j.HostPort {
|
||||
return i.HostPort < j.HostPort
|
||||
}
|
||||
|
||||
return i.ContainerPort < j.ContainerPort
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import (
|
||||
"net"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/containers/buildah/pkg/jail"
|
||||
"github.com/containers/common/libnetwork/types"
|
||||
@ -264,71 +263,6 @@ func (c *Container) reloadRootlessRLKPortMapping() error {
|
||||
return errors.New("unsupported (*Container).reloadRootlessRLKPortMapping")
|
||||
}
|
||||
|
||||
// ocicniPortsToNetTypesPorts convert the old port format to the new one
|
||||
// while deduplicating ports into ranges
|
||||
func ocicniPortsToNetTypesPorts(ports []types.OCICNIPortMapping) []types.PortMapping {
|
||||
if len(ports) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
newPorts := make([]types.PortMapping, 0, len(ports))
|
||||
|
||||
// first sort the ports
|
||||
sort.Slice(ports, func(i, j int) bool {
|
||||
return compareOCICNIPorts(ports[i], ports[j])
|
||||
})
|
||||
|
||||
// we already check if the slice is empty so we can use the first element
|
||||
currentPort := types.PortMapping{
|
||||
HostIP: ports[0].HostIP,
|
||||
HostPort: uint16(ports[0].HostPort),
|
||||
ContainerPort: uint16(ports[0].ContainerPort),
|
||||
Protocol: ports[0].Protocol,
|
||||
Range: 1,
|
||||
}
|
||||
|
||||
for i := 1; i < len(ports); i++ {
|
||||
if ports[i].HostIP == currentPort.HostIP &&
|
||||
ports[i].Protocol == currentPort.Protocol &&
|
||||
ports[i].HostPort-int32(currentPort.Range) == int32(currentPort.HostPort) &&
|
||||
ports[i].ContainerPort-int32(currentPort.Range) == int32(currentPort.ContainerPort) {
|
||||
currentPort.Range = currentPort.Range + 1
|
||||
} else {
|
||||
newPorts = append(newPorts, currentPort)
|
||||
currentPort = types.PortMapping{
|
||||
HostIP: ports[i].HostIP,
|
||||
HostPort: uint16(ports[i].HostPort),
|
||||
ContainerPort: uint16(ports[i].ContainerPort),
|
||||
Protocol: ports[i].Protocol,
|
||||
Range: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
newPorts = append(newPorts, currentPort)
|
||||
return newPorts
|
||||
}
|
||||
|
||||
// compareOCICNIPorts will sort the ocicni ports by
|
||||
// 1) host ip
|
||||
// 2) protocol
|
||||
// 3) hostPort
|
||||
// 4) container port
|
||||
func compareOCICNIPorts(i, j types.OCICNIPortMapping) bool {
|
||||
if i.HostIP != j.HostIP {
|
||||
return i.HostIP < j.HostIP
|
||||
}
|
||||
|
||||
if i.Protocol != j.Protocol {
|
||||
return i.Protocol < j.Protocol
|
||||
}
|
||||
|
||||
if i.HostPort != j.HostPort {
|
||||
return i.HostPort < j.HostPort
|
||||
}
|
||||
|
||||
return i.ContainerPort < j.ContainerPort
|
||||
}
|
||||
|
||||
func (c *Container) setupRootlessNetwork() error {
|
||||
return nil
|
||||
}
|
||||
|
@ -13,7 +13,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
@ -864,68 +863,3 @@ func (w *logrusDebugWriter) Write(p []byte) (int, error) {
|
||||
logrus.Debugf("%s%s", w.prefix, string(p))
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// ocicniPortsToNetTypesPorts convert the old port format to the new one
|
||||
// while deduplicating ports into ranges
|
||||
func ocicniPortsToNetTypesPorts(ports []types.OCICNIPortMapping) []types.PortMapping {
|
||||
if len(ports) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
newPorts := make([]types.PortMapping, 0, len(ports))
|
||||
|
||||
// first sort the ports
|
||||
sort.Slice(ports, func(i, j int) bool {
|
||||
return compareOCICNIPorts(ports[i], ports[j])
|
||||
})
|
||||
|
||||
// we already check if the slice is empty so we can use the first element
|
||||
currentPort := types.PortMapping{
|
||||
HostIP: ports[0].HostIP,
|
||||
HostPort: uint16(ports[0].HostPort),
|
||||
ContainerPort: uint16(ports[0].ContainerPort),
|
||||
Protocol: ports[0].Protocol,
|
||||
Range: 1,
|
||||
}
|
||||
|
||||
for i := 1; i < len(ports); i++ {
|
||||
if ports[i].HostIP == currentPort.HostIP &&
|
||||
ports[i].Protocol == currentPort.Protocol &&
|
||||
ports[i].HostPort-int32(currentPort.Range) == int32(currentPort.HostPort) &&
|
||||
ports[i].ContainerPort-int32(currentPort.Range) == int32(currentPort.ContainerPort) {
|
||||
currentPort.Range++
|
||||
} else {
|
||||
newPorts = append(newPorts, currentPort)
|
||||
currentPort = types.PortMapping{
|
||||
HostIP: ports[i].HostIP,
|
||||
HostPort: uint16(ports[i].HostPort),
|
||||
ContainerPort: uint16(ports[i].ContainerPort),
|
||||
Protocol: ports[i].Protocol,
|
||||
Range: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
newPorts = append(newPorts, currentPort)
|
||||
return newPorts
|
||||
}
|
||||
|
||||
// compareOCICNIPorts will sort the ocicni ports by
|
||||
// 1) host ip
|
||||
// 2) protocol
|
||||
// 3) hostPort
|
||||
// 4) container port
|
||||
func compareOCICNIPorts(i, j types.OCICNIPortMapping) bool {
|
||||
if i.HostIP != j.HostIP {
|
||||
return i.HostIP < j.HostIP
|
||||
}
|
||||
|
||||
if i.Protocol != j.Protocol {
|
||||
return i.Protocol < j.Protocol
|
||||
}
|
||||
|
||||
if i.HostPort != j.HostPort {
|
||||
return i.HostPort < j.HostPort
|
||||
}
|
||||
|
||||
return i.ContainerPort < j.ContainerPort
|
||||
}
|
||||
|
Reference in New Issue
Block a user