Unverified Commit be747e50 authored by gaoyan's avatar gaoyan Committed by GitHub

Optimize k8s delete cluster (#3368)

parent ea4599d3
...@@ -212,12 +212,25 @@ public abstract class KubernetesGateway extends AbstractGateway { ...@@ -212,12 +212,25 @@ public abstract class KubernetesGateway extends AbstractGateway {
throw new GatewayException( throw new GatewayException(
"No cluster id was specified. Please specify a cluster to which you would like" + " to connect."); "No cluster id was specified. Please specify a cluster to which you would like" + " to connect.");
} }
if (k8sClientHelper.getClusterIsPresent(clusterId)) {
try (KubernetesClusterDescriptor clusterDescriptor = try (KubernetesClusterDescriptor clusterDescriptor =
clusterClientFactory.createClusterDescriptor(configuration)) { clusterClientFactory.createClusterDescriptor(configuration)) {
clusterDescriptor.killCluster(clusterId); clusterDescriptor.killCluster(clusterId);
} catch (Exception e) { int retryCount = 0;
logger.error(e.getMessage(), e); while (k8sClientHelper.getClusterIsPresent(clusterId)) {
retryCount++;
log.warn("cluster id: {} is still running, recheck at 1s later", clusterId);
if (retryCount > 60) {
throw new GatewayException("The cluster " + clusterId
+ " still running, abort wait kill cluster, please check your k8s cluster.");
}
Thread.sleep(1000);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
} else {
logger.info("Cluster {} is not present, ignore kill", clusterId);
} }
} }
......
...@@ -26,6 +26,7 @@ import org.apache.flink.configuration.Configuration; ...@@ -26,6 +26,7 @@ import org.apache.flink.configuration.Configuration;
import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions; import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient; import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient;
import org.apache.flink.kubernetes.kubeclient.FlinkKubeClientFactory; import org.apache.flink.kubernetes.kubeclient.FlinkKubeClientFactory;
import org.apache.flink.kubernetes.kubeclient.decorators.ExternalServiceDecorator;
import org.apache.http.util.TextUtils; import org.apache.http.util.TextUtils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
...@@ -33,6 +34,7 @@ import java.io.InputStream; ...@@ -33,6 +34,7 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
...@@ -68,6 +70,25 @@ public class K8sClientHelper { ...@@ -68,6 +70,25 @@ public class K8sClientHelper {
initKubeClient(kubeConfig); initKubeClient(kubeConfig);
} }
public Optional<Deployment> getJobService(String clusterId) {
String serviceName = ExternalServiceDecorator.getExternalServiceName(clusterId);
Deployment deployment = kubernetesClient
.apps()
.deployments()
.inNamespace(configuration.getString(KubernetesConfigOptions.NAMESPACE))
.withName(configuration.getString(KubernetesConfigOptions.CLUSTER_ID))
.get();
if (deployment == null) {
log.debug("Service {} does not exist", serviceName);
return Optional.empty();
}
return Optional.of(deployment);
}
public boolean getClusterIsPresent(String clusterId) {
return getJobService(clusterId).isPresent();
}
/** /**
* initKubeClient * initKubeClient
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment