|
@@ -40,13 +40,14 @@ public class KubernetesUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 执行 yaml
|
|
* 执行 yaml
|
|
|
|
+ *
|
|
* @param hostname 主机名
|
|
* @param hostname 主机名
|
|
* @param username 用户名
|
|
* @param username 用户名
|
|
* @param password 密码
|
|
* @param password 密码
|
|
* @param yamlPath yaml 文件路径
|
|
* @param yamlPath yaml 文件路径
|
|
*/
|
|
*/
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
- public static void applyYaml(String hostname, String username, String password, String yamlPath){
|
|
|
|
|
|
+ public static void applyYaml(String hostname, String username, String password, String yamlPath) {
|
|
SshClient client = SshUtil.getClient();
|
|
SshClient client = SshUtil.getClient();
|
|
ClientSession session = SshUtil.getSession(client, hostname, username, password);
|
|
ClientSession session = SshUtil.getSession(client, hostname, username, password);
|
|
SshUtil.execute(session, "kubectl apply -f " + yamlPath);
|
|
SshUtil.execute(session, "kubectl apply -f " + yamlPath);
|
|
@@ -57,8 +58,8 @@ public class KubernetesUtil {
|
|
/**
|
|
/**
|
|
* 创建 namespace 命名空间
|
|
* 创建 namespace 命名空间
|
|
*
|
|
*
|
|
- * @param apiClient 客户端
|
|
|
|
- * @param nsName 命名空间名称
|
|
|
|
|
|
+ * @param apiClient 客户端
|
|
|
|
+ * @param nsName 命名空间名称
|
|
* @return 命名空间对象
|
|
* @return 命名空间对象
|
|
*/
|
|
*/
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
@@ -201,13 +202,50 @@ public class KubernetesUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 删除 pod
|
|
* 删除 pod
|
|
|
|
+ * 异步删除会等待 pod 完全删除完毕,即资源释放完全
|
|
|
|
+ *
|
|
|
|
+ * @param apiClient api 客户端
|
|
|
|
+ * @param namespaceName namespace 名称
|
|
|
|
+ * @param podName pod 名称
|
|
|
|
+ */
|
|
|
|
+ @SneakyThrows
|
|
|
|
+ public static void deletePod(ApiClient apiClient, String namespaceName, String podName) {
|
|
|
|
+ deletePodSync(apiClient, namespaceName, podName);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除 pod
|
|
|
|
+ * 异步删除会等待 pod 完全删除完毕,即资源释放完全
|
|
*
|
|
*
|
|
* @param apiClient api 客户端
|
|
* @param apiClient api 客户端
|
|
* @param namespaceName namespace 名称
|
|
* @param namespaceName namespace 名称
|
|
* @param podName pod 名称
|
|
* @param podName pod 名称
|
|
- * @throws ApiException 异常
|
|
|
|
*/
|
|
*/
|
|
- public static void deletePod(ApiClient apiClient, String namespaceName, String podName) throws ApiException {
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
|
+ public static void deletePodSync(ApiClient apiClient, String namespaceName, String podName) {
|
|
|
|
+ log.info("KubernetesUtil--deletePod 删除 " + namespaceName + ":" + podName);
|
|
|
|
+ CoreV1Api coreV1Api = new CoreV1Api(apiClient);
|
|
|
|
+ coreV1Api.deleteNamespacedPod(podName, namespaceName, null, null, null, null, null, null);
|
|
|
|
+ // 检查是否删除完毕
|
|
|
|
+ while (true) {
|
|
|
|
+ Thread.sleep(5000L);
|
|
|
|
+ List<String> podNameList = getPod(apiClient, namespaceName);
|
|
|
|
+ if (!podNameList.contains(podName)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除 pod
|
|
|
|
+ * 异步删除不会等待 pod 完全删除完毕,即资源还没有释放
|
|
|
|
+ *
|
|
|
|
+ * @param apiClient api 客户端
|
|
|
|
+ * @param namespaceName namespace 名称
|
|
|
|
+ * @param podName pod 名称
|
|
|
|
+ */
|
|
|
|
+ @SneakyThrows
|
|
|
|
+ public static void deletePodAsync(ApiClient apiClient, String namespaceName, String podName) {
|
|
log.info("KubernetesUtil--deletePod 删除 " + namespaceName + ":" + podName);
|
|
log.info("KubernetesUtil--deletePod 删除 " + namespaceName + ":" + podName);
|
|
CoreV1Api coreV1Api = new CoreV1Api(apiClient);
|
|
CoreV1Api coreV1Api = new CoreV1Api(apiClient);
|
|
coreV1Api.deleteNamespacedPod(podName, namespaceName, null, null, null, null, null, null);
|
|
coreV1Api.deleteNamespacedPod(podName, namespaceName, null, null, null, null, null, null);
|