Puppet Function: nomad_cni::cni_ranges_v4
- Defined in:
- lib/puppet/functions/nomad_cni/cni_ranges_v4.rb
- Function type:
- Ruby 4.x API
Overview
]
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 |
# File 'lib/puppet/functions/nomad_cni/cni_ranges_v4.rb', line 26 Puppet::Functions.create_function(:'nomad_cni::cni_ranges_v4') do dispatch :calculate_cni_ranges_v4 do param 'Stdlib::IP::Address::V4::CIDR', :network_address param 'Array[String]', :agent_names param 'Optional[Integer]', :min_networks return_type 'Array[Array]' end def calculate_cni_ranges_v4(network_address, agent_names, min_networks) netmask = network_address.split('/')[1].to_i address = network_address.split('/')[0].to_s first_ip = IPAddr.new(network_address).to_range.first.to_s if first_ip != address raise ArgumentError, "Invalid network address: #{network_address}. The correct address for this network is: #{first_ip}/#{netmask}" end sorted_agent_names = agent_names.sort last_ip_int = IPAddr.new(network_address).to_range.last.to_i first_ip_int = IPAddr.new(network_address).to_range.first.to_i + 1 free_hosts = last_ip_int - first_ip_int - 1 agent_number = agent_names.length if !min_networks.nil? raise ArgumentError, "Invalid number of networks: #{min_networks}. It must be Undef or greater than the number of agents: #{agent_number}" if min_networks < agent_number number_of_networks = min_networks else number_of_networks = agent_number end chunk_size = (free_hosts / number_of_networks).floor if chunk_size < 3 # we have one gateway, one first usable IP, and one last usable IP raise ArgumentError, "Invalid network address: #{network_address}. The network is too small to be split in #{number_of_networks} networks" end agents_array = (0..agent_number - 1).to_a agents_array.map do |item| [ sorted_agent_names[item], # agent name IPAddr.new((first_ip_int + (chunk_size * item) + 1).to_i, Socket::AF_INET).to_s, # gateway, and VXLAN IP on the host: NOW THIS IS THE FIRST IP IN THE RANGE IPAddr.new((first_ip_int + (chunk_size * item) + 2).to_i, Socket::AF_INET).to_s, # first usable IP in the range: THIS IS NO LONGER NEEDED IPAddr.new((first_ip_int + (chunk_size * item) + chunk_size).to_i, Socket::AF_INET).to_s, # last usable IP in the range netmask, # netmask ] end end end |