Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add some new labels to every container #4324

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions app/Actions/Database/StartClickhouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 1 addition & 5 deletions app/Actions/Database/StartDragonfly.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 1 addition & 5 deletions app/Actions/Database/StartKeydb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 1 addition & 5 deletions app/Actions/Database/StartMariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 1 addition & 5 deletions app/Actions/Database/StartMongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 1 addition & 5 deletions app/Actions/Database/StartMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 1 addition & 5 deletions app/Actions/Database/StartPostgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 1 addition & 5 deletions app/Actions/Database/StartRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ApplicationDeploymentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->application->environment->name, $this->pull_request_id))->toArray();

// Check for custom HEALTHCHECK
if ($this->application->build_pack === 'dockerfile' || $this->application->dockerfile) {
Expand Down
22 changes: 21 additions & 1 deletion bootstrap/helpers/docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,42 @@ 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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add a service name for a database?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with the others containers that have a coolify.serviceName label.

This way, we can rely solely on coolify.serviceName everywhere (for example, in our PromQL queries) to fetch metrics about a container. If we don’t add coolify.serviceName, we’ll be forced to write tedious conditions everywhere: checking for the presence of coolify.serviceName, and if it’s not available, falling back to coolify.resourceName.

Also that's why I said coolify.serviceName might not be the best name. Maybe we can find something better

$labels->push('coolify.projectName='.Str::slug($database->project()->name));
Julien-R44 marked this conversation as resolved.
Show resolved Hide resolved
$labels->push('coolify.environment='.Str::slug($database->environment->name));

return $labels;
}

function defaultLabels($id, $name, string $projectName, string $resourceName, string $environment, $pull_request_id = 0, string $type = 'application', $subType = null, $subId = null, $subName = null)
{
$labels = collect([]);
$labels->push('coolify.managed=true');
$labels->push('coolify.version='.config('version'));
$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.environment='.Str::slug($environment));

$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) {
Expand Down
42 changes: 38 additions & 4 deletions bootstrap/helpers/shared.php
Original file line number Diff line number Diff line change
Expand Up @@ -2059,7 +2059,17 @@ 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,
environment: $resource->environment->name,
);
$serviceLabels = $serviceLabels->merge($defaultLabels);
if (! $isDatabase && $fqdns->count() > 0) {
if ($fqdns) {
Expand Down Expand Up @@ -2887,7 +2897,16 @@ 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,
environment: $resource->environment->name,
pull_request_id: $pull_request_id,
type: 'application'
);
$serviceLabels = $serviceLabels->merge($defaultLabels);

if ($server->isLogDrainEnabled()) {
Expand Down Expand Up @@ -3670,19 +3689,34 @@ 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'
type: 'application',
environment: $resource->environment->name,
);
} elseif ($isService) {
if ($savedService->serviceType()) {
$fqdns = generateServiceSpecificFqdns($savedService);
} 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,
environment: $resource->environment->name,
);
}
// Add COOLIFY_FQDN & COOLIFY_URL to environment
if (! $isDatabase && $fqdns instanceof Collection && $fqdns->count() > 0) {
Expand Down