Prechádzať zdrojové kódy

refactor LimitRangeApi 删除过期接口

raledong 7 rokov pred
rodič
commit
a8bc1ded8d

+ 0 - 36
src/main/java/nju/seec/SEECdemo/logic/api/k8s/LimitRangeApi.java

@@ -11,26 +11,6 @@ import java.util.Map;
 
 public interface LimitRangeApi {
 
-    /**
-     * 传入参数转换为V1LimitRange对象
-     *
-     * @param namespace 命名空间名称需要唯一
-     * @param name      LimitRange的资源名称
-     * @param _default  默认资源配额,存放资源名称和配额的键值对,String : cpu or memory (optional)
-     * @param defaultRequest 默认资源请求配额 (optional)
-     * @param max 最大资源配额 (optional)
-     * @param min 最小资源配额 (optional)
-     * note: 如果不需要optional的参数可传入null
-     */
-    V1LimitRange toLimitRange(String namespace, String name, Map<String, Integer> _default, Map<String, Integer> defaultRequest, Map<String, Integer> max, Map<String, Integer> min);
-
-    /**
-     * 传入参数转换为V1LimitRange对象
-     * @param namespace 命名空间名称需要唯一
-     * @param body      配置好的V1LimitRange,可由toLimitRange()获得
-     */
-    V1LimitRange createLimitRange(String namespace, V1LimitRange body) throws K8sApiException;
-
     /**
      * 创建LimitRange
      *
@@ -62,16 +42,6 @@ public interface LimitRangeApi {
      */
     LimitRange getLimitRangeByName(String namespace, String name) throws K8sApiException;
 
-    /**
-     * 更新命名空间内的指定名称的LimitRange
-     *
-     * @param namespace 命名空间名称需要唯一
-     * @param name      LimitRange的资源名称
-     * @param body
-     */
-    V1LimitRange updateLimitRange(String namespace, String name, V1LimitRange body) throws K8sApiException;
-
-
     /**
      * 更新命名空间内的指定名称的LimitRange
      *
@@ -81,11 +51,5 @@ public interface LimitRangeApi {
      */
     LimitRange updateLimitRange(String namespace, String name, LimitRange body) throws K8sApiException;
 
-
-
-
-
-
-
 }
 

+ 33 - 49
src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/LimitRangeApiImpl.java

@@ -10,6 +10,7 @@ import nju.seec.SEECdemo.logic.api.k8s.LimitRangeApi;
 import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
 import nju.seec.SEECdemo.logic.api.k8s.model.LimitRange;
 import nju.seec.SEECdemo.logic.api.k8s.vo.LimitRangeVO;
+import nju.seec.SEECdemo.util.LoggerUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -17,6 +18,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
+
 
 @Service
 public class LimitRangeApiImpl implements LimitRangeApi {
@@ -25,51 +28,18 @@ public class LimitRangeApiImpl implements LimitRangeApi {
     private CoreV1Api coreV1Api;
 
     @Override
-    public V1LimitRange toLimitRange(String namespace, String name, Map<String, Integer> _default, Map<String, Integer> defaultRequest, Map<String, Integer> max, Map<String, Integer> min) {
-        LimitRangeVO limitRangeVO = new LimitRangeVO();
-        limitRangeVO.setName(name);
-        limitRangeVO.setNamespace(namespace);
-        limitRangeVO.set_default(_default);
-        limitRangeVO.setDefault_request(defaultRequest);
-        limitRangeVO.setMax(max);
-        limitRangeVO.setMin(min);
-        return limitRangeVO.toV1LimitRange();
-    }
-
-    @Override
-    public V1LimitRange createLimitRange(String namespace, V1LimitRange body) throws K8sApiException {
-
-        try {
-            return coreV1Api.createNamespacedLimitRange(namespace, body, "OK");
-        } catch (ApiException e) {
-            //namespace NotFound 404
-            //Already Exist 409
-            //422 if default value greater than max value, or min value greater than defaultRequest value
-            switch (e.getCode()) {
-                case K8sApiException.NOT_FOUND:
-                    throw new K8sApiException(K8sApiException.NOT_FOUND);
-                case K8sApiException.ALREADY_EXIST:
-                    throw new K8sApiException(K8sApiException.ALREADY_EXIST);
-                case K8sApiException.INVALID_VALUE:
-                    throw new K8sApiException(K8sApiException.INVALID_VALUE);
-                default:
-                    throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
-            }
-        }
-    }
-
-    @Override
-    public LimitRange createLimitRange(String namespace, LimitRange limitRange) throws K8sApiException {
-        V1LimitRange v1LimitRange =  createLimitRange(namespace, limitRange.toV1LimitRange());
-        return limitRange;
+    public LimitRange createLimitRange(String namespace, LimitRange limitRange) {
+        V1LimitRange v1LimitRange =  limitRange.toV1LimitRange();
+        v1LimitRange = createLimitRange(namespace, v1LimitRange);
+        return v1LimitRange == null ? null : new LimitRange(v1LimitRange);
     }
 
     @Override
-    public void deleteLimitRange(String namespace, String name) throws K8sApiException {
+    public void deleteLimitRange(String namespace, String name) {
         V1DeleteOptions body = new V1DeleteOptions();
         try {
             //暂时没有用到deleteNamespacedSecret()中的其他参数。
-            V1Status v1Status = coreV1Api.deleteNamespacedLimitRange(name, namespace, body, "", 5, false, "");
+            V1Status v1Status = coreV1Api.deleteNamespacedLimitRange(name, namespace, body, PRETTY_FORMAT, 5, false, "");
         } catch (ApiException e) {
             if (e.getCode() == K8sApiException.NOT_FOUND) {
                 throw new K8sApiException(K8sApiException.NOT_FOUND);
@@ -80,7 +50,7 @@ public class LimitRangeApiImpl implements LimitRangeApi {
     }
 
     @Override
-    public List<LimitRange> getLimitRangeList(String namespace) throws K8sApiException {
+    public List<LimitRange> getLimitRangeList(String namespace) {
         try {
             V1LimitRangeList limitRangeList = coreV1Api.listNamespacedLimitRange(namespace, "OK", "", "", false, "", 5, "", 5, false);
             List<V1LimitRange> itemList = limitRangeList.getItems();
@@ -89,7 +59,7 @@ public class LimitRangeApiImpl implements LimitRangeApi {
             } else {
                 List<LimitRange> result = new ArrayList<>();
                 for (V1LimitRange v1LimitRange : itemList)
-                    result.add(LimitRange.toLimitRange(v1LimitRange));
+                    result.add(new LimitRange(v1LimitRange));
                 return result;
             }
         } catch (ApiException e) {
@@ -99,7 +69,7 @@ public class LimitRangeApiImpl implements LimitRangeApi {
     }
 
     @Override
-    public LimitRange getLimitRangeByName(String namespace, String name) throws K8sApiException {
+    public LimitRange getLimitRangeByName(String namespace, String name) {
         List<LimitRange> list = getLimitRangeList(namespace);
         for (LimitRange limitRange : list) {
             if (limitRange.getName().equals(name)) {
@@ -109,18 +79,32 @@ public class LimitRangeApiImpl implements LimitRangeApi {
         throw new K8sApiException(K8sApiException.NOT_FOUND);
     }
 
-    @Override
-    public V1LimitRange updateLimitRange(String namespace, String name, V1LimitRange body) throws K8sApiException {
-        //暂时没研究清楚Api参数,该方法实现为删除并重新创建
-        deleteLimitRange(namespace, name);
-        return createLimitRange(namespace, body);
-    }
 
     @Override
-    public LimitRange updateLimitRange(String namespace, String name, LimitRange body) throws K8sApiException {
+    public LimitRange updateLimitRange(String namespace, String name, LimitRange body) {
         deleteLimitRange(namespace, name);
         return createLimitRange(namespace, body);
     }
 
 
+    private V1LimitRange createLimitRange(String namespace, V1LimitRange body) {
+
+        try {
+            return coreV1Api.createNamespacedLimitRange(namespace, body, PRETTY_FORMAT);
+        } catch (ApiException e) {
+            //namespace NotFound 404
+            //Already Exist 409
+            //422 if default value greater than max value, or min value greater than defaultRequest value
+            switch (e.getCode()) {
+                case K8sApiException.NOT_FOUND:
+                    throw new K8sApiException(K8sApiException.NOT_FOUND);
+                case K8sApiException.ALREADY_EXIST:
+                    throw new K8sApiException(K8sApiException.ALREADY_EXIST);
+                case K8sApiException.INVALID_VALUE:
+                    throw new K8sApiException(K8sApiException.INVALID_VALUE);
+                default:
+                    throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
+            }
+        }
+    }
 }

+ 43 - 37
src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/LimitRange.java

@@ -17,6 +17,12 @@ import static nju.seec.SEECdemo.util.Constants.NAME_PATTERN;
 @Data
 @NoArgsConstructor
 public class LimitRange {
+
+    private static final String API_VERSION = "v1";
+    private static final String KIND = "LimitRange";
+    private static final String CPU_KEY = "cpu";
+    private static final String MEMORY_KEY = "memory";
+
     @NotNull
     private String namespace;
 
@@ -24,89 +30,89 @@ public class LimitRange {
     @Pattern(regexp = NAME_PATTERN)
     private String name;
 
-    private CpuQuantity default_cpu;
-    private StorageQuantity default_memory;
-    private CpuQuantity default_request_cpu;
-    private StorageQuantity default_request_memory;
-    private CpuQuantity max_cpu;
-    private StorageQuantity max_memory;
-    private CpuQuantity min_cpu;
-    private StorageQuantity min_memory;
+    private CpuQuantity defaultCPU;
+    private StorageQuantity defaultMemory;
+    private CpuQuantity defaultRequestCPU;
+    private StorageQuantity defaultRequestMemory;
+    private CpuQuantity maxCpu;
+    private StorageQuantity maxMemory;
+    private CpuQuantity minCpu;
+    private StorageQuantity minMemory;
 
 
     private String type = "Container";
 
-    public static LimitRange toLimitRange(V1LimitRange v1LimitRange) {
+    public LimitRange(V1LimitRange v1LimitRange) {
         LimitRange limitRange = new LimitRange();
         V1LimitRangeItem v1LimitRangeItem = v1LimitRange.getSpec().getLimits().get(0);
         limitRange.setName(v1LimitRange.getMetadata().getName());
         limitRange.setNamespace(v1LimitRange.getMetadata().getNamespace());
         limitRange.setType(v1LimitRangeItem.getType());
         limitRange.setValues(v1LimitRangeItem);
-        return limitRange;
     }
 
+
     public V1LimitRange toV1LimitRange() {
         return new V1LimitRangeBuilder()
-                .withApiVersion("v1")
-                .withKind("LimitRangeVO")
+                .withApiVersion(API_VERSION)
+                .withKind(KIND)
                 .withMetadata(this.toV1ObjectMeta())
                 .withSpec(this.toV1LimitRangeSpec())
                 .build();
     }
 
-    public V1ObjectMeta toV1ObjectMeta() {
+    private V1ObjectMeta toV1ObjectMeta() {
         return new V1ObjectMetaBuilder()
                 .withNamespace(namespace)
                 .withName(name)
                 .build();
     }
 
-    public V1LimitRangeSpec toV1LimitRangeSpec() {
+    private V1LimitRangeSpec toV1LimitRangeSpec() {
         return new V1LimitRangeSpec().addLimitsItem(this.toV1LimitRangeItem());
     }
 
-    public V1LimitRangeItem toV1LimitRangeItem() {
+    private V1LimitRangeItem toV1LimitRangeItem() {
 
         V1LimitRangeItem v1LimitRangeItem = new V1LimitRangeItem();
-        if (!getItemValue(default_cpu, default_memory).isEmpty())
-            v1LimitRangeItem.setDefault(getItemValue(default_cpu, default_memory));
-        if (!getItemValue(default_request_cpu, default_request_memory).isEmpty())
-            v1LimitRangeItem.setDefaultRequest(getItemValue(default_request_cpu, default_request_memory));
-        if (!getItemValue(max_cpu, max_memory).isEmpty())
-            v1LimitRangeItem.setMax(getItemValue(max_cpu, max_memory));
-        if (!getItemValue(min_cpu, min_memory).isEmpty())
-            v1LimitRangeItem.setMin(getItemValue(min_cpu, min_memory));
+        if (!getItemValue(defaultCPU, defaultMemory).isEmpty())
+            v1LimitRangeItem.setDefault(getItemValue(defaultCPU, defaultMemory));
+        if (!getItemValue(defaultRequestCPU, defaultRequestMemory).isEmpty())
+            v1LimitRangeItem.setDefaultRequest(getItemValue(defaultRequestCPU, defaultRequestMemory));
+        if (!getItemValue(maxCpu, maxMemory).isEmpty())
+            v1LimitRangeItem.setMax(getItemValue(maxCpu, maxMemory));
+        if (!getItemValue(minCpu, minMemory).isEmpty())
+            v1LimitRangeItem.setMin(getItemValue(minCpu, minMemory));
         v1LimitRangeItem.setType(type);
         return v1LimitRangeItem;
     }
 
 
-    public Map<String, Quantity> getItemValue(CpuQuantity cpu_value, StorageQuantity memory_value) {
+    private Map<String, Quantity> getItemValue(CpuQuantity cpuValue, StorageQuantity memoryValue) {
         Map<String, Quantity> itemValue = new HashMap<>();
-        if (cpu_value != null) {
-            itemValue.put("cpu", cpu_value.toQuantity());
+        if (cpuValue != null) {
+            itemValue.put(CPU_KEY, cpuValue.toQuantity());
         }
-        if (memory_value != null) {
-            itemValue.put("memory", memory_value.toQuantity());
+        if (memoryValue != null) {
+            itemValue.put(MEMORY_KEY, memoryValue.toQuantity());
         }
         return itemValue;
     }
 
 
-    public void setValues(V1LimitRangeItem v1LimitRangeItem) {
+    private void setValues(V1LimitRangeItem v1LimitRangeItem) {
         Map<String, Quantity> _default = v1LimitRangeItem.getDefault();
         Map<String, Quantity> default_request = v1LimitRangeItem.getDefaultRequest();
         Map<String, Quantity> max = v1LimitRangeItem.getMax();
         Map<String, Quantity> min = v1LimitRangeItem.getMin();
-        setDefault_cpu(CpuQuantity.getCpuQuantity(_default.getOrDefault("cpu", null)));
-        setDefault_memory(StorageQuantity.getStorageQuantity(_default.getOrDefault("memory", null)));
-        setDefault_request_cpu(CpuQuantity.getCpuQuantity(default_request.getOrDefault("cpu", null)));
-        setDefault_request_memory(StorageQuantity.getStorageQuantity(default_request.getOrDefault("memory", null)));
-        setMax_cpu(CpuQuantity.getCpuQuantity(max.getOrDefault("cpu", null)));
-        setMax_memory(StorageQuantity.getStorageQuantity(max.getOrDefault("memory", null)));
-        setMin_cpu(CpuQuantity.getCpuQuantity(min.getOrDefault("cpu", null)));
-        setMin_memory(StorageQuantity.getStorageQuantity(min.getOrDefault("memory", null)));
+        setDefaultCPU(CpuQuantity.getCpuQuantity(_default.getOrDefault(CPU_KEY, null)));
+        setDefaultMemory(StorageQuantity.getStorageQuantity(_default.getOrDefault(MEMORY_KEY, null)));
+        setDefaultRequestCPU(CpuQuantity.getCpuQuantity(default_request.getOrDefault(CPU_KEY, null)));
+        setDefaultRequestMemory(StorageQuantity.getStorageQuantity(default_request.getOrDefault(MEMORY_KEY, null)));
+        setMaxCpu(CpuQuantity.getCpuQuantity(max.getOrDefault(CPU_KEY, null)));
+        setMaxMemory(StorageQuantity.getStorageQuantity(max.getOrDefault(MEMORY_KEY, null)));
+        setMinCpu(CpuQuantity.getCpuQuantity(min.getOrDefault(CPU_KEY, null)));
+        setMinMemory(StorageQuantity.getStorageQuantity(min.getOrDefault(MEMORY_KEY, null)));
     }
 
 }

+ 11 - 9
src/main/java/nju/seec/SEECdemo/logic/service/impl/ProjectServiceImpl.java

@@ -6,6 +6,7 @@ import nju.seec.SEECdemo.logic.api.k8s.LimitRangeApi;
 import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
 import nju.seec.SEECdemo.logic.api.k8s.ResourceQuotaApi;
 import nju.seec.SEECdemo.logic.api.k8s.SecretApi;
+import nju.seec.SEECdemo.logic.api.k8s.model.LimitRange;
 import nju.seec.SEECdemo.logic.api.k8s.model.Namespace;
 import nju.seec.SEECdemo.logic.api.k8s.model.ResourceQuota;
 import nju.seec.SEECdemo.logic.service.DBService;
@@ -79,8 +80,8 @@ public class ProjectServiceImpl implements ProjectService{
             resourceQuotaApi.create(resourceQuota);
 
             //创建项目资源默认配额
-            V1LimitRange v1LimitRange = buildLimitRange(projectName, seecIITemplate.getDefaultResourceConfig());
-            limitRangeApi.createLimitRange(projectName, v1LimitRange);
+            LimitRange limitRange = buildLimitRange(projectName, seecIITemplate.getDefaultResourceConfig());
+            limitRangeApi.createLimitRange(projectName, limitRange);
 
             //创建项目同名数据库
             DBDetailVO dbDetailVO = dbService.createSchemaAndUserForProject(projectName);
@@ -146,12 +147,13 @@ public class ProjectServiceImpl implements ProjectService{
         return resourceQuota;
     }
 
-    private V1LimitRange buildLimitRange(String projectName, ResourceConfig resourceConfig) {
-        //配置应用的默认配额
-        Map<String, Integer> defaultValue = new HashMap<>();
-        defaultValue.put("cpu", 50);
-        defaultValue.put("memory", 500);
-        V1LimitRange v1LimitRange = limitRangeApi.toLimitRange(projectName, projectName, defaultValue, defaultValue, null, null);
-        return v1LimitRange;
+    private LimitRange buildLimitRange(String projectName, ResourceConfig resourceConfig) {
+        LimitRange limitRange = new LimitRange();
+        limitRange.setDefaultCPU(resourceConfig.getDefaultCPU());
+        limitRange.setDefaultMemory(resourceConfig.getDefaultMemory());
+        limitRange.setDefaultRequestCPU(resourceConfig.getDefaultCPU());
+        limitRange.setMaxCpu(resourceConfig.getMaxCPU());
+        limitRange.setMaxMemory(resourceConfig.getMaxMemory());
+        return limitRange;
     }
 }

+ 0 - 8
src/test/java/nju/seec/SEECdemo/service/api/LimitRangeTest.java

@@ -133,12 +133,6 @@ public class LimitRangeTest {
         Map<String, Integer> min1 = new HashMap<>();
         min1.put("cpu", 50);
         min1.put("memory", 512);
-        V1LimitRange body = limitRangeApi.toLimitRange(namespace, name, _default, defaultrequest, max1, min1);
-        try {
-            limitRangeApi.updateLimitRange(namespace, name, body);
-        } catch (K8sApiException e) {
-            e.printStackTrace();
-        }
     }
 
     @Test
@@ -146,7 +140,5 @@ public class LimitRangeTest {
         Map<String, Integer> _default = new HashMap<>();
         _default.put("cpu", 100);
         _default.put("memory", 1024);
-        V1LimitRange v1LimitRange = limitRangeApi.toLimitRange("demo", "demo", _default, _default, null, null);
-        limitRangeApi.createLimitRange("demo", v1LimitRange);
     }
 }