Skip to content

Commit

Permalink
bugfix: 动态分组拉取到的主机信息含有多个IP时接口报错 TencentBlueKing#1716
Browse files Browse the repository at this point in the history
明确区分展示用多IP与单个IP,消除“ip”字段的混淆使用
  • Loading branch information
jsonwan committed Feb 3, 2023
1 parent 5c721c1 commit 3b0a481
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 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 @@ -693,7 +693,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 @@ -709,10 +709,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 @@ -832,13 +834,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 @@ -892,7 +894,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 @@ -248,4 +248,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 @@ -211,16 +211,16 @@ public List<HostDTO> 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;
}
HostDTO ip = new HostDTO(hostCloudId.getInstanceId(), hostProp.getIp());
HostDTO ip = new HostDTO(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 @@ -530,7 +530,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 3b0a481

Please sign in to comment.