From 5e25ea4a43564efa239ecca73532dd6f39b3bb60 Mon Sep 17 00:00:00 2001 From: PalmarHealer Date: Mon, 17 Nov 2025 10:56:48 +0100 Subject: [PATCH] fix: use port range on free allocation lookup (#1882) Co-authored-by: MartinOscar <40749467+rmartinoscar@users.noreply.github.com> Co-authored-by: Boy132 --- .../FindAssignableAllocationService.php | 17 +++++++++-------- .../FindAssignableAllocationServiceTest.php | 3 +++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/Services/Allocations/FindAssignableAllocationService.php b/app/Services/Allocations/FindAssignableAllocationService.php index eb70bebcb..d242564be 100644 --- a/app/Services/Allocations/FindAssignableAllocationService.php +++ b/app/Services/Allocations/FindAssignableAllocationService.php @@ -40,6 +40,12 @@ class FindAssignableAllocationService // Attempt to find a given available allocation for a server. If one cannot be found // we will fall back to attempting to create a new allocation that can be used for the // server. + $start = config('panel.client_features.allocations.range_start', null); + $end = config('panel.client_features.allocations.range_end', null); + + Assert::integerish($start); + Assert::integerish($end); + // // Note: We use withoutGlobalScopes() to bypass Filament's tenant scoping when called // from the Server panel context, which would otherwise filter allocations to only @@ -50,11 +56,12 @@ class FindAssignableAllocationService ->when($server->allocation, function ($query) use ($server) { $query->where('ip', $server->allocation->ip); }) + ->whereBetween('port', [$start, $end]) ->whereNull('server_id') ->inRandomOrder() ->first(); - $allocation = $allocation ?? $this->createNewAllocation($server); + $allocation ??= $this->createNewAllocation($server, $start, $end); $allocation->update(['server_id' => $server->id]); @@ -72,18 +79,12 @@ class FindAssignableAllocationService * @throws PortOutOfRangeException * @throws TooManyPortsInRangeException */ - protected function createNewAllocation(Server $server): Allocation + protected function createNewAllocation(Server $server, ?int $start, ?int $end): Allocation { - $start = config('panel.client_features.allocations.range_start', null); - $end = config('panel.client_features.allocations.range_end', null); - if (!$start || !$end) { throw new NoAutoAllocationSpaceAvailableException(); } - Assert::integerish($start); - Assert::integerish($end); - // Get all the currently allocated ports for the node so that we can figure out // which port might be available. // Use withoutGlobalScopes() to bypass tenant filtering. diff --git a/tests/Integration/Services/Allocations/FindAssignableAllocationServiceTest.php b/tests/Integration/Services/Allocations/FindAssignableAllocationServiceTest.php index b1b3173e0..2a50ede78 100644 --- a/tests/Integration/Services/Allocations/FindAssignableAllocationServiceTest.php +++ b/tests/Integration/Services/Allocations/FindAssignableAllocationServiceTest.php @@ -29,10 +29,13 @@ class FindAssignableAllocationServiceTest extends IntegrationTestCase public function test_existing_allocation_is_preferred(): void { $server = $this->createServerModel(); + config()->set('panel.client_features.allocations.range_start', 5000); + config()->set('panel.client_features.allocations.range_end', 5005); $created = Allocation::factory()->create([ 'node_id' => $server->node_id, 'ip' => $server->allocation->ip, + 'port' => 5005, ]); $response = $this->getService()->handle($server);