feat: respect individual ports instead of replacing the entire slice of ports
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

The following patch adapts the logic of the merge strategy existing and last
win for service ports.

In the past, the complete object has been replaced based on the merge strategy.
This behavior has been adapted, that each port should now considered
individually per strategy.

Both strategies now focus on the src port of the host system. With a last-win,
the dest port of the container is overwritten with an existing src port.

```diff
  service:
    my-app:
      ports:
- - 0.0.0.0:8080:80
+ - 0.0.0.0:8080:8080
      - 0.0.0.0:8443:8443
```

The situation is different with the existing win strategy. There, the destination
port can no longer be changed once there is a connection with a sourc port.
This commit is contained in:
2025-02-21 13:40:40 +01:00
parent f8b4fe9af6
commit 003db26fe5
4 changed files with 1038 additions and 135 deletions

View File

@ -122,7 +122,7 @@ func TestPort_DstIP(t *testing.T) {
}
for i, testCase := range testCases {
p := port(testCase.s)
p := Port(testCase.s)
require.Equal(testCase.expectedBool, p.existsDstIP(), "TestCase %v", i)
require.Equal(testCase.expectedString, p.getDstIP(), "TestCase %v", i)
}
@ -191,7 +191,7 @@ func TestPort_DstPort(t *testing.T) {
}
for i, testCase := range testCases {
p := port(testCase.s)
p := Port(testCase.s)
require.Equal(testCase.expectedBool, p.existsDstPort(), "TestCase %v", i)
require.Equal(testCase.expectedString, p.getDstPort(), "TestCase %v", i)
}
@ -263,7 +263,7 @@ func TestPort_Protocol(t *testing.T) {
}
for i, testCase := range testCases {
p := port(testCase.s)
p := Port(testCase.s)
require.Equal(testCase.expectedBool, p.existsProtocol(), "TestCase %v", i)
require.Equal(testCase.expectedString, p.getProtocol(), "TestCase %v", i)
}
@ -342,7 +342,7 @@ func TestPort_SrcIP(t *testing.T) {
}
for i, testCase := range testCases {
p := port(testCase.s)
p := Port(testCase.s)
require.Equal(testCase.expectedBool, p.existsSrcIP(), "TestCase %v", i)
require.Equal(testCase.expectedString, p.getSrcIP(), "TestCase %v", i)
}
@ -411,7 +411,7 @@ func TestPort_SrcPort(t *testing.T) {
}
for i, testCase := range testCases {
p := port(testCase.s)
p := Port(testCase.s)
require.Equal(testCase.expectedBool, p.existsSrcPort(), "TestCase %v", i)
require.Equal(testCase.expectedString, p.getSrcPort(), "TestCase %v", i)
}