Skip to content

Commit 75b6cb4

Browse files
committed
fix looping logic for pfsense-dns-haproxy-ingress-proxy
1 parent 8a80575 commit 75b6cb4

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/KubernetesPfSenseController/Plugin/DNSHAProxyIngressProxy.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,35 @@ public function doAction()
116116
$haProxyConfig = HAProxyConfig::getInstalledPackagesConfigBlock($this->getController()->getRegistryItem('pfSenseClient'), 'haproxy');
117117

118118
$store = $this->getStore();
119+
if (!key_exists('managed_hosts', $store)) {
120+
$store['managed_hosts'] = [];
121+
}
119122
$ingressProxyPluginStore = $ingressProxyPlugin->getStore();
123+
$managedFrontends = $ingressProxyPluginStore['managed_frontends'];
124+
if (empty($managedFrontends)) {
125+
$managedFrontends = [];
126+
}
120127

121128
$managedHostsPreSave = [];
122129
$managedHosts = $store['managed_hosts'];
123130
if (empty($managedHosts)) {
124131
$managedHosts = [];
125132
}
126133
$hosts = [];
127-
foreach ($ingressProxyPluginStore['managed_frontends'] as $frontendName => $frontendDetails) {
134+
foreach ($managedFrontends as $frontendName => $frontendDetails) {
128135
$hosts[$frontendName] = [];
129136
$primaryFrontendName = $haProxyConfig->getFrontend($frontendName)['primary_frontend'];
130137
$hostName = $pluginConfig['frontends'][$primaryFrontendName]['hostname'];
138+
131139
$ingress = KubernetesUtils::getResourceByNamespaceName($this->state['ingresses'], $frontendDetails['resource']['namespace'], $frontendDetails['resource']['name']);
132140
if (!empty($ingress)) {
133141
foreach ($ingress['spec']['rules'] as $rule) {
134142
if ($this->shouldCreateAlias($rule['host'])) {
143+
if (empty($hostName)) {
144+
$this->log(('missing hostname in config for primary frontend: '.$primaryFrontendName.', ingress host: '.$rule['host']));
145+
continue;
146+
}
147+
135148
$hosts[$frontendName][] = $rule['host'];
136149
$managedHostsPreSave[$hostName][] = $rule['host'];
137150
}
@@ -141,35 +154,36 @@ public function doAction()
141154

142155
if ($dnsmasqEnabled) {
143156
$dnsmasqConfig = PfSenseConfigBlock::getRootConfigBlock($this->getController()->getRegistryItem('pfSenseClient'), 'dnsmasq');
157+
if (!key_exists('hosts', $dnsmasqConfig->data)) {
158+
$dnsmasqConfig->data['hosts'] = [];
159+
}
144160
}
145161

146162
if ($unboundEnabled) {
147163
$unboundConfig = PfSenseConfigBlock::getRootConfigBlock($this->getController()->getRegistryItem('pfSenseClient'), 'unbound');
164+
if (!key_exists('hosts', $unboundConfig->data)) {
165+
$unboundConfig->data['hosts'] = [];
166+
}
148167
}
149168

150-
foreach ($hosts as $frontendName => $value) {
151-
$host = null;
152-
$primaryFrontendName = $haProxyConfig->getFrontend($frontendName)['primary_frontend'];
153-
$hostName = $pluginConfig['frontends'][$primaryFrontendName]['hostname'];
169+
//foreach ($hosts as $frontendName => $value) {
170+
foreach ($managedHostsPreSave as $hostName => $aliases) {
154171
$itemId = [
155172
'host' => explode('.', $hostName, 2)[0],
156173
'domain' => explode('.', $hostName, 2)[1],
157174
];
158175
$itemKey = ['host', 'domain'];
159-
if (empty($hostName)) {
160-
$this->log(('missing hostname for primary frontend: '.$primaryFrontendName));
161-
continue;
162-
}
163176

164177
if ($dnsmasqEnabled) {
178+
$this->log('setting dnsmasq host aliases for host: '.$hostName.', alias count: '. count($aliases));
165179
$host = null;
166180
$host = Utils::getListItemMultiKey($dnsmasqConfig->data['hosts'], $itemId, $itemKey);
167181
if ($host !== null) {
168-
if (empty($hosts[$frontendName])) {
182+
if (empty($aliases)) {
169183
$host['aliases'] = '';
170184
} else {
171185
$host['aliases'] = [];
172-
foreach ($hosts[$frontendName] as $alias) {
186+
foreach ($aliases as $alias) {
173187
$host['aliases']['item'][] = [
174188
'host' => explode('.', $alias, 2)[0],
175189
'domain' => explode('.', $alias, 2)[1],
@@ -185,14 +199,15 @@ public function doAction()
185199
}
186200

187201
if ($unboundEnabled) {
202+
$this->log('setting unbound host aliases for host: '.$hostName.', alias count: '. count($aliases));
188203
$host = null;
189204
$host = Utils::getListItemMultiKey($unboundConfig->data['hosts'], $itemId, $itemKey);
190205
if ($host !== null) {
191-
if (empty($hosts[$frontendName])) {
206+
if (empty($aliases)) {
192207
$host['aliases'] = '';
193208
} else {
194209
$host['aliases'] = [];
195-
foreach ($hosts[$frontendName] as $alias) {
210+
foreach ($aliases as $alias) {
196211
$host['aliases']['item'][] = [
197212
'host' => explode('.', $alias, 2)[0],
198213
'domain' => explode('.', $alias, 2)[1],
@@ -210,13 +225,14 @@ public function doAction()
210225

211226
$toDeleteHosts = array_diff(@array_keys($managedHosts), @array_keys($managedHostsPreSave));
212227
foreach ($toDeleteHosts as $hostName) {
213-
$itemId = $itemId = [
228+
$itemId = [
214229
'host' => explode('.', $hostName, 2)[0],
215230
'domain' => explode('.', $hostName, 2)[1],
216231
];
217232
$itemKey = ['host', 'domain'];
218233

219234
if ($dnsmasqEnabled) {
235+
$this->log('removing dnsmasq host aliases for host: '.$hostName);
220236
$host = null;
221237
$host = Utils::getListItemMultiKey($dnsmasqConfig->data['hosts'], $itemId, $itemKey);
222238
if ($host !== null) {
@@ -226,6 +242,7 @@ public function doAction()
226242
}
227243

228244
if ($unboundEnabled) {
245+
$this->log('removing unbound host aliases for host: '.$hostName);
229246
$host = null;
230247
$host = Utils::getListItemMultiKey($unboundConfig->data['hosts'], $itemId, $itemKey);
231248
if ($host !== null) {

src/KubernetesPfSenseController/Plugin/Utils.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public static function getListItemMultiKey(&$list, $itemId, $itemKeyNames)
3939
{
4040
foreach ($list as $key => $item) {
4141
foreach ($itemKeyNames as $itemKeyName) {
42+
if (!key_exists($itemKeyName, $item)) {
43+
continue 2;
44+
}
4245
if ($itemId[$itemKeyName] != $item[$itemKeyName]) {
4346
continue 2;
4447
}

0 commit comments

Comments
 (0)