Skip to content

Commit

Permalink
IcingaConfig: render host templates to all...
Browse files Browse the repository at this point in the history
...non-global zones, instead of the default global zone

refs #2410
  • Loading branch information
Thomas-Gelf committed Sep 21, 2023
1 parent 612165c commit b5486fc
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
12 changes: 12 additions & 0 deletions library/Director/IcingaConfig/IcingaConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class IcingaConfig

protected $zoneMap = array();

/** @var ?array Exists for caching reasons at rendering time */
protected $nonGlobalZones = null;

protected $lastActivityChecksum;

/** @var \Zend_Db_Adapter_Abstract */
Expand Down Expand Up @@ -349,6 +352,15 @@ public function getZoneName($id)
return $this->zoneMap[$id];
}

public function listNonGlobalZones(): array
{
if ($this->nonGlobalZones === null) {
$this->nonGlobalZones = array_values($this->connection->enumNonglobalZones());
}

return $this->nonGlobalZones;
}

/**
* @return self
*/
Expand Down
14 changes: 14 additions & 0 deletions library/Director/Objects/IcingaHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ public function getUniqueIdentifier()
}
}

protected function rendersConditionalTemplate(): bool
{
return $this->getRenderingZone() === self::ALL_NON_GLOBAL_ZONES;
}

protected function getDefaultZone(IcingaConfig $config = null)
{
if ($this->isTemplate()) {
return self::ALL_NON_GLOBAL_ZONES;
}

return parent::getDefaultZone();
}

public function beforeDelete()
{
foreach ($this->fetchServices() as $service) {
Expand Down
41 changes: 35 additions & 6 deletions library/Director/Objects/IcingaObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
const RESOLVE_ERROR = '(unable to resolve)';
const ALL_NON_GLOBAL_ZONES = '(all non-global zones)';

protected $keyName = 'object_name';

Expand Down Expand Up @@ -1800,9 +1801,21 @@ public function renderToConfig(IcingaConfig $config)
return;
}

$config->configFile(
'zones.d/' . $this->getRenderingZone($config) . '/' . $this->getRenderingFilename()
)->addObject($this);
foreach ($this->getRenderingZones($config) as $zone) {
$config->configFile(
'zones.d/' . $zone . '/' . $this->getRenderingFilename()
)->addObject($this);
}
}

protected function getRenderingZones(IcingaConfig $config): array
{
$zone = $this->getRenderingZone($config);
if ($zone === self::ALL_NON_GLOBAL_ZONES) {
return $config->listNonGlobalZones();
}

return [$zone];
}

public function getRenderingFilename()
Expand Down Expand Up @@ -2181,7 +2194,12 @@ protected function renderPropertyAsSeconds($key)

protected function renderSuffix()
{
return "}\n\n";
$prefix = '';
if ($this->rendersConditionalTemplate()) {
$prefix = '} ';
}

return "$prefix}\n\n";
}

protected function renderLegacySuffix()
Expand Down Expand Up @@ -2406,14 +2424,25 @@ protected function renderLegacyCustomExtensions()

protected function renderObjectHeader()
{
$prefix = '';
$renderedName = c::renderString($this->getObjectName());
if ($this->rendersConditionalTemplate()) {
$prefix = sprintf('if (! get_template(%s, %s)) { ', $this->getType(), $renderedName);
}
return sprintf(
"%s %s %s {\n",
"%s%s %s %s {\n",
$prefix,
$this->getObjectTypeName(),
$this->getType(),
c::renderString($this->getObjectName())
$renderedName
);
}

protected function rendersConditionalTemplate(): bool
{
return false;
}

public function getLegacyObjectType()
{
return strtolower($this->getType());
Expand Down
18 changes: 17 additions & 1 deletion library/Director/Objects/IcingaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ protected function getLegacyObjectKeyName()
}
}

protected function rendersConditionalTemplate(): bool
{
return $this->getRenderingZone() === self::ALL_NON_GLOBAL_ZONES;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -578,11 +583,22 @@ public function getOnDeleteUrl()

protected function getDefaultZone(IcingaConfig $config = null)
{
// Hint: this isn't possible yet, as we're unable to render dependent apply rules to multiple zones as well
// if ($this->isTemplate()) {
// return self::ALL_NON_GLOBAL_ZONES;
// }
if ($this->get('host_id') === null) {
return parent::getDefaultZone();
} else {
return $this->getRelatedObject('host', $this->get('host_id'))
$zone = $this->getRelatedObject('host', $this->get('host_id'))
->getRenderingZone($config);

// Hint: this avoids problems with host templates rendered to all non-global zones
if ($zone === self::ALL_NON_GLOBAL_ZONES) {
$zone = $this->connection->getDefaultGlobalZoneName();
}

return $zone;
}
}

Expand Down
7 changes: 4 additions & 3 deletions library/Director/Objects/IcingaServiceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,10 @@ public function renderToConfig(IcingaConfig $config)
}

$this->copyVarsToService($service);
$zone = $service->getRenderingZone($config);
$file = $this->getConfigFileWithHeader($config, $zone, $files);
$file->addObject($service);
foreach ($service->getRenderingZones($config) as $serviceZone) {
$file = $this->getConfigFileWithHeader($config, $serviceZone, $files);
$file->addObject($service);
}
}

if (empty($files)) {
Expand Down

0 comments on commit b5486fc

Please sign in to comment.