Skip to content

Commit

Permalink
Add config tab for Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Aug 22, 2024
1 parent 02dabe2 commit f1f5bc1
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
44 changes: 44 additions & 0 deletions application/controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

use Icinga\Application\Config;
use Icinga\Module\Kubernetes\Forms\DatabaseConfigForm;
use Icinga\Module\Kubernetes\Forms\PrometheusConfigForm;
use Icinga\Module\Kubernetes\Web\Controller;
use Icinga\Module\Kubernetes\Common\Database;
use Icinga\Module\Kubernetes\Model\Config as KConfig;
use Icinga\Web\Notification;
use Icinga\Web\Widget\Tab;
use Icinga\Web\Widget\Tabs;
use ipl\Stdlib\Filter;

class ConfigController extends Controller
{
Expand Down Expand Up @@ -37,6 +41,46 @@ public function databaseAction()
$this->addContent($form);
}

public function prometheusAction()
{
$db = Database::connection();
$dbConfig = KConfig::on($db)->filter(Filter::equal('key', 'prometheus.url'))->first();

// $config = Config::module('kubernetes');
$form = (new PrometheusConfigForm())
->populate(['prometheus_url' => $dbConfig->value])
->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $dbConfig) {
if ($form->isLocked()) {
Notification::error($this->translate('Prometheus configuration is locked'));
return;
}

// $config->setSection('prometheus', $form->getValues());
// $config->saveIni();

if ($dbConfig) {
$db->update('config',
['value' => $form->getValue('prometheus_url')],
[$db->quoteIdentifier('key') . ' = ?' => 'prometheus.url']
);
} else {
$db->insert('config',
[
$db->quoteIdentifier('key') => 'prometheus.url',
'value' => $form->getValue('prometheus_url')
]
);
}


Notification::success($this->translate('New configuration has successfully been stored'));
})->handleRequest($this->getServerRequest());

$this->mergeTabs($this->Module()->getConfigTabs()->activate('prometheus'));

$this->addContent($form);
}

/**
* Merge tabs with other tabs contained in this tab panel
*
Expand Down
68 changes: 68 additions & 0 deletions application/forms/PrometheusConfigForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

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

namespace Icinga\Module\Kubernetes\Forms;

use Icinga\Data\ResourceFactory;
use ipl\Html\Attributes;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Web\Compat\CompatForm;
use Icinga\Module\Kubernetes\Model\Config;
use ipl\Stdlib\Filter;
use Icinga\Module\Kubernetes\Common\Database;

class PrometheusConfigForm extends CompatForm
{
protected function assemble(): void
{
if ($this->isLocked()) {
$this->addHtml(
Html::tag('div', Attributes::create(['class' => 'control-group']), [
Html::tag(
'div',
Attributes::create(['class' => 'control-label-group']),
),
Html::tag(
'p',
Attributes::create(),
"Prometheus configuration is provided via YAML."
)
])
);
}

$this->addElement(
'text',
'prometheus_url',
[
'label' => $this->translate('URL'),
'required' => true,
'disabled' => $this->isLocked(),
'value' => ''
]
);

$this->addElement(
'submit',
'submit',
[
'label' => $this->translate('Save Changes'),
'disabled' => $this->isLocked()
]
);
}

public function isLocked(): bool
{
$config = Config::on(Database::connection());
$config->filter(Filter::equal('key', 'prometheus.locked'));

if ($config->first()->value === 'true') {
return true;
}

return false;
}
}
8 changes: 8 additions & 0 deletions configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@
]
);

$this->provideConfigTab(
'prometheus',
[
'title' => $this->translate('Prometheus'),
'label' => $this->translate('Prometheus'),
'url' => 'config/prometheus'
]
);

$this->provideJsFile('vendor/chart.umd.js');

Expand Down

0 comments on commit f1f5bc1

Please sign in to comment.