mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 16:51:18 -08:00
k8s-operator: stop PortRanges.All() at the last port in the range (#21305)
* k8s-operator: stop PortRanges.All() at the last port in the range PortRanges.All() walks a range with a uint16 counter and a port <= end condition. When end is 65535 the increment wraps back to zero instead of ending the loop, so the iterator restarts at 0 and yields the whole port space again, and again. ClashesWith is the caller that drains All(), so it is where this shows. With one ProxyClass holding 40000-65535, validating a second one holding 30000-30100 walks up to 65535, wraps, and reaches 30000 on the next pass, where Contains returns true. The two ranges are disjoint, but the second ProxyClass is rejected as clashing and never goes valid. allocatePorts stops at the first free port so it returns, but once past 65535 the port it picks is outside the range that was configured. endPort is only bounded by the cluster's --service-node-port-range, so 65535 is reachable on a cluster configured up to it. Yield end and then stop. Also skip a range whose Port is above its EndPort, which the old condition did for free and the new one would not. Fixes #21304 Change-Id: I7d60b133a4a83678bc6aba76c5f9b6b6e9e4e984 Signed-off-by: leoca <[email protected]> * k8s-operator: fix copyright header on types_proxyclass_test.go TestLicenseHeaders wants "Tailscale Inc & contributors"; the new test file had the older "Tailscale Inc & AUTHORS" wording and failed the licenses check. Change-Id: I3217a0795d1c23aba5eb01b3b62ee825efa0f137 Signed-off-by: leoca <[email protected]> * k8s-operator: count PortRanges.All() with an int An int counter cannot wrap at 65535 and an inverted range still yields nothing, so the explicit guard and the break on end are not needed. Change-Id: Ia3de3c0e1336f821a6c99af4e23f221024582e53 Signed-off-by: leoca <[email protected]> --------- Signed-off-by: leoca <[email protected]>
This commit is contained in:
1 parent
2e8f381c2e
commit
519a9f502c
2 files changed
+101
-2
No files matched your search
@@ -133,8 +133,8 @@ func (prs PortRanges) All() iter.Seq[uint16] {
|
||||
end = pr.Port
|
||||
}
|
||||
|
||||
for port := pr.Port; port <= end; port++ {
|
||||
if !yield(port) {
|
||||
for port := int(pr.Port); port <= int(end); port++ {
|
||||
if !yield(uint16(port)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package v1alpha1
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPortRangesAll(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
prs PortRanges
|
||||
want []uint16
|
||||
}{
|
||||
{
|
||||
name: "single_port",
|
||||
prs: PortRanges{{Port: 30000}},
|
||||
want: []uint16{30000},
|
||||
},
|
||||
{
|
||||
name: "small_range",
|
||||
prs: PortRanges{{Port: 30000, EndPort: 30002}},
|
||||
want: []uint16{30000, 30001, 30002},
|
||||
},
|
||||
{
|
||||
name: "range_ending_at_max_port",
|
||||
prs: PortRanges{{Port: 65533, EndPort: 65535}},
|
||||
want: []uint16{65533, 65534, 65535},
|
||||
},
|
||||
{
|
||||
name: "single_max_port",
|
||||
prs: PortRanges{{Port: 65535}},
|
||||
want: []uint16{65535},
|
||||
},
|
||||
{
|
||||
name: "multiple_ranges",
|
||||
prs: PortRanges{{Port: 30000, EndPort: 30001}, {Port: 65535}},
|
||||
want: []uint16{30000, 30001, 65535},
|
||||
},
|
||||
{
|
||||
name: "inverted_range_yields_nothing",
|
||||
prs: PortRanges{{Port: 30100, EndPort: 30000}},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var got []uint16
|
||||
for p := range tt.prs.All() {
|
||||
got = append(got, p)
|
||||
if len(got) > len(tt.want) {
|
||||
t.Fatalf("All() yielded more than the %d expected ports; got %v...", len(tt.want), got)
|
||||
}
|
||||
}
|
||||
if !slices.Equal(got, tt.want) {
|
||||
t.Errorf("All() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPortRangesClashesWith(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
prs PortRanges
|
||||
pr PortRange
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "overlapping",
|
||||
prs: PortRanges{{Port: 30000, EndPort: 30100}},
|
||||
pr: PortRange{Port: 30050, EndPort: 30150},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "disjoint",
|
||||
prs: PortRanges{{Port: 30000, EndPort: 30100}},
|
||||
pr: PortRange{Port: 30101, EndPort: 30200},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "disjoint_below_range_ending_at_max_port",
|
||||
prs: PortRanges{{Port: 40000, EndPort: 65535}},
|
||||
pr: PortRange{Port: 30000, EndPort: 30100},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.prs.ClashesWith(tt.pr); got != tt.want {
|
||||
t.Errorf("ClashesWith(%s) = %v, want %v", tt.pr, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user