From 3b7456a5614fa313501c079c3ca7fd8d4adc5b83 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 17 May 2023 11:59:48 +0200 Subject: [PATCH] feat: resource limits --- .../Project/Application/ResourceLimits.php | 51 +++++++++++++++++++ app/Jobs/DeployApplicationJob.php | 8 +++ app/Models/Application.php | 1 + ...03_27_081716_create_applications_table.php | 10 ++++ .../application/resource-limits.blade.php | 20 ++++++++ .../application/configuration.blade.php | 7 +++ 6 files changed, 97 insertions(+) create mode 100644 app/Http/Livewire/Project/Application/ResourceLimits.php create mode 100644 resources/views/livewire/project/application/resource-limits.blade.php diff --git a/app/Http/Livewire/Project/Application/ResourceLimits.php b/app/Http/Livewire/Project/Application/ResourceLimits.php new file mode 100644 index 000000000..d3f1096f8 --- /dev/null +++ b/app/Http/Livewire/Project/Application/ResourceLimits.php @@ -0,0 +1,51 @@ + 'required|string', + 'application.limits_memory_swap' => 'required|string', + 'application.limits_memory_swappiness' => 'required|integer|min:0|max:100', + 'application.limits_memory_reservation' => 'required|string', + 'application.limits_memory_oom_kill' => 'boolean', + 'application.limits_cpus' => 'nullable', + 'application.limits_cpuset' => 'nullable', + 'application.limits_cpu_shares' => 'nullable', + ]; + public function submit() + { + try { + if (!$this->application->limits_memory) { + $this->application->limits_memory = "0"; + } + if (!$this->application->limits_memory_swap) { + $this->application->limits_memory_swap = "0"; + } + if (!$this->application->limits_memory_swappiness) { + $this->application->limits_memory_swappiness = "60"; + } + if (!$this->application->limits_memory_reservation) { + $this->application->limits_memory_reservation = "0"; + } + if (!$this->application->limits_cpus) { + $this->application->limits_cpus = "0"; + } + if (!$this->application->limits_cpuset) { + $this->application->limits_cpuset = "0"; + } + if (!$this->application->limits_cpu_shares) { + $this->application->limits_cpu_shares = 1024; + } + $this->validate(); + $this->application->save(); + } catch (\Exception $e) { + return generalErrorHandler($e, $this); + } + } +} diff --git a/app/Jobs/DeployApplicationJob.php b/app/Jobs/DeployApplicationJob.php index 1f949a59a..e90d627d4 100644 --- a/app/Jobs/DeployApplicationJob.php +++ b/app/Jobs/DeployApplicationJob.php @@ -309,6 +309,14 @@ COPY --from={$this->application->uuid}:{$this->git_commit}-build /app/{$this->ap 'retries' => $this->application->health_check_retries, 'start_period' => $this->application->health_check_start_period . 's' ], + 'mem_limit' => $this->application->limits_memory, + 'memswap_limit' => $this->application->limits_memory_swap, + 'mem_swappiness' => $this->application->limits_memory_swappiness, + 'mem_reservation' => $this->application->limits_memory_reservation, + 'oom_kill_disable' => $this->application->limits_memory_oom_kill, + 'cpus' => $this->application->limits_cpus, + 'cpuset' => $this->application->limits_cpuset, + 'cpu_shares' => $this->application->limits_cpu_shares, ] ], 'networks' => [ diff --git a/app/Models/Application.php b/app/Models/Application.php index 57ef28501..6170b670c 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -43,6 +43,7 @@ class Application extends BaseModel public $casts = [ 'previews' => SchemalessAttributes::class, + 'limits_memory_oom_kill' => 'boolean', ]; public function scopeWithExtraAttributes(): Builder { diff --git a/database/migrations/2023_03_27_081716_create_applications_table.php b/database/migrations/2023_03_27_081716_create_applications_table.php index 500befe2c..9cabae3e1 100644 --- a/database/migrations/2023_03_27_081716_create_applications_table.php +++ b/database/migrations/2023_03_27_081716_create_applications_table.php @@ -55,6 +55,16 @@ return new class extends Migration $table->integer('health_check_retries')->default(10); $table->integer('health_check_start_period')->default(5); + $table->string('limits_memory')->default("0"); + $table->string('limits_memory_swap')->default("0"); + $table->integer('limits_memory_swappiness')->default(60); + $table->string('limits_memory_reservation')->default("0"); + $table->boolean('limits_memory_oom_kill')->default(false); + + $table->string('limits_cpus')->default("0"); + $table->string('limits_cpuset')->nullable()->default(""); + $table->integer('limits_cpu_shares')->default(1024); + $table->string('status')->default('exited'); $table->nullableMorphs('destination'); diff --git a/resources/views/livewire/project/application/resource-limits.blade.php b/resources/views/livewire/project/application/resource-limits.blade.php new file mode 100644 index 000000000..f0d9402f9 --- /dev/null +++ b/resources/views/livewire/project/application/resource-limits.blade.php @@ -0,0 +1,20 @@ +
+

Resource Limits

+
+

Memory

+ + + + + +

CPU

+ + + +
+ Save +
+ +
diff --git a/resources/views/project/application/configuration.blade.php b/resources/views/project/application/configuration.blade.php index bcececbed..f13216480 100644 --- a/resources/views/project/application/configuration.blade.php +++ b/resources/views/project/application/configuration.blade.php @@ -21,6 +21,10 @@ Revert + Resource Limits + {{-- Previews --}} @@ -44,6 +48,9 @@
+
+ +
{{--
--}}