@@ -116,22 +116,35 @@ public function doAction()
116
116
$ haProxyConfig = HAProxyConfig::getInstalledPackagesConfigBlock ($ this ->getController ()->getRegistryItem ('pfSenseClient ' ), 'haproxy ' );
117
117
118
118
$ store = $ this ->getStore ();
119
+ if (!key_exists ('managed_hosts ' , $ store )) {
120
+ $ store ['managed_hosts ' ] = [];
121
+ }
119
122
$ ingressProxyPluginStore = $ ingressProxyPlugin ->getStore ();
123
+ $ managedFrontends = $ ingressProxyPluginStore ['managed_frontends ' ];
124
+ if (empty ($ managedFrontends )) {
125
+ $ managedFrontends = [];
126
+ }
120
127
121
128
$ managedHostsPreSave = [];
122
129
$ managedHosts = $ store ['managed_hosts ' ];
123
130
if (empty ($ managedHosts )) {
124
131
$ managedHosts = [];
125
132
}
126
133
$ hosts = [];
127
- foreach ($ ingressProxyPluginStore [ ' managed_frontends ' ] as $ frontendName => $ frontendDetails ) {
134
+ foreach ($ managedFrontends as $ frontendName => $ frontendDetails ) {
128
135
$ hosts [$ frontendName ] = [];
129
136
$ primaryFrontendName = $ haProxyConfig ->getFrontend ($ frontendName )['primary_frontend ' ];
130
137
$ hostName = $ pluginConfig ['frontends ' ][$ primaryFrontendName ]['hostname ' ];
138
+
131
139
$ ingress = KubernetesUtils::getResourceByNamespaceName ($ this ->state ['ingresses ' ], $ frontendDetails ['resource ' ]['namespace ' ], $ frontendDetails ['resource ' ]['name ' ]);
132
140
if (!empty ($ ingress )) {
133
141
foreach ($ ingress ['spec ' ]['rules ' ] as $ rule ) {
134
142
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
+
135
148
$ hosts [$ frontendName ][] = $ rule ['host ' ];
136
149
$ managedHostsPreSave [$ hostName ][] = $ rule ['host ' ];
137
150
}
@@ -141,35 +154,36 @@ public function doAction()
141
154
142
155
if ($ dnsmasqEnabled ) {
143
156
$ dnsmasqConfig = PfSenseConfigBlock::getRootConfigBlock ($ this ->getController ()->getRegistryItem ('pfSenseClient ' ), 'dnsmasq ' );
157
+ if (!key_exists ('hosts ' , $ dnsmasqConfig ->data )) {
158
+ $ dnsmasqConfig ->data ['hosts ' ] = [];
159
+ }
144
160
}
145
161
146
162
if ($ unboundEnabled ) {
147
163
$ unboundConfig = PfSenseConfigBlock::getRootConfigBlock ($ this ->getController ()->getRegistryItem ('pfSenseClient ' ), 'unbound ' );
164
+ if (!key_exists ('hosts ' , $ unboundConfig ->data )) {
165
+ $ unboundConfig ->data ['hosts ' ] = [];
166
+ }
148
167
}
149
168
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 ) {
154
171
$ itemId = [
155
172
'host ' => explode ('. ' , $ hostName , 2 )[0 ],
156
173
'domain ' => explode ('. ' , $ hostName , 2 )[1 ],
157
174
];
158
175
$ itemKey = ['host ' , 'domain ' ];
159
- if (empty ($ hostName )) {
160
- $ this ->log (('missing hostname for primary frontend: ' .$ primaryFrontendName ));
161
- continue ;
162
- }
163
176
164
177
if ($ dnsmasqEnabled ) {
178
+ $ this ->log ('setting dnsmasq host aliases for host: ' .$ hostName .', alias count: ' . count ($ aliases ));
165
179
$ host = null ;
166
180
$ host = Utils::getListItemMultiKey ($ dnsmasqConfig ->data ['hosts ' ], $ itemId , $ itemKey );
167
181
if ($ host !== null ) {
168
- if (empty ($ hosts [ $ frontendName ] )) {
182
+ if (empty ($ aliases )) {
169
183
$ host ['aliases ' ] = '' ;
170
184
} else {
171
185
$ host ['aliases ' ] = [];
172
- foreach ($ hosts [ $ frontendName ] as $ alias ) {
186
+ foreach ($ aliases as $ alias ) {
173
187
$ host ['aliases ' ]['item ' ][] = [
174
188
'host ' => explode ('. ' , $ alias , 2 )[0 ],
175
189
'domain ' => explode ('. ' , $ alias , 2 )[1 ],
@@ -185,14 +199,15 @@ public function doAction()
185
199
}
186
200
187
201
if ($ unboundEnabled ) {
202
+ $ this ->log ('setting unbound host aliases for host: ' .$ hostName .', alias count: ' . count ($ aliases ));
188
203
$ host = null ;
189
204
$ host = Utils::getListItemMultiKey ($ unboundConfig ->data ['hosts ' ], $ itemId , $ itemKey );
190
205
if ($ host !== null ) {
191
- if (empty ($ hosts [ $ frontendName ] )) {
206
+ if (empty ($ aliases )) {
192
207
$ host ['aliases ' ] = '' ;
193
208
} else {
194
209
$ host ['aliases ' ] = [];
195
- foreach ($ hosts [ $ frontendName ] as $ alias ) {
210
+ foreach ($ aliases as $ alias ) {
196
211
$ host ['aliases ' ]['item ' ][] = [
197
212
'host ' => explode ('. ' , $ alias , 2 )[0 ],
198
213
'domain ' => explode ('. ' , $ alias , 2 )[1 ],
@@ -210,13 +225,14 @@ public function doAction()
210
225
211
226
$ toDeleteHosts = array_diff (@array_keys ($ managedHosts ), @array_keys ($ managedHostsPreSave ));
212
227
foreach ($ toDeleteHosts as $ hostName ) {
213
- $ itemId = $ itemId = [
228
+ $ itemId = [
214
229
'host ' => explode ('. ' , $ hostName , 2 )[0 ],
215
230
'domain ' => explode ('. ' , $ hostName , 2 )[1 ],
216
231
];
217
232
$ itemKey = ['host ' , 'domain ' ];
218
233
219
234
if ($ dnsmasqEnabled ) {
235
+ $ this ->log ('removing dnsmasq host aliases for host: ' .$ hostName );
220
236
$ host = null ;
221
237
$ host = Utils::getListItemMultiKey ($ dnsmasqConfig ->data ['hosts ' ], $ itemId , $ itemKey );
222
238
if ($ host !== null ) {
@@ -226,6 +242,7 @@ public function doAction()
226
242
}
227
243
228
244
if ($ unboundEnabled ) {
245
+ $ this ->log ('removing unbound host aliases for host: ' .$ hostName );
229
246
$ host = null ;
230
247
$ host = Utils::getListItemMultiKey ($ unboundConfig ->data ['hosts ' ], $ itemId , $ itemKey );
231
248
if ($ host !== null ) {
0 commit comments