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 Dec 29, 2023
1 parent 54b04c8 commit 5d6b4fb
Show file tree
Hide file tree
Showing 17 changed files with 471 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ public class BaseRuleDTO implements IRule {
* 操作值,不同的operator对应不同的value格式
*/
private Object value;

public BaseRuleDTO() {
}

public BaseRuleDTO(String field, String operator, Object value) {
this.field = field;
this.operator = operator;
this.value = value;
}

public static BaseRuleDTO in(String field, Object value) {
return new BaseRuleDTO(field, "in", value);
}

public static BaseRuleDTO equals(String field, Object value) {
return new BaseRuleDTO(field, "equals", value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class KubeNode {
public class KubeNodeDTO {
/**
* 节点的资源类型
*/
Expand All @@ -59,13 +59,13 @@ public class KubeNode {
* 节点下的子节点信息
*/
@JsonProperty("nds")
private List<KubeNode> nodes;
private List<KubeNodeDTO> nodes;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
KubeNode kubeNode = (KubeNode) o;
KubeNodeDTO kubeNode = (KubeNodeDTO) o;
return kind.equals(kubeNode.kind) &&
id.equals(kubeNode.id);
}
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.cc.model.container;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
* 容器拓扑节点ID
*/
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class KubeNodeID {
/**
* 节点的资源类型
*/
private String kind;
/**
* 节点 ID
*/
private Long id;

public KubeNodeID(String kind, Long id) {
this.kind = kind;
this.id = id;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
KubeNodeID kubeNode = (KubeNodeID) o;
return kind.equals(kubeNode.kind) &&
id.equals(kubeNode.id);
}

@Override
public int hashCode() {
return Objects.hash(kind, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ public class KubeTopologyDTO {
private KubeBizDTO biz;

@JsonProperty("nds")
private List<KubeNode> nds;
private List<KubeNodeDTO> nodes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.cc.model.PropertyFilterDTO;
import com.tencent.bk.job.common.cc.model.container.KubeNodeID;
import com.tencent.bk.job.common.esb.model.EsbReq;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -43,8 +44,8 @@ public class ListKubeContainerByTopoReq extends EsbReq {
@JsonProperty("bk_biz_id")
private Long bizId;

@JsonProperty("bk_cluster_id")
private Long clusterId;
@JsonProperty("bk_nodes")
private List<KubeNodeID> nodeIdList;

@JsonProperty("bk_namespace_id")
private Long namespaceId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ public Page(Integer start, Integer limit) {
this.start = start;
this.limit = limit;
}

public Page(Integer start, Integer limit, String sort) {
this.start = start;
this.limit = limit;
this.sort = sort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @description
* @date 2019/3/4
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
package com.tencent.bk.job.common.util;

import com.tencent.bk.job.common.model.PageData;
import com.tencent.bk.job.common.model.dto.PageDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -233,4 +236,45 @@ public static <T, R> PageData<R> copyPageWithNewData(PageData<T> srcPageData, Li
targetPageData.setData(newDataList);
return targetPageData;
}

/**
* 查询全量 - 通过循环分页查询
*
* @param pageLimit 每页限制大小
* @param pageQuery 查询操作
* @param extractCountFunction 从分页查询结果提取总数
* @param extractElementsFunction 从分页查询结果提取返回的对象列表
* @param elementConverter 分页查询对象转换为最终对象
* @param <T1> 分页查询返回的对象
* @param <T2> 转换之后作为方法返回值的对象
* @param <R> 分页查询结果
* @return 全量对象列表
*/
public static <T1, T2, R> List<T2> queryAllWithLoopPageQuery(int pageLimit,
Function<PageDTO, R> pageQuery,
Function<R, Integer> extractCountFunction,
Function<R, Collection<T1>> extractElementsFunction,
Function<T1, T2> elementConverter) {
int start = 0;
int total;
List<T2> elements = new ArrayList<>();
do {
PageDTO page = new PageDTO(start, pageLimit, "");
R result = pageQuery.apply(page);

Collection<T1> originElements = extractElementsFunction.apply(result);
if (CollectionUtils.isEmpty(originElements)) {
break;
}
elements.addAll(originElements.stream()
.map(elementConverter)
.filter(Objects::nonNull)
.collect(Collectors.toList()));

total = extractCountFunction.apply(result);
start += pageLimit;
} while (start < total);

return elements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@
* 容器管理 WEB API
*/
@Api(tags = "job-manage:web:Container_Management")
@RequestMapping("/web")
@RequestMapping("/web/scope/{scopeType}/{scopeId}/")
@RestController
@WebAPI
public interface WebContainerResource {

// 容器选择器标准接口-1
@ApiOperation(value = "获取容器拓扑树(含各节点容器数)", produces = "application/json")
@PostMapping(value = {"/scope/{scopeType}/{scopeId}/topology/container"})
@PostMapping(value = {"/topology/container"})
Response<List<ContainerTopologyNodeVO>> listTopologyTrees(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
Expand All @@ -81,7 +81,7 @@ Response<List<ContainerTopologyNodeVO>> listTopologyTrees(

// 容器选择器标准接口-2
@ApiOperation(value = "容器选择器根据拓扑节点集合获取容器列表", produces = "application/json")
@PostMapping(value = {"/scope/{scopeType}/{scopeId}/topology/containers/nodes"})
@PostMapping(value = {"/topology/containers/nodes"})
Response<PageData<ContainerVO>> listContainerByTopologyNodes(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
Expand All @@ -103,7 +103,7 @@ Response<PageData<ContainerVO>> listContainerByTopologyNodes(
// 容器选择器标准接口-3
@ApiOperation(value = "容器选择器根据拓扑节点集合获取容器资源ID列表,用于跨页全选容器功能"
, produces = "application/json")
@PostMapping(value = {"/scope/{scopeType}/{scopeId}/topology/containerIds/nodes"})
@PostMapping(value = {"/topology/containerIds/nodes"})
Response<PageData<ContainerIdWithMeta>> listContainerIdByTopologyNodes(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
Expand All @@ -125,7 +125,7 @@ Response<PageData<ContainerIdWithMeta>> listContainerIdByTopologyNodes(

// 容器选择器标准接口-4
@ApiOperation(value = "根据用户选择/输入的容器信息获取容器")
@PostMapping(value = {"/scope/{scopeType}/{scopeId}/container/check"})
@PostMapping(value = {"/container/check"})
Response<List<ContainerVO>> checkContainers(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
Expand All @@ -146,7 +146,7 @@ Response<List<ContainerVO>> checkContainers(

// 容器选择器标准接口-5
@ApiOperation(value = "根据容器资源 ID批量查询容器详情信息")
@PostMapping(value = {"/scope/{scopeType}/{scopeId}/containers/details"})
@PostMapping(value = {"/containers/details"})
Response<List<ContainerVO>> getContainerDetails(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

@Data
@ApiModel("容器ID及元数据")
@NoArgsConstructor
public class ContainerIdWithMeta {
@ApiModelProperty(value = "容器资源 ID", required = true)
private Long id;

@ApiModelProperty(value = "容器元数据;Job不支持")
Map<String, Object> meta;

public ContainerIdWithMeta(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public class ListContainerByTopologyNodesReq {
@ApiModelProperty(value = "拓扑节点列表", required = true)
private List<ContainerTopologyNodeVO> nodeList;

@ApiModelProperty(value = "筛选条件:容器ID")
private String containerUid;
@ApiModelProperty(value = "筛选条件:容器ID列表")
private List<String> containerUIDList;

@ApiModelProperty(value = "筛选条件:容器名称")
private String name;
@ApiModelProperty(value = "筛选条件:容器名称列表")
private List<String> containerNameList;

@ApiModelProperty(value = "筛选条件:Pod名称")
private String podName;
@ApiModelProperty(value = "筛选条件:Pod名称列表")
private List<String> podNameList;

@ApiModelProperty(value = "筛选条件:Pod label")
private Map<String, String> podLabels;
Expand Down
Loading

0 comments on commit 5d6b4fb

Please sign in to comment.