Skip to content

Instantly share code, notes, and snippets.

@pacoxu
Created July 14, 2023 04:42
Show Gist options
  • Save pacoxu/e5049e44f9a6d8dc112fecb85d7161b9 to your computer and use it in GitHub Desktop.
Save pacoxu/e5049e44f9a6d8dc112fecb85d7161b9 to your computer and use it in GitHub Desktop.
#113245 Tim's demo code to check dup ports
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type ContainerPort struct {
Port int32
Protocol string
HostPort int32
HostIP string
}
func warn(index int, ports []ContainerPort) []string {
type portBlock struct {
index int
port ContainerPort
}
allPorts := map[string][]portBlock{}
var errs []string
// This would need to visit all containers
for i, port := range ports {
if port.HostIP != "" && port.HostPort == 0 {
errs = append(errs, fmt.Sprintf("case %d:: Warning: [%d] hostIP set without hostPort: %+v", index, i, port))
}
k := fmt.Sprintf("%d/%s", port.Port, port.Protocol)
if others, found := allPorts[k]; found {
// keys collided, but may be OK
for _, other := range others {
if port.HostPort == other.port.HostPort && port.HostIP == other.port.HostIP {
// Exactly-equal is obvious. Validation should already filter for this except when these are unspecified.
errs = append(errs, fmt.Sprintf("case %d:: Warning: duplicate ports: [%d] and [%d]: %+v", index, other.index, i, other.port))
} else if port.HostPort == 0 || other.port.HostPort == 0 {
// HostPort = 0 is redundant with any other value, which is odd but not really dangerous. HostIP doesn't matter here.
errs = append(errs, fmt.Sprintf("case %d:: Warning: overlapping ports: [%d] and [%d]: %+v", index, other.index, i, other.port))
} else if a, b := port.HostIP == "", other.port.HostIP == ""; port.HostPort == other.port.HostPort && ((a || b) && !(a && b)) {
// If the HostPorts are the same and either HostIP is not specified while the other is not, the behavior is undefined.
errs = append(errs, fmt.Sprintf("case %d:: Warning: dangerously ambiguous ports: [%d] and [%d]: %+v", index, other.index, i, other.port))
}
}
}
allPorts[k] = append(allPorts[k], portBlock{i, port})
}
return errs
}
func main() {
cases := []struct {
input []ContainerPort
fail bool
}{{
input: []ContainerPort{
{9376, "TCP", 0, ""}}, // OK
fail: false,
}, {
input: []ContainerPort{
{9376, "TCP", 93, ""}}, // OK
fail: false,
}, {
input: []ContainerPort{
{9376, "TCP", 93, "1.2.3.4"}}, // OK
fail: false,
}, {
input: []ContainerPort{
{9376, "TCP", 93, ""}, // OK
{9376, "TCP", 76, ""},
},
fail: false,
}, {
input: []ContainerPort{
{9376, "TCP", 93, "1.2.3.4"}, // OK
{9376, "TCP", 93, "5.6.7.8"},
},
fail: false,
}, {
input: []ContainerPort{
{9376, "TCP", 93, "1.2.3.4"}, // OK
{9376, "TCP", 76, "1.2.3.4"},
},
fail: false,
}, {
input: []ContainerPort{
{9376, "TCP", 93, "1.2.3.4"}, // OK
{9376, "TCP", 76, ""},
},
fail: false,
}, {
input: []ContainerPort{
{9376, "TCP", 0, "1.2.3.4"}, // IP without Port
},
fail: true,
}, {
input: []ContainerPort{
{9376, "TCP", 0, ""}, // DUP
{9376, "TCP", 0, ""},
},
fail: true,
}, {
input: []ContainerPort{
{9376, "TCP", 93, ""}, // DUP
{9376, "TCP", 93, ""},
},
fail: true,
}, {
input: []ContainerPort{
{9376, "TCP", 93, "1.2.3.4"}, // DUP
{9376, "TCP", 93, "1.2.3.4"},
},
fail: true,
}, {
input: []ContainerPort{
{9376, "TCP", 0, ""}, // OVERLAP
{9376, "TCP", 93, ""},
},
fail: true,
}, {
input: []ContainerPort{
{9376, "TCP", 93, ""}, // OVERLAP
{9376, "TCP", 0, ""},
},
fail: true,
}, {
input: []ContainerPort{
{9376, "TCP", 93, ""}, // OVERLAP
{9376, "TCP", 0, ""},
{9376, "TCP", 76, ""},
},
fail: true,
}, {
input: []ContainerPort{
{9376, "TCP", 93, "1.2.3.4"}, // AMBIGUOUS
{9376, "TCP", 93, ""},
},
fail: true,
}, {
input: []ContainerPort{
{9376, "TCP", 93, ""}, // AMBIGUOUS
{9376, "TCP", 93, "1.2.3.4"},
},
fail: true,
}}
for i, input := range cases {
errs := warn(i, input.input)
if (len(errs) > 0) != input.fail {
fmt.Println("TEST FAILED for case", i)
} else if len(errs) > 0 {
fmt.Println(errs)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment