-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement ephemeral port reservation for k3d
Prior to this commit we discovered that CI can flake when standing up multiple k3d clusters due to port conflicts. This commit attempts to implement "port reservation" such that pkg/k3d can always create k3d clusters without conflicts without coordination across different processes. It's unclear if this will actually work as engineering an intentional failure is not feasible.
- Loading branch information
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2024 Redpanda Data, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.md | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0 | ||
|
||
package k3d | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReservePort(t *testing.T) { | ||
const network = "tcp4" | ||
const iterations = 100 | ||
|
||
reserved := map[int]struct{}{} | ||
for i := 0; i < iterations; i++ { | ||
port, err := reserveEphemeralPort("tcp4") | ||
assert.NoError(t, err) | ||
assert.NotZero(t, port) | ||
|
||
// Assert that we can listen on the provided port. | ||
lis, err := net.Listen("tcp4", fmt.Sprintf("127.0.0.1:%d", port)) | ||
assert.NoError(t, err) | ||
assert.NoError(t, lis.Close()) | ||
|
||
reserved[port] = struct{}{} | ||
} | ||
|
||
// Not the best test as failures are exceptionally unlikely to be | ||
// reproducible. | ||
// Bind a bunch of ephemeral ports and assert that we don't get allocated | ||
// any of the ports we've reserved. | ||
for i := 0; i < iterations; i++ { | ||
lis, err := net.Listen(network, "127.0.0.1:0") | ||
assert.NoError(t, err) | ||
|
||
// Defer closing of this listener to ensure we always get new ports | ||
// from listening to 0. | ||
defer lis.Close() | ||
|
||
_, portStr, err := net.SplitHostPort(lis.Addr().String()) | ||
assert.NoError(t, err) | ||
|
||
port, err := strconv.Atoi(portStr) | ||
assert.NoError(t, err) | ||
|
||
t.Logf("%d", port) | ||
|
||
assert.NotContains(t, reserved, port) | ||
} | ||
} |