Quellcode durchsuchen

调整代码结构

raledong vor 7 Jahren
Ursprung
Commit
daf13fbc6b

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

@@ -2,7 +2,6 @@ package nju.seec.SEECdemo.logic.api.k8s;
 
 import nju.seec.SEECdemo.logic.api.k8s.dto.DeploymentDTO;
 import nju.seec.SEECdemo.logic.api.k8s.vo.ApiResult;
-import nju.seec.SEECdemo.logic.api.k8s.vo.Deployment;
 
 /**
  * author: rale

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

@@ -1,5 +1,6 @@
 package nju.seec.SEECdemo.logic.api.k8s;
 
+import nju.seec.SEECdemo.logic.api.k8s.dto.ResourceQuotaDTO;
 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.ResourceQuota;
@@ -11,14 +12,14 @@ public interface ResourceQuotaApi {
      * @param resourceQuota
      * @return
      */
-    void create(ResourceQuota resourceQuota) throws K8sApiException;
+    void create(ResourceQuotaDTO resourceQuota) throws K8sApiException;
 
     /**
      * 更新资源限额
      * @param resourceQuota
      * @return
      */
-    void update(ResourceQuota resourceQuota) throws K8sApiException;
+    void update(ResourceQuotaDTO resourceQuota) throws K8sApiException;
 
     /**
      * 删除namespace下名为name的资源限额

+ 77 - 0
src/main/java/nju/seec/SEECdemo/logic/api/k8s/dto/ResourceQuotaDTO.java

@@ -0,0 +1,77 @@
+package nju.seec.SEECdemo.logic.api.k8s.dto;
+
+import io.kubernetes.client.custom.Quantity;
+import io.kubernetes.client.models.*;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import nju.seec.SEECdemo.logic.api.k8s.util.CpuQuantity;
+import nju.seec.SEECdemo.logic.api.k8s.util.StorageQuantity;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * author: rale
+ * createdAt: 1/4/19
+ */
+@Data
+@NoArgsConstructor
+public class ResourceQuotaDTO {
+    public static final String API_VERSION = "v1";
+
+    public static final String KIND = "ResourceQuota";
+
+    private static final String HARD_REQUEST_CPU = "requests.cpu";
+    private static final String HARD_REQUEST_MEMORY = "requests.memory";
+    private static final String HARD_LIMITS_CPU = "limits.cpu";
+    private static final String HARD_LIMITS_MEMORY = "limits.memory";
+
+    private String namespace;
+
+    private String name;
+
+    private CpuQuantity maxCpuTotalRequest;
+
+    private StorageQuantity maxMemoryTotalRequest;
+
+    private CpuQuantity maxCpuTotalLimit;
+
+    private StorageQuantity maxMemoryTotalLimit;
+
+    public ResourceQuotaDTO(V1ResourceQuota v1ResourceQuota) {
+        this.namespace = v1ResourceQuota.getMetadata().getNamespace();
+        this.name = v1ResourceQuota.getMetadata().getName();
+    }
+
+    public V1ResourceQuota toV1ResourceQuota(){
+        V1ResourceQuota v1ResourceQuota = new V1ResourceQuotaBuilder()
+                .withApiVersion(API_VERSION)
+                .withKind(KIND)
+                .withMetadata(this.toV1ObjectMeta())
+                .withSpec(this.toV1ResourceQuotaSpec())
+                .build();
+        return v1ResourceQuota;
+    }
+
+    private V1ResourceQuotaSpec toV1ResourceQuotaSpec(){
+        Map<String, Quantity> map = new HashMap<>();
+        map.put(HARD_REQUEST_CPU, maxCpuTotalRequest.toQuantity());
+        map.put(HARD_LIMITS_CPU, maxCpuTotalLimit.toQuantity());
+        map.put(HARD_REQUEST_MEMORY, maxMemoryTotalLimit.toQuantity());
+        map.put(HARD_LIMITS_MEMORY, maxMemoryTotalLimit.toQuantity());
+
+        V1ResourceQuotaSpec v1ResourceQuotaSpec = new V1ResourceQuotaSpecBuilder()
+                .withHard(map)
+                .build();
+        return v1ResourceQuotaSpec;
+    }
+
+    private V1ObjectMeta toV1ObjectMeta(){
+        V1ObjectMeta v1ObjectMeta = new V1ObjectMetaBuilder()
+                .withNamespace(namespace)
+                .withName(name)
+                .build();
+        return v1ObjectMeta;
+    }
+
+}

+ 5 - 4
src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/ResourceQuotaImpl.java

@@ -10,6 +10,7 @@ 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.dto.ResourceQuotaDTO;
 import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
 import nju.seec.SEECdemo.logic.api.k8s.vo.ResourceQuota;
 import nju.seec.SEECdemo.util.LoggerUtil;
@@ -33,12 +34,12 @@ public class ResourceQuotaImpl implements ResourceQuotaApi {
     private NamespaceApi namespaceApi;
 
     /**
-     * @see ResourceQuotaApi#create(ResourceQuota)
+     * @see ResourceQuotaApi#create(nju.seec.SEECdemo.logic.api.k8s.dto.ResourceQuotaDTO)
      * @param resourceQuota
      * @return
      */
     @Override
-    public void create(ResourceQuota resourceQuota) throws K8sApiException{
+    public void create(ResourceQuotaDTO resourceQuota) throws K8sApiException{
         if (!namespaceApi.exists(resourceQuota.getNamespace())) {
             throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
         }
@@ -61,12 +62,12 @@ public class ResourceQuotaImpl implements ResourceQuotaApi {
     }
 
     /**
-     * @see ResourceQuotaApi#update(ResourceQuota)
+     * @see ResourceQuotaApi#update(ResourceQuotaDTO)
      * @param resourceQuota
      * @return
      */
     @Override
-    public void update(ResourceQuota resourceQuota) throws K8sApiException{
+    public void update(ResourceQuotaDTO resourceQuota) throws K8sApiException{
         if (!namespaceApi.exists(resourceQuota.getNamespace())) {
             throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
         }

+ 12 - 6
src/main/java/nju/seec/SEECdemo/logic/api/k8s/util/CpuQuantity.java

@@ -10,9 +10,10 @@ import java.math.BigDecimal;
 @NoArgsConstructor
 public class CpuQuantity {
 
-    private String quantity;
+    private Quantity quantity;
 
     private Unit unit = Unit.Mi;
+
     //Cpu单位
     public enum Unit{
         Mi("m");
@@ -22,23 +23,28 @@ public class CpuQuantity {
         Unit(String code) {
             this.code = code;
         }
+
+        public String getCode(){
+            return this.code;
+        }
     }
     public CpuQuantity(int quantity){
         //转化成百分比
-        this.quantity = String.valueOf(quantity);
+        this.quantity = Quantity.fromString(quantity + unit.getCode());
     }
 
     public CpuQuantity(long quantity) {
-        this.quantity = String.valueOf(quantity);
+        this.quantity = Quantity.fromString(quantity + unit.getCode());
     }
 
 
+    public CpuQuantity(Quantity quantity) {
+        this.quantity = quantity;
+    }
+
     public Quantity toQuantity(){
         return Quantity.fromString(quantity + unit.code);
     }
 
-    public int getQuantityIntValue(){
-        return Integer.parseInt(quantity);
-    }
 
 }

+ 14 - 5
src/main/java/nju/seec/SEECdemo/logic/api/k8s/util/StorageQuantity.java

@@ -11,7 +11,7 @@ import java.math.BigDecimal;
 @NoArgsConstructor
 public class StorageQuantity {
 
-    private String quantity;
+    private Quantity quantity;
 
     //存储的默认单位为MB
     private Unit unit = Unit.MB;
@@ -27,12 +27,16 @@ public class StorageQuantity {
         Unit(String code) {
             this.code = code;
         }
+
+        public String getCode() {
+            return code;
+        }
     }
 
     public static final StorageQuantity EMPTY = new StorageQuantity(0);
 
     public StorageQuantity(int quantity){
-        this.quantity = String.valueOf(quantity);
+        this.quantity = Quantity.fromString(quantity + unit.getCode());
     }
 
     public StorageQuantity(int quantity, Unit unit) {
@@ -40,11 +44,16 @@ public class StorageQuantity {
         this.unit = unit;
     }
 
+    public StorageQuantity(Quantity quantity) {
+        this.quantity = quantity;
+    }
+
     public Quantity toQuantity(){
-        return Quantity.fromString(this.quantity + unit.code);
+        return quantity;
     }
 
-    public int getQuantityIntValue() {
-        return Integer.parseInt(quantity);
+    public String getQuantity() {
+        return quantity.toSuffixedString();
     }
+
 }

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

@@ -40,35 +40,5 @@ public class ResourceQuota {
         this.name = v1ResourceQuota.getMetadata().getName();
     }
 
-    public V1ResourceQuota toV1ResourceQuota(){
-        V1ResourceQuota v1ResourceQuota = new V1ResourceQuotaBuilder()
-                .withApiVersion(API_VERSION)
-                .withKind(KIND)
-                .withMetadata(this.toV1ObjectMeta())
-                .withSpec(this.toV1ResourceQuotaSpec())
-                .build();
-        return v1ResourceQuota;
-    }
-
-    private V1ResourceQuotaSpec toV1ResourceQuotaSpec(){
-        Map<String, Quantity> map = new HashMap<>();
-        map.put(HARD_REQUEST_CPU, maxCpuTotalRequest.toQuantity());
-        map.put(HARD_LIMITS_CPU, maxCpuTotalLimit.toQuantity());
-        map.put(HARD_REQUEST_MEMORY, maxMemoryTotalLimit.toQuantity());
-        map.put(HARD_LIMITS_MEMORY, maxMemoryTotalLimit.toQuantity());
-
-        V1ResourceQuotaSpec v1ResourceQuotaSpec = new V1ResourceQuotaSpecBuilder()
-                .withHard(map)
-                .build();
-        return v1ResourceQuotaSpec;
-    }
-
-    private V1ObjectMeta toV1ObjectMeta(){
-        V1ObjectMeta v1ObjectMeta = new V1ObjectMetaBuilder()
-                .withNamespace(namespace)
-                .withName(name)
-                .build();
-        return v1ObjectMeta;
-    }
 
 }

+ 23 - 13
src/main/java/nju/seec/SEECdemo/logic/service/impl/ProjectServiceImpl.java

@@ -7,6 +7,7 @@ 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.dto.NamespaceDTO;
+import nju.seec.SEECdemo.logic.api.k8s.dto.ResourceQuotaDTO;
 import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
 import nju.seec.SEECdemo.logic.api.k8s.vo.*;
 import nju.seec.SEECdemo.logic.service.DBService;
@@ -74,21 +75,11 @@ public class ProjectServiceImpl implements ProjectService{
             secretApi.createPrivateRegistrySecret(projectName, registry.getHost(), registry.getUsername(), registry.getPassword());
 
             //创建项目资源限额
-            ResourceConfig resourceConfig = seecIITemplate.getDefaultResourceConfig();
-            ResourceQuota resourceQuota = new ResourceQuota();
-            resourceQuota.setNamespace(projectName);
-            resourceQuota.setName(projectName);
-            resourceQuota.setMaxMemoryTotalRequest(resourceConfig.getMaxMemory());
-            resourceQuota.setMaxMemoryTotalLimit(resourceConfig.getMaxMemory());
-            resourceQuota.setMaxCpuTotalRequest(resourceConfig.getMaxCPU());
-            resourceQuota.setMaxCpuTotalLimit(resourceConfig.getMaxCPU());
+            ResourceQuotaDTO resourceQuota = buildResourceQuota(projectName, seecIITemplate.getDefaultResourceConfig());
             resourceQuotaApi.create(resourceQuota);
 
-            //配置应用的默认配额
-            Map<String, Integer> defaultValue = new HashMap<>();
-            defaultValue.put("cpu", resourceConfig.getDefaultCPU().getQuantityIntValue());
-            defaultValue.put("memory", resourceConfig.getDefaultMemory().getQuantityIntValue());
-            V1LimitRange v1LimitRange = limitRangeApi.toLimitRange(projectName, projectName, defaultValue, defaultValue, null, null);
+            //创建项目资源默认配额
+            V1LimitRange v1LimitRange = buildLimitRange(projectName, seecIITemplate.getDefaultResourceConfig());
             limitRangeApi.createLimitRange(projectName, v1LimitRange);
 
             //创建项目同名数据库
@@ -144,4 +135,23 @@ public class ProjectServiceImpl implements ProjectService{
         return namespace;
     }
 
+    private ResourceQuotaDTO buildResourceQuota(String projectName, ResourceConfig resourceConfig) {
+        ResourceQuotaDTO resourceQuota = new ResourceQuotaDTO();
+        resourceQuota.setNamespace(projectName);
+        resourceQuota.setName(projectName);
+        resourceQuota.setMaxMemoryTotalRequest(resourceConfig.getMaxMemory());
+        resourceQuota.setMaxMemoryTotalLimit(resourceConfig.getMaxMemory());
+        resourceQuota.setMaxCpuTotalRequest(resourceConfig.getMaxCPU());
+        resourceQuota.setMaxCpuTotalLimit(resourceConfig.getMaxCPU());
+        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;
+    }
 }