Skip to content

Commit

Permalink
Merge pull request #1718 from jsonwan/github_fix/dynamic_group_multi_ip
Browse files Browse the repository at this point in the history
bugfix: 动态分组拉取到的主机信息含有多个IP时接口报错 #1716
  • Loading branch information
wangyu096 authored Feb 3, 2023
2 parents 36eb109 + 9131b9e commit 922ce18
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

package com.tencent.bk.job.common.cc.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.util.ip.IpUtils;
import lombok.Data;

import java.util.List;
Expand All @@ -44,9 +46,15 @@ public class CcGroupHostPropDTO {
@JsonProperty("bk_host_name")
private String name;

// 可能为多IP
@JsonProperty("bk_host_innerip")
private String ip;
private String innerIp;

@JsonProperty("bk_cloud_id")
private List<CcCloudIdDTO> cloudIdList;

@JsonIgnore
public String getFirstIp() {
return IpUtils.getFirstIpFromMultiIp(innerIp, ",");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

package com.tencent.bk.job.common.cc.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.util.ip.IpUtils;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -37,11 +39,16 @@ public class CcHostInfoDTO {
@JsonProperty("bk_host_id")
private Long hostId;
@JsonProperty("bk_host_innerip")
private String ip;
private String innerIp;
@JsonProperty("bk_host_name")
private String hostName;
@JsonProperty("bk_os_name")
private String os;
@JsonProperty("bk_cloud_id")
private Long cloudId;

@JsonIgnore
public String getFirstIp() {
return IpUtils.getFirstIpFromMultiIp(innerIp, ",");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ private ApplicationHostDTO convertHost(long bizId, CcHostInfoDTO hostInfo) {
ApplicationHostDTO ipInfo = new ApplicationHostDTO();
ipInfo.setHostId(hostInfo.getHostId());
// 部分从cmdb同步过来的资源没有ip,需要过滤掉
if (StringUtils.isBlank(hostInfo.getIp())) {
if (StringUtils.isBlank(hostInfo.getInnerIp())) {
return null;
}

Expand All @@ -740,10 +740,12 @@ private ApplicationHostDTO convertHost(long bizId, CcHostInfoDTO hostInfo) {
return null;
}

ipInfo.setDisplayIp(hostInfo.getInnerIp());

if (queryAgentStatusClient != null) {
ipInfo.setIp(queryAgentStatusClient.getHostIpByAgentStatus(hostInfo.getIp(), hostInfo.getCloudId()));
ipInfo.setIp(queryAgentStatusClient.getHostIpByAgentStatus(hostInfo.getInnerIp(), hostInfo.getCloudId()));
} else {
ipInfo.setIp(hostInfo.getIp());
ipInfo.setIp(hostInfo.getFirstIp());
}
ipInfo.setBizId(bizId);
ipInfo.setIpDesc(hostInfo.getHostName());
Expand Down Expand Up @@ -866,13 +868,13 @@ private List<CcGroupHostPropDTO> convertToCcGroupHostPropList(List<CcHostInfoDTO
log.warn(
"host(id={},ip={}) does not have cloud area, ignore",
ccHostInfo.getHostId(),
ccHostInfo.getIp()
ccHostInfo.getInnerIp()
);
} else if (StringUtils.isBlank(ccHostInfo.getIp())) {
} else if (StringUtils.isBlank(ccHostInfo.getInnerIp())) {
log.warn(
"host(id={},ip={}) ip invalid, ignore",
ccHostInfo.getHostId(),
ccHostInfo.getIp()
ccHostInfo.getInnerIp()
);
} else {
ccGroupHostList.add(convertToCcHost(ccHostInfo));
Expand Down Expand Up @@ -926,7 +928,7 @@ private CcGroupHostPropDTO convertToCcHost(CcHostInfoDTO ccHostInfo) {
CcGroupHostPropDTO ccGroupHostPropDTO = new CcGroupHostPropDTO();
ccGroupHostPropDTO.setId(ccHostInfo.getHostId());
ccGroupHostPropDTO.setName(ccHostInfo.getHostName());
ccGroupHostPropDTO.setIp(ccHostInfo.getIp());
ccGroupHostPropDTO.setInnerIp(ccHostInfo.getInnerIp());
CcCloudIdDTO ccCloudIdDTO = new CcCloudIdDTO();
// 仅使用CloudId其余属性未用到,暂不设置
ccCloudIdDTO.setInstanceId(ccHostInfo.getCloudId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -51,9 +57,11 @@ public class IpUtils {
*/
public static final long DEFAULT_CLOUD_ID = 0;
private static final Pattern IP_PATTERN = Pattern.compile(
"\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
"\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.(" +
"(?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
private static final Pattern pattern = Pattern.compile(
"\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
"\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.(" +
"(?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");

/**
* 校验云区域:服务器IP
Expand Down Expand Up @@ -275,4 +283,21 @@ public static String getFirstMachineIP() {
}
return ip;
}

/**
* 从含有多个IP的字符串中获取首个IP,若multiIp不含有任何IP则直接返回multiIp本身
*
* @param multiIp 多IP字符串
* @param separator 分隔符
* @return 首个IP
*/
public static String getFirstIpFromMultiIp(String multiIp, String separator) {
if (StringUtils.isBlank(multiIp)) {
return multiIp;
}
if (multiIp.contains(separator)) {
return multiIp.split(separator)[0];
}
return multiIp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ void testLongStr2Ip() {
String ip = IpUtils.revertIpFromLongStr(ipLongStr);
assertThat(ip).isEqualTo("127.0.0.1");
}

@Test
void testGetFirstIpFromMultiIp() {
assertThat(IpUtils.getFirstIpFromMultiIp(null, ",")).isNull();
assertThat(IpUtils.getFirstIpFromMultiIp("", ",")).isEqualTo("");
assertThat(IpUtils.getFirstIpFromMultiIp("192.168.1.1", ",")).isEqualTo("192.168.1.1");
assertThat(IpUtils.getFirstIpFromMultiIp("192.168.1.1,", ",")).isEqualTo("192.168.1.1");
assertThat(IpUtils.getFirstIpFromMultiIp("192.168.1.1,192.168.1.2", ",")).isEqualTo("192.168.1.1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,16 @@ public List<IpDTO> getIpByDynamicGroupId(long appId, String groupId) {
List<CcCloudIdDTO> hostCloudIdList = hostProp.getCloudIdList();
if (hostCloudIdList == null || hostCloudIdList.isEmpty()) {
log.warn("Get ip by dynamic group id, cmdb return illegal host, skip it!appId={}, groupId={}, " +
"hostIp={}", appId, groupId, hostProp.getIp());
"hostIp={}", appId, groupId, hostProp.getInnerIp());
continue;
}
CcCloudIdDTO hostCloudId = hostCloudIdList.get(0);
if (hostCloudId == null) {
log.warn("Get ip by dynamic group id, cmdb return illegal host, skip it!appId={}, groupId={}, " +
"hostIp={}", appId, groupId, hostProp.getIp());
"hostIp={}", appId, groupId, hostProp.getInnerIp());
continue;
}
IpDTO ip = new IpDTO(hostCloudId.getInstanceId(), hostProp.getIp());
IpDTO ip = new IpDTO(hostCloudId.getInstanceId(), hostProp.getFirstIp());
ips.add(ip);
}
log.info("Get hosts by groupId, appId={}, groupId={}, hosts={}", appId, groupId, ips);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public List<DynamicGroupWithHost> getBizDynamicGroupHostList(String username,
List<String> cloudIpList = new ArrayList<>();
for (CcGroupHostPropDTO groupHost : ccGroupHostProps) {
if (CollectionUtils.isNotEmpty(groupHost.getCloudIdList())) {
cloudIpList.add(groupHost.getCloudIdList().get(0).getInstanceId() + ":" + groupHost.getIp());
cloudIpList.add(groupHost.getCloudIdList().get(0).getInstanceId() + ":" + groupHost.getFirstIp());
} else {
log.warn("Wrong host info! No cloud area!|{}", groupHost);
}
Expand Down

0 comments on commit 922ce18

Please sign in to comment.