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 16, 2024
1 parent 18e3a3b commit f7b6429
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.tencent.bk.job.common.annotation.PersistenceObject;
import com.tencent.bk.job.common.constant.CompatibleType;
import com.tencent.bk.job.common.constant.ExecuteObjectTypeEnum;
import com.tencent.bk.job.common.gse.util.K8sUtils;
import com.tencent.bk.job.common.gse.v2.model.Agent;
import com.tencent.bk.job.common.gse.v2.model.ExecuteObjectGseKey;
import com.tencent.bk.job.common.model.dto.Container;
Expand Down Expand Up @@ -174,9 +175,9 @@ public Agent toGseAgent() {
Agent agent = new Agent();
if (isHostExecuteObject()) {
agent.setAgentId(host.getAgentId());
} else {
} else if (isContainerExecuteObject()) {
agent.setAgentId(container.getNodeAgentId());
agent.setContainerId(container.getContainerId());
agent.setContainerId(K8sUtils.removeContainerIdType(container.getContainerId()));
}
return agent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ public LogExportJobInfoDTO packageLogFile(String username,
String logFileDir,
String logFileName,
Boolean repackage) {
if (log.isDebugEnabled()) {
log.debug("Package log file for {}|{}|{}|{}|{}|{}|{}|{}", username, appId, stepInstanceId,
executeObjectType.getValue() + ":" + executeObjectResourceId, executeCount, logFileDir, logFileName,
repackage);
}
LogExportJobInfoDTO exportJobInfo = new LogExportJobInfoDTO();
exportJobInfo.setJobKey(getExportJobKey(appId, stepInstanceId, executeObjectType, executeObjectResourceId));
exportJobInfo.setStatus(LogExportStatusEnum.INIT);
Expand Down Expand Up @@ -294,19 +289,19 @@ private void writeOneBatchExecuteObjectLogs(PrintWriter out,
StepInstanceBaseDTO stepInstance,
LogBatchQuery query,
List<ExecuteObject> executeObjects) {
List<ExecuteObjectCompositeKey> keys;
List<ExecuteObjectCompositeKey> executeObjectQueryKeys;
if (stepInstance.isSupportExecuteObjectFeature()) {
keys = executeObjects.stream()
executeObjectQueryKeys = executeObjects.stream()
.map(executeObject -> ExecuteObjectCompositeKey.ofExecuteObjectId(executeObject.getId()))
.collect(Collectors.toList());
} else {
keys = executeObjects.stream()
executeObjectQueryKeys = executeObjects.stream()
.map(executeObject -> ExecuteObjectCompositeKey.ofHostId(executeObject.getResourceId()))
.collect(Collectors.toList());
}
List<ScriptExecuteObjectLogContent> scriptExecuteObjectLogContentList =
logService.batchGetScriptExecuteObjectLogContent(jobCreateDate, stepInstance,
query.getExecuteCount(), null, keys);
query.getExecuteCount(), null, executeObjectQueryKeys);
for (ScriptExecuteObjectLogContent scriptExecuteObjectLogContent : scriptExecuteObjectLogContentList) {
if (scriptExecuteObjectLogContent != null
&& StringUtils.isNotEmpty(scriptExecuteObjectLogContent.getContent())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,7 @@ public List<ScriptExecuteObjectLogContent> batchGetScriptExecuteObjectLogContent

List<ExecuteObject> queryExecuteObjects =
stepInstanceService.findExecuteObjectByCompositeKeys(stepInstance, executeObjectCompositeKeys);
if (stepInstance.isSupportExecuteObjectFeature()) {
List<String> executeObjectId = queryExecuteObjects.stream()
.map(ExecuteObject::getId).collect(Collectors.toList());
query.setExecuteObjectIds(executeObjectId);
} else {
// 兼容 hostId 查询
List<Long> hostIds = queryExecuteObjects.stream()
.map(executeObject -> executeObject.getHost().getHostId()).collect(Collectors.toList());
query.setHostIds(hostIds);
}
setExecuteObjectCondition(query, stepInstance, queryExecuteObjects);

long stepInstanceId = stepInstance.getId();
InternalResponse<List<ServiceExecuteObjectLogDTO>> resp =
Expand All @@ -274,25 +265,46 @@ public List<ScriptExecuteObjectLogContent> batchGetScriptExecuteObjectLogContent
.collect(Collectors.toMap(ExecuteObject::getId, executeObject -> executeObject,
(oldValue, newValue) -> newValue));
return resp.getData().stream().map(logDTO -> {
String scriptContent = logDTO.getScriptLog() != null ?
logDTO.getScriptLog().getContent() : "";
String scriptContent = logDTO.getScriptLog() != null ? logDTO.getScriptLog().getContent() : "";
ExecuteObject executeObject = executeObjectMap.get(logDTO.getExecuteObjectId());
if (executeObject == null) {
log.warn("Can not find log execute object by executeObjectId : {}", logDTO.getExecuteObjectId());
}
return new ScriptExecuteObjectLogContent(logDTO.getStepInstanceId(), logDTO.getExecuteCount(),
executeObjectMap.get(logDTO.getExecuteObjectId()), scriptContent, true);
executeObject, scriptContent, true);
}).collect(Collectors.toList());
} else {
Map<Long, ExecuteObject> executeObjectMap = queryExecuteObjects.stream()
.collect(Collectors.toMap(
executeObject -> executeObject.getHost().getHostId(), executeObject -> executeObject,
(oldValue, newValue) -> newValue));
return resp.getData().stream().map(logDTO -> {
String scriptContent = logDTO.getScriptLog() != null ?
logDTO.getScriptLog().getContent() : "";
String scriptContent = logDTO.getScriptLog() != null ? logDTO.getScriptLog().getContent() : "";
ExecuteObject executeObject = executeObjectMap.get(logDTO.getHostId());
if (executeObject == null) {
log.warn("Can not find log execute object by hostId : {}", logDTO.getHostId());
}
return new ScriptExecuteObjectLogContent(logDTO.getStepInstanceId(), logDTO.getExecuteCount(),
executeObjectMap.get(logDTO.getHostId()), scriptContent, true);
executeObject, scriptContent, true);
}).collect(Collectors.toList());
}
}

private void setExecuteObjectCondition(ServiceScriptLogQueryRequest query,
StepInstanceBaseDTO stepInstance,
List<ExecuteObject> queryExecuteObjects) {
if (stepInstance.isSupportExecuteObjectFeature()) {
List<String> executeObjectId = queryExecuteObjects.stream()
.map(ExecuteObject::getId).collect(Collectors.toList());
query.setExecuteObjectIds(executeObjectId);
} else {
// 兼容 hostId 查询
List<Long> hostIds = queryExecuteObjects.stream()
.map(executeObject -> executeObject.getHost().getHostId()).collect(Collectors.toList());
query.setHostIds(hostIds);
}
}

@Override
public FileExecuteObjectLogContent getFileExecuteObjectLogContent(
StepInstanceBaseDTO stepInstance,
Expand Down

0 comments on commit f7b6429

Please sign in to comment.