Skip to content

Commit

Permalink
perf: 容器化-支持各微服务按顺序更新 TencentBlueKing#919
Browse files Browse the repository at this point in the history
支持通过命令行指定参数
  • Loading branch information
jsonwan committed Dec 1, 2022
1 parent 3331874 commit 1494897
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ ext {
set('cronUtilsVersion', "9.1.6")
set('otelJdbcVersion', "1.9.2-alpha")
set('commonsValidatorVersion', "1.6")
set('jcommanderVersion', "1.71")
if (System.getProperty("bkjobVersion")) {
set('bkjobVersion', System.getProperty("bkjobVersion"))
println "bkjobVersoin:" + bkjobVersion
Expand Down Expand Up @@ -271,6 +272,7 @@ subprojects {
dependencySet(group: 'org.hibernate.validator', version: "$hibernateValidatorVersion") {
entry "hibernate-validator"
}
dependency "com.beust:jcommander:$jcommanderVersion"
}
}
configurations {
Expand Down
1 change: 1 addition & 0 deletions src/backend/job-tools/k8s-startup-controller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ext {
version "${jobToolsVersion}"
dependencies {
api 'org.springframework.cloud:spring-cloud-starter-kubernetes-client-all'
api "com.beust:jcommander"
}
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: "application"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.k8s;

import com.beust.jcommander.Parameter;
import lombok.Data;

@Data
public class ServiceDependModel {
@Parameter(names = {"-n", "--namespace"}, description = "当前服务所在的命名空间")
private String namespace;

@Parameter(names = {"-s", "--service"}, description = "当前服务名称")
private String serviceName;

@Parameter(names = {"-d", "--dependency"}, description = "服务间的依赖关系定义,多个依赖关系用英文逗号分隔," +
"例如:(A:B,C),(B:D)表示服务A必须在服务B与服务C启动完成后才启动,服务B必须在服务D启动完成后才启动")
private String dependencyStr;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.tencent.bk.job.k8s;

import com.beust.jcommander.JCommander;
import com.tencent.bk.job.common.util.StringUtil;
import com.tencent.bk.job.common.util.ThreadUtils;
import io.kubernetes.client.openapi.ApiClient;
Expand Down Expand Up @@ -69,30 +70,80 @@ public class StartupController {
}

public static void main(String[] args) {
String namespace = System.getenv(Consts.KEY_KUBERNETES_NAMESPACE);
String dependenciesStr = System.getenv(Consts.KEY_STARTUP_DEPENDENCIES_STR);
String currentService = System.getenv(Consts.KEY_CURRENT_SERVICE_NAME);
// 解析需要的依赖参数
ServiceDependModel serviceDependModel = parseDependModelFromArgsOrEnv(args);
String namespace = serviceDependModel.getNamespace();
String currentService = serviceDependModel.getServiceName();
String dependenciesStr = serviceDependModel.getDependencyStr();
log.info("namespace={}", namespace);
log.info("dependenciesStr={}", dependenciesStr);
log.info("currentService={}", currentService);
// 解析出结构化的依赖映射表
Map<String, List<String>> dependencyMap = parseDependencyMap(dependenciesStr);
printDependencyMap(dependencyMap);
if (StringUtils.isBlank(currentService)) {
log.warn("currentService is blank, ignore dependency check");
return;
}
// 获取依赖服务列表
List<String> dependServiceList = dependencyMap.get(currentService);
if (CollectionUtils.isEmpty(dependServiceList)) {
log.info("There is no depend service for {}", currentService);
return;
}
log.info("{} depend service found for {}:{}", dependServiceList.size(), currentService, dependServiceList);
// 等待所有依赖服务启动完成
while (!isAllDependServiceReady(namespace, dependServiceList)) {
ThreadUtils.sleep(3000);
}
log.info("all depend services are ready, it`s time for {} to start", currentService);
}

/**
* 从命令行参数或环境变量解析出程序运行需要的服务依赖参数
*
* @param args 命令行参数
* @return 服务依赖数据
*/
private static ServiceDependModel parseDependModelFromArgsOrEnv(String[] args) {
ServiceDependModel serviceDependModel = new ServiceDependModel();
JCommander.newBuilder()
.addObject(serviceDependModel)
.build()
.parse(args);
String namespace = serviceDependModel.getNamespace();
if (StringUtils.isBlank(namespace)) {
namespace = System.getenv(Consts.KEY_KUBERNETES_NAMESPACE);
log.info(
"Commandline param [-n,--namespace] is null or blank, use env variable {}={}",
Consts.KEY_KUBERNETES_NAMESPACE,
namespace
);
serviceDependModel.setNamespace(namespace);
}
String serviceName = serviceDependModel.getServiceName();
if (StringUtils.isBlank(serviceName)) {
serviceName = System.getenv(Consts.KEY_CURRENT_SERVICE_NAME);
log.info(
"Commandline param [-s,--service] is null or blank, use env variable {}={}",
Consts.KEY_CURRENT_SERVICE_NAME,
serviceName
);
serviceDependModel.setServiceName(serviceName);
}
String dependenciesStr = serviceDependModel.getDependencyStr();
if (StringUtils.isBlank(dependenciesStr)) {
dependenciesStr = System.getenv(Consts.KEY_STARTUP_DEPENDENCIES_STR);
log.info(
"Commandline param [-d,--dependency] is null or blank, use env variable {}={}",
Consts.KEY_STARTUP_DEPENDENCIES_STR,
dependenciesStr
);
serviceDependModel.setDependencyStr(dependenciesStr);
}
return serviceDependModel;
}

/**
* 根据依赖定义字符串解析出依赖关系Map
*
Expand Down

0 comments on commit 1494897

Please sign in to comment.