Browse Source

Merge remote-tracking branch 'origin/platform-dev2' into platform-dev

raledong 7 năm trước cách đây
mục cha
commit
5e077eae70

+ 28 - 2
src/main/java/nju/seec/SEECdemo/logic/api/k8s/LimitRangeApi.java

@@ -1,7 +1,9 @@
 package nju.seec.SEECdemo.logic.api.k8s;
 
 import io.kubernetes.client.models.V1LimitRange;
+import io.kubernetes.client.models.V1LimitRangeItem;
 import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
+import nju.seec.SEECdemo.logic.api.k8s.model.LimitRange;
 
 import java.util.List;
 import java.util.Map;
@@ -29,6 +31,14 @@ public interface LimitRangeApi {
      */
     V1LimitRange createLimitRange(String namespace, V1LimitRange body) throws K8sApiException;
 
+    /**
+     * 创建LimitRange
+     *
+     * @param namespace 命名空间名称需要唯一
+     * @param limitRange 配置好的LimitRange对象
+     */
+    LimitRange createLimitRange(String namespace, LimitRange limitRange) throws K8sApiException;
+
     /**
      * 删除命名空间内的指定名称的LimitRange
      *
@@ -42,7 +52,7 @@ public interface LimitRangeApi {
      *
      * @param namespace 命名空间名称需要唯一
      */
-    List<V1LimitRange> getLimitRangeList(String namespace) throws K8sApiException;
+    List<LimitRange> getLimitRangeList(String namespace) throws K8sApiException;
 
     /**
      * 获取命名空间内的指定名称的LimitRange
@@ -50,7 +60,7 @@ public interface LimitRangeApi {
      * @param namespace 命名空间名称需要唯一
      * @param name      LimitRange的资源名称
      */
-    V1LimitRange getLimitRangeByName(String namespace, String name) throws K8sApiException;
+    LimitRange getLimitRangeByName(String namespace, String name) throws K8sApiException;
 
     /**
      * 更新命名空间内的指定名称的LimitRange
@@ -61,5 +71,21 @@ public interface LimitRangeApi {
      */
     V1LimitRange updateLimitRange(String namespace, String name, V1LimitRange body) throws K8sApiException;
 
+
+    /**
+     * 更新命名空间内的指定名称的LimitRange
+     *
+     * @param namespace 命名空间名称需要唯一
+     * @param name      LimitRange的资源名称
+     * @param body
+     */
+    LimitRange updateLimitRange(String namespace, String name, LimitRange body) throws K8sApiException;
+
+
+
+
+
+
+
 }
 

+ 35 - 17
src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/LimitRangeApiImpl.java

@@ -8,10 +8,12 @@ import io.kubernetes.client.models.V1LimitRangeList;
 import io.kubernetes.client.models.V1Status;
 import nju.seec.SEECdemo.logic.api.k8s.LimitRangeApi;
 import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
-import nju.seec.SEECdemo.logic.api.k8s.vo.LimitRange;
+import nju.seec.SEECdemo.logic.api.k8s.model.LimitRange;
+import nju.seec.SEECdemo.logic.api.k8s.vo.LimitRangeVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -24,14 +26,14 @@ public class LimitRangeApiImpl implements LimitRangeApi {
 
     @Override
     public V1LimitRange toLimitRange(String namespace, String name, Map<String, Integer> _default, Map<String, Integer> defaultRequest, Map<String, Integer> max, Map<String, Integer> min) {
-        LimitRange limitRange = new LimitRange();
-        limitRange.setName(name);
-        limitRange.setNamespace(namespace);
-        limitRange.set_default(_default);
-        limitRange.setDefault_request(defaultRequest);
-        limitRange.setMax(max);
-        limitRange.setMin(min);
-        return limitRange.toV1LimitRange();
+        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
@@ -56,6 +58,12 @@ public class LimitRangeApiImpl implements LimitRangeApi {
         }
     }
 
+    @Override
+    public LimitRange createLimitRange(String namespace, LimitRange limitRange) throws K8sApiException {
+        V1LimitRange v1LimitRange =  createLimitRange(namespace, limitRange.toV1LimitRange());
+        return limitRange;
+    }
+
     @Override
     public void deleteLimitRange(String namespace, String name) throws K8sApiException {
         V1DeleteOptions body = new V1DeleteOptions();
@@ -72,27 +80,29 @@ public class LimitRangeApiImpl implements LimitRangeApi {
     }
 
     @Override
-    public List<V1LimitRange> getLimitRangeList(String namespace) throws K8sApiException {
+    public List<LimitRange> getLimitRangeList(String namespace) throws K8sApiException {
         try {
             V1LimitRangeList limitRangeList = coreV1Api.listNamespacedLimitRange(namespace, "OK", "", "", false, "", 5, "", 5, false);
-            List<V1LimitRange> result = limitRangeList.getItems();
-            if (result.isEmpty()) {
+            List<V1LimitRange> itemList = limitRangeList.getItems();
+            if (itemList.isEmpty()) {
                 throw new K8sApiException(K8sApiException.NOT_FOUND);
             } else {
+                List<LimitRange> result = new ArrayList<>();
+                for (V1LimitRange v1LimitRange : itemList)
+                    result.add(LimitRange.toLimitRange(v1LimitRange));
                 return result;
             }
         } catch (ApiException e) {
             //如果namespace不存在也不会抛出异常,会返回一个空的list
             throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
         }
-
     }
 
     @Override
-    public V1LimitRange getLimitRangeByName(String namespace, String name) throws K8sApiException {
-        List<V1LimitRange> list = getLimitRangeList(namespace);
-        for (V1LimitRange limitRange : list) {
-            if (limitRange.getMetadata().getName().equals(name)) {
+    public LimitRange getLimitRangeByName(String namespace, String name) throws K8sApiException {
+        List<LimitRange> list = getLimitRangeList(namespace);
+        for (LimitRange limitRange : list) {
+            if (limitRange.getName().equals(name)) {
                 return limitRange;
             }
         }
@@ -105,4 +115,12 @@ public class LimitRangeApiImpl implements LimitRangeApi {
         deleteLimitRange(namespace, name);
         return createLimitRange(namespace, body);
     }
+
+    @Override
+    public LimitRange updateLimitRange(String namespace, String name, LimitRange body) throws K8sApiException {
+        deleteLimitRange(namespace, name);
+        return createLimitRange(namespace, body);
+    }
+
+
 }

+ 9 - 0
src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/CpuQuantity.java

@@ -56,4 +56,13 @@ public class CpuQuantity {
     public String getQuantity() {
         return quantity.toSuffixedString();
     }
+
+    public static CpuQuantity getCpuQuantity(Quantity quantity) {
+        if (quantity == null) {
+            return null;
+        } else {
+            return new CpuQuantity(quantity);
+        }
+    }
+
 }

+ 112 - 0
src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/LimitRange.java

@@ -0,0 +1,112 @@
+package nju.seec.SEECdemo.logic.api.k8s.model;
+
+
+import io.kubernetes.client.custom.Quantity;
+import io.kubernetes.client.models.*;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static nju.seec.SEECdemo.util.Constants.NAME_PATTERN;
+
+@Data
+@NoArgsConstructor
+public class LimitRange {
+    @NotNull
+    private String namespace;
+
+    @NotNull
+    @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 String type = "Container";
+
+    public static LimitRange toLimitRange(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")
+                .withMetadata(this.toV1ObjectMeta())
+                .withSpec(this.toV1LimitRangeSpec())
+                .build();
+    }
+
+    public V1ObjectMeta toV1ObjectMeta() {
+        return new V1ObjectMetaBuilder()
+                .withNamespace(namespace)
+                .withName(name)
+                .build();
+    }
+
+    public V1LimitRangeSpec toV1LimitRangeSpec() {
+        return new V1LimitRangeSpec().addLimitsItem(this.toV1LimitRangeItem());
+    }
+
+    public 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));
+        v1LimitRangeItem.setType(type);
+        return v1LimitRangeItem;
+    }
+
+
+    public Map<String, Quantity> getItemValue(CpuQuantity cpu_value, StorageQuantity memory_value) {
+        Map<String, Quantity> itemValue = new HashMap<>();
+        if (cpu_value != null) {
+            itemValue.put("cpu", cpu_value.toQuantity());
+        }
+        if (memory_value != null) {
+            itemValue.put("memory", memory_value.toQuantity());
+        }
+        return itemValue;
+    }
+
+
+    public 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)));
+    }
+
+}

+ 8 - 0
src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/StorageQuantity.java

@@ -61,4 +61,12 @@ public class StorageQuantity {
         return quantity.toSuffixedString();
     }
 
+    public static StorageQuantity getStorageQuantity(Quantity quantity) {
+        if (quantity == null) {
+            return null;
+        } else {
+            return new StorageQuantity(quantity);
+        }
+    }
+
 }

+ 0 - 83
src/main/java/nju/seec/SEECdemo/logic/api/k8s/vo/LimitRange.java

@@ -1,83 +0,0 @@
-package nju.seec.SEECdemo.logic.api.k8s.vo;
-
-import io.kubernetes.client.custom.Quantity;
-import io.kubernetes.client.models.*;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-import nju.seec.SEECdemo.logic.api.k8s.model.CpuQuantity;
-import nju.seec.SEECdemo.logic.api.k8s.model.StorageQuantity;
-
-import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Pattern;
-import java.util.HashMap;
-import java.util.Map;
-
-import static nju.seec.SEECdemo.util.Constants.NAME_PATTERN;
-
-/**
- * author: mjj
- * createdAt: 2018/12/30
- * description:
- */
-
-@Data
-@NoArgsConstructor
-public class LimitRange {
-
-    @NotNull
-    private String namespace;
-
-    @NotNull
-    @Pattern(regexp = NAME_PATTERN)
-    private String name;
-
-    private Map<String, Integer> _default;
-    private Map<String, Integer> default_request;
-    private Map<String, Integer> max;
-    private Map<String, Integer> min;
-    private String type = "Container";
-
-
-
-    public V1LimitRange toV1LimitRange() {
-        return new V1LimitRangeBuilder()
-                .withApiVersion("v1")
-                .withKind("LimitRange")
-                .withMetadata(this.toV1ObjectMeta())
-                .withSpec(this.toV1LimitRangeSpec())
-                .build();
-    }
-
-    public V1ObjectMeta toV1ObjectMeta() {
-        return new V1ObjectMetaBuilder()
-                .withNamespace(namespace)
-                .withName(name)
-                .build();
-    }
-
-    public V1LimitRangeSpec toV1LimitRangeSpec() {
-        return new V1LimitRangeSpec().addLimitsItem(this.toV1LimitRangeItem());
-    }
-
-    public V1LimitRangeItem toV1LimitRangeItem() {
-        V1LimitRangeItem v1LimitRangeItem = new V1LimitRangeItem();
-        if (_default != null) v1LimitRangeItem.setDefault(exchange(_default));
-        if (default_request != null) v1LimitRangeItem.setDefaultRequest(exchange(default_request));
-        if (max != null) v1LimitRangeItem.setMax(exchange(max));
-        if (min != null) v1LimitRangeItem.setMin(exchange(min));
-        v1LimitRangeItem.setType(type);
-        return v1LimitRangeItem;
-    }
-
-    public Map<String, Quantity> exchange(Map<String, Integer> setting) {
-        Map<String, Quantity> result = new HashMap<>();
-        for (Map.Entry<String, Integer> entry : setting.entrySet()) {
-            if (entry.getKey().equals("cpu")) {
-                result.put(entry.getKey(), new CpuQuantity(entry.getValue()).toQuantity());
-            } else if (entry.getKey().equals("memory")) {
-                result.put(entry.getKey(), new StorageQuantity(entry.getValue()).toQuantity());
-            }
-        }
-        return result;
-    }
-}

+ 79 - 0
src/main/java/nju/seec/SEECdemo/logic/api/k8s/vo/LimitRangeVO.java

@@ -1,4 +1,83 @@
 package nju.seec.SEECdemo.logic.api.k8s.vo;
 
+import io.kubernetes.client.custom.Quantity;
+import io.kubernetes.client.models.*;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import nju.seec.SEECdemo.logic.api.k8s.model.CpuQuantity;
+import nju.seec.SEECdemo.logic.api.k8s.model.StorageQuantity;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+import java.util.HashMap;
+import java.util.Map;
+
+import static nju.seec.SEECdemo.util.Constants.NAME_PATTERN;
+
+/**
+ * author: mjj
+ * createdAt: 2018/12/30
+ * description:
+ */
+
+@Data
+@NoArgsConstructor
 public class LimitRangeVO {
+
+    @NotNull
+    private String namespace;
+
+    @NotNull
+    @Pattern(regexp = NAME_PATTERN)
+    private String name;
+
+    private Map<String, Integer> _default;
+    private Map<String, Integer> default_request;
+    private Map<String, Integer> max;
+    private Map<String, Integer> min;
+    private String type = "Container";
+
+
+
+    public V1LimitRange toV1LimitRange() {
+        return new V1LimitRangeBuilder()
+                .withApiVersion("v1")
+                .withKind("LimitRangeVO")
+                .withMetadata(this.toV1ObjectMeta())
+                .withSpec(this.toV1LimitRangeSpec())
+                .build();
+    }
+
+    public V1ObjectMeta toV1ObjectMeta() {
+        return new V1ObjectMetaBuilder()
+                .withNamespace(namespace)
+                .withName(name)
+                .build();
+    }
+
+    public V1LimitRangeSpec toV1LimitRangeSpec() {
+        return new V1LimitRangeSpec().addLimitsItem(this.toV1LimitRangeItem());
+    }
+
+    public V1LimitRangeItem toV1LimitRangeItem() {
+        V1LimitRangeItem v1LimitRangeItem = new V1LimitRangeItem();
+        if (_default != null) v1LimitRangeItem.setDefault(exchange(_default));
+        if (default_request != null) v1LimitRangeItem.setDefaultRequest(exchange(default_request));
+        if (max != null) v1LimitRangeItem.setMax(exchange(max));
+        if (min != null) v1LimitRangeItem.setMin(exchange(min));
+        v1LimitRangeItem.setType(type);
+        return v1LimitRangeItem;
+    }
+
+    public Map<String, Quantity> exchange(Map<String, Integer> setting) {
+        Map<String, Quantity> result = new HashMap<>();
+        for (Map.Entry<String, Integer> entry : setting.entrySet()) {
+            if (entry.getKey().equals("cpu")) {
+                result.put(entry.getKey(), new CpuQuantity(entry.getValue()).toQuantity());
+            } else if (entry.getKey().equals("memory")) {
+                result.put(entry.getKey(), new StorageQuantity(entry.getValue()).toQuantity());
+            }
+        }
+        return result;
+    }
 }

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

@@ -50,7 +50,7 @@ public class LimitRangeTest {
                 .min(min)
                 .type(type);
         V1ObjectMeta meta = new V1ObjectMeta().name("limit-range-test2").namespace("mjj-test2");
-        String kind = "LimitRange";
+        String kind = "LimitRangeVO";
         V1LimitRange body = new V1LimitRange()
                 .apiVersion(apiVersion)
                 .kind(kind)
@@ -65,7 +65,7 @@ public class LimitRangeTest {
 
 
         //Test update
-        //LimitRange Not Found 404
+        //LimitRangeVO Not Found 404
 
         /*try {
             String body1 = "{ \"defualt\" : {\"cpu\" : 1, \"memory\": 1024Mi}}";
@@ -106,7 +106,7 @@ public class LimitRangeTest {
         }*/
 
 
-        //Test get LimitRange
+        //Test get LimitRangeVO
         /*String namespace = "mjj-test2";
         String name = "limit-range-test2";
         try {
@@ -118,7 +118,7 @@ public class LimitRangeTest {
 
 
 
-        //Test update LimitRange
+        //Test update LimitRangeVO
         String namespace = "mjj-test2";
         String name = "limit-range-test2";
         Map<String, Integer> _default = new HashMap<>();