|
|
@@ -5,6 +5,7 @@ import org.apache.http.HttpStatus;
|
|
|
import seecoder.devcloud.api.k8s.K8sConstants;
|
|
|
import seecoder.devcloud.api.k8s.ServiceApi;
|
|
|
import seecoder.devcloud.api.k8s.exception.K8sApiException;
|
|
|
+import seecoder.devcloud.api.k8s.model.K8sObjectRequest;
|
|
|
import seecoder.devcloud.api.k8s.model.Service;
|
|
|
import seecoder.devcloud.api.k8s.model.ServicePort;
|
|
|
import seecoder.devcloud.common.util.SpringUtil;
|
|
|
@@ -12,6 +13,7 @@ import seecoder.devcloud.core.pipeline.Context;
|
|
|
import seecoder.devcloud.core.pipeline.PipelineException;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author PuHong Weng
|
|
|
@@ -41,7 +43,7 @@ public class K8sServiceHandler extends AbstractHandler {
|
|
|
//1. 配置K8s的service
|
|
|
Service service = new Service();
|
|
|
service.setNamespace(context.getNamespace());
|
|
|
- service.setName(context.getDeployName()+ K8sConstants.SERVICE_SUFFIX);
|
|
|
+ service.setName(context.getDeployName() + K8sConstants.SERVICE_SUFFIX);
|
|
|
//标签选择器选择 此项目对应的deployment的name
|
|
|
service.setSelectors(Collections.singletonMap(K8sConstants.APPLICATION_LABEL, context.getDeployName()));
|
|
|
service.setServicePorts(
|
|
|
@@ -50,20 +52,24 @@ public class K8sServiceHandler extends AbstractHandler {
|
|
|
|
|
|
//2. 创建
|
|
|
try {
|
|
|
- serviceApi.create(service);
|
|
|
- log.info("service创建成功: [namespace: " + context.getNamespace() +", deployName: " + context.getTemplateName() + "]");
|
|
|
- } catch (K8sApiException e){
|
|
|
- if (e.getCode() == K8sApiException.ALREADY_EXIST){
|
|
|
- serviceApi.update(service);
|
|
|
- log.info("service更新成功: [namespace: " + context.getNamespace() +", deployName: " + context.getTemplateName() + "]");
|
|
|
+ List<Service> services = serviceApi.getByCondition(K8sObjectRequest.builder()
|
|
|
+ .namespace(context.getNamespace())
|
|
|
+ .name(context.getDeployName() + K8sConstants.SERVICE_SUFFIX)
|
|
|
+ .build());
|
|
|
+ if (services.size() == 0) {
|
|
|
+ serviceApi.create(service);
|
|
|
+ log.info("service创建成功: [namespace: " + context.getNamespace() + ", deployName: " + context.getTemplateName() + "]");
|
|
|
} else {
|
|
|
- context.appendErrorResult(PipelineException.SERVICE_CREATE_ERROR,e);
|
|
|
- throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE,PipelineException.SERVICE_CREATE_ERROR,e);
|
|
|
+ serviceApi.update(service);
|
|
|
+ log.info("service更新成功: [namespace: " + context.getNamespace() + ", deployName: " + context.getTemplateName() + "]");
|
|
|
}
|
|
|
+ } catch (K8sApiException e) {
|
|
|
+ context.appendErrorResult(PipelineException.SERVICE_CREATE_ERROR, e);
|
|
|
+ throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.SERVICE_CREATE_ERROR, e);
|
|
|
}
|
|
|
|
|
|
//3. 存放暴露的端口
|
|
|
context.getConfigs().put("servicePort", String.valueOf(servicePort));
|
|
|
- context.appendSuccessResult("K8s Service创建成功: [namespace: " + context.getNamespace() +", deployName: " + context.getTemplateName() + "]");
|
|
|
+ context.appendSuccessResult("K8s Service创建成功: [namespace: " + context.getNamespace() + ", deployName: " + context.getTemplateName() + "]");
|
|
|
}
|
|
|
}
|