From ec925c3d8dabfeb6569d4315cff4f081868c8c13 Mon Sep 17 00:00:00 2001 From: Johannes Rauh Date: Tue, 20 Aug 2024 13:54:39 +0200 Subject: [PATCH] Add config tab for Prometheus --- application/controllers/ConfigController.php | 41 ++++++++++++ application/forms/PrometheusConfigForm.php | 68 ++++++++++++++++++++ configuration.php | 9 +++ 3 files changed, 118 insertions(+) create mode 100644 application/forms/PrometheusConfigForm.php diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index 9309b543..e13a8818 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -11,6 +11,7 @@ use Icinga\Module\Kubernetes\Common\Database; use Icinga\Module\Kubernetes\Forms\DatabaseConfigForm; use Icinga\Module\Kubernetes\Forms\NotificationsConfigForm; +use Icinga\Module\Kubernetes\Forms\PrometheusConfigForm; use Icinga\Module\Kubernetes\Model\Config as KConfig; use Icinga\Module\Kubernetes\Web\Controller; use Icinga\Module\Notifications\Common\Database as NotificationsDatabase; @@ -177,6 +178,46 @@ function (NotificationsConfigForm $form) use ($kconfig, $sourceForm) { $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 * diff --git a/application/forms/PrometheusConfigForm.php b/application/forms/PrometheusConfigForm.php new file mode 100644 index 00000000..e68522ce --- /dev/null +++ b/application/forms/PrometheusConfigForm.php @@ -0,0 +1,68 @@ +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 (isset($config->first()->value) && $config->first()->value === 'true') { + return true; + } + + return false; + } +} diff --git a/configuration.php b/configuration.php index 5f2f4293..3ec9be74 100644 --- a/configuration.php +++ b/configuration.php @@ -189,6 +189,15 @@ ); } +$this->provideConfigTab( + 'prometheus', + [ + 'title' => $this->translate('Prometheus'), + 'label' => $this->translate('Prometheus'), + 'url' => 'config/prometheus' + ] +); + $this->providePermission( Auth::SHOW_CONFIG_MAPS, $this->translate('Allow to show config maps')