From fe6b1091848eb44d53119e7df6e2699ca4e0989b Mon Sep 17 00:00:00 2001 From: mm Date: Mon, 22 Apr 2019 17:23:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=88=B6=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bridge/game/service/K8sService.java | 38 ++++++++++++++----- .../game/service/KubernetesService.java | 20 +++++----- .../game/service/PodManagerService.java | 19 ++++++---- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/educoder/bridge/game/service/K8sService.java b/src/main/java/com/educoder/bridge/game/service/K8sService.java index 28e681a..99da61b 100644 --- a/src/main/java/com/educoder/bridge/game/service/K8sService.java +++ b/src/main/java/com/educoder/bridge/game/service/K8sService.java @@ -1,6 +1,16 @@ package com.educoder.bridge.game.service; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @@ -9,18 +19,28 @@ import com.educoder.bridge.common.utils.BeanFactory; import com.educoder.bridge.common.utils.ConstantUtil; import com.educoder.bridge.common.utils.ShellUtil; import com.spotify.docker.client.exceptions.DockerException; -import io.fabric8.kubernetes.api.model.*; + +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.DoneablePod; +import io.fabric8.kubernetes.api.model.HostPathVolumeSource; +import io.fabric8.kubernetes.api.model.Node; +import io.fabric8.kubernetes.api.model.NodeStatus; +import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.api.model.PodList; +import io.fabric8.kubernetes.api.model.Quantity; +import io.fabric8.kubernetes.api.model.ResourceRequirements; +import io.fabric8.kubernetes.api.model.SecurityContext; +import io.fabric8.kubernetes.api.model.ServicePort; +import io.fabric8.kubernetes.api.model.VolumeMount; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.ConfigBuilder; -import io.fabric8.kubernetes.client.*; +import io.fabric8.kubernetes.client.DefaultKubernetesClient; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.Watch; +import io.fabric8.kubernetes.client.Watcher; import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable; import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; import io.fabric8.kubernetes.client.dsl.PodResource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -import java.util.*; @Service("K8sService") @@ -311,7 +331,7 @@ public class K8sService { logger.debug("创建pod成功,podName: {}, podPhase: {}", pod.getMetadata().getName(), pod.getStatus().getPhase()); return pod; } - + /** * 创pod * @@ -499,7 +519,7 @@ public class K8sService { return pod.getMetadata().getName(); } else { logger.warn("pod状态异常,将删除pod! 声明拥有的标签:{},声明不拥有的标签:{}", JSON.toJSONString(inLabels), JSON.toJSONString(notInLabels)); - podList.delete(); + podList.withGracePeriod(0).delete(); } return getPod(inLabels, notInLabels); } else { diff --git a/src/main/java/com/educoder/bridge/game/service/KubernetesService.java b/src/main/java/com/educoder/bridge/game/service/KubernetesService.java index 52c62fd..70ca1a3 100644 --- a/src/main/java/com/educoder/bridge/game/service/KubernetesService.java +++ b/src/main/java/com/educoder/bridge/game/service/KubernetesService.java @@ -1,5 +1,11 @@ package com.educoder.bridge.game.service; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.educoder.bridge.common.utils.Base64Util; @@ -7,14 +13,6 @@ import com.educoder.bridge.common.utils.GameHelper; import com.educoder.bridge.common.utils.JedisUtil; import com.educoder.bridge.common.utils.ShellUtil; import com.educoder.bridge.game.exception.GameException; -import org.apache.commons.lang.StringUtils; -import org.omg.CORBA.TIMEOUT; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.concurrent.TimeoutException; /** * 与kubernetes交互的service @@ -98,8 +96,8 @@ public class KubernetesService { return out; } - - + + /** * 在pod中执行评测脚本 */ @@ -196,7 +194,7 @@ public class KubernetesService { podManagerService.cancelDel(podName); logger.debug("直接删除pod:{}", podName); - String command = "kubectl delete pod " + podName + " --now"; + String command = "kubectl delete pod " + podName + " --grace-period=0 --force"; ShellUtil.executeAndGetExitStatus(command); } } diff --git a/src/main/java/com/educoder/bridge/game/service/PodManagerService.java b/src/main/java/com/educoder/bridge/game/service/PodManagerService.java index 3a45e0f..aec281c 100644 --- a/src/main/java/com/educoder/bridge/game/service/PodManagerService.java +++ b/src/main/java/com/educoder/bridge/game/service/PodManagerService.java @@ -1,16 +1,18 @@ package com.educoder.bridge.game.service; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.annotation.PostConstruct; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + import com.alibaba.fastjson.JSONObject; import com.educoder.bridge.common.utils.BeanFactory; import com.educoder.bridge.common.utils.JedisUtil; import com.educoder.bridge.common.utils.ShellUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -import javax.annotation.PostConstruct; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicBoolean; /** * pod生命周期管理 @@ -68,7 +70,8 @@ public class PodManagerService { private synchronized void delPod(String podName) throws Exception { // 根据podName来删除pod - JSONObject result = ShellUtil.executeAndGetExitStatus("kubectl delete pod " + podName + " --now"); + JSONObject result = ShellUtil + .executeAndGetExitStatus("kubectl delete pod " + podName + " --grace-period=0 --force"); logger.info("定时任务: 删除pod-{}结果: {}", podName, result.getString("out")); // 若存在端口映射service,则一并删除