fix(dockerCompoe): compare srcIP and srcPort of mergeExistingWinPorts()

This commit is contained in:
Markus Pesch 2025-02-21 09:51:53 +01:00
parent 2c91222753
commit f8b4fe9af6
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 61 additions and 6 deletions

View File

@ -856,15 +856,33 @@ func (s *Service) mergeExistingWinPorts(ports []string) {
case s.Ports == nil && ports == nil:
return
default:
for _, port := range ports {
if len(port) <= 0 {
continue
LOOP:
for i := range ports {
if len(ports[i]) <= 0 {
continue LOOP
}
src, dest, protocol := splitStringInPortMapping(port)
if !s.ExistsDestinationPort(dest) {
s.SetPort(src, dest, protocol)
newPort := port(ports[i])
for j := range s.Ports {
existingPort := port(s.Ports[j])
switch {
case newPort.existsSrcIP() && existingPort.existsSrcIP() &&
newPort.getSrc() == existingPort.getSrc():
continue LOOP
case !newPort.existsSrcIP() && existingPort.existsSrcIP() &&
newPort.getSrcPort() == existingPort.getSrcPort():
continue LOOP
case newPort.existsSrcIP() && !existingPort.existsSrcIP() &&
newPort.getSrcPort() == existingPort.getSrcPort():
continue LOOP
case !newPort.existsSrcIP() && !existingPort.existsSrcIP() &&
newPort.getSrcPort() == existingPort.getSrcPort():
continue LOOP
}
}
s.Ports = append(s.Ports, ports[i])
}
}
}

View File

@ -1183,6 +1183,43 @@ func TestService_MergeExistingWin(t *testing.T) {
Ports: []string{"80:80"},
},
},
{
serviceDeploymentA: &dockerCompose.Service{
Ports: []string{
"0.0.0.0:15005:5005/tcp",
"0.0.0.0:18080:8080/tcp",
},
},
serviceDeploymentB: &dockerCompose.Service{
Ports: []string{"0.0.0.0:6300:6300/tcp"},
},
expectedService: &dockerCompose.Service{
Ports: []string{
"0.0.0.0:15005:5005/tcp",
"0.0.0.0:18080:8080/tcp",
"0.0.0.0:6300:6300/tcp",
},
},
},
{
serviceDeploymentA: &dockerCompose.Service{
Ports: []string{
"0.0.0.0:15005:5005/tcp",
"0.0.0.0:18080:8080/tcp",
},
},
serviceDeploymentB: &dockerCompose.Service{
Ports: []string{
"15005:15005",
},
},
expectedService: &dockerCompose.Service{
Ports: []string{
"0.0.0.0:15005:5005/tcp",
"0.0.0.0:18080:8080/tcp",
},
},
},
// Secrets
{