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 Apr 2, 2024
1 parent 28acc11 commit f7259db
Show file tree
Hide file tree
Showing 13 changed files with 564 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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.validation;

import java.util.Collection;

/**
* 字段验证错误
*/
public class FieldError {

/**
* 字段校验错误类型
*/
private final ErrorType type;
/**
* 字段名
*/
private final Path field;
/**
* 校验不通过的值
*/
private final Object badValue;
/**
* 详细错误
*/
private final String detail;

private FieldError(ErrorType type, Path field, Object badValue, String detail) {
this.type = type;
this.field = field;
this.badValue = badValue;
this.detail = detail;
}

enum ErrorType {
/**
* 必填字段没有值(比如空字符、null, 空的列表)
*/
Required("Required value"),
/**
* 字段值重复
*/
Duplicate("Duplicate value"),
/**
* 不合法-通用
*/
Invalid("Invalid value"),
/**
* 无法识别的枚举值
*/
NotSupported("Unsupported value"),
/**
* 字段对应的字符超长
*/
TooLong("Too long"),
/**
* 字段对应的列表包含太多的元素
*/
TooMany("Too many"),
/**
* 表示值与该字段的 schema 定义不匹配
*/
TypeInvalid("Invalid value");

private final String value;

ErrorType(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}

public static FieldError invalid(Path field, Object value, String detail) {
return new FieldError(ErrorType.Invalid, field, value, detail);
}

public static FieldError required(Path field, String detail) {
return new FieldError(ErrorType.Required, field, "", detail);
}

public static FieldError duplicate(Path field, Object value) {
return new FieldError(ErrorType.Duplicate, field, value, "");
}

public static FieldError notSupported(Path field, Object value, Collection<String> validValues) {
String detail = "supported values: " + String.join(", ", validValues);
return new FieldError(ErrorType.NotSupported, field, value, detail);
}

public static FieldError tooLong(Path field, Object value, int maxLength) {
String detail = "must have at most " + maxLength + " chars";
return new FieldError(ErrorType.TooLong, field, value, detail);
}

public static FieldError tooMany(Path field, int actualSize, int maxSize) {
String detail = "must have at most " + maxSize + " items";
return new FieldError(ErrorType.TooMany, field, actualSize, detail);
}

public ErrorType getType() {
return type;
}

public Path getField() {
return field;
}

public Object getBadValue() {
return badValue;
}

public String getDetail() {
return detail;
}

@Override
public String toString() {
String s = field.toString();
if (type == ErrorType.Required || type == ErrorType.TooLong) {
s += ": " + type.toString();
} else {
s += ": " + type.toString() + ": " + (badValue == null ? "null" : badValue.toString());
}
if (detail != null && !detail.isEmpty()) {
s += ": " + detail;
}
return s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.validation;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class FieldErrors {
private final List<FieldError> errorList = new ArrayList<>();

public List<FieldError> getErrorList() {
return errorList;
}

public FieldErrors add(FieldError fieldError) {
if (fieldError == null) {
return this;
}
this.errorList.add(fieldError);
return this;
}

public boolean hasError() {
return CollectionUtils.isNotEmpty(errorList);
}

@Override
public String toString() {
return "errors: " + errorList.toString();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* 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.validation;

import java.util.ArrayList;
import java.util.List;

/**
* 表示对象图中从一个对象到另一个对象的导航路径, 比如 root.first.second[0].third[key]
*/
public class Path {
/**
* 字段名;当 PATH 为位置索引的时候,值为""
*/
private final String name;
/**
* 如果 name == "",这是前一个元素的下标(索引或映射键)
*/
private final String index;

/**
* 根路径
*/
private Path parent;

private Path(String name, String index, Path parent) {
this.name = name;
this.index = index;
this.parent = parent;
}

/**
* 创建一个根对象导航路径
*
* @param name 根路径名
* @param moreNames 子路径名
*/
public static Path newPath(String name, String... moreNames) {
Path r = new Path(name, null, null);
for (String moreName : moreNames) {
r = new Path(moreName, null, r);
}
return r;
}

/**
* 返回根路径
*/
public Path root() {
Path p = this;
while (p.parent != null) {
p = p.parent;
}
return p;
}

/**
* 返回父路径
*/
public Path parent() {
return this.parent;
}

/**
* 在当前路径下创建子路径
*
* @param name 路径名
* @param moreNames 子路径名列表
*/
public Path child(String name, String... moreNames) {
Path r = newPath(name, moreNames);
r.root().parent = this;
return r;
}

/**
* 在当前路径下创建索引路径(array)
*
* @param index 索引
*/
public Path index(int index) {
return new Path("", String.valueOf(index), this);
}

/**
* 在当前路径下创建映射键路径(map)
*
* @param key 映射键
*/
public Path key(String key) {
return new Path("", key, this);
}

@Override
public String toString() {

List<Path> elements = new ArrayList<>();
Path p = this;
while (p != null) {
elements.add(p);
p = p.parent;
}

StringBuilder buf = new StringBuilder();
for (int i = elements.size() - 1; i >= 0; i--) {
p = elements.get(i);
if (p.parent != null && p.name.length() > 0) {
buf.append(".");
}
if (p.name.length() > 0) {
buf.append(p.name);
} else {
buf.append("[").append(p.index).append("]");
}
}
return buf.toString();
}
}

Loading

0 comments on commit f7259db

Please sign in to comment.