Skip to content

Commit

Permalink
feature: Job 支持容器执行 - 脚本任务 TencentBlueKing#2631
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Jan 17, 2024
1 parent fd4dffb commit c6db8f0
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@
import com.tencent.bk.job.common.esb.model.EsbResp;
import com.tencent.bk.job.common.exception.InternalCmdbException;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.model.PageCondition;
import com.tencent.bk.job.common.model.PageData;
import com.tencent.bk.job.common.model.dto.ApplicationDTO;
import com.tencent.bk.job.common.model.dto.ApplicationHostDTO;
import com.tencent.bk.job.common.model.dto.HostDTO;
import com.tencent.bk.job.common.model.dto.ResourceScope;
import com.tencent.bk.job.common.model.error.ErrorType;
import com.tencent.bk.job.common.util.CollectionUtil;
import com.tencent.bk.job.common.util.FlowController;
import com.tencent.bk.job.common.util.JobContextUtil;
import com.tencent.bk.job.common.util.PageUtil;
Expand Down Expand Up @@ -1520,30 +1520,56 @@ private void setSupplierAccount(EsbReq esbReq) {
* @return 容器列表
*/
public List<ContainerDetailDTO> listKubeContainerByIds(long bizId, Collection<Long> containerIds) {
ListKubeContainerByTopoReq req = makeCmdbBaseReq(ListKubeContainerByTopoReq.class);
return listBizKubeContainerByContainerFieldWithInCondition(
bizId, ContainerFields.ID, containerIds);
}

// 查询条件
req.setBizId(bizId);
PropertyFilterDTO containerFilter = new PropertyFilterDTO();
containerFilter.setCondition("AND");
containerFilter.addRule(BaseRuleDTO.in(ContainerFields.ID, containerIds));
req.setContainerFilter(containerFilter);
/**
* 根据容器ID批量查询容器
*
* @param bizId CMDB 业务 ID
* @param containerUIds 容器 UID 集合
* @return 容器列表
*/
public List<ContainerDetailDTO> listKubeContainerByUIds(long bizId, Collection<String> containerUIds) {
return listBizKubeContainerByContainerFieldWithInCondition(
bizId, ContainerFields.CONTAINER_UID, containerUIds);
}

// 返回参数设置
req.setContainerFields(ContainerFields.FIELDS);
req.setPodFields(PodFields.FIELDS);
private <T> List<ContainerDetailDTO> listBizKubeContainerByContainerFieldWithInCondition(
long bizId,
String containerField,
Collection<T> filedValues) {

return PageUtil.queryAllWithLoopPageQuery(
500,
page -> listPageContainersByIds(req, page),
PageData::getData,
container -> container
);
}
int maxFiledValuesPerBatch = 500; // cmdb 限制每次查询传入的字段值
List<List<T>> containerFieldValueBatches =
CollectionUtil.partitionCollection(filedValues, maxFiledValuesPerBatch);

List<ContainerDetailDTO> containers = new ArrayList<>();

containerFieldValueBatches.forEach(containerFieldValueBatch -> {
ListKubeContainerByTopoReq req = makeCmdbBaseReq(ListKubeContainerByTopoReq.class);

// 查询条件
req.setBizId(bizId);
PropertyFilterDTO containerFilter = new PropertyFilterDTO();
containerFilter.setCondition("AND");
containerFilter.addRule(BaseRuleDTO.in(containerField, containerFieldValueBatch));
req.setContainerFilter(containerFilter);

// 返回参数设置
req.setContainerFields(ContainerFields.FIELDS);
req.setPodFields(PodFields.FIELDS);

// 分页设置
req.setPage(new Page(0, maxFiledValuesPerBatch));

PageData<ContainerDetailDTO> pageData = listPageKubeContainerByTopo(req);
if (CollectionUtils.isNotEmpty(pageData.getData())) {
containers.addAll(pageData.getData());
}
});

private PageData<ContainerDetailDTO> listPageContainersByIds(ListKubeContainerByTopoReq req,
PageCondition pageCondition) {
req.setPage(new Page(pageCondition.getStart(), pageCondition.getLength()));
return listPageKubeContainerByTopo(req);
return containers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ public enum CompatibleType {
*/
HISTORY_DATA,
/**
* API 参数兼容。该参数正式废弃之后可删除
* 历史业务逻辑兼容。该业务逻辑不会再次启用之后可删除
*/
API_PARAM,
HISTORY_LOGIC,
/**
* API 协议兼容。协议正式废弃之后可删除
*/
API,
/**
* 发布兼容。发布完成后可删除
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class EsbAppScopeDTO {
/**
* 兼容字段,表示cmdb 业务/业务集ID
*/
@CompatibleImplementation(name = "bizId", type = CompatibleType.API_PARAM,
@CompatibleImplementation(name = "bizId", type = CompatibleType.API,
explain = "兼容字段,表示业务ID或者业务集ID", deprecatedVersion = "3.6.x")
@JsonProperty("bk_biz_id")
@JsonPropertyDescription("bk_biz_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class EsbAppScopeReq extends EsbJobReq {
/**
* 兼容字段,表示cmdb 业务/业务集ID
*/
@CompatibleImplementation(name = "bizId", type = CompatibleType.API_PARAM,
@CompatibleImplementation(name = "bizId", type = CompatibleType.API,
explain = "兼容字段,表示业务ID或者业务集ID", deprecatedVersion = "3.6.x")
@JsonProperty("bk_biz_id")
@Min(value = 1L, message = "{validation.constraints.InvalidBkBizId.message}", groups = UseBkBizIdParam.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,13 @@ public Response<List<ContainerVO>> checkContainers(String username,
String scopeType,
String scopeId,
ContainerCheckReq req) {
ContainerQuery containerQuery =
ContainerQuery.builder()
.bizId(Long.parseLong(scopeId))
.containerUIDs(req.getUidList())
.build();

List<ContainerVO> containerVOS = listContainerVOS(containerQuery);
List<ContainerDetailDTO> containers =
containerService.listKubeContainerByUIds(Long.parseLong(scopeId), req.getUidList());

return Response.buildSuccessResp(containerVOS);
return Response.buildSuccessResp(toContainerVOS(containers));
}

private List<ContainerVO> listContainerVOS(ContainerQuery containerQuery) {
List<ContainerDetailDTO> containers = containerService.listKubeContainerByTopo(containerQuery);

private List<ContainerVO> toContainerVOS(List<ContainerDetailDTO> containers) {
if (CollectionUtils.isEmpty(containers)) {
return Collections.emptyList();
}
Expand All @@ -237,15 +230,11 @@ public Response<List<ContainerVO>> getContainerDetails(String username,
String scopeType,
String scopeId,
ContainerDetailReq req) {
ContainerQuery containerQuery =
ContainerQuery.builder()
.bizId(Long.parseLong(scopeId))
.ids(req.getContainerList().stream()
.map(ContainerIdWithMeta::getId).collect(Collectors.toList()))
.build();

List<ContainerVO> containerVOS = listContainerVOS(containerQuery);
List<ContainerDetailDTO> containers = containerService.listKubeContainerByIds(
Long.parseLong(scopeId),
req.getContainerList().stream()
.map(ContainerIdWithMeta::getId).collect(Collectors.toList()));

return Response.buildSuccessResp(containerVOS);
return Response.buildSuccessResp(toContainerVOS(containers));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,22 @@ public interface ContainerService {
*/
List<ContainerDetailDTO> listKubeContainerByTopo(ContainerQuery query);

/**
* 根据容器 ID 批量获取容器详情
*
* @param bizId 业务 ID
* @param containerIds 容器 ID 列表
* @return 容器列表
*/
List<ContainerDetailDTO> listKubeContainerByIds(Long bizId, List<Long> containerIds);

/**
* 根据容器 UId 批量获取容器详情
*
* @param bizId 业务 ID
* @param containerUIds 容器 UId 列表
* @return 容器列表
*/
List<ContainerDetailDTO> listKubeContainerByUIds(Long bizId, List<String> containerUIds);

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ public List<ContainerDetailDTO> listKubeContainerByTopo(ContainerQuery query) {
ListKubeContainerByTopoReq req = query.toListKubeContainerByTopoReq();
return bizCmdbClient.listKubeContainerByTopo(req);
}

@Override
public List<ContainerDetailDTO> listKubeContainerByIds(Long bizId, List<Long> containerIds) {
return bizCmdbClient.listKubeContainerByIds(bizId, containerIds);
}

@Override
public List<ContainerDetailDTO> listKubeContainerByUIds(Long bizId, List<String> containerUIds) {
return bizCmdbClient.listKubeContainerByUIds(bizId, containerUIds);
}
}

0 comments on commit c6db8f0

Please sign in to comment.