**Problem**
Right now the sre.dns.netbox cookbook generates numerous different files which ultimate go into ///etc/gdnsd/zones/netbox/// on auth dns servers. Each of these needs to have an INCLUDE statement pointing to it in the static zone files we create in the [[ https://gerrit.wikimedia.org/r/admin/repos/operations/dns | dns repo. ]] This is extremely cumbersome, tedious and error-prone, especially for IPv6 reverses where we have a separate file for every single link on the Network (as each have their own /64 subnet).
Right now, for instance, our zone files have entries like this:
```
cmooney@dns3003:/etc/gdnsd/zones$ tail -20 0.8.c.e.2.0.a.2.ip6.arpa
; cr1-magru <-> asw1-b3-magru (2a02:ec80:700:fe06::/64)
$ORIGIN 6.0.e.f.0.0.7.0.@Z
$INCLUDE netbox/6.0.e.f.0.0.7.0.0.8.c.e.2.0.a.2.ip6.arpa
; cr1-magru <-> asw1-b4-magru (2a02:ec80:700:fe07::/64)
$ORIGIN 7.0.e.f.0.0.7.0.@Z
$INCLUDE netbox/7.0.e.f.0.0.7.0.0.8.c.e.2.0.a.2.ip6.arpa
; cr2-magru <-> asw1-b3-magru (2a02:ec80:700:fe08::/64)
$ORIGIN 8.0.e.f.0.0.7.0.@Z
$INCLUDE netbox/8.0.e.f.0.0.7.0.0.8.c.e.2.0.a.2.ip6.arpa
; cr2-magru <-> asw1-b4-magru (2a02:ec80:700:fe09::/64)
$ORIGIN 9.0.e.f.0.0.7.0.@Z
$INCLUDE netbox/9.0.e.f.0.0.7.0.0.8.c.e.2.0.a.2.ip6.arpa
; Magru network loopbacks (2a02:ec80:700:ffff::/64)
$ORIGIN f.f.f.f.0.0.7.0.@Z
$INCLUDE netbox/f.f.f.f.0.0.7.0.0.8.c.e.2.0.a.2.ip6.arpa
```
The contents of each of the included files, generated by Netbox, only have the PTR (or A / AAAA) records that go under that $ORIGIN:
```
cmooney@dns3003:/etc/gdnsd/zones$ cat ./netbox/9.0.e.f.0.0.7.0.0.8.c.e.2.0.a.2.ip6.arpa
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-2.cr2-magru.wikimedia.org.
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-50.asw1-b4-magru.wikimedia.org.
```
**Proposal**
What I would propose is we change how we do things as follows:
* For each zone file we are authoritative for we should have a single INCLUDE statement for netbox entries
* The single file this points to should have all the records that Netbox knows about under that zone
* To achieve this the netbox-generated files will need to add $ORIGIN directives at the correct points before records
What makes this possible is the fact that the $ORIGIN directive can appear multiple times in the same zone file, affecting the way records between it and the next $ORIRIN directive are handled. In other words we can have Netbox generated records under a $ORIGIN in the file Netbox creates, but we can use that $ORIGIN again later in the static zone file for manual entries that need to appear under it.
To go back to our example we could modify the files as follows:
**/etc/gdnsd/zones/0.8.c.e.2.0.a.2.ip6.arpa:**
```
@ 1H IN SOA ns0.wikimedia.org. hostmaster.wikimedia.org. 2024041716 12H 2H 2W 1H
; Name servers
@ 1D IN NS ns0.wikimedia.org.
@ 1D IN NS ns1.wikimedia.org.
@ 1D IN NS ns2.wikimedia.org.
; Netbox generated entries
$INCLUDE netbox/0.8.c.e.2.0.a.2.ip6.arpa
; Static entries
$ORIGIN x xx x x
yyy IN PTR blah.wikimedia.org
```
**/etc/gdnsd/zones/netbox/0.8.c.e.2.0.a.2.ip6.arpa:**
```
$ORIGIN 6.0.e.f.0.0.7.0.@Z
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-1.cr1-magru.wikimedia.org.
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-48.asw1-b3-magru.wikimedia.org.
$ORIGIN 7.0.e.f.0.0.7.0.@Z
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-2.cr1-magru.wikimedia.org.
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-48.asw1-b4-magru.wikimedia.org.
$ORIGIN 8.0.e.f.0.0.7.0.@Z
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-1.cr2-magru.wikimedia.org.
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-50.asw1-b3-magru.wikimedia.org.
$ORIGIN 9.0.e.f.0.0.7.0.@Z
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-2.cr2-magru.wikimedia.org.
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR et-0-0-50.asw1-b4-magru.wikimedia.org.
$ORIGIN f.f.f.f.0.0.7.0.@Z
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR cr1-magru.wikimedia.org.
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR cr2-magru.wikimedia.org.
3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR asw1-b3-magru.wikimedia.org.
4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR asw1-b4-magru.wikimedia.org.
5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 1H IN PTR mr1-magru.wikimedia.org.
```
The benefit of this approach is we will not need to modify the static zone file or dns repo when we add new sub-ranges. The sre.dns.netbox cookbook will just modify the above file, but no INCLUDEs will change.
NOTE: It may also be possible to keep a single $ORIGIN statement in the zone file, covering the entire zone, and make Netbox create each PTR/A/AAAA relative to that overall ORIGIN. To be discussed.
**Details**
There are a few things we need to consider:
# How does Netbox know what the parent zones are?
## For forward entries this should be easy? Just make a file for each domain, i.e. last two labels in the FQDNs?
## For IP ranges this is a little tricker
### For instance the above zone covers the entire 2a02:ec80::/32 range, but the range of assigned IPs in Netbox would fit into just the first /36.
### We need to see if there is a way to handle/derive this from Netbox itself, or if we need to maintain a list of zones elsewhere
Interested to get people's thoughts on this. Day to day the current setup is an annoyance, when it comes to doing new sites/DC rows etc. it is a real chore so it would be good to improve things.