mirror of
https://github.com/pelican-dev/panel.git
synced 2025-12-08 17:30:18 +01:00
fix: use port range on free allocation lookup (#1882)
Co-authored-by: MartinOscar <40749467+rmartinoscar@users.noreply.github.com> Co-authored-by: Boy132 <Boy132@users.noreply.github.com>
This commit is contained in:
parent
886836c60a
commit
5e25ea4a43
@ -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.
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user