From 8b30123add289c4e169692c79a36be5af611b352 Mon Sep 17 00:00:00 2001 From: Julien Date: Sun, 17 Nov 2024 22:49:44 +0100 Subject: [PATCH] feat: add service/resource/project labels --- app/Actions/Database/StartClickhouse.php | 6 +--- app/Actions/Database/StartDragonfly.php | 6 +--- app/Actions/Database/StartKeydb.php | 6 +--- app/Actions/Database/StartMariadb.php | 6 +--- app/Actions/Database/StartMongodb.php | 6 +--- app/Actions/Database/StartMysql.php | 6 +--- app/Actions/Database/StartPostgresql.php | 6 +--- app/Actions/Database/StartRedis.php | 6 +--- app/Jobs/ApplicationDeploymentJob.php | 2 +- bootstrap/helpers/docker.php | 19 ++++++++++++- bootstrap/helpers/shared.php | 35 ++++++++++++++++++++++-- 11 files changed, 59 insertions(+), 45 deletions(-) diff --git a/app/Actions/Database/StartClickhouse.php b/app/Actions/Database/StartClickhouse.php index 13667e8297..ca850d4366 100644 --- a/app/Actions/Database/StartClickhouse.php +++ b/app/Actions/Database/StartClickhouse.php @@ -49,11 +49,7 @@ public function handle(StandaloneClickhouse $database) 'hard' => 262144, ], ], - 'labels' => [ - 'coolify.managed' => 'true', - 'coolify.type' => 'database', - 'coolify.databaseId' => $this->database->id, - ], + 'labels' => defaultDatabaseLabels($this->database)->toArray(), 'healthcheck' => [ 'test' => "clickhouse-client --password {$this->database->clickhouse_admin_password} --query 'SELECT 1'", 'interval' => '5s', diff --git a/app/Actions/Database/StartDragonfly.php b/app/Actions/Database/StartDragonfly.php index c72714e1cd..a7ee6c9949 100644 --- a/app/Actions/Database/StartDragonfly.php +++ b/app/Actions/Database/StartDragonfly.php @@ -46,11 +46,7 @@ public function handle(StandaloneDragonfly $database) 'networks' => [ $this->database->destination->network, ], - 'labels' => [ - 'coolify.managed' => 'true', - 'coolify.type' => 'database', - 'coolify.databaseId' => $this->database->id, - ], + 'labels' => defaultDatabaseLabels($this->database)->toArray(), 'healthcheck' => [ 'test' => "redis-cli -a {$this->database->dragonfly_password} ping", 'interval' => '5s', diff --git a/app/Actions/Database/StartKeydb.php b/app/Actions/Database/StartKeydb.php index bd98258ab6..b41abaf5ea 100644 --- a/app/Actions/Database/StartKeydb.php +++ b/app/Actions/Database/StartKeydb.php @@ -48,11 +48,7 @@ public function handle(StandaloneKeydb $database) 'networks' => [ $this->database->destination->network, ], - 'labels' => [ - 'coolify.managed' => 'true', - 'coolify.type' => 'database', - 'coolify.databaseId' => $this->database->id, - ], + 'labels' => defaultDatabaseLabels($this->database)->toArray(), 'healthcheck' => [ 'test' => "keydb-cli --pass {$this->database->keydb_password} ping", 'interval' => '5s', diff --git a/app/Actions/Database/StartMariadb.php b/app/Actions/Database/StartMariadb.php index 696dd7ff4b..c47bf82cc5 100644 --- a/app/Actions/Database/StartMariadb.php +++ b/app/Actions/Database/StartMariadb.php @@ -43,11 +43,7 @@ public function handle(StandaloneMariadb $database) 'networks' => [ $this->database->destination->network, ], - 'labels' => [ - 'coolify.managed' => 'true', - 'coolify.type' => 'database', - 'coolify.databaseId' => $this->database->id, - ], + 'labels' => defaultDatabaseLabels($this->database)->toArray(), 'healthcheck' => [ 'test' => ['CMD', 'healthcheck.sh', '--connect', '--innodb_initialized'], 'interval' => '5s', diff --git a/app/Actions/Database/StartMongodb.php b/app/Actions/Database/StartMongodb.php index 26a0f82d0a..20b722a6e5 100644 --- a/app/Actions/Database/StartMongodb.php +++ b/app/Actions/Database/StartMongodb.php @@ -51,11 +51,7 @@ public function handle(StandaloneMongodb $database) 'networks' => [ $this->database->destination->network, ], - 'labels' => [ - 'coolify.managed' => 'true', - 'coolify.type' => 'database', - 'coolify.databaseId' => $this->database->id, - ], + 'labels' => defaultDatabaseLabels($this->database)->toArray(), 'healthcheck' => [ 'test' => [ 'CMD', diff --git a/app/Actions/Database/StartMysql.php b/app/Actions/Database/StartMysql.php index a3694648f9..33272b37e4 100644 --- a/app/Actions/Database/StartMysql.php +++ b/app/Actions/Database/StartMysql.php @@ -43,11 +43,7 @@ public function handle(StandaloneMysql $database) 'networks' => [ $this->database->destination->network, ], - 'labels' => [ - 'coolify.managed' => 'true', - 'coolify.type' => 'database', - 'coolify.databaseId' => $this->database->id, - ], + 'labels' => defaultDatabaseLabels($this->database)->toArray(), 'healthcheck' => [ 'test' => ['CMD', 'mysqladmin', 'ping', '-h', 'localhost', '-u', 'root', "-p{$this->database->mysql_root_password}"], 'interval' => '5s', diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.php index f5e85087ff..0cd35e20bc 100644 --- a/app/Actions/Database/StartPostgresql.php +++ b/app/Actions/Database/StartPostgresql.php @@ -47,11 +47,7 @@ public function handle(StandalonePostgresql $database) 'networks' => [ $this->database->destination->network, ], - 'labels' => [ - 'coolify.managed' => 'true', - 'coolify.type' => 'database', - 'coolify.databaseId' => $this->database->id, - ], + 'labels' => defaultDatabaseLabels($this->database)->toArray(), 'healthcheck' => [ 'test' => [ 'CMD-SHELL', diff --git a/app/Actions/Database/StartRedis.php b/app/Actions/Database/StartRedis.php index 7a2d2b34d5..6f12408471 100644 --- a/app/Actions/Database/StartRedis.php +++ b/app/Actions/Database/StartRedis.php @@ -48,11 +48,7 @@ public function handle(StandaloneRedis $database) 'networks' => [ $this->database->destination->network, ], - 'labels' => [ - 'coolify.managed' => 'true', - 'coolify.type' => 'database', - 'coolify.databaseId' => $this->database->id, - ], + 'labels' => defaultDatabaseLabels($this->database)->toArray(), 'healthcheck' => [ 'test' => [ 'CMD-SHELL', diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index cd89f55f37..7177b27f35 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -1683,7 +1683,7 @@ private function generate_compose_file() return escapeDollarSign($value); }); } - $labels = $labels->merge(defaultLabels($this->application->id, $this->application->uuid, $this->pull_request_id))->toArray(); + $labels = $labels->merge(defaultLabels($this->application->id, $this->application->uuid, $this->application->project()->name, $this->application->name, $this->pull_request_id))->toArray(); // Check for custom HEALTHCHECK if ($this->application->build_pack === 'dockerfile' || $this->application->dockerfile) { diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index da99c5cbdb..f80f7a69cc 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -188,7 +188,19 @@ function get_port_from_dockerfile($dockerfile): ?int return null; } -function defaultLabels($id, $name, $pull_request_id = 0, string $type = 'application', $subType = null, $subId = null) +function defaultDatabaseLabels($database) { + $labels = collect([]); + $labels->push('coolify.managed=true'); + $labels->push('coolify.type=database'); + $labels->push('coolify.databaseId='.$database->id); + $labels->push('coolify.resourceName='.Str::slug($database->name)); + $labels->push('coolify.serviceName='.Str::slug($database->name)); + $labels->push('coolify.projectName='.Str::slug($database->project()->name)); + + return $labels; +} + +function defaultLabels($id, $name, string $projectName, string $resourceName, $pull_request_id = 0, string $type = 'application', $subType = null, $subId = null, $subName = null) { $labels = collect([]); $labels->push('coolify.managed=true'); @@ -196,14 +208,19 @@ function defaultLabels($id, $name, $pull_request_id = 0, string $type = 'applica $labels->push('coolify.'.$type.'Id='.$id); $labels->push("coolify.type=$type"); $labels->push('coolify.name='.$name); + $labels->push('coolify.resourceName='.Str::slug($resourceName)); + $labels->push('coolify.projectName='.Str::slug($projectName)); + $labels->push('coolify.serviceName='.Str::slug($subName ?? $resourceName)); $labels->push('coolify.pullRequestId='.$pull_request_id); if ($type === 'service') { $subId && $labels->push('coolify.service.subId='.$subId); $subType && $labels->push('coolify.service.subType='.$subType); + $subName && $labels->push('coolify.service.subName='.Str::slug($subName)); } return $labels; } + function generateServiceSpecificFqdns(ServiceApplication|Application $resource) { if ($resource->getMorphClass() === \App\Models\ServiceApplication::class) { diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index dc49247892..fe01e046e9 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -2059,7 +2059,16 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal } else { $fqdns = collect(data_get($savedService, 'fqdns'))->filter(); } - $defaultLabels = defaultLabels($resource->id, $containerName, type: 'service', subType: $isDatabase ? 'database' : 'application', subId: $savedService->id); + $defaultLabels = defaultLabels( + id: $resource->id, + name: $containerName, + projectName: $resource->project()->name, + resourceName: $resource->name, + type: 'service', + subType: $isDatabase ? 'database' : 'application', + subId: $savedService->id, + subName: $savedService->name, + ); $serviceLabels = $serviceLabels->merge($defaultLabels); if (! $isDatabase && $fqdns->count() > 0) { if ($fqdns) { @@ -2887,7 +2896,15 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal } } } - $defaultLabels = defaultLabels($resource->id, $containerName, $pull_request_id, type: 'application'); + + $defaultLabels = defaultLabels( + id: $resource->id, + name: $containerName, + projectName: $resource->project()->name, + resourceName: $resource->name, + pull_request_id: $pull_request_id, + type: 'application' + ); $serviceLabels = $serviceLabels->merge($defaultLabels); if ($server->isLogDrainEnabled()) { @@ -3673,6 +3690,8 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int $defaultLabels = defaultLabels( id: $resource->id, name: $containerName, + projectName: $resource->project()->name, + resourceName: $resource->name, pull_request_id: $pullRequestId, type: 'application' ); @@ -3682,7 +3701,17 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int } else { $fqdns = collect(data_get($savedService, 'fqdns'))->filter(); } - $defaultLabels = defaultLabels($resource->id, $containerName, type: 'service', subType: $isDatabase ? 'database' : 'application', subId: $savedService->id); + + $defaultLabels = defaultLabels( + id: $resource->id, + name: $containerName, + projectName: $resource->project()->name, + resourceName: $resource->name, + type: 'service', + subType: $isDatabase ? 'database' : 'application', + subId: $savedService->id, + subName: $savedService->human_name ?? $savedService->name, + ); } // Add COOLIFY_FQDN & COOLIFY_URL to environment if (! $isDatabase && $fqdns instanceof Collection && $fqdns->count() > 0) {