raledong 7 лет назад
Родитель
Сommit
4cf7d7cd56

+ 39 - 39
src/main/java/nju/seec/SEECdemo/logic/api/k8s/ResourceQuotaApi.java

@@ -6,43 +6,43 @@ import nju.seec.SEECdemo.logic.api.k8s.vo.ResourceQuota;
 
 public interface ResourceQuotaApi {
 
-//    /**
-//     * 新建资源限额,如果该命名空间下的限额已经存在,则更新限额
-//     * @param resourceQuota
-//     * @return
-//     */
-//    void create(ResourceQuota resourceQuota) throws K8sApiException;
-//
-//    /**
-//     * 更新资源限额
-//     * @param resourceQuota
-//     * @return
-//     */
-//    void update(ResourceQuota resourceQuota) throws K8sApiException;
-//
-//    /**
-//     * 删除namespace下名为name的资源限额
-//     * @param namespace
-//     * @param name
-//     * @return
-//     */
-//    void delete(String namespace, String name) throws K8sApiException;
-//
-//
-//    /**
-//     * 获取namespace下名为name的资源
-//     * @param namespace
-//     * @param name
-//     * @return
-//     * @throws K8sApiException
-//     */
-//    ResourceQuota getByName(String namespace, String name) throws K8sApiException;
-//
-//    /**
-//     * 当前的资源限额是否存在
-//     * @param namespace
-//     * @param name
-//     * @throws K8sApiException
-//     */
-//    boolean exist(String namespace, String name) throws K8sApiException;
+    /**
+     * 新建资源限额,如果该命名空间下的限额已经存在,则更新限额
+     * @param resourceQuota
+     * @return
+     */
+    void create(ResourceQuota resourceQuota) throws K8sApiException;
+
+    /**
+     * 更新资源限额
+     * @param resourceQuota
+     * @return
+     */
+    void update(ResourceQuota resourceQuota) throws K8sApiException;
+
+    /**
+     * 删除namespace下名为name的资源限额
+     * @param namespace
+     * @param name
+     * @return
+     */
+    void delete(String namespace, String name) throws K8sApiException;
+
+
+    /**
+     * 获取namespace下名为name的资源
+     * @param namespace
+     * @param name
+     * @return
+     * @throws K8sApiException
+     */
+    ResourceQuota getByName(String namespace, String name) throws K8sApiException;
+
+    /**
+     * 当前的资源限额是否存在
+     * @param namespace
+     * @param name
+     * @throws K8sApiException
+     */
+    boolean exist(String namespace, String name) throws K8sApiException;
 }

+ 158 - 205
src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/ResourceQuotaImpl.java

@@ -2,211 +2,164 @@ package nju.seec.SEECdemo.logic.api.k8s.impl;
 
 import nju.seec.SEECdemo.logic.api.k8s.ResourceQuotaApi;
 
-//
-//import io.kubernetes.client.ApiException;
-//import io.kubernetes.client.apis.CoreV1Api;
-//import io.kubernetes.client.models.V1DeleteOptions;
-//import io.kubernetes.client.models.V1DeleteOptionsBuilder;
-//import io.kubernetes.client.models.V1ResourceQuotaList;
-//import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
-//import nju.seec.SEECdemo.logic.api.k8s.ResourceQuotaApi;
-//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.logic.api.k8s.vo.ResourceQuota;
-//import nju.seec.SEECdemo.util.LoggerUtil;
-//import org.slf4j.Logger;
-//import org.springframework.beans.factory.annotation.Autowired;
-//import org.springframework.stereotype.Service;
-//import org.springframework.util.CollectionUtils;
-//
-//
-//import java.awt.*;
-//import java.util.List;
-//import java.util.stream.Collectors;
-//
-//import static nju.seec.SEECdemo.util.Constants.FOREGROUND_PROPAGATION_POLICY;
-//import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
-//
-//@Service
+
+import io.kubernetes.client.ApiException;
+import io.kubernetes.client.apis.CoreV1Api;
+import io.kubernetes.client.models.V1DeleteOptions;
+import io.kubernetes.client.models.V1DeleteOptionsBuilder;
+import io.kubernetes.client.models.V1ResourceQuotaList;
+import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
+import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
+import nju.seec.SEECdemo.logic.api.k8s.vo.ResourceQuota;
+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;
+
+@Service
 public class ResourceQuotaImpl implements ResourceQuotaApi {
-//
-//    private static final Logger logger = LoggerUtil.getLogger(ResourceQuotaApi.class);
-//
-//    @Autowired
-//    private CoreV1Api coreV1Api;
-//
-//    @Autowired
-//    private NamespaceApi namespaceApi;
-//
-//    /**
-//     * @see ResourceQuotaApi#create(ResourceQuota)
-//     * @param resourceQuota
-//     * @return
-//     */
-//    @Override
-//    public void create(ResourceQuota resourceQuota) throws K8sApiException{
-//        ApiResult<Namespace> namespaceRequest = namespaceApi.getByName(resourceQuota.getName());
-//        //Namespace不存在
-//        if (namespaceRequest.getErrorCode() == ApiResult.NOT_EXIST) {
-//            return new ApiResult<>( ApiResult.NOT_EXIST, "该命名空间不存在");
-//        }
-//        ApiResult<Boolean> resourceQuotaExistRequest = this.exist(resourceQuota.getNamespace(), resourceQuota.getName());
-//        if (!resourceQuotaExistRequest.isSuccess()) {
-//            return new ApiResult<>(ApiResult.SYSTEM_ERROR, "resource quota查询失败");
-//        }
-//
-//        //该资源限额已经存在,直接更新资源限额
-//        if (resourceQuotaExistRequest.getValue()) {
-//            return update(resourceQuota);
-//        }
-//
-//        try {
-//            coreV1Api.createNamespacedResourceQuota(
-//                    resourceQuota.getNamespace(),
-//                    resourceQuota.toV1ResourceQuota(),
-//                    PRETTY_FORMAT);
-//        } catch (ApiException e) {
-//            LoggerUtil.error(logger, e, "创建resource quota失败, resourceQuota={}, response={}",
-//                    resourceQuota, e.getResponseBody());
-//            return new ApiResult<>(e.getCode(), e.getMessage());
-//        }
-//        return new ApiResult<>(null);
-//    }
-//
-//    /**
-//     * @see ResourceQuotaApi#update(ResourceQuota)
-//     * @param resourceQuota
-//     * @return
-//     */
-//    @Override
-//    public ApiResult<Void> update(ResourceQuota resourceQuota) {
-//        ApiResult<Namespace> namespaceRequest = namespaceApi.getByName(resourceQuota.getName());
-//        //Namespace不存在
-//        if (namespaceRequest.getErrorCode() == ApiResult.NOT_EXIST) {
-//            return new ApiResult<>( ApiResult.NOT_EXIST, "该命名空间不存在");
-//        }
-//
-//        ApiResult<Boolean> resourceQuotaExistRequest = this.exist(resourceQuota.getNamespace(), resourceQuota.getName());
-//        if (!resourceQuotaExistRequest.isSuccess()) {
-//            return new ApiResult<>(ApiResult.SYSTEM_ERROR, "resource quota查询失败");
-//        }
-//        if (!resourceQuotaExistRequest.getValue()) {
-//            return new ApiResult<>(ApiResult.NOT_EXIST, "名称为" + resourceQuota.getName() + "的资源限额不存在");
-//        }
-//
-//        try {
-//            coreV1Api.patchNamespacedResourceQuota(
-//                    resourceQuota.getNamespace(),
-//                    resourceQuota.getNamespace(),
-//                    resourceQuota.toV1ResourceQuota(),
-//                    PRETTY_FORMAT);
-//        } catch (ApiException e) {
-//            LoggerUtil.error(logger, e, "更新Resource Quota失败, resourceQuota={}, response={}", resourceQuota, e.getResponseBody());
-//            return new ApiResult<>(e.getCode(), e.getMessage());
-//        }
-//        return new ApiResult<>(null);
-//    }
-//
-//    @Override
-//    public ApiResult<Void> delete(String namespace, String name) {
-//        ApiResult<Namespace> namespaceRequest = namespaceApi.getByName(namespace);
-//        //系统错误
-//        if (!namespaceRequest.isSuccess()) {
-//            return new ApiResult<>(namespaceRequest.getErrorCode(), namespaceRequest.getErrorDescription());
-//        }
-//        //Namespace不存在
-//        if (namespaceRequest.getErrorCode() == ApiResult.NOT_EXIST) {
-//            return new ApiResult<>( ApiResult.NOT_EXIST, "该命名空间不存在");
-//        }
-//
-//
-//        ApiResult<Boolean> resourceQuotaExistRequest = this.exist(namespace, name);
-//        //系统错误
-//        if (!resourceQuotaExistRequest.isSuccess()) {
-//            return new ApiResult<>(ApiResult.SYSTEM_ERROR, "resource quota查询失败");
-//        }
-//        //该资源限额不存在
-//        if (!resourceQuotaExistRequest.getValue()) {
-//            return new ApiResult<>(ApiResult.NOT_EXIST, "该资源限额不存在");
-//        }
-//
-//        V1DeleteOptions deleteOptions = new V1DeleteOptionsBuilder()
-//                .withApiVersion(ResourceQuota.API_VERSION)
-//                .withApiVersion(ResourceQuota.KIND)
-//                .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
-//                .build();
-//
-//        try {
-//            coreV1Api.deleteNamespacedResourceQuota(
-//                    name,
-//                    namespace,
-//                    deleteOptions,
-//                    PRETTY_FORMAT,
-//                    null,
-//                    null,
-//                    FOREGROUND_PROPAGATION_POLICY);
-//        } catch (ApiException e) {
-//            LoggerUtil.error(logger, e, "删除resource quota异常,namespace={}, name={}, response={}",
-//                    namespace, name, e.getResponseBody());
-//        }
-//        return new ApiResult<>(null);
-//    }
-//
-//    @Override
-//    public ResourceQuota getByName(String namespace, String name) throws K8sApiException {
-//        return null;
-//    }
-//
-//    @Override
-//    public boolean exist(String namespace, String name) throws K8sApiException {
-//        try {
-//
-//            V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
-//                    PRETTY_FORMAT,
-//                    null,
-//                    "metadata.name="+name,
-//                    null,
-//                    null,
-//                    null,
-//                    null,
-//                    null,
-//                    null);
-//            return resourceQuotaList.getItems().size() > 0;
-//        } catch (ApiException e) {
-//            LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, response={}", namespace, e.getResponseBody());
-//            return false;
-//        }
-//    }
-//
-//    /**
-//     * @see ResourceQuotaApi#getByNamespace(String)
-//     * @param namespace
-//     * @return
-//     */
-//    @Override
-//    public ApiResult<ResourceQuota> getByNamespace(String namespace) {
-//        try {
-//            V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
-//                    PRETTY_FORMAT,
-//                    null,
-//                    null,
-//                    null,
-//                    null,
-//                    null,
-//                    null,
-//                    null,
-//                    null);
-//            if (resourceQuotaList != null && !CollectionUtils.isEmpty(resourceQuotaList.getItems())) {
-//                List<ResourceQuota> result = resourceQuotaList.getItems().stream().map(ResourceQuota::new).collect(Collectors.toList());
-//                return new ApiResult<>(result.get(0));
-//            }
-//        } catch (ApiException e) {
-//            LoggerUtil.error(logger, e, "查找Resource Quota失败, namespace={}, response={}", namespace, e.getResponseBody());
-//            return new ApiResult<>(e.getCode(), e.getMessage());
-//        }
-//        return new ApiResult<>(null);
-//    }
-//
+
+    private static final Logger logger = LoggerUtil.getLogger(ResourceQuotaApi.class);
+
+    @Autowired
+    private CoreV1Api coreV1Api;
+
+    @Autowired
+    private NamespaceApi namespaceApi;
+
+    /**
+     * @see ResourceQuotaApi#create(ResourceQuota)
+     * @param resourceQuota
+     * @return
+     */
+    @Override
+    public void create(ResourceQuota resourceQuota) throws K8sApiException{
+        if (!namespaceApi.exists(resourceQuota.getNamespace())) {
+            throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
+        }
+
+        if (this.exist(resourceQuota.getNamespace(), resourceQuota.getName())) {
+            throw new K8sApiException(K8sApiException.RESOURCE_QUOTA_ALREADY_EXIST);
+        }
+
+        try {
+            coreV1Api.createNamespacedResourceQuota(
+                    resourceQuota.getNamespace(),
+                    resourceQuota.toV1ResourceQuota(),
+                    PRETTY_FORMAT);
+        } catch (ApiException e) {
+            LoggerUtil.error(logger, e, "创建resource quota失败, resourceQuota={}, response={}",
+                    resourceQuota, e.getResponseBody());
+            throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
+        }
+    }
+
+    /**
+     * @see ResourceQuotaApi#update(ResourceQuota)
+     * @param resourceQuota
+     * @return
+     */
+    @Override
+    public void update(ResourceQuota resourceQuota) throws K8sApiException{
+        if (!namespaceApi.exists(resourceQuota.getNamespace())) {
+            throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
+        }
+
+        if (!this.exist(resourceQuota.getNamespace(), resourceQuota.getName())) {
+            throw new K8sApiException(K8sApiException.RESOURCE_QUOTA_NOT_EXIST);
+        }
+        try {
+            coreV1Api.patchNamespacedResourceQuota(
+                    resourceQuota.getNamespace(),
+                    resourceQuota.getNamespace(),
+                    resourceQuota.toV1ResourceQuota(),
+                    PRETTY_FORMAT);
+        } catch (ApiException e) {
+            LoggerUtil.error(logger, e, "更新Resource Quota失败, resourceQuota={}, response={}", resourceQuota, e.getResponseBody());
+            throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
+        }
+    }
+
+    @Override
+    public void delete(String namespace, String name) throws K8sApiException{
+        if (!namespaceApi.exists(namespace)) {
+            throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
+        }
+        if (!this.exist(namespace, name)) {
+            throw new K8sApiException(K8sApiException.RESOURCE_QUOTA_NOT_EXIST);
+        }
+
+        V1DeleteOptions deleteOptions = new V1DeleteOptionsBuilder()
+                .withApiVersion(ResourceQuota.API_VERSION)
+                .withApiVersion(ResourceQuota.KIND)
+                .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
+                .build();
+
+        try {
+            coreV1Api.deleteNamespacedResourceQuota(
+                    name,
+                    namespace,
+                    deleteOptions,
+                    PRETTY_FORMAT,
+                    null,
+                    null,
+                    FOREGROUND_PROPAGATION_POLICY);
+        } catch (ApiException e) {
+            LoggerUtil.error(logger, e, "删除resource quota异常,namespace={}, name={}, response={}",
+                    namespace, name, e.getResponseBody());
+            throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
+        }
+    }
+
+    @Override
+    public ResourceQuota getByName(String namespace, String name) throws K8sApiException {
+        if (exist(namespace, name)) {
+            return null;
+        }
+        try {
+
+            V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
+                    PRETTY_FORMAT,
+                    null,
+                    "metadata.name="+name,
+                    null,
+                    null,
+                    null,
+                    null,
+                    null,
+                    null);
+            return new ResourceQuota(resourceQuotaList.getItems().get(0));
+        } catch (ApiException e) {
+            LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, response={}", namespace, e.getResponseBody());
+            throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
+        }
+
+    }
+
+    @Override
+    public boolean exist(String namespace, String name) throws K8sApiException {
+        try {
+
+            V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
+                    PRETTY_FORMAT,
+                    null,
+                    "metadata.name="+name,
+                    null,
+                    null,
+                    null,
+                    null,
+                    null,
+                    null);
+            return resourceQuotaList.getItems().size() > 0;
+        } catch (ApiException e) {
+            LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, response={}", namespace, e.getResponseBody());
+            return false;
+        }
+    }
+
 
 }

+ 0 - 1
src/main/java/nju/seec/SEECdemo/logic/api/k8s/vo/ResourceQuota.java

@@ -36,7 +36,6 @@ public class ResourceQuota {
     public ResourceQuota(V1ResourceQuota v1ResourceQuota) {
         this.namespace = v1ResourceQuota.getMetadata().getNamespace();
         this.name = v1ResourceQuota.getMetadata().getName();
-
     }
 
     public V1ResourceQuota toV1ResourceQuota(){

+ 8 - 0
src/main/java/nju/seec/SEECdemo/logic/exception/ServiceException.java

@@ -0,0 +1,8 @@
+package nju.seec.SEECdemo.logic.exception;
+
+/**
+ * author: rale
+ * createdAt: 12/23/18
+ */
+public class ServiceException {
+}