Skip to content

<fix>[network]: filter reserved IPs in getFreeIp#3341

Open
zstack-robot-1 wants to merge 1 commit into5.5.6from
sync/ye.zou/fix/ZSTAC-81182
Open

<fix>[network]: filter reserved IPs in getFreeIp#3341
zstack-robot-1 wants to merge 1 commit into5.5.6from
sync/ye.zou/fix/ZSTAC-81182

Conversation

@zstack-robot-1
Copy link
Collaborator

Resolves: ZSTAC-81182\n\nFilter out reserved IP addresses (gateway) in IpRangeHelper.getFreeIp() to prevent allocating them.

sync from gitlab !9170

@coderabbitai
Copy link

coderabbitai bot commented Feb 13, 2026

总体说明

L3BasicNetworkhandle(APIGetFreeIpMsg) 方法中添加了过滤逻辑,用于从空闲IP列表中排除落在预留IP范围内的IP地址。

变更

群组 / 文件 摘要
IP预留范围过滤
network/src/main/java/org/zstack/network/l3/L3BasicNetwork.java
在获取空闲IP的处理逻辑中添加了预留IP范围检查,计算L3网络的预留范围,并从空闲IP列表中过滤掉地址落在任何预留范围内的IP。

预估代码审查工作量

🎯 2 (简单) | ⏱️ ~8 分钟

诗歌

🐰 一只兔子跳过网络,
收集空闲的地址如金币闪闪,
但某些IP被标记为禁地,
聪慧的过滤器轻轻推开它们,
只留下真正自由的IP地址。✨

🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed 标题遵循 [scope]: 的格式要求,为 [network]: filter reserved IPs in getFreeIp,共48个字符,符合72字符以内的限制。标题清晰描述了主要变更内容:在getFreeIp方法中过滤保留的IP地址。
Description check ✅ Passed 描述说明了PR的目的:过滤保留的IP地址(网关)以防止分配这些保留地址,明确关联了问题编号ZSTAC-81182,内容与变更集相关。
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into 5.5.6

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sync/ye.zou/fix/ZSTAC-81182

No actionable comments were generated in the recent review. 🎉


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@network/src/main/java/org/zstack/network/l3/IpRangeHelper.java`:
- Around line 158-170: The current logic in IpRangeHelper builds spareIps then
truncates by limit and only afterwards filters out ReservedIpRangeVO ranges
(checked via NetworkUtils.isInRange), which can leave fewer than limit
addresses; change the logic in the method handling ipr (the block using
spareIps, ReservedIpRangeVO and limit) to first exclude reserved ranges when
generating candidate IPs or, if you prefer minimal change, filter out reserved
IPs and then extend the candidate selection (iterate further in the IP range)
until you either reach the requested limit or exhaust the range; update the code
that builds spareIps so it checks ReservedIpRangeVO_.getStartIp()/getEndIp() via
NetworkUtils.isInRange while collecting candidates and only stops when
collected.size() == limit (or no more IPs).

Comment on lines 158 to 170
List<ReservedIpRangeVO> reservedRanges = Q.New(ReservedIpRangeVO.class)
.eq(ReservedIpRangeVO_.l3NetworkUuid, ipr.getL3NetworkUuid())
.list();
if (!reservedRanges.isEmpty()) {
spareIps = spareIps.stream().filter(ip -> {
for (ReservedIpRangeVO reserved : reservedRanges) {
if (NetworkUtils.isInRange(ip, reserved.getStartIp(), reserved.getEndIp())) {
return false;
}
}
return true;
}).collect(Collectors.toList());
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

过滤后可能少于 limit,影响可用 IP 分配。

spareIps 先按 limit 截断,再过滤保留范围,可能导致返回数量少于 limit(例如起始候选恰好是保留 IP),从而在仍有可用 IP 的情况下出现分配失败或不足。建议在生成阶段就排除保留范围,或在过滤后继续向后补足直到满足 limit 或耗尽范围。

🤖 Prompt for AI Agents
In `@network/src/main/java/org/zstack/network/l3/IpRangeHelper.java` around lines
158 - 170, The current logic in IpRangeHelper builds spareIps then truncates by
limit and only afterwards filters out ReservedIpRangeVO ranges (checked via
NetworkUtils.isInRange), which can leave fewer than limit addresses; change the
logic in the method handling ipr (the block using spareIps, ReservedIpRangeVO
and limit) to first exclude reserved ranges when generating candidate IPs or, if
you prefer minimal change, filter out reserved IPs and then extend the candidate
selection (iterate further in the IP range) until you either reach the requested
limit or exhaust the range; update the code that builds spareIps so it checks
ReservedIpRangeVO_.getStartIp()/getEndIp() via NetworkUtils.isInRange while
collecting candidates and only stops when collected.size() == limit (or no more
IPs).

Resolves: ZSTAC-81182

Change-Id: Id1bb642154dc66ae9995dcc4d9fc00cdce9bcaf8
@MatheMatrix MatheMatrix force-pushed the sync/ye.zou/fix/ZSTAC-81182 branch from 60b958c to 6545350 Compare February 13, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants