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

IcingaConfig: render host templates to all... #2804

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions doc/82-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This version hasn't been released yet

### Icinga Configuration
* FEATURE: render fallback template for IfW 1.11 for Icinga < 2.14 (#2776)
* FEATURE: render host templates to all non-global zones per default (#2410)
* FIX: render Set Services to individual zones where required (#1589, #2356)
* FIX: special characters like & and | caused trouble in filters (#2667)

Expand Down
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
8 changes: 7 additions & 1 deletion test/php/library/Director/Objects/IcingaHostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,16 @@ public function testRendersToTheCorrectZone()
$host->object_type = 'template';
$host->zone_id = null;

$zone = $this->newObject('zone', '___TEST___zone2');
$zone->store($db);

$config = new IcingaConfig($db);
$host->renderToConfig($config);
$this->assertEquals(
array('zones.d/director-global/host_templates.conf'),
[
'zones.d/___TEST___zone/host_templates.conf',
'zones.d/___TEST___zone2/host_templates.conf',
],
$config->getFileNames()
);
}
Expand Down