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 26, 2024
1 parent 67352e8 commit d99f7ea
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ void queryRollingConfig() {
assertThat(savedTaskInstanceRollingConfig.getTaskInstanceId()).isEqualTo(1L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail()).isNotNull();
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExpr()).isEqualTo("1 10% 100%");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getMode()).isEqualTo(RollingModeEnum.PAUSE_IF_FAIL.getValue());
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getMode())
.isEqualTo(RollingModeEnum.PAUSE_IF_FAIL.getValue());
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getName()).isEqualTo("config1");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getStepRollingConfigs()).hasSize(4);
Map<Long, StepRollingConfigDO> stepRollingConfigs =
Expand All @@ -82,20 +83,34 @@ void queryRollingConfig() {
assertThat(stepRollingConfigs.get(102L).isBatch()).isEqualTo(true);
assertThat(stepRollingConfigs.get(103L).isBatch()).isEqualTo(true);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly()).hasSize(3);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0).getBatch()).isEqualTo(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0).getExecuteObjectsCompatibly()).hasSize(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0).getExecuteObjectsCompatibly().get(0).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0).getExecuteObjectsCompatibly().get(0).getHost().getIp()).isEqualTo("127.0.0.1");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(1).getBatch()).isEqualTo(2);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(1).getExecuteObjectsCompatibly()).hasSize(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(1).getExecuteObjectsCompatibly().get(0).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(1).getExecuteObjectsCompatibly().get(0).getHost().getIp()).isEqualTo("127.0.0.2");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2).getBatch()).isEqualTo(3);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2).getExecuteObjectsCompatibly()).hasSize(2);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2).getExecuteObjectsCompatibly().get(0).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2).getExecuteObjectsCompatibly().get(0).getHost().getIp()).isEqualTo("127.0.0.3");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2).getExecuteObjectsCompatibly().get(1).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2).getExecuteObjectsCompatibly().get(1).getHost().getIp()).isEqualTo("127.0.0.4");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0)
.getBatch()).isEqualTo(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0)
.getExecuteObjectsCompatibly()).hasSize(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0)
.getExecuteObjectsCompatibly().get(0).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0)
.getExecuteObjectsCompatibly().get(0).getHost().getIp()).isEqualTo("127.0.0.1");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(1)
.getBatch()).isEqualTo(2);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(1)
.getExecuteObjectsCompatibly()).hasSize(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(1)
.getExecuteObjectsCompatibly().get(0).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(1)
.getExecuteObjectsCompatibly().get(0).getHost().getIp()).isEqualTo("127.0.0.2");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2)
.getBatch()).isEqualTo(3);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2)
.getExecuteObjectsCompatibly()).hasSize(2);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2)
.getExecuteObjectsCompatibly().get(0).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2)
.getExecuteObjectsCompatibly().get(0).getHost().getIp()).isEqualTo("127.0.0.3");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2)
.getExecuteObjectsCompatibly().get(1).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(2)
.getExecuteObjectsCompatibly().get(1).getHost().getIp()).isEqualTo("127.0.0.4");
}

@Test
Expand Down Expand Up @@ -139,7 +154,8 @@ void saveRollingConfig() {
assertThat(savedTaskInstanceRollingConfig.getTaskInstanceId()).isEqualTo(10L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail()).isNotNull();
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExpr()).isEqualTo("10%");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getMode()).isEqualTo(RollingModeEnum.PAUSE_IF_FAIL.getValue());
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getMode())
.isEqualTo(RollingModeEnum.PAUSE_IF_FAIL.getValue());
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getName()).isEqualTo("default");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getStepRollingConfigs()).hasSize(4);
Map<Long, StepRollingConfigDO> savedStepRollingConfigs =
Expand All @@ -149,10 +165,14 @@ void saveRollingConfig() {
assertThat(savedStepRollingConfigs.get(1002L).isBatch()).isEqualTo(true);
assertThat(savedStepRollingConfigs.get(1003L).isBatch()).isEqualTo(true);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly()).hasSize(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0).getBatch()).isEqualTo(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0).getExecuteObjectsCompatibly().get(0).getHost().getHostId()).isEqualTo(1L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0).getExecuteObjectsCompatibly().get(0).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0).getExecuteObjectsCompatibly().get(0).getHost().getIp()).isEqualTo("127.0.0.1");
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0)
.getBatch()).isEqualTo(1);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0)
.getExecuteObjectsCompatibly().get(0).getHost().getHostId()).isEqualTo(1L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0)
.getExecuteObjectsCompatibly().get(0).getHost().getBkCloudId()).isEqualTo(0L);
assertThat(savedTaskInstanceRollingConfig.getConfigDetail().getExecuteObjectsBatchListCompatibly().get(0)
.getExecuteObjectsCompatibly().get(0).getHost().getIp()).isEqualTo("127.0.0.1");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public EsbResp<EsbJobExecuteDTO> executeJob(
taskVariableDTO.setName(globalVar.getName());
if ((globalVar.getIpList() != null || globalVar.getDynamicGroupIdList() != null
|| globalVar.getTargetServer() != null) && StringUtils.isEmpty(globalVar.getValue())) {
ExecuteObjectsDTO executeObjectsDTO = convertToServersDTO(globalVar.getTargetServer(), globalVar.getIpList(),
globalVar.getDynamicGroupIdList());
ExecuteObjectsDTO executeObjectsDTO = convertToServersDTO(
globalVar.getTargetServer(), globalVar.getIpList(), globalVar.getDynamicGroupIdList());
taskVariableDTO.setTargetServers(executeObjectsDTO);
} else {
taskVariableDTO.setValue(globalVar.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public EsbResp<EsbJobInstanceStatusDTO> getJobInstanceStatusUsingPost(
TaskInstanceDTO taskInstance = taskInstanceService.getTaskInstance(username,
request.getAppResourceScope().getAppId(), request.getTaskInstanceId());

List<StepInstanceBaseDTO> stepInstances = stepInstanceService.listBaseStepInstanceByTaskInstanceId(taskInstanceId);
List<StepInstanceBaseDTO> stepInstances =
stepInstanceService.listBaseStepInstanceByTaskInstanceId(taskInstanceId);
if (stepInstances == null || stepInstances.isEmpty()) {
log.warn("Get job instance status by taskInstanceId:{}, stepInstanceList is empty!", taskInstanceId);
throw new NotFoundException(ErrorCode.STEP_INSTANCE_NOT_EXIST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public EsbResp<EsbJobInstanceStatusV3DTO> getJobInstanceStatusUsingPost(
TaskInstanceDTO taskInstance = taskInstanceService.getTaskInstance(
username, request.getAppResourceScope().getAppId(), request.getTaskInstanceId());

List<StepInstanceBaseDTO> stepInstances = stepInstanceService.listBaseStepInstanceByTaskInstanceId(taskInstanceId);
List<StepInstanceBaseDTO> stepInstances =
stepInstanceService.listBaseStepInstanceByTaskInstanceId(taskInstanceId);
if (stepInstances == null || stepInstances.isEmpty()) {
log.warn("Get job instance status by taskInstanceId:{}, stepInstanceList is empty!", taskInstanceId);
throw new NotFoundException(ErrorCode.STEP_INSTANCE_NOT_EXIST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,9 @@ private boolean shouldSkipStep() {

private void saveStatusWhenSkip() {
List<ExecuteObjectTask> notFinishedExecuteObjectTasks =
targetExecuteObjectTasks.values().stream().filter(not(ExecuteObjectTask::isFinished)).collect(Collectors.toList());
targetExecuteObjectTasks.values().stream()
.filter(not(ExecuteObjectTask::isFinished))
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(notFinishedExecuteObjectTasks)) {
notFinishedExecuteObjectTasks.forEach(executeObjectTask -> {
executeObjectTask.setStatus(ExecuteObjectTaskStatusEnum.UNKNOWN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public abstract class RollingExprPart {
* @param context 解析上下文
* @return 当前批次的执行对象
*/
public abstract List<ExecuteObject> compute(RollingExecuteObjectBatchContext context) throws RollingExprParseException;
public abstract List<ExecuteObject> compute(RollingExecuteObjectBatchContext context)
throws RollingExprParseException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ public ExecuteObject findExecuteObjectByCompositeKey(ExecuteObjectCompositeKey e
switch (compositeKeyType) {
case EXECUTE_OBJECT_ID:
return getExecuteObjectsCompatibly().stream()
.filter(executeObject -> executeObjectCompositeKey.getExecuteObjectId().equals(executeObject.getId()))
.filter(executeObject ->
executeObjectCompositeKey.getExecuteObjectId().equals(executeObject.getId()))
.findFirst()
.orElse(null);
case RESOURCE_ID:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1751,8 +1751,12 @@ username, new AppResourceScope(appId), plan.getTaskTemplateId(),
}
}

private Pair<ExecuteObjectsDTO, Set<Long>> extractNeedAuthHostsAndAccounts(StepInstanceDTO stepInstance,
Map<Long, List<String>> whiteHostAllowActions) {
private Pair<ExecuteObjectsDTO, Set<Long>> extractNeedAuthHostsAndAccounts
(
StepInstanceDTO stepInstance,
Map<Long, List<String>> whiteHostAllowActions
) {

ExecuteObjectsDTO authServers = new ExecuteObjectsDTO();
Set<Long> accountIds = new HashSet<>();
accountIds.add(stepInstance.getAccountId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,13 @@ void testSaveFileLog() {
}


private TaskExecuteObjectLog buildFileTaskHostLog(long stepInstanceId, int executeCount, String jobCreateDate, Long hostId,
String ip, String ipv6, List<FileTaskLogDoc> fileTaskLogs) {
private TaskExecuteObjectLog buildFileTaskHostLog(long stepInstanceId,
int executeCount,
String jobCreateDate,
Long hostId,
String ip,
String ipv6,
List<FileTaskLogDoc> fileTaskLogs) {
TaskExecuteObjectLog taskExecuteObjectLog = new TaskExecuteObjectLog();
taskExecuteObjectLog.setStepInstanceId(stepInstanceId);
taskExecuteObjectLog.setExecuteCount(executeCount);
Expand Down

0 comments on commit d99f7ea

Please sign in to comment.