Skip to content

Commit

Permalink
feat: 容器执行支持 label selector 表达式 TencentBlueKing#2858
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Mar 29, 2024
1 parent 7b20af8 commit f233cc6
Show file tree
Hide file tree
Showing 28 changed files with 1,524 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ public static BaseRuleDTO in(String field, Object value) {
return new BaseRuleDTO(field, RuleOperatorEnum.IN.getOperator(), value);
}

public static BaseRuleDTO notIn(String field, Object value) {
return new BaseRuleDTO(field, RuleOperatorEnum.NOT_IN.getOperator(), value);
}

public static BaseRuleDTO equals(String field, Object value) {
return new BaseRuleDTO(field, RuleOperatorEnum.EQUAL.getOperator(), value);
}

public static BaseRuleDTO notEquals(String field, Object value) {
return new BaseRuleDTO(field, RuleOperatorEnum.NOT_EQUAL.getOperator(), value);
}

public static BaseRuleDTO contains(String field, Object value) {
return new BaseRuleDTO(field, RuleOperatorEnum.CONTAINS.getOperator(), value);
}
Expand All @@ -73,5 +81,20 @@ public static BaseRuleDTO filterObject(String field, Object value) {
return new BaseRuleDTO(field, RuleOperatorEnum.FILTER_OBJECT.getOperator(), value);
}

public static BaseRuleDTO exists(String field) {
return new BaseRuleDTO(field, RuleOperatorEnum.EXIST.getOperator(), null);
}

public static BaseRuleDTO notExists(String field) {
return new BaseRuleDTO(field, RuleOperatorEnum.NOT_EXIST.getOperator(), null);
}

public static BaseRuleDTO greaterThan(String field, Object value) {
return new BaseRuleDTO(field, RuleOperatorEnum.GREATER.getOperator(), value);
}

public static BaseRuleDTO lessThan(String field, Object value) {
return new BaseRuleDTO(field, RuleOperatorEnum.LESS.getOperator(), value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
1244028=步骤 [{0}] 的目标执行对象为空
1244029=步骤 [{0}] 的源文件执行对象为空
1244030=作业引用的执行对象不存在。不存在的执行对象个数:{0},执行对象列表[{1}]
1244031=Label Selector 不合法

## 业务错误-定时任务(job-crontab)
1245008=删除定时任务失败
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
1244028=Step [{0}] target execute object is empty
1244029=Step [{0}] source execute object is empty
1244030=Execute object referenced by the job does not exist. Number of non-existent execution objects: {0}, execution object list: [{1}]
1244031=Invalid Label Selector

## Business error - job-crontab
1245008=Failed to delete cron
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
1244028=Step [{0}] target execute object is empty
1244029=Step [{0}] source execute object is empty
1244030=Execute object referenced by the job does not exist. Number of non-existent execution objects: {0}, execution object list: [{1}]
1244031=Invalid Label Selector

## Business error - job-crontab
1245008=Failed to delete cron
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
1244028=步骤 [{0}] 的目标执行对象为空
1244029=步骤 [{0}] 的源文件执行对象为空
1244030=作业引用的执行对象不存在。不存在的执行对象个数:{0},执行对象列表[{1}]
1244031=Label Selector 不合法

## 业务错误-定时任务(job-crontab)
1245008=删除定时任务失败
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
1244028=步骤 [{0}] 的目标执行对象为空
1244029=步骤 [{0}] 的源文件执行对象为空
1244030=作业引用的执行对象不存在。不存在的执行对象个数:{0},执行对象列表[{1}]
1244031=Label Selector 不合法

## 业务错误-定时任务(job-crontab)
1245008=删除定时任务失败
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public class ErrorCode {
public static final int STEP_SOURCE_EXECUTE_OBJECT_EMPTY = 1244029;
// 执行对象不存在。无效的{0}个执行对象:[{1}]
public static final int EXECUTE_OBJECT_NOT_EXIST = 1244030;
public static final int INVALID_LABEL_SELECTOR = 1244031;
// 作业执行 end

// 定时作业 start
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.constant;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* Label selector 操作符
*/
public enum LabelSelectorOperatorEnum {
NOT_EXISTS("not_exists"),
EQUALS("equals"),
IN("in"),
NOT_EQUALS("not_equals"),
NOT_IN("not_in"),
EXISTS("exists"),
GREATER_THAN("gt"),
LESS_THAN("lt");

private final String op;

LabelSelectorOperatorEnum(String op) {
this.op = op;
}

public String getOp() {
return op;
}

@JsonValue
public static LabelSelectorOperatorEnum valOf(String op) {
for (LabelSelectorOperatorEnum operatorEnum : values()) {
if (operatorEnum.getOp().equals(op)) {
return operatorEnum;
}
}
throw new IllegalArgumentException("No LabelSelectorOperatorEnum constant: " + op);
}

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static LabelSelectorOperatorEnum forOp(String op) {
return LabelSelectorOperatorEnum.valOf(op);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ public class OpenApiKubePodFilterDTO {
*/
@JsonProperty("label_selector")
private List<OpenApiLabelSelectExprDTO> labelSelector;

@JsonProperty("label_selector_expr")
private String labelSelectorExpr;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.tencent.bk.job.common.model.openapi.v4;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.constant.LabelSelectorOperatorEnum;
import lombok.Data;

import java.util.List;
Expand All @@ -43,7 +44,7 @@ public class OpenApiLabelSelectExprDTO {
/**
* 计算操作符
*/
private String operator;
private LabelSelectorOperatorEnum operator;

/**
* Label value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.tencent.bk.job.common.annotation.CompatibleImplementation;
import com.tencent.bk.job.common.constant.CompatibleType;
import com.tencent.bk.job.common.util.json.DecimalFormatJsonSerializer;
import com.tencent.bk.job.common.util.json.LongTimestampSerializer;
import io.swagger.annotations.ApiModel;
Expand All @@ -40,8 +42,14 @@
public class StepExecutionVO {
@ApiModelProperty("步骤实例ID")
private Long stepInstanceId;
@ApiModelProperty("执行次数,默认为0")

@ApiModelProperty(value = "重试次数", hidden = true)
@CompatibleImplementation(name = "execute_object", deprecatedVersion = "3.9.x", type = CompatibleType.DEPLOY,
explain = "使用 executeCount 参数替换。发布完成后可以删除")
private Integer retryCount;

@ApiModelProperty("执行次数,默认为0")
private Integer executeCount;
@ApiModelProperty("步骤名称")
private String name;
@ApiModelProperty("步骤类型,1-脚本,2-文件,3-人工确认")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package com.tencent.bk.job.execute.model.web.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.tencent.bk.job.common.annotation.CompatibleImplementation;
import com.tencent.bk.job.common.constant.CompatibleType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
Expand All @@ -38,12 +40,19 @@
public class StepOperationVO {
@ApiModelProperty("步骤实例ID")
private Long stepInstanceId;
@ApiModelProperty("执行次数")

@ApiModelProperty(value = "重试次数", hidden = true)
@CompatibleImplementation(name = "execute_object", deprecatedVersion = "3.9.x", type = CompatibleType.DEPLOY,
explain = "使用 executeCount 参数替换。发布完成后可以删除")
private Integer retryCount;

public StepOperationVO(Long stepInstanceId, Integer retryCount) {
@ApiModelProperty("执行次数")
private Integer executeCount;

public StepOperationVO(Long stepInstanceId, Integer executeCount) {
this.stepInstanceId = stepInstanceId;
this.retryCount = retryCount;
this.retryCount = executeCount;
this.executeCount = executeCount;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ private TaskExecuteResultVO convertToTaskExecuteResultVO(TaskExecuteResultDTO ta
StepExecutionVO stepExecutionVO = new StepExecutionVO();
stepExecutionVO.setName(stepExecutionDTO.getName());
stepExecutionVO.setRetryCount(stepExecutionDTO.getExecuteCount());
stepExecutionVO.setExecuteCount(stepExecutionDTO.getExecuteCount());
stepExecutionVO.setStepInstanceId(stepExecutionDTO.getStepInstanceId());
stepExecutionVO.setStartTime(stepExecutionDTO.getStartTime());
stepExecutionVO.setEndTime(stepExecutionDTO.getEndTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.tencent.bk.job.common.annotation.PersistenceObject;
import com.tencent.bk.job.common.cc.model.container.LabelSelectExprDTO;
import com.tencent.bk.job.common.esb.model.job.EsbIpDTO;
import com.tencent.bk.job.common.esb.model.job.v3.EsbServerV3DTO;
import com.tencent.bk.job.common.gse.util.AgentUtils;
Expand All @@ -42,8 +41,10 @@
import com.tencent.bk.job.common.model.vo.TaskHostNodeVO;
import com.tencent.bk.job.common.model.vo.TaskTargetVO;
import com.tencent.bk.job.execute.engine.model.ExecuteObject;
import com.tencent.bk.job.execute.util.label.selector.LabelSelectorParse;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -579,10 +580,14 @@ private static List<KubeContainerFilter> convertToKubeContainerFilter(OpenApiExe
.map(labelSelectExpr -> new LabelSelectExprDTO(
labelSelectExpr.getKey(),
labelSelectExpr.getOperator(),
labelSelectExpr.getValue(),
labelSelectExpr.getValues()))
.collect(Collectors.toList()));

} else if (StringUtils.isNotBlank(originContainerFilter.getPodFilter().getLabelSelectorExpr())) {
// 优先解析 label_selector;如果 label_selector 不存在,那么解析 label_selector_expr
List<LabelSelectExprDTO> labelSelectExprList = LabelSelectorParse.parseToLabelSelectExprList(
originContainerFilter.getPodFilter().getLabelSelectorExpr());
podFilter.setLabelSelector(labelSelectExprList);
}
containerFilter.setPodFilter(podFilter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

package com.tencent.bk.job.execute.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.annotation.PersistenceObject;
import com.tencent.bk.job.common.cc.model.container.LabelSelectExprDTO;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

Expand All @@ -48,9 +46,13 @@ public class KubePodFilter implements Cloneable {
/**
* label selector
*/
@JsonProperty("label_selector")
private List<LabelSelectExprDTO> labelSelector;

/**
* pod label selector expression
*/
private String labelSelectorExpr;

@Override
public KubePodFilter clone() {
KubePodFilter clone = new KubePodFilter();
Expand All @@ -62,6 +64,7 @@ public KubePodFilter clone() {
labelSelector.forEach(labelSelectExpr -> cloneLabelSelectExprList.add(labelSelectExpr.clone()));
clone.setLabelSelector(cloneLabelSelectExprList);
}
clone.setLabelSelectorExpr(labelSelectorExpr);
return clone;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.cc.model.container;
package com.tencent.bk.job.execute.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.annotation.PersistenceObject;
import com.tencent.bk.job.common.constant.LabelSelectorOperatorEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -43,38 +43,28 @@ public class LabelSelectExprDTO implements Cloneable {
/**
* Label key
*/
@JsonProperty("label_key")
private String key;

/**
* 计算操作符
*/
private String operator;

/**
* Label value
*/
@JsonProperty("label_value")
private String value;
**/
private LabelSelectorOperatorEnum operator;

/**
* Label values
*/
@JsonProperty("label_values")
private List<String> values;

public LabelSelectExprDTO(String key, String operator, String value, List<String> values) {
public LabelSelectExprDTO(String key, LabelSelectorOperatorEnum operator, List<String> values) {
this.key = key;
this.operator = operator;
this.value = value;
this.values = values;
}

@Override
public LabelSelectExprDTO clone() {
LabelSelectExprDTO clone = new LabelSelectExprDTO();
clone.setKey(key);
clone.setValue(value);
clone.setOperator(operator);
if (CollectionUtils.isNotEmpty(values)) {
clone.setValues(new ArrayList<>(values));
Expand Down
Loading

0 comments on commit f233cc6

Please sign in to comment.