Merge pull request #11111 from briancain/bug/docker_network_subnet_prefix

Fixes #11094: Determine prefix for docker public networks
This commit is contained in:
Brian Cain 2019-10-09 16:26:22 -07:00 committed by GitHub
commit 2147c6544f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 11 deletions

View File

@ -153,6 +153,10 @@ module VagrantPlugins
# Generate configuration for public network # Generate configuration for public network
# #
# TODO: When the Vagrant installer upgrades to Ruby 2.5.x,
# remove all instances of the roundabout way of determining a prefix
# and instead just use the built-in `.prefix` method
#
# @param [Hash] root_options Root networking options # @param [Hash] root_options Root networking options
# @param [Hash] net_options Docker scoped networking options # @param [Hash] net_options Docker scoped networking options
# @param [Hash] env Local call env # @param [Hash] env Local call env
@ -191,7 +195,9 @@ module VagrantPlugins
base_opts[:opt] = "parent=#{bridge_interface.name}" base_opts[:opt] = "parent=#{bridge_interface.name}"
subnet = IPAddr.new(bridge_interface.addr.ip_address << subnet = IPAddr.new(bridge_interface.addr.ip_address <<
"/" << bridge_interface.netmask.ip_unpack.first) "/" << bridge_interface.netmask.ip_unpack.first)
base_opts[:subnet] = "#{subnet}/#{subnet.prefix}" netmask = bridge_interface.netmask.ip_unpack.first
prefix = IPAddr.new("255.255.255.255/#{netmask}").to_i.to_s(2).count("1")
base_opts[:subnet] = "#{subnet}/#{prefix}"
subnet_addr = IPAddr.new(base_opts[:subnet]) subnet_addr = IPAddr.new(base_opts[:subnet])
base_opts[:driver] = "macvlan" base_opts[:driver] = "macvlan"
base_opts[:gateway] = subnet_addr.succ.to_s base_opts[:gateway] = subnet_addr.succ.to_s
@ -213,7 +219,7 @@ module VagrantPlugins
network_options, bridge_interface.name, env) network_options, bridge_interface.name, env)
end end
network_options[:ip_range] = request_public_iprange( network_options[:ip_range] = request_public_iprange(
network_options, bridge_interface.name, env) network_options, bridge_interface, env)
end end
end end
[network_name, network_options] [network_name, network_options]
@ -257,8 +263,12 @@ module VagrantPlugins
# Request the IP range allowed for use by docker when creating a new # Request the IP range allowed for use by docker when creating a new
# public network # public network
# #
# TODO: When the Vagrant installer upgrades to Ruby 2.5.x,
# remove all instances of the roundabout way of determining a prefix
# and instead just use the built-in `.prefix` method
#
# @param [Hash] network_options Docker scoped networking options # @param [Hash] network_options Docker scoped networking options
# @param [String] interface The bridge interface used # @param [Socket::Ifaddr] interface The bridge interface used
# @param [Hash] env Local call env # @param [Hash] env Local call env
# @return [String] Address range # @return [String] Address range
def request_public_iprange(network_options, interface, env) def request_public_iprange(network_options, interface, env)
@ -272,7 +282,7 @@ module VagrantPlugins
while !range while !range
range = env[:ui].ask(I18n.t( range = env[:ui].ask(I18n.t(
"docker_provider.network_bridge_iprange_request", "docker_provider.network_bridge_iprange_request",
interface: interface, interface: interface.name,
default_range: network_options[:subnet]) + " ", default_range: network_options[:subnet]) + " ",
prefix: false prefix: false
).strip ).strip
@ -282,11 +292,12 @@ module VagrantPlugins
begin begin
range = IPAddr.new(range) range = IPAddr.new(range)
if !subnet.include?(range) if !subnet.include?(range)
puts "we in here" netmask = interface.netmask.ip_unpack.first
prefix = IPAddr.new("255.255.255.255/#{netmask}").to_i.to_s(2).count("1")
env[:ui].warn(I18n.t( env[:ui].warn(I18n.t(
"docker_provider.network_bridge_iprange_outofbounds", "docker_provider.network_bridge_iprange_outofbounds",
subnet: network_options[:subnet], subnet: network_options[:subnet],
range: "#{range}/#{range.prefix}" range: "#{range}/#{prefix}"
) + "\n", prefix: false) ) + "\n", prefix: false)
range = nil range = nil
end end
@ -297,7 +308,10 @@ module VagrantPlugins
range = nil range = nil
end end
end end
"#{range}/#{range.prefix}"
netmask = interface.netmask.ip_unpack.first
prefix = IPAddr.new("255.255.255.255/#{netmask}").to_i.to_s(2).count("1")
"#{range}/#{prefix}"
end end
# Execute the action # Execute the action

View File

@ -305,7 +305,8 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
describe "#process_public_network" do describe "#process_public_network" do
let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} } let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} }
let(:ipaddr) { double("ipaddr", prefix: 22, succ: "10.1.10.2", ipv6?: false) } let(:ipaddr) { double("ipaddr", prefix: 22, succ: "10.1.10.2", ipv4?: true,
ipv6?: false, to_i: 4294967040) }
it "raises an error if there are no network interfaces" do it "raises an error if there are no network interfaces" do
expect(subject).to receive(:list_interfaces).and_return([]) expect(subject).to receive(:list_interfaces).and_return([])
@ -331,7 +332,7 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
describe "#request_public_gateway" do describe "#request_public_gateway" do
let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} } let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} }
let(:ipaddr) { double("ipaddr", to_s: "172.30.130.2", prefix: 22, succ: "172.30.130.3", let(:ipaddr) { double("ipaddr", to_s: "172.30.130.2", prefix: 22, succ: "172.30.130.3",
ipv6?: false) } ipv4?: true, ipv6?: false) }
it "requests a gateway" do it "requests a gateway" do
allow(IPAddr).to receive(:new).and_return(ipaddr) allow(IPAddr).to receive(:new).and_return(ipaddr)
@ -347,17 +348,24 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
describe "#request_public_iprange" do describe "#request_public_iprange" do
let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} } let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} }
let(:ipaddr) { double("ipaddr", to_s: "172.30.100.2", prefix: 22, succ: "172.30.100.3", let(:ipaddr) { double("ipaddr", to_s: "172.30.100.2", prefix: 22, succ: "172.30.100.3",
ipv6?: false) } ipv4?: true, ipv6?: false) }
let(:subnet) { double("ipaddr", to_s: "172.30.130.2", prefix: 22, succ: "172.30.130.3", let(:subnet) { double("ipaddr", to_s: "172.30.130.2", prefix: 22, succ: "172.30.130.3",
ipv6?: false) } ipv6?: false) }
let(:ipaddr_prefix) { double("ipaddr_prefix", to_s: "255.255.255.255/255.255.255.0",
to_i: 4294967040 ) }
let(:netmask) { double("netmask", ip_unpack: ["255.255.255.0", 0]) }
let(:interface) { double("interface", name: "bridge", netmask: netmask) }
it "requests a public ip range" do it "requests a public ip range" do
allow(IPAddr).to receive(:new).with(options[:subnet]).and_return(subnet) allow(IPAddr).to receive(:new).with(options[:subnet]).and_return(subnet)
allow(IPAddr).to receive(:new).with("172.30.130.2").and_return(ipaddr) allow(IPAddr).to receive(:new).with("172.30.130.2").and_return(ipaddr)
allow(IPAddr).to receive(:new).with("255.255.255.255/255.255.255.0").and_return(ipaddr_prefix)
allow(subnet).to receive(:include?).and_return(true) allow(subnet).to receive(:include?).and_return(true)
allow(machine.ui).to receive(:ask).and_return(options[:ip]) allow(machine.ui).to receive(:ask).and_return(options[:ip])
addr = subject.request_public_iprange(options, "bridge", env) addr = subject.request_public_iprange(options, interface, env)
end end
end end
end end