|
@@ -64,7 +64,6 @@ public class ProjectUtil {
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
|
|
|
-
|
|
|
@SneakyThrows
|
|
|
public void deleteYamlByProjectId(String projectId) {
|
|
|
List<String> absolutePathList = FileUtil.listAbsolutePath(podYamlDirectory);
|
|
@@ -108,14 +107,10 @@ public class ProjectUtil {
|
|
|
|
|
|
public void deletePod(String podName) {
|
|
|
try {
|
|
|
- String key = "pod:" + podName + ":node";
|
|
|
// 先删除 redis key
|
|
|
- RedisUtil.deleteByKey(stringRedisTemplate, "pod:" + podName + ":node");
|
|
|
KubernetesUtil.deletePod(apiClient, kubernetesConfiguration.getNamespace(), podName);
|
|
|
-
|
|
|
log.info("deletePod() 等待 pod " + podName + " 的资源释放完成。");
|
|
|
- TimeUnit.SECONDS.sleep(10);
|
|
|
- log.info("deletePod() 删除 pod 并删除 redis 键值对:" + key);
|
|
|
+ TimeUnit.SECONDS.sleep(7);
|
|
|
} catch (ApiException apiException) {
|
|
|
log.info("deletePod() pod " + podName + " 已删除。");
|
|
|
} catch (Exception e) {
|
|
@@ -125,51 +120,15 @@ public class ProjectUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
public String getNodeNameOfPod(String podName) {
|
|
|
- return stringRedisTemplate.opsForValue().get("pod:" + podName + ":node");
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更改一个名字继续启动
|
|
|
- *
|
|
|
- * @param projectId 项目 id
|
|
|
- * @param nodeName 运行 pod 的节点名称
|
|
|
- * @param lastPodName 即将删除的 pod 名称
|
|
|
- */
|
|
|
- @SneakyThrows
|
|
|
- public void createNextPod2(String projectId, String nodeName, String lastPodName) {
|
|
|
- //1 删除上一个 pod 和 redis 键值对 和 旧的 yaml 文件
|
|
|
-// new Thread(() -> deletePod(lastPodName), "delete-" + lastPodName).start();
|
|
|
- //
|
|
|
- String cpuOrderString = stringRedisTemplate.opsForValue().get("pod:" + lastPodName + ":cpu");
|
|
|
- deletePod(lastPodName);
|
|
|
- List<String> list = FileUtil.listAbsolutePath(podYamlDirectory);
|
|
|
- Iterator<String> iterator1 = list.iterator();
|
|
|
- while (iterator1.hasNext()) {
|
|
|
- String absolutePath = iterator1.next();
|
|
|
- if (absolutePath.contains(nodeName) && absolutePath.contains(lastPodName)) {
|
|
|
- FileUtil.rm(absolutePath);
|
|
|
- list.remove(absolutePath);
|
|
|
- break;
|
|
|
- }
|
|
|
+ final String s = stringRedisTemplate.opsForValue().get("pod:" + podName + ":node");
|
|
|
+ if (StringUtil.isEmpty(s)) {
|
|
|
+ throw new RuntimeException("getNodeNameOfPod() 无法获取 pod 运行所在节点:" + "pod:" + podName + ":node");
|
|
|
}
|
|
|
- for (String absolutePath : list) {
|
|
|
- if (absolutePath.contains(nodeName) && absolutePath.contains(projectId)) {
|
|
|
- Optional.ofNullable(cpuOrderString).orElseThrow(() -> new RuntimeException("createNextPod2() pod " + lastPodName + " 缓存的 cpu 编号为空。"));
|
|
|
- final String read = FileUtil.read(absolutePath);
|
|
|
- final String replace = read.replace("cpu-order", "\"" + cpuOrderString + "\"");
|
|
|
- FileUtil.writeStringToLocalFile(replace, absolutePath);
|
|
|
- createPod2(absolutePath);
|
|
|
- log.info("createNextPod2() 创建项目 " + projectId + " 在节点 " + nodeName + " 的下一个 pod。");
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- // 如果当前节点没有下一个yaml,则返回一个并行度。
|
|
|
- log.info("createNextPod2() 节点 " + nodeName + " 已经执行完被分配的项目 " + projectId + " 的所有 pod。");
|
|
|
- addOneParallelismToNode(nodeName);
|
|
|
+ return s;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 更改一个名字继续启动
|
|
|
*
|
|
@@ -179,7 +138,7 @@ public class ProjectUtil {
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
public void createNextPod3(String projectId, String nodeName, String lastPodName) {
|
|
|
- //1 删除上一个 pod 和 redis 键值对 和 旧的 yaml 文件
|
|
|
+ log.info("createNextPod3() 删除上一个 pod:projectId={},nodeName={},lastPodName={}", projectId, nodeName, lastPodName);
|
|
|
String cpuOrderString = stringRedisTemplate.opsForValue().get("pod:" + lastPodName + ":cpu");
|
|
|
deletePod(lastPodName);
|
|
|
RedisUtil.deleteByKey(stringRedisTemplate, "pod:" + lastPodName + ":cpu");
|
|
@@ -200,39 +159,11 @@ public class ProjectUtil {
|
|
|
FileUtil.writeStringToLocalFile(replace, absolutePath);
|
|
|
// 创建 pod
|
|
|
createPod3(yamlPathCacheKey);
|
|
|
- log.info("createNextPod3() 创建项目 " + projectId + " 在节点 " + nodeName + " 的下一个 pod。");
|
|
|
+ log.info("createNextPod3() 创建项目 " + projectId + " 在节点 " + nodeName + " 的下一个 pod,使用 cpu 编号为 " + cpuOrderString);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * @param nodeName 节点名称
|
|
|
- * @param podName pod 名称
|
|
|
- * @param podYamlContent pod 文件内容
|
|
|
- */
|
|
|
- @SneakyThrows
|
|
|
- public void createPod(String nodeName, String podName, String podYamlContent) {
|
|
|
- stringRedisTemplate.opsForValue().set("pod:" + podName + ":node", nodeName); // 将 pod 运行在哪个 node 上记录到 redis
|
|
|
- String podYamlName = getPodYamlName(nodeName, podName);
|
|
|
- String podYamlPath = podYamlDirectory + podYamlName;
|
|
|
- FileUtil.writeStringToLocalFile(podYamlContent, podYamlPath);
|
|
|
- KubernetesUtil.createNs(apiClient, kubernetesConfiguration.getNamespace());
|
|
|
- KubernetesUtil.applyYaml(hostname, username, password, podYamlPath);
|
|
|
-// 下面这种执行方法报错
|
|
|
-// V1Pod v1Pod = (V1Pod) Yaml.load(nextPodString);
|
|
|
-// KubernetesUtil.createPod(apiClient, kubernetesNamespace, v1Pod);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param podYamlPath yaml 文件地址
|
|
|
- */
|
|
|
- public void createPod2(String podYamlPath) {
|
|
|
- String nodeName = new File(podYamlPath).getName().split("#")[0];
|
|
|
- String podName = podYamlPath.split("#")[1].split("\\.")[0];
|
|
|
- stringRedisTemplate.opsForValue().set("pod:" + podName + ":node", nodeName); // 将 pod 运行在哪个 node 上记录到 redis
|
|
|
- new Thread(() -> KubernetesUtil.applyYaml(hostname, username, password, podYamlPath), "create-" + podName).start();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* @param redisKey yaml 地址的缓存 key
|
|
|
*/
|
|
@@ -538,7 +469,7 @@ public class ProjectUtil {
|
|
|
} else if (DictConstants.PROJECT_TYPE_AUTO_SUB.equals(projectType)) {
|
|
|
userId = autoSubProjectMapper.selectCreateUserById(projectId);
|
|
|
} else {
|
|
|
- throw new RuntimeException("ProjectUtil--getRedisPrefixByProjectIdAndProjectType 未知的项目类型!");
|
|
|
+ throw new RuntimeException("getRedisPrefixByProjectIdAndProjectType() 未知的项目类型!");
|
|
|
}
|
|
|
|
|
|
//3 获取用户类型(管理员账户、管理员子账户、普通账户、普通子账户)(独占、共享)
|
|
@@ -587,10 +518,11 @@ public class ProjectUtil {
|
|
|
String key = "node:" + nodeName + ":parallelism";
|
|
|
String parallelismString = stringRedisTemplate.opsForValue().get(key);
|
|
|
if (StringUtil.isEmpty(parallelismString)) {
|
|
|
- throw new RuntimeException("ProjectUtil--addOneParallelismToNode redisKey " + key + " 为空。");
|
|
|
+ throw new RuntimeException("addOneParallelismToNode() redisKey " + key + " 为空。");
|
|
|
}
|
|
|
- int parallelism = Integer.parseInt(parallelismString);
|
|
|
- stringRedisTemplate.opsForValue().set(key, (parallelism + 1) + "");
|
|
|
- log.info("ProjectUtil--addOneParallelismToNode 归还节点 " + nodeName + " 并行度从 " + parallelism + " 加一。");
|
|
|
+ final int parallelismBefore = Integer.parseInt(parallelismString);
|
|
|
+ final int parallelismAfter = parallelismBefore + 1;
|
|
|
+ stringRedisTemplate.opsForValue().set(key, parallelismAfter + "");
|
|
|
+ log.info("addOneParallelismToNode() 归还节点 " + nodeName + " 并行度:" + parallelismBefore + " --> " + parallelismAfter);
|
|
|
}
|
|
|
}
|