Fix IP Allocation Bug: Reserved Range Not Detected#2657
Open
Conversation
Ivaylogi98
reviewed
Jan 30, 2026
src/bosh-director/lib/bosh/director/deployment_plan/ip_provider/ip_repo.rb
Show resolved
Hide resolved
…r/ip_repo.rb Co-authored-by: Ivaylo Ivanov <ivaylogi98@gmail.com>
…_provider/ip_repo.rb" This reverts commit 6f689a9.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix IP Allocation Bug: Non-Deterministic CIDR Deduplication
What is this change about?
This PR fixes a bug in the IP allocation algorithm where IPs from reserved CIDR ranges were incorrectly allocated, causing CPI failures with "Address is in subnet's reserved address range" errors.
Root Cause: The deduplication logic that merges overlapping CIDR blocks (e.g., individual /32 IPs contained within a /30 block) relied on Ruby
Setiteration order, which is non-deterministic. Depending on the iteration order, the algorithm would sometimes fail to deduplicate properly, keeping individual /32 IPs even though they were fully contained within a larger reserved CIDR block.The Fix: Sort IP addresses by
[ip.to_i, ip.prefix]before performing deduplication. This ensures deterministic, correct behavior regardless of Set iteration order.Please provide contextual information.
Production Failure Example:
CPI error 'Bosh::Clouds::CloudError' with message 'Failed to create network interface for nic_group 'credhub_network': Address is in subnet's reserved address range'Example Network Configuration:
Technical Details:
The old deduplication code:
Example of Buggy Set Iteration Order:
Set contains:
[10.0.11.32/32, 10.0.11.33/32, 10.0.11.34/32, 10.0.11.35/32, 10.0.11.32/30]Scenario 1 - Bad Order (Bug Manifests):
Set iterates in order:
10.0.11.34/32, 10.0.11.35/32, 10.0.11.32/32, 10.0.11.33/32, 10.0.11.32/3010.0.11.34/32: "Does ANY other IP contain me with smaller prefix?"10.0.11.35/32- No (same prefix)10.0.11.32/32- No (same prefix)10.0.11.33/32- No (same prefix)10.0.11.32/30- YES, but this happens AFTER the decision to keep/reject10.0.11.34/32is KEPTScenario 2 - Good Order (Works by Chance):
Set iterates in order:
10.0.11.32/30, 10.0.11.32/32, 10.0.11.33/32, 10.0.11.34/32, 10.0.11.35/3210.0.11.34/32: "Does ANY other IP contain me with smaller prefix?"10.0.11.32/30first - YES (contains .34/32 with prefix 30 < 32)10.0.11.34/32is REJECTEDThe bug depends entirely on whether
Set#any?happens to iterate the /30 block before or after checking the /32 entry. Ruby Sets do not guarantee iteration order.Reference - class Set
What tests have you run against this PR?
Unit Tests:
ip_repo_spec.rbpass.Production Validation:
How should this change be described in bosh release notes?
Fixed: IP allocation no longer fails with "Address is in subnet's reserved address range" errors. The algorithm now correctly deduplicates overlapping CIDR blocks (e.g., individual /32 IPs within a /30 reserved range) by sorting IPs before deduplication, ensuring deterministic behavior regardless of internal Set iteration order. This fixes intermittent failures where IPs from reserved ranges were incorrectly allocated.
Does this PR introduce a breaking change?
No. This is a bug fix that makes the IP allocation algorithm work correctly and deterministically.