BerlinVagrant / vagrant-dns

A plugin to manage DNS records for vagrant environments
MIT License
490 stars 50 forks source link

Allow for static IP entries #80

Open miminar opened 1 year ago

miminar commented 1 year ago

First of all, thanks for the great plugin.

Maybe I am missing something, but I cannot find a way how to add a static IP entry not belonging to any managed VM.

My use-case: 3 managed VMs are configured with keepalived to create a virtual IP in the same network. I'd like to assign a name to this IP using this plugin.

Here's my shortened Vagrant file:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = false
    vb.default_nic_type = "virtio"
    vb.linked_clone = true
    vb.memory = "#{1024 * 6}"
    vb.cpus = 3
  end

  NET = "192.168.56"
  GW = "#{NET}.1"       # localhost IP on the VM network
  API_VIP = "#{NET}.9"

  config.dns.tld = "vdev"
  config.dns.patterns = [/^(?:\w+\.)?k0s.vdev$/]

  N = 3  # number of schedulable control-plane nodes
  (1..N).each do |machine_id|
    config.vm.define ("ctrl-%02d" % [machine_id]) do |machine|

      machine.vm.hostname = "ctrl-%02d" % [machine_id]

      machine.vm.network "private_network",
        ip: "#{NET}.#{10+machine_id}"

      machine.dns.patterns = "#{machine.vm.hostname}.k0s.vdev"

      machine.vm.provision "shell",
        run: 'always',
        inline: <<-EOF
        set -euo pipefail
        if ip route | grep -q '^default via #{GW} dev.*'; then
          echo "replacing default GW ..."
          ip route replace default via #{GW} metric 10
        else
          echo "adding default GW ..."
          ip route add default via #{GW} metric 10
        fi
        EOF

      if machine_id == N
        machine.vm.provision :ansible do |ansible|
          ...

          ansible.groups = {
            "masters"                             => ["ctrl-[01:%02d]" % [N]],
            "masters:vars"                        => {
              "k8s_master_keepalived_vip_address" => "#{API_VIP}/24",
              # TODO: allow to add custom DNS entries without adding more VMs
              #"k0s_api_url"                       => "https://api.k0s.vdev:6443",
              "k0s_api_url"                       => "https://#{API_VIP}:6443",
              ...
            }
          }
        end
    end
  end
end

I wonder if in addition to config.dns.patterns = [/^(?:\w+\.)?k0s.vdev$/], one could add something like:

config.dns.hosts = {
    "api.k0s.vdev"    => "192.168.56.09",
    "*.apps.k0s.vdev" => "192.168.56.10",
}

Your help would be appreciated.

fnordfish commented 1 year ago

There’s (currently) no direct way of doing that.

However, you could edit - either manually or using ruby in your vagrant file. ~/.vagrant.d/tmp/dns/config (which is a YAML file). If you can do that before the machines getting up, you won’t even need to restart the vagrant-dns server. But remember, that there’s no one cleaning up after you :)

fnordfish commented 1 year ago

If you opt for editing using ruby, and your names are regular expression, you should use https://github.com/BerlinVagrant/vagrant-dns/blob/master/lib/vagrant-dns/registry.rb which takes care of converting RegExps correctly.

fnordfish commented 1 year ago

If you opt for editing using ruby, and your names are regular expression, you should use https://github.com/BerlinVagrant/vagrant-dns/blob/master/lib/vagrant-dns/registry.rb which takes care of converting RegExps correctly.