Skip to content

Commit

Permalink
Merge pull request #3285 from TencentBlueKing/master
Browse files Browse the repository at this point in the history
Merge branch master into 3.10.x
  • Loading branch information
jsonwan authored Nov 4, 2024
2 parents c6f63c1 + a44c484 commit 5c94463
Show file tree
Hide file tree
Showing 63 changed files with 1,450 additions and 670 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public Container toContainer() {
}
if (topo != null) {
container.setNodeHostId(topo.getHostId());
container.setClusterId(topo.getClusterId());
container.setNamespaceId(topo.getNamespaceId());
container.setWorkloadType(topo.getWorkloadType());
container.setWorkloadId(topo.getWorkloadId());
}
return container;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,72 @@ public class Container implements Cloneable {
@JsonProperty("nodeHostId")
private Long nodeHostId;

/**
* node 主机 ip
*/
@JsonProperty("nodeIp")
private String nodeIp;

/**
* 容器所在 Node 对应的 Agent ID
*/
@JsonProperty("nodeAgentId")
private String nodeAgentId;

/**
* 容器所在集群 ID
* cluster在cmdb中的唯一ID
*/
@JsonProperty("clusterId")
private String clusterId;
private Long clusterId;

/**
* 集群 ID
*/
@JsonProperty("clusterUID")
private String clusterUID;

/**
* 集群名称
*/
@JsonProperty("clusterName")
private String clusterName;

/**
* 容器所在命名空间
* namespace在cmdb中的唯一ID
*/
@JsonProperty("namespaceId")
private Long namespaceId;

/**
* 命名空间名称
*/
@JsonProperty("namespace")
private String namespace;

/**
* 容器所在 POD 名称
* POD 名称
*/
@JsonProperty("podName")
private String podName;

/**
* 容器所在 POD 名称
* pod labels
*/
@JsonProperty("podLabels")
private Map<String, String> podLabels;

/**
* workload 类型(Deployment/Job ...)
*/
@JsonProperty("workloadType")
private String workloadType;

/**
* workload在cmdb中的唯一ID
*/
@JsonProperty("workloadId")
private Long workloadId;

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -120,13 +156,19 @@ public Container clone() {
Container clone = new Container();
clone.setId(id);
clone.setNodeHostId(nodeHostId);
clone.setNodeIp(nodeIp);
clone.setNodeAgentId(nodeAgentId);
clone.setContainerId(containerId);
clone.setPodLabels(podLabels);
clone.setClusterId(clusterId);
clone.setClusterUID(clusterUID);
clone.setClusterName(clusterName);
clone.setNamespaceId(namespaceId);
clone.setNamespace(namespace);
clone.setPodName(podName);
clone.setName(name);
clone.setWorkloadType(workloadType);
clone.setWorkloadId(workloadId);
return clone;
}

Expand All @@ -136,8 +178,15 @@ public ContainerVO toContainerVO() {
vo.setName(name);
vo.setUid(containerId);
vo.setNodeHostId(nodeHostId);
vo.setNodeIp(nodeIp);
vo.setPodName(podName);
vo.setPodLabels(podLabels);
vo.setClusterId(clusterId);
vo.setClusterUID(clusterUID);
vo.setClusterName(clusterName);
vo.setNamespaceId(namespaceId);
vo.setNamespace(namespace);
vo.setWorkloadType(workloadType);
return vo;
}

Expand All @@ -154,8 +203,14 @@ public void updatePropsByContainer(Container container) {
this.containerId = container.getContainerId();
this.nodeHostId = container.getNodeHostId();
this.nodeAgentId = container.getNodeAgentId();
this.nodeIp = container.getNodeIp();
this.clusterId = container.getClusterId();
this.clusterUID = container.getClusterUID();
this.clusterName = container.getClusterName();
this.namespaceId = container.getNamespaceId();
this.namespace = container.getNamespace();
this.workloadType = container.getWorkloadType();
this.workloadId = container.getWorkloadId();
this.podName = container.getPodName();
this.podLabels = container.getPodLabels();
this.name = container.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,22 @@ public class ContainerVO {

@ApiModelProperty("所属 Node GSE agent 状态")
private String nodeAgentStatus;

@ApiModelProperty("cluster在cmdb中的唯一ID")
private Long clusterId;

@ApiModelProperty(value = "集群 ID", example = "BCS-K8S-00000")
private String clusterUID;

@ApiModelProperty("集群名称")
private String clusterName;

@ApiModelProperty("命名空间在 cmdb 的唯一 ID")
private Long namespaceId;

@ApiModelProperty("命名空间名称")
private String namespace;

@ApiModelProperty("workload 类型")
private String workloadType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private void fillNodeHostInfo(long appId, List<Container> containers) {
return;
}
container.setNodeAgentId(nodeHost.getAgentId());
container.setNodeIp(nodeHost.getPrimaryIp());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

package com.tencent.bk.job.execute.service.impl;

import com.tencent.bk.job.common.cc.model.container.KubeClusterDTO;
import com.tencent.bk.job.common.cc.model.container.KubeNamespaceDTO;
import com.tencent.bk.job.common.cc.model.query.KubeClusterQuery;
import com.tencent.bk.job.common.cc.model.query.NamespaceQuery;
import com.tencent.bk.job.common.cc.sdk.BizCmdbClient;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.constant.TaskVariableTypeEnum;
import com.tencent.bk.job.common.exception.FailedPreconditionException;
Expand Down Expand Up @@ -102,21 +107,24 @@ public class TaskInstanceExecuteObjectProcessor {

private final MeterRegistry meterRegistry;

private final BizCmdbClient bizCmdbClient;

public TaskInstanceExecuteObjectProcessor(HostService hostService,
ApplicationService applicationService,
ContainerService containerService,
AppScopeMappingService appScopeMappingService,
WhiteHostCache whiteHostCache,
@Qualifier(DefaultBeanNames.PREFER_V2_AGENT_STATE_CLIENT)
AgentStateClient preferV2AgentStateClient,
MeterRegistry meterRegistry) {
MeterRegistry meterRegistry, BizCmdbClient bizCmdbClient) {
this.hostService = hostService;
this.applicationService = applicationService;
this.containerService = containerService;
this.appScopeMappingService = appScopeMappingService;
this.whiteHostCache = whiteHostCache;
this.preferV2AgentStateClient = preferV2AgentStateClient;
this.meterRegistry = meterRegistry;
this.bizCmdbClient = bizCmdbClient;
}

/**
Expand Down Expand Up @@ -627,7 +635,7 @@ private void acquireAndSetContainers(TaskInstanceExecuteObjects taskInstanceExec
TaskInstanceDTO taskInstance,
List<StepInstanceDTO> stepInstances) {

// // 根据静态容器列表方式获取并设置容器执行对象
// 根据静态容器列表方式获取并设置容器执行对象
acquireAndSetContainersByStaticContainerList(taskInstanceExecuteObjects,
taskInstance, stepInstances);

Expand All @@ -637,6 +645,62 @@ private void acquireAndSetContainers(TaskInstanceExecuteObjects taskInstanceExec

taskInstanceExecuteObjects.setContainsAnyContainer(
CollectionUtils.isNotEmpty(taskInstanceExecuteObjects.getValidContainers()));

// 增加容器 topo 信息(集群 UID,集群名称、命名空间名称等)
fillContainerTopoInfo(taskInstance.getAppId(), taskInstanceExecuteObjects.getValidContainers(), stepInstances);
}

private void fillContainerTopoInfo(long appId,
Collection<Container> containers,
List<StepInstanceDTO> stepInstances) {
if (CollectionUtils.isEmpty(containers)) {
return;
}
long bizId = Long.parseLong(appScopeMappingService.getScopeByAppId(appId).getId());
// 从 cmdb 获取集群信息
List<Long> ccKubeClusterIds =
containers.stream().map(Container::getClusterId).distinct().collect(Collectors.toList());
List<KubeClusterDTO> clusters =
bizCmdbClient.listKubeClusters(KubeClusterQuery.Builder.builder(bizId).ids(ccKubeClusterIds).build());
Map<Long, KubeClusterDTO> clusterMap = clusters.stream().collect(
Collectors.toMap(KubeClusterDTO::getId, cluster -> cluster));

// 从 cmdb 获取命名空间信息
List<Long> ccKubeNamespaceIds =
containers.stream().map(Container::getNamespaceId).distinct().collect(Collectors.toList());
List<KubeNamespaceDTO> namespaces =
bizCmdbClient.listKubeNamespaces(NamespaceQuery.Builder.builder(bizId).ids(ccKubeNamespaceIds).build());
Map<Long, KubeNamespaceDTO> namespaceMap = namespaces.stream().collect(
Collectors.toMap(KubeNamespaceDTO::getId, namespace -> namespace));

// 填充 cluster、 namespace 信息
for (StepInstanceDTO stepInstance : stepInstances) {
stepInstance.forEachExecuteObjects(executeObjects -> {
if (CollectionUtils.isNotEmpty(executeObjects.getContainerFilters())) {
executeObjects.getContainerFilters().forEach(containerFilter -> {
if (CollectionUtils.isNotEmpty(containerFilter.getContainers())) {
containerFilter.getContainers().forEach(
container -> addTopoDetail(container, clusterMap, namespaceMap));
}
});
}
if (CollectionUtils.isNotEmpty(executeObjects.getStaticContainerList())) {
executeObjects.getStaticContainerList().forEach(
container -> addTopoDetail(container, clusterMap, namespaceMap));
}
});
}
}

private void addTopoDetail(Container container,
Map<Long, KubeClusterDTO> clusterMap,
Map<Long, KubeNamespaceDTO> namespaceMap) {
KubeClusterDTO cluster = clusterMap.get(container.getClusterId());
container.setClusterName(cluster.getName());
container.setClusterUID(cluster.getUid());

KubeNamespaceDTO namespace = namespaceMap.get(container.getNamespaceId());
container.setNamespace(namespace.getName());
}

private void acquireAndSetContainersByStaticContainerList(TaskInstanceExecuteObjects taskInstanceExecuteObjects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.tencent.bk.job.common.model.dto.AppResourceScope;
import com.tencent.bk.job.manage.model.web.request.TaskPlanCreateUpdateReq;
import com.tencent.bk.job.manage.model.web.request.TaskVariableValueUpdateReq;
import com.tencent.bk.job.manage.model.web.vo.task.TaskPlanBasicInfoVO;
import com.tencent.bk.job.manage.model.web.vo.task.TaskPlanSyncInfoVO;
import com.tencent.bk.job.manage.model.web.vo.task.TaskPlanVO;
import io.swagger.annotations.Api;
Expand Down Expand Up @@ -273,6 +274,26 @@ Response<List<TaskPlanVO>> listPlanBasicInfoByIds(
String planIds
);

@ApiOperation(value = "根据执行方案 ID 批量拉执行方案基础信息", produces = "application/json")
@GetMapping(value = {"/scope/{scopeType}/{scopeId}/task/plan/basicInfo"})
Response<List<TaskPlanBasicInfoVO>> listTaskPlanBasicInfoByIds(
@ApiParam(value = "用户名,网关自动传入")
@RequestHeader("username")
String username,
@ApiIgnore
@RequestAttribute(value = "appResourceScope")
AppResourceScope appResourceScope,
@ApiParam(value = "资源范围类型", required = true)
@PathVariable(value = "scopeType")
String scopeType,
@ApiParam(value = "资源范围ID", required = true)
@PathVariable(value = "scopeId")
String scopeId,
@ApiParam(value = "执行方案 ID 列表,逗号分隔", required = true, example = "1,2,3")
@QueryParam("ids")
String planIds
);

@ApiOperation(value = "检查执行方案名称是否已占用", produces = "application/json")
@GetMapping("/scope/{scopeType}/{scopeId}/task/plan/{templateId}/{planId}/check_name")
Response<Boolean> checkPlanName(
Expand Down
Loading

0 comments on commit 5c94463

Please sign in to comment.