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

Add icinga states to cron job and service #123

Open
wants to merge 37 commits into
base: favorites-new
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e95c61c
Fix detail view
jrauh01 Jan 15, 2025
5f56538
Fix referents in event detail view
jrauh01 Jan 15, 2025
dc64ad0
Remove redundant line
jrauh01 Jan 16, 2025
6866b0c
Add missing parameter and return type
jrauh01 Jan 16, 2025
6d7c948
Use match expressions instead of if-else
jrauh01 Jan 16, 2025
e4d309b
Use ->viewMode instead of ->getViewMode()
jrauh01 Jan 16, 2025
e70a710
Remove superfluous if statement
jrauh01 Jan 16, 2025
f279c66
Simplify namespace's caption and footer
jrauh01 Jan 16, 2025
215a04d
Use view mode constants
jrauh01 Jan 16, 2025
efbad1f
Remove tabular view mode from ViewModeSwitcher
jrauh01 Jan 16, 2025
4d4ee16
Add docs for addIgnoredViewModes()
jrauh01 Jan 16, 2025
c75631c
Hide view mode switcher
jrauh01 Jan 16, 2025
dfb8ee1
Format docs
jrauh01 Jan 16, 2025
dd2767a
Fix license headers
jrauh01 Jan 16, 2025
564dd27
Pass db via argument
jrauh01 Jan 21, 2025
64f7f1d
Make getIgnoredViewModes() not abstract anymore
jrauh01 Jan 22, 2025
46a481f
Add function to canonicalize resource kinds
jrauh01 Jan 22, 2025
e8691ed
Add default traits for list item parts
jrauh01 Jan 22, 2025
8e153ab
Adjust function names to canonicalized kind names
jrauh01 Jan 22, 2025
610011e
Use default traits for item list parts
jrauh01 Jan 22, 2025
8530c79
Add styles for list items
jrauh01 Feb 28, 2025
c700c0b
Use DetailState in resource details
jrauh01 Dec 18, 2024
2d6244e
Use own action list
jrauh01 Jan 2, 2025
0905b2e
Add model for favorites
jrauh01 Jan 2, 2025
cd0c34d
Add QuickActions
jrauh01 Feb 20, 2025
d3c0461
Add quick actions to toggle favorites
jrauh01 Feb 20, 2025
d49f0e3
Show favorite icon in action list
jrauh01 Feb 20, 2025
c7e8da0
Build data-icinga-detail-url out of names without dashes
jrauh01 Jan 7, 2025
7b2e11d
Remove multi select feature from action list
jrauh01 Feb 21, 2025
ef40158
Remove load more feature from action list
jrauh01 Feb 21, 2025
1dfb03d
Add priority column to favorites
jrauh01 Feb 24, 2025
b340f9b
Add toggle to show only favorites
jrauh01 Feb 27, 2025
4099219
Add reordering via drag & drop
jrauh01 Feb 27, 2025
593900d
Dashboard to show all favorites
jrauh01 Mar 3, 2025
32197b7
Add toggle to show only favorites
jrauh01 Feb 27, 2025
a63a8dd
Add icinga state for services
jrauh01 Mar 3, 2025
cdda684
Add icinga state for cron jobs
jrauh01 Mar 3, 2025
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
8 changes: 7 additions & 1 deletion application/controllers/ConfigmapsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Icinga\Module\Kubernetes\Model\ConfigMap;
use Icinga\Module\Kubernetes\Web\ConfigMapList;
use Icinga\Module\Kubernetes\Web\ListController;
use Icinga\Module\Kubernetes\Web\ViewModeSwitcher;
use ipl\Orm\Query;

class ConfigmapsController extends ListController
Expand Down Expand Up @@ -44,6 +45,11 @@ protected function getPermission(): string

protected function getIgnoredViewModes(): array
{
return ['common', 'detailed'];
return [ViewModeSwitcher::VIEW_MODE_COMMON, ViewModeSwitcher::VIEW_MODE_DETAILED];
}

protected function getFavorable(): bool
{
return false;
}
}
21 changes: 21 additions & 0 deletions application/controllers/CronjobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
use Icinga\Module\Kubernetes\Common\Auth;
use Icinga\Module\Kubernetes\Common\Database;
use Icinga\Module\Kubernetes\Model\CronJob;
use Icinga\Module\Kubernetes\Model\Favorite;
use Icinga\Module\Kubernetes\Web\Controller;
use Icinga\Module\Kubernetes\Web\CronJobDetail;
use Icinga\Module\Kubernetes\Web\CronJobList;
use Icinga\Module\Kubernetes\Web\QuickActions;
use Icinga\Module\Kubernetes\Web\ViewModeSwitcher;
use ipl\Stdlib\Filter;
use Ramsey\Uuid\Uuid;

Expand All @@ -28,10 +32,27 @@ public function indexAction(): void
->filter(Filter::equal('uuid', $uuidBytes))
->first();

$favorite = Favorite::on(Database::connection())
->filter(
Filter::all(
Filter::equal('resource_uuid', $uuidBytes),
Filter::equal('username', Auth::getInstance()->getUser()->getUsername())
)
)
->first();

if ($cronJob === null) {
$this->httpNotFound($this->translate('Cron Job not found'));
}

$this->addControl(
(new CronJobList([$cronJob]))
->setActionList(false)
->setViewMode(ViewModeSwitcher::VIEW_MODE_MINIMAL)
);

$this->addControl(new QuickActions($cronJob, $favorite));

$this->addContent(new CronJobDetail($cronJob));
}
}
4 changes: 2 additions & 2 deletions application/controllers/CronjobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ protected function getPermission(): string
return Auth::SHOW_CRON_JOBS;
}

protected function getIgnoredViewModes(): array
protected function getFavorable(): bool
{
return [];
return true;
}
}
20 changes: 19 additions & 1 deletion application/controllers/DaemonsetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use Icinga\Module\Kubernetes\Common\Auth;
use Icinga\Module\Kubernetes\Common\Database;
use Icinga\Module\Kubernetes\Model\DaemonSet;
use Icinga\Module\Kubernetes\Model\Favorite;
use Icinga\Module\Kubernetes\Web\Controller;
use Icinga\Module\Kubernetes\Web\DaemonSetDetail;
use Icinga\Module\Kubernetes\Web\DaemonSetList;
use Icinga\Module\Kubernetes\Web\QuickActions;
use Icinga\Module\Kubernetes\Web\ViewModeSwitcher;
use ipl\Stdlib\Filter;
use Ramsey\Uuid\Uuid;

Expand All @@ -29,11 +32,26 @@ public function indexAction(): void
->filter(Filter::equal('uuid', $uuidBytes))
->first();

$favorite = Favorite::on(Database::connection())
->filter(
Filter::all(
Filter::equal('resource_uuid', $uuidBytes),
Filter::equal('username', Auth::getInstance()->getUser()->getUsername())
)
)
->first();

if ($daemonSet === null) {
$this->httpNotFound($this->translate('Daemon Set not found'));
}

$this->addControl((new DaemonSetList([$daemonSet]))->setActionList(false));
$this->addControl(
(new DaemonSetList([$daemonSet]))
->setActionList(false)
->setViewMode(ViewModeSwitcher::VIEW_MODE_MINIMAL)
);

$this->addControl(new QuickActions($daemonSet, $favorite));

$this->addContent(new DaemonSetDetail($daemonSet));
}
Expand Down
4 changes: 2 additions & 2 deletions application/controllers/DaemonsetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ protected function getPermission(): string
return Auth::SHOW_DAEMON_SETS;
}

protected function getIgnoredViewModes(): array
protected function getFavorable(): bool
{
return [];
return true;
}
}
20 changes: 19 additions & 1 deletion application/controllers/DeploymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use Icinga\Module\Kubernetes\Common\Auth;
use Icinga\Module\Kubernetes\Common\Database;
use Icinga\Module\Kubernetes\Model\Deployment;
use Icinga\Module\Kubernetes\Model\Favorite;
use Icinga\Module\Kubernetes\Web\Controller;
use Icinga\Module\Kubernetes\Web\DeploymentDetail;
use Icinga\Module\Kubernetes\Web\DeploymentList;
use Icinga\Module\Kubernetes\Web\QuickActions;
use Icinga\Module\Kubernetes\Web\ViewModeSwitcher;
use ipl\Stdlib\Filter;
use Ramsey\Uuid\Uuid;

Expand All @@ -29,11 +32,26 @@ public function indexAction(): void
->filter(Filter::equal('uuid', $uuidBytes))
->first();

$favorite = Favorite::on(Database::connection())
->filter(
Filter::all(
Filter::equal('resource_uuid', $uuidBytes),
Filter::equal('username', Auth::getInstance()->getUser()->getUsername())
)
)
->first();

if ($deployment === null) {
$this->httpNotFound($this->translate('Deployment not found'));
}

$this->addControl((new DeploymentList([$deployment]))->setActionList(false));
$this->addControl(
(new DeploymentList([$deployment]))
->setActionList(false)
->setViewMode(ViewModeSwitcher::VIEW_MODE_MINIMAL)
);

$this->addControl(new QuickActions($deployment, $favorite));

$this->addContent(new DeploymentDetail($deployment));
}
Expand Down
4 changes: 2 additions & 2 deletions application/controllers/DeploymentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ protected function getPermission(): string
return Auth::SHOW_DEPLOYMENTS;
}

protected function getIgnoredViewModes(): array
protected function getFavorable(): bool
{
return [];
return true;
}
}
8 changes: 7 additions & 1 deletion application/controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Icinga\Module\Kubernetes\Model\Event;
use Icinga\Module\Kubernetes\Web\EventList;
use Icinga\Module\Kubernetes\Web\ListController;
use Icinga\Module\Kubernetes\Web\ViewModeSwitcher;
use ipl\Orm\Query;
use ipl\Stdlib\Filter;

Expand Down Expand Up @@ -54,6 +55,11 @@ protected function getPermission(): string

protected function getIgnoredViewModes(): array
{
return ['detailed'];
return [ViewModeSwitcher::VIEW_MODE_DETAILED];
}

protected function getFavorable(): bool
{
return false;
}
}
134 changes: 134 additions & 0 deletions application/controllers/FavoriteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/* Icinga for Kubernetes Web | (c) 2025 Icinga GmbH | AGPLv3 */

namespace Icinga\Module\Kubernetes\Controllers;

use Icinga\Application\Logger;
use Icinga\Module\Kubernetes\Common\Auth;
use Icinga\Module\Kubernetes\Common\Database;
use Icinga\Module\Kubernetes\Forms\FavorForm;
use Icinga\Module\Kubernetes\Forms\UnfavorForm;
use Icinga\Module\Kubernetes\Model\Favorite;
use Icinga\Module\Kubernetes\Web\Controller;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;
use Throwable;

class FavoriteController extends Controller
{
/**
* Favors a resource by adding it into the database. Throws an exception if the database operation fails.
*
* @return void
*/
public function favorAction(): void
{
(new FavorForm())
->on(FavorForm::ON_SUCCESS, function () {
$db = Database::connection();
$uuid = $this->params->get('uuid');
$kind = $this->params->get('kind');
$username = Auth::getInstance()->getUser()->getUsername();

try {
$highestPriorityFavorite = Favorite::on($db)
->columns('priority')
->filter(
Filter::all(
Filter::equal('kind', $kind),
Filter::equal('username', $username)
)
)
->orderBy('priority', SORT_DESC)
->first();

$db->insert(
'favorite',
[
'resource_uuid' => $uuid,
'kind' => $kind,
'username' => $username,
'priority' => ($highestPriorityFavorite?->priority ?? -1) + 1,
]
);
} catch (Throwable $e) {
Logger::error($e);
Logger::error($e->getTraceAsString());

throw $e;
}
})
->handleRequest($this->getServerRequest());

$this->closeModalAndRefreshRemainingViews('__REFRESH__');
}

/**
* Unfavors a resource by removing it from the database. Throws an exception if the database operation fails.
*
* @return void
*/
public function unfavorAction(): void
{
(new UnfavorForm())
->on(FavorForm::ON_SUCCESS, function () {
$db = Database::connection();
$uuid = $this->params->get('uuid');
$username = Auth::getInstance()->getUser()->getUsername();
try {
$transactionStarted = false;
if (! $db->inTransaction()) {
$transactionStarted = true;
$db->beginTransaction();
}

$favoriteToDelete = Favorite::on($db)
->filter(Filter::all(
Filter::equal('resource_uuid', $uuid),
Filter::equal('username', $username)
))
->first();

$db->delete(
'favorite',
[
'resource_uuid = ?' => $uuid,
'username = ?' => $username,
]
);

$affectedFavorites = Favorite::on($db)
->columns(['resource_uuid', 'username'])
->filter(
Filter::all(
Filter::equal('kind', $favoriteToDelete->kind),
Filter::equal('username', $username),
Filter::greaterThan('priority', $favoriteToDelete->priority)
)
)
->orderBy('priority', SORT_ASC);

foreach ($affectedFavorites as $favorite) {
$db->update(
'favorite',
['priority' => new Expression('priority - 1')],
['resource_uuid = ?' => $favorite->resource_uuid, 'username = ?' => $favorite->username]
);
}

if ($transactionStarted) {
$db->commitTransaction();
}
} catch (Throwable $e) {
Logger::error($e);
Logger::error($e->getTraceAsString());

throw $e;
}
})
->handleRequest($this->getServerRequest());

$this->closeModalAndRefreshRemainingViews('__REFRESH__');
}
}
59 changes: 59 additions & 0 deletions application/controllers/FavoritesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/* Icinga for Kubernetes Web | (c) 2025 Icinga GmbH | AGPLv3 */

namespace Icinga\Module\Kubernetes\Controllers;

use Icinga\Module\Kubernetes\Common\Auth;
use Icinga\Module\Kubernetes\Common\Database;
use Icinga\Module\Kubernetes\Model\Favorite;
use Icinga\Module\Kubernetes\Web\Controller;
use Icinga\Module\Kubernetes\Web\Factory;
use Icinga\Module\Kubernetes\Web\FavoriteDashboard;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Widget\Dashboard\Dashlet;
use Icinga\Web\Widget\Dashboard\Pane;
use ipl\Stdlib\Filter;

class FavoritesController extends Controller
{
const array FAVORABLE_KINDS = [
'cronjob',
'daemonset',
'deployment',
'ingress',
'job',
'namespace',
'node',
'persistentvolumeclaim',
'persistentvolume',
'pod',
'replicaset',
'service',
'statefulset'
];

public function indexAction(): void
{
$this->addTitleTab('Favorites');
$dashboard = new Dashboard();
$pane = (new Pane('favorites'))->setTitle('Favorites');
$dashboard->addPane($pane);

foreach (self::FAVORABLE_KINDS as $kind) {
$hasFavorites = Favorite::on(Database::connection())->filter(
Filter::all(
Filter::equal('kind', $kind),
Filter::equal('username', Auth::getInstance()->getUser()->getUsername())
)
)->first() !== null;
if ($hasFavorites) {
$dashlet = new Dashlet(Factory::createTitle($kind), Factory::createListUrl($kind) . '?view=minimal&show-favorites=y&sort=favorite.priority desc', $pane);
$pane->addDashlet($dashlet);
}
}

$dashboard->activate('favorites');
$this->addContent(new FavoriteDashboard($dashboard));
}
}
Loading