Page MenuHomePhabricator

Authdns: automate reverse DNS zone delegation for k8s pod IP ranges
Open, MediumPublic

Description

The parent task describes the work going on to run a coredns service on our k8s infrastructure to serve PTR records for k8s pod IPs, which will aid troubleshooting by giving us a way to work back from an IP address in a log to a particular k8s pod.

Part of this involves creating NS records in the DNS zones hosted by our authdns servers, to delegate the ranges managed by kubernetes to the correct hosts. The plan currently is that the control-plane nodes for each kubernetes cluster will host the service, which will be available on the actual host's main IP address through the use of NodePort.

This task will be used to track the work to get the authdns delegation side of this up and working.

Subnets and name servers

The list of subnets used by each cluster, and the control-plane nodes for each, is available in hiera under kubernetes::clusters. Reverse zone(s) covering the IP ranges listed under cluster_cidr for each cluster need to be delegated to the set of servers listed under control_plane_nodes.

Generating the zone file data

I've been experimenting with how to generate the DNS records we need from the above data, some of which I've put on github here:

https://github.com/topranks/random_wmf/tree/main/hackathon_k8s_dns

At a high-level I think we can generate 'snippet' files, much as the Netbox generate_dns_snippets script does. Like that case we can publish the generated files to a sub-directory under /etc/gdnsd/templates/, and use INCLUDE statements in the dns repo zone templates to include the new records.

Unlike the Netbox snippet script we can probably run the script to generate the NS entries directly on the authdns hosts themselves. Or at any rate there is no particular reason the script needs to run on a netbox host. The script I've been playing with will need the information about the parent zones as well as the kubernetes k8s info. We can add this parent zone data to hiera. My idea was to then make this data available in YAML files local to where the script runs, which we can potentially create using puppet.

Open questions

  • Is this a good approach?
  • Where should the script be run from? authdns servers themselves?
  • How should the data it needs be created? Using puppet?
  • How and when should the script run to (re-)generate the zone file snippets?
  • How should it trigger authdns-update
  • How do we automate all of this?

Event Timeline

cmooney triaged this task as Medium priority.
cmooney updated the task description. (Show Details)

Is there plan to try to get away from the very long hardcoded lists in hiera?
How often do you expect the data to change? This might affect how we want to trigger the changes.

Is there plan to try to get away from the very long hardcoded lists in hiera?

No idea :)

How often do you expect the data to change? This might affect how we want to trigger the changes.

The range of IPs being delegated is usually determined at cluster creation time, and not often changed afterwards.
Control plane servers are mostly determined at cluster creation time, and aside from that I think change ~every few years (per k8s cluster).

Is there plan to try to get away from the very long hardcoded lists in hiera?

I'm more taking advantage of that data here than anything else. So I guess the answer - at least for now - is no?

I think the list of zones in our authdns needs to be made available somewhere. Admittedly it is not great to be adding new data to hiera, however there isn't any other great source for this that I know of. The list of files ending 'in-addr.arpa' or 'ip6.arpa' in the dns repo templates dir is about the only place. However that doesn't give us the associated subnets (though it could be derived with a bit of effort). This data will also be needed when we tackle T362985.

How often do you expect the data to change? This might affect how we want to trigger the changes.

The reverse zones I'm planning to add in hiera rarely change. Typically it only happens when we aquire new public IP ranges.

From what I can tell we only add new k8s clusters occasionally too, so the generated data probably also won't change much. How often the set of 'control plane' nodes for a given cluster change I am unsure of though, they'll at minimum go through normal refresh cycle.

Thanks for filing this task!

  1. So it seems like there is a possibility that this list (or rather, these lists) can be maintained manually given how infrequent the updates are. I think that was my first question. (This also ties in somewhat to how much duplication of network information we have in Puppet but I guess that has to be matched with some realistic expectation of what we can do within Puppet.)
    • It seem the network data in dns_reverse_zones.yaml and the corresponding reverse zones can be wrangled from and generated in a Puppet function using network::constants. I am not sure if there is any use of it (except a trivial use in modules/dnsrecursor/manifests/init.pp but that's irrelevant mostly) so it might just make sense to keep this manual. I leave this fun to you but happy to brainstorm on a function that does it -- who knows, might be helpful later.
  1. You can run it from the DNS hosts if you want but if you have another place where it makes more sense for the data to exist and be pulled in from, then that probably is better.
    • There are various approaches you can take here. Maybe you can use the existing Netbox DNS cookbook and the associated workflow and add the zones there and let it pick them up and that also negates running authdns-update as a separate step. Is that the best place to put them? No. Does it help given that that workflow has already been tested and is established? Yes.
    • You could carve out another directory for this if you don't want to conflate with Netbox stuff but there will be some process duplication there. The sre.dns.netbox cookbook is a good place to get started on what we can do here but basically we can use deploy-check and use that to deploy the zones and reload gdnsd, on each DNS host. (When used with --deploy).
  1. I think the data in dns_reverse_zones.yaml can probably be wrangled with a Puppet function. So can dns_k8s_reverse_delegation.yaml but not sure if the effort is worth it.
  1. You are the expert here but automation for this first depends on where you want to run this script from or rather where it should be run from. The authdns delegation parts of this and the zone file appends come at the end probably and are easy to solve. So the question I do have is which host is best suited for this script to be run?

I may be looking at this in the wrong way but I feel hooking this into the existing Netbox cookbook and given how frequently it runs while not nice may be the best path forward. @Volans can comment better on those part and then we can discuss the gdnsd sync!

Re: the point regarding snippets and using INCLUDE: I think that's not optional anyway -- we have to keep only one 10.in-addr.arpa file (example) and keep it updated -- so we will have to do INCLUDE statements, so you are right about that.

  • It seem the network data in dns_reverse_zones.yaml and the corresponding reverse zones can be wrangled from and generated in a Puppet function using network::constants.

I'm not sure that is possible. There is flexibility in what zones one creates in DNS to cover a given range of IP addresses. Ours zones are sensible but I don't believe there is a pattern in network::constants that would give us the exact list. I'd worry the function would end up picking out a bunch of specific keys, and even positions in arrays, to get the correct set of prefixes we happen to know correspond to zones. Which to me sounds worse than hard coding them.

We could get them from the file names in the dns repo, but tbh I don't think hard coding them in hiera is a massive problem. We've 23 in total, and have only added 3 since 2011 so it's not much overhead.

  1. I think the data in dns_reverse_zones.yaml can probably be wrangled with a Puppet function. So can dns_k8s_reverse_delegation.yaml but not sure if the effort is worth it.

Not 100% what you mean here?

  1. automation for this first depends on where you want to run this script from or rather where it should be run from. The authdns delegation parts of this and the zone file appends come at the end probably and are easy to solve. So the question I do have is which host is best suited for this script to be run?

Hey, I thought that was my question to you! Tbh I'm not sure.

I may be looking at this in the wrong way but I feel hooking this into the existing Netbox cookbook and the associated workflow

@Volans can comment better on those part

Ok. I'll touch-base with him and see what if maybe we can augment generate_dns_snippets.py to also add this data, which would allow us to copy it to the dns hosts with the existing method. As you say it's tried & tested so probably not a bad option.

Change #1077486 had a related patch set uploaded (by Cathal Mooney; author: Cathal Mooney):

[operations/dns@master] Delegate Kubernetes POD IP reverse ranges to k8s control-plane nodes

https://gerrit.wikimedia.org/r/1077486

  • It seem the network data in dns_reverse_zones.yaml and the corresponding reverse zones can be wrangled from and generated in a Puppet function using network::constants.

I'm not sure that is possible. There is flexibility in what zones one creates in DNS to cover a given range of IP addresses. Ours zones are sensible but I don't believe there is a pattern in network::constants that would give us the exact list. I'd worry the function would end up picking out a bunch of specific keys, and even positions in arrays, to get the correct set of prefixes we happen to know correspond to zones. Which to me sounds worse than hard coding them.

Looking at https://github.com/topranks/random_wmf/blob/main/hackathon_k8s_dns/dns_reverse_zones.yaml, I see some overlap with the private ranges, network::customers, network::external. What I was trying to say here was that if you can, you can create a list from those. But after reading your comment, I looked and some of the subnets differ.

So which networks have made this list? Is it intended to cover all the Kubernetes IP ranges? I was suggesting automation based on those but again, I think it's probably not worth it unless you want to remove duplication (and if possible).

We could get them from the file names in the dns repo, but tbh I don't think hard coding them in hiera is a massive problem. We've 23 in total, and have only added 3 since 2011 so it's not much overhead.

This is true, I am fine with this given that this is unlikely to change.

  1. I think the data in dns_reverse_zones.yaml can probably be wrangled with a Puppet function. So can dns_k8s_reverse_delegation.yaml but not sure if the effort is worth it.

Not 100% what you mean here?

You are basing dns_k8s_reverse_delegation on hieradata/common/kubernetes.yaml, correct? I was hoping that you can read the data directly from there and wrangle it to the format you desire, instead of the duplication. Similarly, we can probably generate the reverse zones such as 10.in-addr.arpa for 10.0.0.0/8, 174.198.91.in-addr.arpa for 91.198.174.0/24 and so on from within Puppet instead of hardcoding them.

Again, I don't think it is worth it but it is a possibility if you want to explore. (Certainly neater/easier to do in Python vs Puppet.)

  1. automation for this first depends on where you want to run this script from or rather where it should be run from. The authdns delegation parts of this and the zone file appends come at the end probably and are easy to solve. So the question I do have is which host is best suited for this script to be run?

Hey, I thought that was my question to you! Tbh I'm not sure.

I would say the Netbox hosts so that you can make use of the existing work on the Netbox + authdns syncs.

I may be looking at this in the wrong way but I feel hooking this into the existing Netbox cookbook and the associated workflow

@Volans can comment better on those part

Ok. I'll touch-base with him and see what if maybe we can augment generate_dns_snippets.py to also add this data, which would allow us to copy it to the dns hosts with the existing method. As you say it's tried & tested so probably not a bad option.

I am curious to hear what Riccardo thinks as well and then we can figure out the gdnsd bits.

(I think my comments might be misconstrued to mean that I want to automate everything and that's certainly not my intention: I know you care about not duplicating network data and so the above comes from there. There is also the thing that whatever you come up with now, even with manual data, is better than what we have so we shouldn't forget that.)

You are basing dns_k8s_reverse_delegation on hieradata/common/kubernetes.yaml, correct? I was hoping that you can read the data directly from there and wrangle it to the format you desire, instead of the duplication.

The example puppetcode I shared creates the YAML file from the info in hiera. Are you suggesting to not use a local YAML file where the Python script runs, but instead have the Python script to somehow get data directly from the puppet repo?

Similarly, we can probably generate the reverse zones such as 10.in-addr.arpa for 10.0.0.0/8, 174.198.91.in-addr.arpa for 91.198.174.0/24 and so on from within Puppet instead of hardcoding them.

Creating the reverse zone for a given subnet is not difficult. How to know which subnets to include or exclude from network::constants (because there is an equivalent zone for it on our authdns) is where the tricky bit.

Again, I don't think it is worth it but it is a possibility if you want to explore. (Certainly neater/easier to do in Python vs Puppet.)

We could probably replicate what the Python script does directly in puppet, and dump the final zone file snippets directly from there. But that's probably beyond my abilities with puppet, or at least to reason about well enough to be sure everything is being done 100% right for all scenarios. Using a Python script, being able to use ipaddress library etc. seemed the easier way forward. And if doing that feeding the input data to it from YAML files created by puppet seemed sensible.

(I think my comments might be misconstrued to mean that I want to automate everything and that's certainly not my intention: I know you care about not duplicating network data and so the above comes from there.

No absolutely we should aim to automate everything. And I no way like duplication as you know. But there is only 23 lines of "duplicate" data going into hiera here, and that data is not available elsewhere except in the dns_repo. That said if the script ultimately runs from a Netbox host it may be able to pull in the dns repo, so maybe there is an option there.

I had a quick stab at creating the data for the dns_reverse_zones.yaml file from the dns repo and it's fairly simple:

https://github.com/topranks/random_wmf/blob/main/hackathon_k8s_dns/gen_dns_reverse.py

So we can hopefully avoid adding it to hiera as long as we the dns repo data can be available to the script.

You are basing dns_k8s_reverse_delegation on hieradata/common/kubernetes.yaml, correct? I was hoping that you can read the data directly from there and wrangle it to the format you desire, instead of the duplication.

The example puppetcode I shared creates the YAML file from the info in hiera. Are you suggesting to not use a local YAML file where the Python script runs, but instead have the Python script to somehow get data directly from the puppet repo?

Yes. But that does involve Puppet and Ruby and all and so your Python script also works for me. I don't think we need to over-engineer this and should be very mindful of that!

Similarly, we can probably generate the reverse zones such as 10.in-addr.arpa for 10.0.0.0/8, 174.198.91.in-addr.arpa for 91.198.174.0/24 and so on from within Puppet instead of hardcoding them.

Creating the reverse zone for a given subnet is not difficult. How to know which subnets to include or exclude from network::constants (because there is an equivalent zone for it on our authdns) is where the tricky bit.

(I take it back, not worth the automation, please ignore my comment)

Again, I don't think it is worth it but it is a possibility if you want to explore. (Certainly neater/easier to do in Python vs Puppet.)

We could probably replicate what the Python script does directly in puppet, and dump the final zone file snippets directly from there. But that's probably beyond my abilities with puppet, or at least to reason about well enough to be sure everything is being done 100% right for all scenarios. Using a Python script, being able to use ipaddress library etc. seemed the easier way forward. And if doing that feeding the input data to it from YAML files created by puppet seemed sensible.

(I think my comments might be misconstrued to mean that I want to automate everything and that's certainly not my intention: I know you care about not duplicating network data and so the above comes from there.

No absolutely we should aim to automate everything. And I no way like duplication as you know. But there is only 23 lines of "duplicate" data going into hiera here, and that data is not available elsewhere except in the dns_repo. That said if the script ultimately runs from a Netbox host it may be able to pull in the dns repo, so maybe there is an option there.

I am fine with this on the DNS host as well if that makes things easier.

Mentioned in SAL (#wikimedia-operations) [2024-10-03T15:50:29Z] <topranks> merging patch to add k8s pod IP range reverse delegations to dns T376291

Change #1077486 merged by Cathal Mooney:

[operations/dns@master] Delegate Kubernetes POD IP reverse ranges to k8s control-plane nodes

https://gerrit.wikimedia.org/r/1077486

cmooney renamed this task from Delegate reverse DNS zones for k8s pod IP ranges on authdns servers to Authdns: automate reverse DNS zone delegation for k8s pod IP ranges.Oct 3 2024, 7:02 PM

Change #1080276 had a related patch set uploaded (by Cathal Mooney; author: Cathal Mooney):

[operations/puppet@production] Authdns: add class to create zonefile snippets for K8s PTR delegation

https://gerrit.wikimedia.org/r/1080276

The above patch is my current best-stab at accomplishing this. I won't have a huge amount of time to look at this going forward, and I can understand there are concerns about the exact mechanism proposed (certainly not perfect), but I thought it best to document the basic shape of what I'd been working on which might server as the basis for a solution, or even inspiration for a more elegant approach.

Change #1171182 had a related patch set uploaded (by Cathal Mooney; author: Cathal Mooney):

[operations/dns@master] Update Kubernetes reverse DNS PTR delegations

https://gerrit.wikimedia.org/r/1171182

Change #1171182 merged by Cathal Mooney:

[operations/dns@master] Update Kubernetes reverse DNS PTR delegations

https://gerrit.wikimedia.org/r/1171182