forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<fix>[network]: set nic ip out of l3 cidr scope #3301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zstack-robot-2
wants to merge
4
commits into
5.5.6
Choose a base branch
from
sync/shixin.ruan/shixin-ZSTAC-81969@@2
base: 5.5.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,3 +34,4 @@ envDSLTree | |
| test/zstack-integration-test-result/ | ||
| premium/test-premium/zstack-api.log | ||
| **/bin/ | ||
| CLAUDE.md | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| import org.zstack.header.message.APIMessage; | ||
| import org.zstack.header.message.MessageReply; | ||
| import org.zstack.header.network.l3.*; | ||
| import org.zstack.network.l3.L3NetworkSystemTags; | ||
| import org.zstack.header.tag.SystemTagCreateMessageValidator; | ||
| import org.zstack.header.tag.SystemTagVO; | ||
| import org.zstack.header.tag.SystemTagVO_; | ||
|
|
@@ -30,6 +31,7 @@ | |
|
|
||
| import javax.persistence.Tuple; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -134,6 +136,17 @@ public Map<String, NicIpAddressInfo> getNicNetworkInfoBySystemTag(List<String> s | |
| } | ||
| ret.get(l3Uuid).ipv6Prefix = token.get(VmSystemTags.IPV6_PREFIX_TOKEN); | ||
| } | ||
| if(L3NetworkSystemTags.STATIC_DNS.isMatch(sysTag)) { | ||
| Map<String, String> token = TagUtils.parse(L3NetworkSystemTags.STATIC_DNS.getTagFormat(), sysTag); | ||
| String l3Uuid = token.get(L3NetworkSystemTags.STATIC_DNS_L3_UUID_TOKEN); | ||
| if (ret.get(l3Uuid) == null) { | ||
| continue; | ||
| } | ||
| String dnsStr = token.get(L3NetworkSystemTags.STATIC_DNS_TOKEN); | ||
| if (dnsStr != null && !dnsStr.isEmpty()) { | ||
| ret.get(l3Uuid).dnsAddresses = Arrays.asList(dnsStr.split(",")); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ret; | ||
|
|
@@ -222,6 +235,66 @@ public void deleteStaticIpByL3NetworkUuid(String l3Uuid) { | |
| ))); | ||
| } | ||
|
|
||
| public void setStaticDns(String vmUuid, String l3Uuid, List<String> dnsAddresses) { | ||
| if (dnsAddresses == null || dnsAddresses.isEmpty()) { | ||
| deleteStaticDnsByVmUuidAndL3Uuid(vmUuid, l3Uuid); | ||
| return; | ||
| } | ||
|
|
||
| // Validate DNS addresses | ||
| for (String dns : dnsAddresses) { | ||
| if (!NetworkUtils.isIpv4Address(dns) && !IPv6NetworkUtils.isIpv6Address(dns)) { | ||
| throw new ApiMessageInterceptionException(argerr( | ||
| "invalid DNS address[%s], must be a valid IPv4 or IPv6 address", dns)); | ||
| } | ||
| } | ||
|
|
||
| String dnsStr = String.join(",", dnsAddresses); | ||
|
|
||
| SimpleQuery<SystemTagVO> q = dbf.createQuery(SystemTagVO.class); | ||
| q.select(SystemTagVO_.uuid); | ||
| q.add(SystemTagVO_.resourceType, Op.EQ, VmInstanceVO.class.getSimpleName()); | ||
| q.add(SystemTagVO_.resourceUuid, Op.EQ, vmUuid); | ||
| q.add(SystemTagVO_.tag, Op.LIKE, TagUtils.tagPatternToSqlPattern(L3NetworkSystemTags.STATIC_DNS.instantiateTag( | ||
| map(e(L3NetworkSystemTags.STATIC_DNS_L3_UUID_TOKEN, l3Uuid)) | ||
| ))); | ||
| String tagUuid = q.findValue(); | ||
|
|
||
| if (tagUuid == null) { | ||
| SystemTagCreator creator = L3NetworkSystemTags.STATIC_DNS.newSystemTagCreator(vmUuid); | ||
| creator.setTagByTokens(map( | ||
| e(L3NetworkSystemTags.STATIC_DNS_L3_UUID_TOKEN, l3Uuid), | ||
| e(L3NetworkSystemTags.STATIC_DNS_TOKEN, dnsStr) | ||
| )); | ||
| creator.create(); | ||
| } else { | ||
| L3NetworkSystemTags.STATIC_DNS.updateByTagUuid(tagUuid, L3NetworkSystemTags.STATIC_DNS.instantiateTag(map( | ||
| e(L3NetworkSystemTags.STATIC_DNS_L3_UUID_TOKEN, l3Uuid), | ||
| e(L3NetworkSystemTags.STATIC_DNS_TOKEN, dnsStr) | ||
| ))); | ||
| } | ||
| } | ||
|
|
||
| public void deleteStaticDnsByVmUuidAndL3Uuid(String vmUuid, String l3Uuid) { | ||
| L3NetworkSystemTags.STATIC_DNS.delete(vmUuid, TagUtils.tagPatternToSqlPattern(L3NetworkSystemTags.STATIC_DNS.instantiateTag( | ||
| map(e(L3NetworkSystemTags.STATIC_DNS_L3_UUID_TOKEN, l3Uuid)) | ||
| ))); | ||
| } | ||
|
|
||
| public List<String> getStaticDnsByVmUuidAndL3Uuid(String vmUuid, String l3Uuid) { | ||
| List<Map<String, String>> tokenList = L3NetworkSystemTags.STATIC_DNS.getTokensOfTagsByResourceUuid(vmUuid); | ||
| for (Map<String, String> tokens : tokenList) { | ||
| String uuid = tokens.get(L3NetworkSystemTags.STATIC_DNS_L3_UUID_TOKEN); | ||
| if (uuid.equals(l3Uuid)) { | ||
| String dnsStr = tokens.get(L3NetworkSystemTags.STATIC_DNS_TOKEN); | ||
| if (dnsStr != null && !dnsStr.isEmpty()) { | ||
| return Arrays.asList(dnsStr.split(",")); | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
Comment on lines
+284
to
+296
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 潜在空指针异常 Line 288: 另外,Line 295 返回 🛡️ 修复建议 public List<String> getStaticDnsByVmUuidAndL3Uuid(String vmUuid, String l3Uuid) {
List<Map<String, String>> tokenList = L3NetworkSystemTags.STATIC_DNS.getTokensOfTagsByResourceUuid(vmUuid);
for (Map<String, String> tokens : tokenList) {
String uuid = tokens.get(L3NetworkSystemTags.STATIC_DNS_L3_UUID_TOKEN);
- if (uuid.equals(l3Uuid)) {
+ if (l3Uuid.equals(uuid)) {
String dnsStr = tokens.get(L3NetworkSystemTags.STATIC_DNS_TOKEN);
if (dnsStr != null && !dnsStr.isEmpty()) {
return Arrays.asList(dnsStr.split(","));
}
}
}
- return null;
+ return new ArrayList<>();
}🤖 Prompt for AI Agents |
||
|
|
||
| public Map<Integer, String> getNicStaticIpMap(List<String> nicStaticIpList) { | ||
| Map<Integer, String> nicStaticIpMap = new HashMap<>(); | ||
| if (nicStaticIpList != null) { | ||
|
|
@@ -264,6 +337,11 @@ public boolean isIpChange(String vmUuid, String l3Uuid) { | |
| } | ||
|
|
||
| public Boolean checkIpRangeConflict(VmNicVO nicVO){ | ||
| // If global config allows IP outside range, skip the conflict check | ||
| if (VmGlobalConfig.ALLOW_IP_OUTSIDE_RANGE.value(Boolean.class)) { | ||
| return Boolean.FALSE; | ||
| } | ||
|
|
||
| if (Q.New(IpRangeVO.class).eq(IpRangeVO_.l3NetworkUuid, nicVO.getL3NetworkUuid()).list().isEmpty()) { | ||
| return Boolean.FALSE; | ||
| } | ||
|
|
@@ -321,6 +399,8 @@ public void validateSystemTagInCreateMessage(APICreateMessage msg) { | |
|
|
||
| public List<String> fillUpStaticIpInfoToVmNics(Map<String, NicIpAddressInfo> staticIps) { | ||
| List<String> newSystags = new ArrayList<>(); | ||
| boolean allowOutsideRange = VmGlobalConfig.ALLOW_IP_OUTSIDE_RANGE.value(Boolean.class); | ||
|
|
||
| for (Map.Entry<String, NicIpAddressInfo> e : staticIps.entrySet()) { | ||
| String l3Uuid = e.getKey(); | ||
| NicIpAddressInfo nicIp = e.getValue(); | ||
|
|
@@ -338,11 +418,22 @@ public List<String> fillUpStaticIpInfoToVmNics(Map<String, NicIpAddressInfo> sta | |
| .eq(NormalIpRangeVO_.l3NetworkUuid, l3Uuid) | ||
| .eq(NormalIpRangeVO_.ipVersion, IPv6Constants.IPv4) | ||
| .limit(1).find(); | ||
| if (ipRangeVO == null) { | ||
|
|
||
| // Check if IP is within the range | ||
| boolean ipInRange = ipRangeVO != null && | ||
| NetworkUtils.isInRange(nicIp.ipv4Address, ipRangeVO.getStartIp(), ipRangeVO.getEndIp()); | ||
|
|
||
| if (ipRangeVO == null || (allowOutsideRange && !ipInRange)) { | ||
| // No IP range or IP is outside range with allowOutsideRange enabled | ||
| // User must provide netmask and gateway | ||
| if (StringUtils.isEmpty(nicIp.ipv4Netmask)) { | ||
| throw new ApiMessageInterceptionException(operr(ORG_ZSTACK_COMPUTE_VM_10310, "netmask must be set")); | ||
| throw new ApiMessageInterceptionException(operr(ORG_ZSTACK_COMPUTE_VM_10310, "netmask must be set for IP outside range")); | ||
| } | ||
| if (StringUtils.isEmpty(nicIp.ipv4Gateway)) { | ||
| throw new ApiMessageInterceptionException(operr(ORG_ZSTACK_COMPUTE_VM_10312, "gateway must be set for IP outside range")); | ||
| } | ||
| } else { | ||
| // IP is within range, use IpRange values or validate user-provided values | ||
| if (StringUtils.isEmpty(nicIp.ipv4Netmask)) { | ||
| newSystags.add(VmSystemTags.IPV4_NETMASK.instantiateTag( | ||
| map(e(VmSystemTags.IPV4_NETMASK_L3_UUID_TOKEN, l3Uuid), | ||
|
|
@@ -370,11 +461,22 @@ public List<String> fillUpStaticIpInfoToVmNics(Map<String, NicIpAddressInfo> sta | |
| .eq(NormalIpRangeVO_.l3NetworkUuid, l3Uuid) | ||
| .eq(NormalIpRangeVO_.ipVersion, IPv6Constants.IPv6) | ||
| .limit(1).find(); | ||
| if (ipRangeVO == null) { | ||
|
|
||
| // Check if IPv6 is within the range | ||
| boolean ipInRange = ipRangeVO != null && | ||
| IPv6NetworkUtils.isIpv6InRange(nicIp.ipv6Address, ipRangeVO.getStartIp(), ipRangeVO.getEndIp()); | ||
|
|
||
| if (ipRangeVO == null || (allowOutsideRange && !ipInRange)) { | ||
| // No IP range or IP is outside range with allowOutsideRange enabled | ||
| // User must provide prefixLen and gateway | ||
| if (StringUtils.isEmpty(nicIp.ipv6Prefix)) { | ||
| throw new ApiMessageInterceptionException(operr(ORG_ZSTACK_COMPUTE_VM_10313, "ipv6 prefix length must be set")); | ||
| throw new ApiMessageInterceptionException(operr(ORG_ZSTACK_COMPUTE_VM_10313, "ipv6 prefix length must be set for IP outside range")); | ||
| } | ||
| if (StringUtils.isEmpty(nicIp.ipv6Gateway)) { | ||
| throw new ApiMessageInterceptionException(operr(ORG_ZSTACK_COMPUTE_VM_10315, "ipv6 gateway must be set for IP outside range")); | ||
| } | ||
| } else { | ||
| // IP is within range, use IpRange values or validate user-provided values | ||
| if (StringUtils.isEmpty(nicIp.ipv6Prefix)) { | ||
| newSystags.add(VmSystemTags.IPV6_PREFIX.instantiateTag( | ||
| map(e(VmSystemTags.IPV6_PREFIX_L3_UUID_TOKEN, l3Uuid), | ||
|
|
||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| -- Add prefixLen column to UsedIpVO for IPv6 addresses outside IP range | ||
| CALL ADD_COLUMN('UsedIpVO', 'prefixLen', 'INT', 1, NULL); | ||
|
|
||
| -- Backfill prefixLen from IpRangeVO for existing IPv6 UsedIpVO records | ||
| UPDATE UsedIpVO u | ||
| INNER JOIN IpRangeVO r ON u.ipRangeUuid = r.uuid | ||
| SET u.prefixLen = r.prefixLen | ||
| WHERE u.ipVersion = 6 AND u.ipRangeUuid IS NOT NULL AND u.prefixLen IS NULL; | ||
|
|
||
| -- Modify ipRangeUuid foreign key constraint to SET NULL on delete (instead of CASCADE) | ||
| -- This allows UsedIpVO records to exist without an IpRange (for IPs outside range) | ||
| DELIMITER $$ | ||
|
|
||
| CREATE PROCEDURE ModifyUsedIpVOForeignKey() | ||
| BEGIN | ||
| DECLARE constraint_exists INT; | ||
|
|
||
| -- Check if the constraint exists | ||
| SELECT COUNT(*) | ||
| INTO constraint_exists | ||
| FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS | ||
| WHERE TABLE_SCHEMA = 'zstack' | ||
| AND TABLE_NAME = 'UsedIpVO' | ||
| AND CONSTRAINT_NAME = 'fkUsedIpVOIpRangeEO'; | ||
|
|
||
| IF constraint_exists > 0 THEN | ||
| -- Drop the existing constraint | ||
| ALTER TABLE `zstack`.`UsedIpVO` DROP FOREIGN KEY `fkUsedIpVOIpRangeEO`; | ||
|
|
||
| -- Re-create with SET NULL on delete | ||
| ALTER TABLE `zstack`.`UsedIpVO` | ||
| ADD CONSTRAINT `fkUsedIpVOIpRangeEO` | ||
| FOREIGN KEY (`ipRangeUuid`) REFERENCES `IpRangeEO`(`uuid`) ON DELETE SET NULL; | ||
| END IF; | ||
| END $$ | ||
|
|
||
| DELIMITER ; | ||
|
|
||
| CALL ModifyUsedIpVOForeignKey(); | ||
| DROP PROCEDURE IF EXISTS ModifyUsedIpVOForeignKey; |
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DNS 入参需 trim 且 IPv6 需做 tag 编码/解码
dnsAddresses 可能含空格/换行,直接校验会误判;另外 IPv6 若直接写入 system tag,含 “::” 的地址在解析时可能被截断,建议写入前编码、读取时解码。
🔧 建议修改
As per coding guidelines: 注意检查来自 Message 的参数是否做过 trim,用户可能在浏览器上复制粘贴的数据带有空格、换行符等。
Also applies to: 244-275, 289-292
🤖 Prompt for AI Agents