强制删除

This commit is contained in:
mm 2019-04-22 17:23:21 +08:00
parent e5ad2c1e6a
commit fe6b109184
3 changed files with 49 additions and 28 deletions

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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则一并删除