|
|
@@ -9,12 +9,19 @@ import io.kubernetes.client.models.*;
|
|
|
import io.kubernetes.client.proto.V1beta1Apps;
|
|
|
import io.kubernetes.client.proto.V1beta1Extensions;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.IngressApi;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.dto.IngressDTO;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.vo.ApiResult;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.vo.Namespace;
|
|
|
+import nju.seec.SEECdemo.util.ApplicationProperties;
|
|
|
import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import static nju.seec.SEECdemo.util.Constants.FOREGROUND_PROPAGATION_POLICY;
|
|
|
+import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
|
|
|
+
|
|
|
/**
|
|
|
* author: rale
|
|
|
* createdAt: 12/24/18
|
|
|
@@ -23,54 +30,64 @@ import org.springframework.stereotype.Service;
|
|
|
public class IngressApiImpl implements IngressApi{
|
|
|
private static final Logger logger = LoggerUtil.getLogger(IngressApi.class);
|
|
|
@Autowired
|
|
|
- private ExtensionsV1beta1Api extensionsV1beta1Api;
|
|
|
+ private final ExtensionsV1beta1Api extensionsV1beta1Api;
|
|
|
|
|
|
- //@todo fix bug
|
|
|
- @Override
|
|
|
- public ApiResult<String> create(String namespace, String name, String path, String serviceName, int port) {
|
|
|
- V1ObjectMeta v1ObjectMeta = new V1ObjectMetaBuilder()
|
|
|
- .withName(name)
|
|
|
- .withNamespace(namespace)
|
|
|
- .build();
|
|
|
+ @Autowired
|
|
|
+ public IngressApiImpl(ApplicationProperties applicationProperties, ExtensionsV1beta1Api extensionsV1beta1Api){
|
|
|
+ this.extensionsV1beta1Api = extensionsV1beta1Api;
|
|
|
+ }
|
|
|
|
|
|
- V1beta1HTTPIngressPath v1beta1HTTPIngressPath = new V1beta1HTTPIngressPathBuilder()
|
|
|
- .withPath(path)
|
|
|
- .withBackend(new V1beta1IngressBackendBuilder()
|
|
|
- .withServiceName(serviceName)
|
|
|
- .withServicePort(new IntOrString(port))
|
|
|
- .build())
|
|
|
- .build();
|
|
|
- V1beta1HTTPIngressRuleValue v1beta1HTTPIngressRuleValue = new V1beta1HTTPIngressRuleValueBuilder()
|
|
|
- .addToPaths(v1beta1HTTPIngressPath)
|
|
|
- .build();
|
|
|
- V1beta1IngressRule v1beta1IngressRule = new V1beta1IngressRuleBuilder()
|
|
|
- .withHost("deernowl.cn")
|
|
|
- .withHttp(v1beta1HTTPIngressRuleValue)
|
|
|
- .build();
|
|
|
- V1beta1IngressSpec v1beta1IngressSpec = new V1beta1IngressSpecBuilder()
|
|
|
- .withRules(v1beta1IngressRule)
|
|
|
- .build();
|
|
|
- V1beta1Ingress v1beta1Ingress = new V1beta1IngressBuilder()
|
|
|
- .withApiVersion("extensions/v1beta1")
|
|
|
- .withKind("Ingress")
|
|
|
- .withMetadata(v1ObjectMeta)
|
|
|
- .withSpec(v1beta1IngressSpec)
|
|
|
- .build();
|
|
|
+ @Override
|
|
|
+ public void create(IngressDTO ingressDTO) {
|
|
|
+ V1beta1Ingress v1beta1Ingress = ingressDTO.toV1beta1Ingress();
|
|
|
|
|
|
try {
|
|
|
- extensionsV1beta1Api.createNamespacedIngress(namespace, v1beta1Ingress, "pretty");
|
|
|
+ extensionsV1beta1Api.createNamespacedIngress(ingressDTO.getNamespace(), v1beta1Ingress, PRETTY_FORMAT);
|
|
|
} catch (ApiException e) {
|
|
|
- LoggerUtil.error(logger, e, "ingress 创建失败,v1BetaIngress={}, response={}", v1beta1Ingress, e.getResponseBody());
|
|
|
- ApiResult<String> apiResult = new ApiResult<>();
|
|
|
- apiResult.setSuccess(false);
|
|
|
- apiResult.setErrorCode(e.getCode());
|
|
|
- apiResult.setErrorDescription(e.getMessage());
|
|
|
- return apiResult;
|
|
|
+ LoggerUtil.error(logger, e, "ingress 创建失败,ingressDTO={}, response={}", ingressDTO, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
}
|
|
|
- ApiResult<String> result = new ApiResult<>();
|
|
|
- result.setSuccess(true);
|
|
|
- result.setValue("deernowl.cn" + path);
|
|
|
- return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @see IngressApi#delete(String, String)
|
|
|
+ * @param namespace
|
|
|
+ * @param name
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void delete(String namespace, String name) {
|
|
|
+ //@todo 判断是否存在
|
|
|
+ V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
|
|
|
+ .withApiVersion(IngressDTO.API_VERSION)
|
|
|
+ .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ extensionsV1beta1Api.deleteNamespacedIngress(name, namespace, v1DeleteOptions, PRETTY_FORMAT, null, null, FOREGROUND_PROPAGATION_POLICY);;
|
|
|
+ }catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "ingress 删除失败,name={}, namespace={}, response={}", name, namespace, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }catch (Exception e) {
|
|
|
+ //K8s swagger 异常, 无需进行处理
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see IngressApi#deleteIfExists(String, String)
|
|
|
+ * @param namespace
|
|
|
+ * @param name
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void deleteIfExists(String namespace, String name) {
|
|
|
+ V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
|
|
|
+ .withApiVersion(IngressDTO.API_VERSION)
|
|
|
+ .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ extensionsV1beta1Api.deleteNamespacedIngress(name, namespace, v1DeleteOptions, PRETTY_FORMAT, null, null, FOREGROUND_PROPAGATION_POLICY);;
|
|
|
+ }catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "ingress 删除失败,name={}, namespace={}, response={}", name, namespace, e.getResponseBody());
|
|
|
+ }catch (Exception e) {
|
|
|
+ //K8s Swagger 异常, 无需进行处理
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|