Skip to content

Commit c474602

Browse files
committed
remove duplicate ip addresses with smaller prefix
1 parent 147e076 commit c474602

File tree

2 files changed

+29
-0
lines changed
  • src/bosh-director

2 files changed

+29
-0
lines changed

src/bosh-director/lib/bosh/director/deployment_plan/ip_provider/ip_repo.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ def try_to_allocate_dynamic_ip(reservation, subnet)
9696
addresses_we_cant_allocate.delete_if { |ipaddr| ipaddr.ipv6? }
9797
end
9898

99+
addresses_we_cant_allocate.reject! do |ip|
100+
addresses_we_cant_allocate.any? do |other_ip|
101+
includes = other_ip.include?(ip) rescue false
102+
includes && other_ip.prefix < ip.prefix
103+
end
104+
end
105+
99106
ip_address_cidr = find_next_available_ip(addresses_we_cant_allocate, first_range_address, subnet.prefix)
100107

101108
if !(subnet.range == ip_address_cidr || subnet.range.include?(ip_address_cidr))

src/bosh-director/spec/unit/bosh/director/deployment_plan/ip_provider/ip_repo_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,28 @@ def dynamic_reservation_with_ip(ip)
279279
end
280280
end
281281

282+
context 'when existing ips overlap with restricted ips' do
283+
def save_ip(ip)
284+
Bosh::Director::Models::IpAddress.new(
285+
address_str: ip.to_s,
286+
network_name: 'my-manual-network',
287+
instance: instance_model,
288+
task_id: Bosh::Director::Config.current_job.task_id
289+
).save
290+
end
291+
292+
it 'clears similar ips with smaller prefix' do
293+
network_spec['subnets'].first['reserved'] = ['192.168.1.0 - 192.168.1.12']
294+
network_spec['subnets'].first['range'] = '192.168.1.0/24'
295+
save_ip(cidr_ip('192.168.1.8'))
296+
297+
ip_address = ip_repo.allocate_dynamic_ip(reservation, subnet)
298+
expected_ip_address = cidr_ip('192.168.1.13')
299+
expect(ip_address).to eq(expected_ip_address)
300+
301+
end
302+
end
303+
282304
context 'when there are static and restricted ips' do
283305
it 'does not reserve them' do
284306
network_spec['subnets'].first['reserved'] = ['192.168.1.2']

0 commit comments

Comments
 (0)