-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDNSResourceTrait.php
More file actions
190 lines (163 loc) · 6.59 KB
/
DNSResourceTrait.php
File metadata and controls
190 lines (163 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace KubernetesPfSenseController\Plugin;
/**
* Common code for DNS plugins
*
* Trait DNSResourceTrait
* @package KubernetesPfSenseController\Plugin
*/
trait DNSResourceTrait
{
/**
* Update pfSense state
*
* @return bool
*/
public function doAction()
{
$pluginConfig = $this->getConfig();
$dnsmasqEnabled = $pluginConfig['dnsBackends']['dnsmasq']['enabled'];
$unboundEnabled = $pluginConfig['dnsBackends']['unbound']['enabled'];
// only supported options move along
if (!$dnsmasqEnabled && !$unboundEnabled) {
$this->log('plugin enabled without valid dnsBackends');
return true;
}
$resourceHosts = [];
foreach ($this->state['resources'] as $resource) {
$this->buildResourceHosts($resourceHosts, $resource);
}
$hosts = [];
$managedHostsPreSave = [];
foreach ($resourceHosts as $hostName => $struct) {
$ip = $struct['ip'];
$this->log("setting hostname entry: Host - {$hostName}, IP - {$ip}");
$managedHostsPreSave[$hostName] = [
'resource' => $this->getKubernetesResourceDetails($struct['resource']),
];
$hosts[] = [
'host' => explode('.', $hostName, 2)[0],
'domain' => explode('.', $hostName, 2)[1],
'ip' => $ip,
'descr' => 'created by kpc - do not edit',
'aliases' => '',
];
}
try {
// get store data
$store = $this->getStore();
if (empty($store)) {
$store = [];
}
$managedHosts = $store['managed_hosts'] ?? [];
// actually remove them from config
$toDeleteHosts = array_diff(@array_keys($managedHosts), @array_keys($managedHostsPreSave));
foreach ($toDeleteHosts as $hostName) {
$this->log("deleting hostname entry for host: {$hostName}");
}
$dnsmasqConfig = null;
$unboundConfig = null;
if ($dnsmasqEnabled) {
$dnsmasqConfig = PfSenseConfigBlock::getRootConfigBlock($this->getController()->getRegistryItem('pfSenseClient'), 'dnsmasq');
if (!isset($dnsmasqConfig->data) || !is_array($dnsmasqConfig->data)) {
$dnsmasqConfig->data = [];
}
if (!isset($dnsmasqConfig->data['hosts']) || !is_array($dnsmasqConfig->data['hosts'])) {
$dnsmasqConfig->data['hosts'] = [];
}
foreach ($hosts as $host) {
$host['ip'] = explode(',', $host['ip'], 2)[0];
Utils::putListItemMultiKey($dnsmasqConfig->data['hosts'], $host, ['host', 'domain']);
}
foreach ($toDeleteHosts as $hostName) {
$itemId = [
'host' => explode('.', $hostName, 2)[0],
'domain' => explode('.', $hostName, 2)[1],
];
Utils::removeListItemMultiKey($dnsmasqConfig->data['hosts'], $itemId, ['host', 'domain']);
}
}
if ($unboundEnabled) {
$unboundConfig = PfSenseConfigBlock::getRootConfigBlock($this->getController()->getRegistryItem('pfSenseClient'), 'unbound');
if (!isset($unboundConfig->data) || !is_array($unboundConfig->data)) {
$unboundConfig->data = [];
}
if (!isset($unboundConfig->data['hosts']) || !is_array($unboundConfig->data['hosts'])) {
$unboundConfig->data['hosts'] = [];
}
foreach ($hosts as $host) {
Utils::putListItemMultiKey($unboundConfig->data['hosts'], $host, ['host', 'domain']);
}
foreach ($toDeleteHosts as $hostName) {
$itemId = [
'host' => explode('.', $hostName, 2)[0],
'domain' => explode('.', $hostName, 2)[1],
];
Utils::removeListItemMultiKey($unboundConfig->data['hosts'], $itemId, ['host', 'domain']);
}
}
if ($dnsmasqEnabled && !empty($dnsmasqConfig)) {
$this->savePfSenseConfigBlock($dnsmasqConfig);
$this->reloadDnsmasq();
}
if ($unboundEnabled && !empty($unboundConfig)) {
$this->savePfSenseConfigBlock($unboundConfig);
$this->reloadUnbound();
}
// save data to store
$store['managed_hosts'] = $managedHostsPreSave;
$this->saveStore($store);
// reload DHCP sesrvice
$this->reloadDHCP();
return true;
} catch (\Exception $e) {
$this->log('failed update/reload: '.$e->getMessage().' ('.$e->getCode().')');
return false;
}
}
/**
* Does a sanity check to prevent over-aggressive updates when watch resources are technically
* modified but the things we care about are not
*
* @param $event
* @param $oldItem
* @param $item
* @param $stateKey
* @param $options
* @return bool
*/
public function shouldTriggerFromWatchUpdate($event, $oldItem, $item, $stateKey, $options = [])
{
if ($stateKey == "resources") {
// will be NULL for ADDED and DELETED
if ($oldItem === null) {
$tmpResourceHosts = [];
switch ($event['type']) {
case "ADDED":
case "DELETED":
$this->buildResourceHosts($tmpResourceHosts, $item);
if (count($tmpResourceHosts) > 0) {
return true;
}
break;
}
return false;
}
$oldResourceHosts = [];
$newResourceHosts = [];
$this->buildResourceHosts($oldResourceHosts, $oldItem);
$this->buildResourceHosts($newResourceHosts, $item);
foreach ($oldResourceHosts as $host => $value) {
$oldResourceHosts[$host] = ['ip' => $value['ip']];
}
foreach ($newResourceHosts as $host => $value) {
$newResourceHosts[$host] = ['ip' => $value['ip']];
}
if (md5(json_encode($oldResourceHosts)) != md5(json_encode($newResourceHosts))) {
return true;
}
return false;
}
return false;
}
}