feat: resource limits

This commit is contained in:
Andras Bacsai 2023-05-17 11:59:48 +02:00
parent 70466bb9b5
commit 3b7456a561
6 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,51 @@
<?php
namespace App\Http\Livewire\Project\Application;
use App\Models\Application;
use Livewire\Component;
class ResourceLimits extends Component
{
public Application $application;
protected $rules = [
'application.limits_memory' => '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);
}
}
}

View File

@ -309,6 +309,14 @@ private function generate_docker_compose()
'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' => [

View File

@ -43,6 +43,7 @@ protected static function booted()
public $casts = [
'previews' => SchemalessAttributes::class,
'limits_memory_oom_kill' => 'boolean',
];
public function scopeWithExtraAttributes(): Builder
{

View File

@ -55,6 +55,16 @@ public function up(): void
$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');

View File

@ -0,0 +1,20 @@
<div>
<h2>Resource Limits</h2>
<form wire:submit.prevent='submit'>
<h3>Memory</h3>
<x-inputs.input placeholder="69b or 420k or 1337m or 1g" label="Limit" id="application.limits_memory" />
<x-inputs.input placeholder="69b or 420k or 1337m or 1g" label="Swap" id="application.limits_memory_swap" />
<x-inputs.input placeholder="0-100" type="number" min="0" max="100" label="Swappiness"
id="application.limits_memory_swappiness" />
<x-inputs.input placeholder="69b or 420k or 1337m or 1g" label="Soft Limit"
id="application.limits_memory_reservation" />
<x-inputs.input type="checkbox" label="Is OOM Kill disabled?" id="application.limits_memory_oom_kill" />
<h3>CPU</h3>
<x-inputs.input placeholder="1.5" label="Number of CPUs" id="application.limits_cpus" />
<x-inputs.input placeholder="0-2" label="CPU set to use" id="application.limits_cpuset" />
<x-inputs.input placeholder="1024" label="CPU Weight" id="application.limits_cpu_shares" />
<div class="pt-4">
<x-inputs.button>Save</x-inputs.button>
</div>
</form>
</div>

View File

@ -21,6 +21,10 @@
<a :class="activeTab === 'revert' && 'text-white'"
@click.prevent="activeTab = 'revert'; window.location.hash = 'revert'" href="#">Revert
</a>
<a :class="activeTab === 'resource-limits' && 'text-white'"
@click.prevent="activeTab = 'resource-limits'; window.location.hash = 'resource-limits'"
href="#">Resource Limits
</a>
{{-- <a :class="activeTab === 'previews' && 'text-white'"
@click.prevent="activeTab = 'previews'; window.location.hash = 'previews'" href="#">Previews
</a> --}}
@ -44,6 +48,9 @@
<div x-cloak x-show="activeTab === 'revert'">
<livewire:project.application.revert :application="$application" />
</div>
<div x-cloak x-show="activeTab === 'resource-limits'">
<livewire:project.application.resource-limits :application="$application" />
</div>
{{-- <div x-cloak x-show="activeTab === 'previews'">
<livewire:project.application.previews :application="$application" />
</div> --}}