Skip to content

Commit

Permalink
Add multi cluster support
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Dec 18, 2024
1 parent e019d98 commit 93f5449
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 39 deletions.
44 changes: 24 additions & 20 deletions application/controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ipl\Sql\Connection;
use ipl\Stdlib\Filter;
use LogicException;
use Ramsey\Uuid\Uuid;
use Throwable;

class ConfigController extends Controller
Expand Down Expand Up @@ -187,33 +188,30 @@ function (NotificationsConfigForm $form) use ($kconfig, $sourceForm) {

public function prometheusAction()
{
$db = Database::connection();
$dbConfig = KConfig::on($db)->filter(
Filter::any(
Filter::equal('key', self::PROMETHEUS_URL),
Filter::equal('key', self::PROMETHEUS_USERNAME),
Filter::equal('key', self::PROMETHEUS_PASSWORD)
)
);

$data = [];

foreach ($dbConfig as $pair) {
$data[str_replace('.', '_', $pair->key)] = $pair->value;
}

$form = (new PrometheusConfigForm())
->populate($data)
->on(PrometheusConfigForm::ON_SUCCESS, function ($form) use ($db, $dbConfig) {
if ($form->isLocked()) {
->on(PrometheusConfigForm::ON_SUCCESS, function (PrometheusConfigForm $form) {
$clusterUuid = $form->getValue('cluster_uuid');
if ($form->isLocked($clusterUuid)) {
Notification::error($this->translate('Prometheus configuration is locked'));
return;
}

try {
$db = Database::connection();
$db->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
$db->beginTransaction();

$dbConfig = KConfig::on($db)->filter(
Filter::all(
Filter::equal('cluster_uuid', $clusterUuid),
Filter::any(
Filter::equal('key', self::PROMETHEUS_URL),
Filter::equal('key', self::PROMETHEUS_USERNAME),
Filter::equal('key', self::PROMETHEUS_PASSWORD)
)
)
);

$data = [];

foreach ($dbConfig as $pair) {
Expand All @@ -223,13 +221,15 @@ public function prometheusAction()
if (isset($data[self::PROMETHEUS_URL]) && $data[self::PROMETHEUS_URL]['locked'] !== 'y') {
$db->update(
'config',
['cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes()],
['value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_URL))],
[$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_URL]
);
} elseif (! isset($data[self::PROMETHEUS_URL])) {
$db->insert(
'config',
[
'cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') => self::PROMETHEUS_URL,
'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_URL))
]
Expand All @@ -239,13 +239,15 @@ public function prometheusAction()
if (isset($data[self::PROMETHEUS_USERNAME]) && $data[self::PROMETHEUS_USERNAME]['locked'] !== 'y') {
$db->update(
'config',
['cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes()],
['value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_USERNAME))],
[$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_USERNAME]
);
} elseif (! isset($data[self::PROMETHEUS_USERNAME])) {
$db->insert(
'config',
[
'cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') => self::PROMETHEUS_USERNAME,
'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_USERNAME))
]
Expand All @@ -255,23 +257,25 @@ public function prometheusAction()
if (isset($data[self::PROMETHEUS_PASSWORD]) && $data[self::PROMETHEUS_PASSWORD]['locked'] !== 'y') {
$db->update(
'config',
['cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes()],
['value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_PASSWORD))],
[$db->quoteIdentifier('key') . ' = ?' => self::PROMETHEUS_PASSWORD]
);
} elseif (! isset($data[self::PROMETHEUS_PASSWORD])) {
$db->insert(
'config',
[
'cluster_uuid' => Uuid::fromString($clusterUuid)->getBytes(),
$db->quoteIdentifier('key') => self::PROMETHEUS_PASSWORD,
'value' => $form->getValue($this->fieldForForm(self::PROMETHEUS_PASSWORD))
]
);
}

$db->commitTransaction();
} catch (Exception $_) {
} catch (Exception $e) {
$db->rollBackTransaction();
Notification::error($this->translate('Failed to store new configuration'));
Notification::error($this->translate('Failed to store new configuration') . ': ' . $e->getMessage());
return;
}

Expand Down
76 changes: 61 additions & 15 deletions application/forms/PrometheusConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,60 @@

use Icinga\Module\Kubernetes\Common\Database;
use Icinga\Module\Kubernetes\Controllers\ConfigController;
use Icinga\Module\Kubernetes\Model\Cluster;
use Icinga\Module\Kubernetes\Model\Config;
use Icinga\Module\Kubernetes\Web\ClusterForm;
use ipl\Html\Attributes;
use ipl\Html\Html;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatForm;
use Ramsey\Uuid\Uuid;

class PrometheusConfigForm extends CompatForm
{
protected function assemble(): void
{
if ($this->isLocked()) {
$clusterUuid = $this->getPopulatedValue('cluster_uuid')
?? (string) Uuid::fromBytes(Cluster::on(
Database::connection())->orderBy('uuid')->first()->uuid
);

$this->addElement('hidden', 'old_cluster_uuid', ['value' => $clusterUuid]);

if ($clusterUuid !== $this->getPopulatedValue('old_cluster_uuid', $clusterUuid)) {
$this->clearPopulatedValue('prometheus_url');
}

$dbConfig = Config::on(Database::connection())->filter(
Filter::all(
Filter::equal('cluster_uuid', Uuid::fromString($clusterUuid)->getBytes()),
Filter::any(
Filter::equal('key', ConfigController::PROMETHEUS_URL),
Filter::equal('key', ConfigController::PROMETHEUS_USERNAME),
Filter::equal('key', ConfigController::PROMETHEUS_PASSWORD)
)
)
);

$data = [];

foreach ($dbConfig as $pair) {
$data[$pair->key] = ['value' => $pair->value, 'locked' => $pair->locked];
}

$this->addElement(
'select',
'cluster_uuid',
[
'required' => true,
'class' => 'autosubmit',
'label' => $this->translate('Cluster'),
'options' => iterator_to_array(ClusterForm::yieldClusters()),
'value' => $clusterUuid,
],
);

if ($this->isLocked($clusterUuid)) {
$this->addHtml(
Html::tag('div', Attributes::create(['class' => 'control-group']), [
Html::tag(
Expand All @@ -38,7 +81,8 @@ protected function assemble(): void
[
'label' => $this->translate('URL'),
'required' => true,
'disabled' => $this->isLocked(),
'value' => $data[ConfigController::PROMETHEUS_URL]['value'] ?? '',
'disabled' => $this->isLocked($clusterUuid),
]
);

Expand All @@ -47,7 +91,8 @@ protected function assemble(): void
'prometheus_username',
[
'label' => $this->translate('Username'),
'disabled' => $this->isLocked(),
'value' => $data[ConfigController::PROMETHEUS_USERNAME]['value'] ?? '',
'disabled' => $this->isLocked($clusterUuid),
]
);

Expand All @@ -56,7 +101,8 @@ protected function assemble(): void
'prometheus_password',
[
'label' => $this->translate('Password'),
'disabled' => $this->isLocked(),
'value' => $data[ConfigController::PROMETHEUS_PASSWORD]['value'] ?? '',
'disabled' => $this->isLocked($clusterUuid),
]
);

Expand All @@ -65,22 +111,22 @@ protected function assemble(): void
'submit',
[
'label' => $this->translate('Save Changes'),
'disabled' => $this->isLocked()
'disabled' => $this->isLocked($clusterUuid),
]
);
}

public function isLocked(): bool
public function isLocked(string $clusterUuid): bool
{
$config = Config::on(Database::connection());
$config->filter(Filter::equal('key', ConfigController::PROMETHEUS_URL));

$temp = $config->first();

if (isset($config->first()->locked) && $config->first()->locked === 'y') {
return true;
}
$config = Config::on(Database::connection())
->filter(
Filter::all(
Filter::equal('cluster_uuid', Uuid::fromString($clusterUuid)->getBytes()),
Filter::equal('key', ConfigController::PROMETHEUS_URL),
)
)
->first();

return false;
return $config->locked ?? false;
}
}
5 changes: 3 additions & 2 deletions library/Kubernetes/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public function getTableName(): string
return 'config';
}

public function getKeyName(): string
public function getKeyName(): array
{
return 'key';
return ['cluster_uuid', 'key'];
}

public function getColumns(): array
{
return [
'cluster_uuid',
'key',
'value',
'locked'
Expand Down
4 changes: 2 additions & 2 deletions library/Kubernetes/Web/ClusterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ClusterForm extends CompatForm
{
public const ALL_CLUSTERS = '-';

protected function yieldClusters(): Generator
public static function yieldClusters(): Generator
{
$clusters = Cluster::on(Database::connection())
->columns(['uuid', 'name']);
Expand All @@ -37,7 +37,7 @@ protected function assemble(): void
'label' => $this->translate('Cluster'),
'options' => [
static::ALL_CLUSTERS => $this->translate('All clusters'),
] + iterator_to_array($this->yieldClusters()),
] + iterator_to_array(ClusterForm::yieldClusters()),
],
);
}
Expand Down

0 comments on commit 93f5449

Please sign in to comment.