From ec7e60d1f6447e30eec1a570b6f0696abc66c699 Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Wed, 15 May 2024 14:20:06 +0200 Subject: [PATCH] Add Icinga states to replica set --- library/Kubernetes/Model/ReplicaSet.php | 12 ++++++++ library/Kubernetes/Web/ReplicaSetDetail.php | 20 ++++++++----- library/Kubernetes/Web/ReplicaSetListItem.php | 30 ++++++++++--------- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/library/Kubernetes/Model/ReplicaSet.php b/library/Kubernetes/Model/ReplicaSet.php index c4f0b9b..28cf8b0 100644 --- a/library/Kubernetes/Model/ReplicaSet.php +++ b/library/Kubernetes/Model/ReplicaSet.php @@ -15,6 +15,14 @@ class ReplicaSet extends Model { use Translation; + public const STATE_OK = "ok"; + + public const STATE_WARNING = "warning"; + + public const STATE_CRITICAL = "critical"; + + public const STATE_UNKNOWN = "unknown"; + public function createBehaviors(Behaviors $behaviors) { $behaviors->add(new Binary([ @@ -64,6 +72,8 @@ public function getColumnDefinitions() 'fully_labeled_replicas' => $this->translate('Fully Labeled Replicas'), 'ready_replicas' => $this->translate('Ready Replicas'), 'available_replicas' => $this->translate('Available Replicas'), + 'icinga_state' => $this->translate('Icinga State'), + 'icinga_state_reason' => $this->translate('Icinga State Reason'), 'created' => $this->translate('Created At') ]; } @@ -81,6 +91,8 @@ public function getColumns() 'fully_labeled_replicas', 'ready_replicas', 'available_replicas', + 'icinga_state', + 'icinga_state_reason', 'created' ]; } diff --git a/library/Kubernetes/Web/ReplicaSetDetail.php b/library/Kubernetes/Web/ReplicaSetDetail.php index de84a9d..f88d6a5 100644 --- a/library/Kubernetes/Web/ReplicaSetDetail.php +++ b/library/Kubernetes/Web/ReplicaSetDetail.php @@ -38,7 +38,9 @@ protected function assemble() $this->translate('Actual Replicas') => $this->replicaSet->actual_replicas, $this->translate('Fully Labeled Replicas') => $this->replicaSet->fully_labeled_replicas, $this->translate('Ready Replicas') => $this->replicaSet->ready_replicas, - $this->translate('Available Replicas') => $this->replicaSet->available_replicas + $this->translate('Available Replicas') => $this->replicaSet->available_replicas, + $this->translate('Icinga State') => $this->replicaSet->icinga_state, + $this->translate('Icinga State Reason') => $this->replicaSet->icinga_state_reason ])), new Labels($this->replicaSet->label), new ConditionTable($this->replicaSet, (new ReplicaSetCondition())->getColumnDefinitions()), @@ -52,12 +54,16 @@ protected function assemble() 'section', null, new HtmlElement('h2', null, new Text($this->translate('Events'))), - new EventList(Event::on(Database::connection()) - ->filter(Filter::all( - Filter::equal('reference_kind', 'ReplicaSet'), - Filter::equal('reference_namespace', $this->replicaSet->namespace), - Filter::equal('reference_name', $this->replicaSet->name) - ))) + new EventList( + Event::on(Database::connection()) + ->filter( + Filter::all( + Filter::equal('reference_kind', 'ReplicaSet'), + Filter::equal('reference_namespace', $this->replicaSet->namespace), + Filter::equal('reference_name', $this->replicaSet->name) + ) + ) + ) ) ); } diff --git a/library/Kubernetes/Web/ReplicaSetListItem.php b/library/Kubernetes/Web/ReplicaSetListItem.php index 7cec62a..b27f08d 100644 --- a/library/Kubernetes/Web/ReplicaSetListItem.php +++ b/library/Kubernetes/Web/ReplicaSetListItem.php @@ -7,10 +7,12 @@ use Icinga\Module\Kubernetes\Common\BaseListItem; use Icinga\Module\Kubernetes\Common\Health; use Icinga\Module\Kubernetes\Common\Links; +use Icinga\Module\Kubernetes\Model\ReplicaSet; use ipl\Html\Attributes; use ipl\Html\BaseHtmlElement; use ipl\Html\Html; use ipl\Html\HtmlElement; +use ipl\Html\HtmlString; use ipl\I18n\Translation; use ipl\Web\Widget\Icon; use ipl\Web\Widget\Link; @@ -61,10 +63,11 @@ protected function assembleMain(BaseHtmlElement $main): void protected function assembleTitle(BaseHtmlElement $title): void { + [, $healthState] = $this->getState(); $content = Html::sprintf( $this->translate('%s is %s', ' is '), new Link($this->item->name, Links::replicaSet($this->item), ['class' => 'subject']), - Html::tag('span', null, $this->getHealth()) + Html::tag('span', null, $healthState) ); $title->addHtml($content); @@ -72,23 +75,22 @@ protected function assembleTitle(BaseHtmlElement $title): void protected function assembleVisual(BaseHtmlElement $visual): void { - $health = $this->getHealth(); - $visual->addHtml(new Icon(Health::icon($health), ['class' => ['health-' . $health]])); + [$icingaState,] = $this->getState(); + $stateBall = new StateBall($icingaState, StateBall::SIZE_MEDIUM); + $visual->addHtml($stateBall); } - protected function getHealth(): string + protected function getState(): array { - if ($this->item->desired_replicas < 1) { - return Health::UNDECIDABLE; - } - - switch (true) { - case $this->item->available_replicas < 1: - return Health::UNHEALTHY; - case $this->item->available_replicas < $this->item->desired_replicas: - return Health::DEGRADED; + switch ($this->item->icinga_state) { + case ReplicaSet::STATE_WARNING: + return ['warning', Health::DEGRADED]; + case ReplicaSet::STATE_CRITICAL: + return ['critical', Health::UNHEALTHY]; + case ReplicaSet::STATE_UNKNOWN: + return ['unknown', Health::UNDECIDABLE]; default: - return Health::HEALTHY; + return ['ok', Health::HEALTHY]; } } }