feat: able to generate only the required labels for resources

This commit is contained in:
Andras Bacsai
2024-08-07 18:27:50 +02:00
parent 92f90d4e52
commit 0b000919cf
5 changed files with 245 additions and 98 deletions

View File

@@ -20,6 +20,10 @@ class Proxy extends Component
protected $listeners = ['proxyStatusUpdated', 'saveConfiguration' => 'submit']; protected $listeners = ['proxyStatusUpdated', 'saveConfiguration' => 'submit'];
protected $rules = [
'server.settings.generate_exact_labels' => 'required|boolean',
];
public function mount() public function mount()
{ {
$this->selectedProxy = $this->server->proxyType(); $this->selectedProxy = $this->server->proxyType();
@@ -31,13 +35,13 @@ class Proxy extends Component
$this->dispatch('refresh')->self(); $this->dispatch('refresh')->self();
} }
public function change_proxy() public function changeProxy()
{ {
$this->server->proxy = null; $this->server->proxy = null;
$this->server->save(); $this->server->save();
} }
public function select_proxy($proxy_type) public function selectProxy($proxy_type)
{ {
$this->server->proxy->set('status', 'exited'); $this->server->proxy->set('status', 'exited');
$this->server->proxy->set('type', $proxy_type); $this->server->proxy->set('type', $proxy_type);
@@ -49,6 +53,17 @@ class Proxy extends Component
$this->dispatch('proxyStatusUpdated'); $this->dispatch('proxyStatusUpdated');
} }
public function instantSave()
{
try {
$this->validate();
$this->server->settings->save();
$this->dispatch('success', 'Settings saved.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function submit() public function submit()
{ {
try { try {

View File

@@ -540,16 +540,73 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
if ($pull_request_id === 0) { if ($pull_request_id === 0) {
if ($application->fqdn) { if ($application->fqdn) {
$domains = str(data_get($application, 'fqdn'))->explode(','); $domains = str(data_get($application, 'fqdn'))->explode(',');
$shouldGenerateLabelsExactly = $application->destination->server->settings->generate_exact_labels;
if ($shouldGenerateLabelsExactly) {
switch ($application->destination->server->proxyType()) {
case ProxyTypes::TRAEFIK->value:
$labels = $labels->merge(fqdnLabelsForTraefik(
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(),
redirect_direction: $application->redirect
));
break;
case ProxyTypes::CADDY->value:
$labels = $labels->merge(fqdnLabelsForCaddy(
network: $application->destination->network,
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(),
redirect_direction: $application->redirect
));
break;
}
} else {
$labels = $labels->merge(fqdnLabelsForTraefik(
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(),
redirect_direction: $application->redirect
));
$labels = $labels->merge(fqdnLabelsForCaddy(
network: $application->destination->network,
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(),
redirect_direction: $application->redirect
));
}
}
} else {
if (data_get($preview, 'fqdn')) {
$domains = str(data_get($preview, 'fqdn'))->explode(',');
} else {
$domains = collect([]);
}
$shouldGenerateLabelsExactly = $application->destination->server->settings->generate_exact_labels;
if ($shouldGenerateLabelsExactly) {
switch ($application->destination->server->proxyType()) { switch ($application->destination->server->proxyType()) {
case ProxyTypes::TRAEFIK_V2->value: case ProxyTypes::TRAEFIK->value:
$labels = $labels->merge(fqdnLabelsForTraefik( $labels = $labels->merge(fqdnLabelsForTraefik(
uuid: $appUuid, uuid: $appUuid,
domains: $domains, domains: $domains,
onlyPort: $onlyPort, onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(), is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(), is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(), is_stripprefix_enabled: $application->isStripprefixEnabled()
redirect_direction: $application->redirect
)); ));
break; break;
case ProxyTypes::CADDY->value: case ProxyTypes::CADDY->value:
@@ -560,41 +617,28 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
onlyPort: $onlyPort, onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(), is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(), is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(), is_stripprefix_enabled: $application->isStripprefixEnabled()
redirect_direction: $application->redirect
)); ));
break; break;
} }
}
} else {
if (data_get($preview, 'fqdn')) {
$domains = str(data_get($preview, 'fqdn'))->explode(',');
} else { } else {
$domains = collect([]); $labels = $labels->merge(fqdnLabelsForTraefik(
} uuid: $appUuid,
domains: $domains,
switch ($application->destination->server->proxyType()) { onlyPort: $onlyPort,
case ProxyTypes::TRAEFIK_V2->value: is_force_https_enabled: $application->isForceHttpsEnabled(),
$labels = $labels->merge(fqdnLabelsForTraefik( is_gzip_enabled: $application->isGzipEnabled(),
uuid: $appUuid, is_stripprefix_enabled: $application->isStripprefixEnabled()
domains: $domains, ));
onlyPort: $onlyPort, $labels = $labels->merge(fqdnLabelsForCaddy(
is_force_https_enabled: $application->isForceHttpsEnabled(), network: $application->destination->network,
is_gzip_enabled: $application->isGzipEnabled(), uuid: $appUuid,
is_stripprefix_enabled: $application->isStripprefixEnabled() domains: $domains,
)); onlyPort: $onlyPort,
break; is_force_https_enabled: $application->isForceHttpsEnabled(),
case ProxyTypes::CADDY->value: is_gzip_enabled: $application->isGzipEnabled(),
$labels = $labels->merge(fqdnLabelsForCaddy( is_stripprefix_enabled: $application->isStripprefixEnabled()
network: $application->destination->network, ));
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled()
));
break;
} }
} }

View File

@@ -1305,32 +1305,57 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$serviceLabels = $serviceLabels->merge($defaultLabels); $serviceLabels = $serviceLabels->merge($defaultLabels);
if (! $isDatabase && $fqdns->count() > 0) { if (! $isDatabase && $fqdns->count() > 0) {
if ($fqdns) { if ($fqdns) {
switch ($resource->destination->server->proxyType()) { $shouldGenerateLabelsExactly = $resource->server->settings->generate_exact_labels;
case ProxyTypes::TRAEFIK_V2->value: if ($shouldGenerateLabelsExactly) {
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik( switch ($resource->server->proxyType()) {
uuid: $resource->uuid, case ProxyTypes::TRAEFIK->value:
domains: $fqdns, $serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik(
is_force_https_enabled: true, uuid: $resource->uuid,
serviceLabels: $serviceLabels, domains: $fqdns,
is_gzip_enabled: $savedService->isGzipEnabled(), is_force_https_enabled: true,
is_stripprefix_enabled: $savedService->isStripprefixEnabled(), serviceLabels: $serviceLabels,
service_name: $serviceName, is_gzip_enabled: $savedService->isGzipEnabled(),
image: data_get($service, 'image') is_stripprefix_enabled: $savedService->isStripprefixEnabled(),
)); service_name: $serviceName,
break; image: data_get($service, 'image')
case ProxyTypes::CADDY->value: ));
$serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy( break;
network: $resource->destination->network, case ProxyTypes::CADDY->value:
uuid: $resource->uuid, $serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy(
domains: $fqdns, network: $resource->destination->network,
is_force_https_enabled: true, uuid: $resource->uuid,
serviceLabels: $serviceLabels, domains: $fqdns,
is_gzip_enabled: $savedService->isGzipEnabled(), is_force_https_enabled: true,
is_stripprefix_enabled: $savedService->isStripprefixEnabled(), serviceLabels: $serviceLabels,
service_name: $serviceName, is_gzip_enabled: $savedService->isGzipEnabled(),
image: data_get($service, 'image') is_stripprefix_enabled: $savedService->isStripprefixEnabled(),
)); service_name: $serviceName,
break; image: data_get($service, 'image')
));
break;
}
} else {
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik(
uuid: $resource->uuid,
domains: $fqdns,
is_force_https_enabled: true,
serviceLabels: $serviceLabels,
is_gzip_enabled: $savedService->isGzipEnabled(),
is_stripprefix_enabled: $savedService->isStripprefixEnabled(),
service_name: $serviceName,
image: data_get($service, 'image')
));
$serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy(
network: $resource->destination->network,
uuid: $resource->uuid,
domains: $fqdns,
is_force_https_enabled: true,
serviceLabels: $serviceLabels,
is_gzip_enabled: $savedService->isGzipEnabled(),
is_stripprefix_enabled: $savedService->isStripprefixEnabled(),
service_name: $serviceName,
image: data_get($service, 'image')
));
} }
} }
} }
@@ -2037,35 +2062,63 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
}); });
} }
} }
switch ($server->proxyType()) { $shouldGenerateLabelsExactly = $server->settings->generate_exact_labels;
case ProxyTypes::TRAEFIK_V2->value: if ($shouldGenerateLabelsExactly) {
$serviceLabels = $serviceLabels->merge( switch ($server->proxyType()) {
fqdnLabelsForTraefik( case ProxyTypes::TRAEFIK->value:
uuid: $resource->uuid, $serviceLabels = $serviceLabels->merge(
domains: $fqdns, fqdnLabelsForTraefik(
serviceLabels: $serviceLabels, uuid: $resource->uuid,
generate_unique_uuid: $resource->build_pack === 'dockercompose', domains: $fqdns,
image: data_get($service, 'image'), serviceLabels: $serviceLabels,
is_force_https_enabled: $resource->isForceHttpsEnabled(), generate_unique_uuid: $resource->build_pack === 'dockercompose',
is_gzip_enabled: $resource->isGzipEnabled(), image: data_get($service, 'image'),
is_stripprefix_enabled: $resource->isStripprefixEnabled(), is_force_https_enabled: $resource->isForceHttpsEnabled(),
) is_gzip_enabled: $resource->isGzipEnabled(),
); is_stripprefix_enabled: $resource->isStripprefixEnabled(),
break; )
case ProxyTypes::CADDY->value: );
$serviceLabels = $serviceLabels->merge( break;
fqdnLabelsForCaddy( case ProxyTypes::CADDY->value:
network: $resource->destination->network, $serviceLabels = $serviceLabels->merge(
uuid: $resource->uuid, fqdnLabelsForCaddy(
domains: $fqdns, network: $resource->destination->network,
serviceLabels: $serviceLabels, uuid: $resource->uuid,
image: data_get($service, 'image'), domains: $fqdns,
is_force_https_enabled: $resource->isForceHttpsEnabled(), serviceLabels: $serviceLabels,
is_gzip_enabled: $resource->isGzipEnabled(), image: data_get($service, 'image'),
is_stripprefix_enabled: $resource->isStripprefixEnabled(), is_force_https_enabled: $resource->isForceHttpsEnabled(),
) is_gzip_enabled: $resource->isGzipEnabled(),
); is_stripprefix_enabled: $resource->isStripprefixEnabled(),
break; )
);
break;
}
} else {
$serviceLabels = $serviceLabels->merge(
fqdnLabelsForTraefik(
uuid: $resource->uuid,
domains: $fqdns,
serviceLabels: $serviceLabels,
generate_unique_uuid: $resource->build_pack === 'dockercompose',
image: data_get($service, 'image'),
is_force_https_enabled: $resource->isForceHttpsEnabled(),
is_gzip_enabled: $resource->isGzipEnabled(),
is_stripprefix_enabled: $resource->isStripprefixEnabled(),
)
);
$serviceLabels = $serviceLabels->merge(
fqdnLabelsForCaddy(
network: $resource->destination->network,
uuid: $resource->uuid,
domains: $fqdns,
serviceLabels: $serviceLabels,
image: data_get($service, 'image'),
is_force_https_enabled: $resource->isForceHttpsEnabled(),
is_gzip_enabled: $resource->isGzipEnabled(),
is_stripprefix_enabled: $resource->isStripprefixEnabled(),
)
);
} }
} }
} }

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('server_settings', function (Blueprint $table) {
$table->boolean('generate_exact_labels')->default(false);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('server_settings', function (Blueprint $table) {
$table->dropColumn('generate_exact_labels');
});
}
};

View File

@@ -7,9 +7,9 @@
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h2>Configuration</h2> <h2>Configuration</h2>
@if ($server->proxy->status === 'exited' || $server->proxy->status === 'removing') @if ($server->proxy->status === 'exited' || $server->proxy->status === 'removing')
<x-forms.button wire:click.prevent="change_proxy">Switch Proxy</x-forms.button> <x-forms.button wire:click.prevent="changeProxy">Switch Proxy</x-forms.button>
@else @else
<x-forms.button disabled wire:click.prevent="change_proxy">Switch Proxy</x-forms.button> <x-forms.button disabled wire:click.prevent="changeProxy">Switch Proxy</x-forms.button>
@endif @endif
<x-forms.button type="submit">Save</x-forms.button> <x-forms.button type="submit">Save</x-forms.button>
@@ -21,6 +21,13 @@
</svg>Before switching proxies, please read <a class="underline dark:text-white" </svg>Before switching proxies, please read <a class="underline dark:text-white"
href="https://coolify.io/docs/knowledge-base/server/proxies#switch-between-proxies">this</a>. href="https://coolify.io/docs/knowledge-base/server/proxies#switch-between-proxies">this</a>.
</div> </div>
<h4>Advanced</h4>
<div class="pb-4 w-96">
<x-forms.checkbox
helper="If set, all resources will only have docker container labels for {{ str($server->proxyType())->title() }}.<br>For applications, labels needs to be regenerated manually. <br>Resources needs to be restarted."
id="server.settings.generate_exact_labels"
label="Generate labels only for {{ str($server->proxyType())->title() }}" instantSave />
</div>
@if ($server->proxyType() === ProxyTypes::TRAEFIK->value) @if ($server->proxyType() === ProxyTypes::TRAEFIK->value)
<h4>Traefik</h4> <h4>Traefik</h4>
@elseif ($server->proxyType() === 'CADDY') @elseif ($server->proxyType() === 'CADDY')
@@ -53,13 +60,13 @@
@elseif($selectedProxy === 'NONE') @elseif($selectedProxy === 'NONE')
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h2>Configuration</h2> <h2>Configuration</h2>
<x-forms.button wire:click.prevent="change_proxy">Switch Proxy</x-forms.button> <x-forms.button wire:click.prevent="changeProxy">Switch Proxy</x-forms.button>
</div> </div>
<div class="pt-2 pb-4">Custom (None) Proxy Selected</div> <div class="pt-2 pb-4">Custom (None) Proxy Selected</div>
@else @else
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h2>Configuration</h2> <h2>Configuration</h2>
<x-forms.button wire:click.prevent="change_proxy">Switch Proxy</x-forms.button> <x-forms.button wire:click.prevent="changeProxy">Switch Proxy</x-forms.button>
</div> </div>
@endif @endif
@else @else
@@ -67,13 +74,13 @@
<h2>Configuration</h2> <h2>Configuration</h2>
<div class="subtitle">Select a proxy you would like to use on this server.</div> <div class="subtitle">Select a proxy you would like to use on this server.</div>
<div class="grid gap-4"> <div class="grid gap-4">
<x-forms.button class="box" wire:click="select_proxy('NONE')"> <x-forms.button class="box" wire:click="selectProxy('NONE')">
Custom (None) Custom (None)
</x-forms.button> </x-forms.button>
<x-forms.button class="box" wire:click="select_proxy('TRAEFIK')"> <x-forms.button class="box" wire:click="selectProxy('TRAEFIK')">
Traefik Traefik
</x-forms.button> </x-forms.button>
<x-forms.button class="box" wire:click="select_proxy('CADDY')"> <x-forms.button class="box" wire:click="selectProxy('CADDY')">
Caddy Caddy
</x-forms.button> </x-forms.button>
<x-forms.button disabled class="box"> <x-forms.button disabled class="box">