Ver Fonte

Merge remote-tracking branch 'remotes/origin/platform-dev'

raledong há 7 anos atrás
pai
commit
990ec7c527
23 ficheiros alterados com 456 adições e 183 exclusões
  1. 7 0
      pom.xml
  2. 27 0
      src/main/java/nju/seec/SEECdemo/config/WebSocketConfig.java
  3. 28 2
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/LimitRangeApi.java
  4. 46 2
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/DeploymentApiImpl.java
  5. 35 17
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/LimitRangeApiImpl.java
  6. 1 1
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/NamespaceApiImpl.java
  7. 3 1
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/Container.java
  8. 9 0
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/CpuQuantity.java
  9. 112 0
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/LimitRange.java
  10. 8 0
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/StorageQuantity.java
  11. 0 83
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/vo/LimitRange.java
  12. 79 0
      src/main/java/nju/seec/SEECdemo/logic/api/k8s/vo/LimitRangeVO.java
  13. 2 1
      src/main/java/nju/seec/SEECdemo/logic/service/ApplicationService.java
  14. 2 2
      src/main/java/nju/seec/SEECdemo/logic/service/ProjectService.java
  15. 20 9
      src/main/java/nju/seec/SEECdemo/logic/service/impl/ApplicationServiceImpl.java
  16. 2 2
      src/main/java/nju/seec/SEECdemo/logic/service/impl/ProjectServiceImpl.java
  17. 4 4
      src/main/java/nju/seec/SEECdemo/util/BeanUtil.java
  18. 5 0
      src/test/java/nju/seec/SEECdemo/service/ApplicationServiceImplTest.java
  19. 1 0
      src/test/java/nju/seec/SEECdemo/service/ProjectServiceImplTest.java
  20. 34 6
      src/test/java/nju/seec/SEECdemo/service/api/DeploymentApiImplTest.java
  21. 4 4
      src/test/java/nju/seec/SEECdemo/service/api/LimitRangeTest.java
  22. 27 1
      src/test/java/nju/seec/SEECdemo/service/api/SecretApiImplTest.java
  23. 0 48
      src/test/java/nju/seec/SEECdemo/service/api/SecretApiTest.java

+ 7 - 0
pom.xml

@@ -46,6 +46,7 @@
 			<artifactId>spring-boot-configuration-processor</artifactId>
 		</dependency>
 
+
 		<!-- Hibernate Dependencies -->
 		<dependency>
 			<groupId>org.hibernate</groupId>
@@ -106,6 +107,12 @@
 			<version>1.12</version>
 		</dependency>
 
+        <!--websocket-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
+        </dependency>
+
 		<!-- Database Driver Dependencies -->
 		<dependency>
 			<groupId>mysql</groupId>

+ 27 - 0
src/main/java/nju/seec/SEECdemo/config/WebSocketConfig.java

@@ -0,0 +1,27 @@
+package nju.seec.SEECdemo.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.messaging.simp.config.MessageBrokerRegistry;
+import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
+import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
+import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
+
+/**
+ * author: rale
+ * createdAt: 1/12/19
+ */
+@Configuration
+@EnableWebSocketMessageBroker
+public class WebSocketConfig  implements WebSocketMessageBrokerConfigurer {
+
+    @Override
+    public void configureMessageBroker(MessageBrokerRegistry config) {
+        config.enableSimpleBroker("/topic");
+        config.setApplicationDestinationPrefixes("/app");
+    }
+
+    @Override
+    public void registerStompEndpoints(StompEndpointRegistry registry) {
+        registry.addEndpoint("/ws").withSockJS();
+    }
+}

+ 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;
+
+
+
+
+
+
+
 }
 

+ 46 - 2
src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/DeploymentApiImpl.java

@@ -1,5 +1,7 @@
 package nju.seec.SEECdemo.logic.api.k8s.impl;
 
+import com.google.gson.JsonSyntaxException;
+import io.kubernetes.client.ApiCallback;
 import io.kubernetes.client.ApiException;
 import io.kubernetes.client.apis.AppsV1Api;
 import io.kubernetes.client.models.*;
@@ -11,6 +13,9 @@ import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+import java.util.Map;
+
 import static nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException.ALREADY_EXIST;
 import static nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException.NOT_FOUND;
 import static nju.seec.SEECdemo.util.Constants.FOREGROUND_PROPAGATION_POLICY;
@@ -52,6 +57,40 @@ public class DeploymentApiImpl implements DeploymentApi{
 
     @Override
     public Deployment createAsync(Deployment deployment) {
+        V1Deployment v1Deployment = deployment.toV1Deployment();
+        try {
+            appsV1Api.createNamespacedDeploymentAsync(deployment.getNamespace(), v1Deployment, PRETTY_FORMAT, new ApiCallback<V1Deployment>() {
+                @Override
+                public void onFailure(ApiException e, int i, Map<String, List<String>> map) {
+                    System.out.println("fail");
+                    System.out.println(e);
+                    System.out.println(i);
+                    System.out.println(map);
+                }
+
+                @Override
+                public void onSuccess(V1Deployment v1Deployment, int i, Map<String, List<String>> map) {
+                    System.out.println("success");
+                    System.out.println(v1Deployment);
+                    System.out.println(i);
+                    System.out.println(map);
+                }
+
+                @Override
+                public void onUploadProgress(long l, long l1, boolean b) {
+                    System.out.println("on upload progress");
+                }
+
+                @Override
+                public void onDownloadProgress(long l, long l1, boolean b) {
+                    System.out.println("on download progress " + l);
+                    System.out.println("on download progress " + l1);
+                    System.out.println("on download progress " + b);
+                }
+            });
+        } catch (ApiException e) {
+            e.printStackTrace();
+        }
         return null;
     }
 
@@ -69,7 +108,9 @@ public class DeploymentApiImpl implements DeploymentApi{
             }else {
                 throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
             }
-        }catch (Exception e) {
+        } catch (JsonSyntaxException e) {
+            //do nothing
+        } catch (Exception e) {
             LoggerUtil.error(logger, e, "删除Deployment异常,namespace={}, name={}", namespace, name);
         }
     }
@@ -85,8 +126,11 @@ public class DeploymentApiImpl implements DeploymentApi{
                 LoggerUtil.error(logger, e, "删除Deployment失败,namespace={}, name={}, response={}", namespace, name, e.getResponseBody());
                 throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
             }
-        }catch (Exception e) {
+        } catch (JsonSyntaxException e) {
+            //Do nothing
+        } catch (Exception e) {
             LoggerUtil.error(logger, e, "删除Deployment异常,namespace={}, name={}", namespace, name);
+            throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
         }
     }
 

+ 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);
+    }
+
+
 }

+ 1 - 1
src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/NamespaceApiImpl.java

@@ -196,7 +196,7 @@ public class NamespaceApiImpl implements NamespaceApi{
             return v1Namespace != null;
         }catch (ApiException e) {
             if (e.getCode() != NOT_FOUND) {
-                LoggerUtil.error(logger, e, "查询命名空间失败, name={}, response={}", name, e.getResponseBody());
+                LoggerUtil.error(logger, e, "查询命名空间失败, 系统异常, name={}, response={}", name, e.getResponseBody());
                 throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
             }
         }

+ 3 - 1
src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/Container.java

@@ -52,7 +52,9 @@ public class Container {
         this.ports = v1Container.getPorts().stream().map(ContainerPort::new).collect(Collectors.toSet());
         this.args = v1Container.getArgs();
         this.command = v1Container.getCommand();
-        this.env = v1Container.getEnv().stream().collect(Collectors.toMap(V1EnvVar::getName, V1EnvVar::getValue));
+        if (v1Container.getEnv() != null && !v1Container.getEnv().isEmpty()) {
+            this.env = v1Container.getEnv().stream().collect(Collectors.toMap(V1EnvVar::getName, V1EnvVar::getValue));
+        }
     }
     public V1Container toV1Container(){
         return new V1ContainerBuilder()

+ 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;
+    }
 }

+ 2 - 1
src/main/java/nju/seec/SEECdemo/logic/service/ApplicationService.java

@@ -15,10 +15,11 @@ public interface ApplicationService {
 
     /**
      * 删除应用
+     * @param projectName
      * @param name
      * @return
      */
-    boolean deleteByName(String name);
+    void delete(String projectName, String name);
 
     /**
      * 更新应用

+ 2 - 2
src/main/java/nju/seec/SEECdemo/logic/service/ProjectService.java

@@ -24,10 +24,10 @@ public interface ProjectService {
     /**
      * 删除项目
      * 如果项目底下仍有尚在运行的应用,则不会删除
-     * @param projectId
+     * @param projectName
      * @return
      */
-     boolean deleteProject(long projectId);
+     void deleteProject(String projectName);
 
     /**
      * 强制删除项目

+ 20 - 9
src/main/java/nju/seec/SEECdemo/logic/service/impl/ApplicationServiceImpl.java

@@ -45,8 +45,10 @@ public class ApplicationServiceImpl implements ApplicationService{
     }
 
     @Override
-    public boolean deleteByName(String name) {
-        return false;
+    public void delete(String projectName, String name) {
+        ingressApi.deleteIfExists(projectName, name);
+        serviceApi.deleteIfExists(projectName, name);
+        deploymentApi.deleteIfExist(projectName, name);
     }
 
     @Override
@@ -70,17 +72,17 @@ public class ApplicationServiceImpl implements ApplicationService{
         try {
             //删除现有的同名deployment
             deploymentApi.deleteIfExist(applicationDTO.getProjectName(), applicationDTO.getName());
-            Deployment deployment = buildDeploymentDTO(applicationDTO);
-            deploymentApi.createSync(deployment);
+            Deployment deployment = buildDeployment(applicationDTO);
+            deploymentApi.createAsync(deployment);
 
             //删除现有的同名Service
             serviceApi.deleteIfExists(applicationDTO.getProjectName(), applicationDTO.getName());
-            Service service = buildServiceDTO(applicationDTO);
+            Service service = buildService(applicationDTO);
             serviceApi.create(service);
 
             //删除现有的同名Ingress
             ingressApi.deleteIfExists(applicationDTO.getProjectName(), applicationDTO.getName());
-            Ingress ingress = buildIngressDTO(applicationDTO);
+            Ingress ingress = buildIngress(applicationDTO);
             ingressApi.create(ingress);
 
             //构建返回数据
@@ -94,14 +96,23 @@ public class ApplicationServiceImpl implements ApplicationService{
                 );
 
             }
+
+            try {
+                Thread.sleep(100000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
             return applicationVO;
         } catch (K8sApiException e) {
             //抛出创建失败异常
+            deploymentApi.deleteIfExist(applicationDTO.getProjectName(), applicationDTO.getName());
+            serviceApi.deleteIfExists(applicationDTO.getProjectName(), applicationDTO.getName());
+            ingressApi.deleteIfExists(applicationDTO.getProjectName(), applicationDTO.getName());
         }
         return null;
     }
 
-    private Deployment buildDeploymentDTO(ApplicationDTO applicationDTO) {
+    private Deployment buildDeployment(ApplicationDTO applicationDTO) {
         Deployment deployment = applicationDTO.toDeployment();
 
         //将registry密钥作为imagePullSecrets注入
@@ -124,13 +135,13 @@ public class ApplicationServiceImpl implements ApplicationService{
         return deployment;
     }
 
-    private Service buildServiceDTO(ApplicationDTO applicationDTO) {
+    private Service buildService(ApplicationDTO applicationDTO) {
         return applicationDTO.toService();
     }
 
     //@todo 构建Ingress
     //@fixme 潜在bug 如果容器上的IP和Service上的IP并非一一对应,根据容器的IP暴露Service会出现映射错误
-    private Ingress buildIngressDTO(ApplicationDTO applicationDTO) {
+    private Ingress buildIngress(ApplicationDTO applicationDTO) {
         String appName = applicationDTO.getName();
         String projectName = applicationDTO.getProjectName();
 

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

@@ -107,8 +107,8 @@ public class ProjectServiceImpl implements ProjectService{
     }
 
     @Override
-    public boolean deleteProject(long projectId) {
-        return false;
+    public void deleteProject(String projectName) {
+        namespaceApi.delete(projectName);
     }
 
     @Override

+ 4 - 4
src/main/java/nju/seec/SEECdemo/util/BeanUtil.java

@@ -18,10 +18,10 @@ public class BeanUtil {
     public ProjectTemplate seecIITemplate() {
         ProjectTemplate projectTemplate = new ProjectTemplate();
         ResourceConfig resourceConfig = new ResourceConfig();
-        resourceConfig.setDefaultCPU(new CpuQuantity(40));
-        resourceConfig.setMaxCPU(new CpuQuantity(50));
-        resourceConfig.setDefaultMemory(new StorageQuantity(500));
-        resourceConfig.setMaxMemory(new StorageQuantity(600));
+        resourceConfig.setDefaultCPU(new CpuQuantity(150));
+        resourceConfig.setMaxCPU(new CpuQuantity(100));
+        resourceConfig.setDefaultMemory(new StorageQuantity(700));
+        resourceConfig.setMaxMemory(new StorageQuantity(1000));
         resourceConfig.setDefaultStorage(StorageQuantity.EMPTY);
         resourceConfig.setMaxStorage(StorageQuantity.EMPTY);
         projectTemplate.setDefaultResourceConfig(resourceConfig);

+ 5 - 0
src/test/java/nju/seec/SEECdemo/service/ApplicationServiceImplTest.java

@@ -45,4 +45,9 @@ public class ApplicationServiceImplTest {
         applicationDTO.setContainers(Collections.singletonList(containerDTO));
         applicationService.deploy(applicationDTO);
     }
+
+    @Test
+    public void testDelete() {
+        applicationService.delete("demo", "test");
+    }
 }

+ 1 - 0
src/test/java/nju/seec/SEECdemo/service/ProjectServiceImplTest.java

@@ -26,5 +26,6 @@ public class ProjectServiceImplTest {
 
     @Test
     public void testDelete(){
+        projectService.deleteProject("demo2");
     }
 }

+ 34 - 6
src/test/java/nju/seec/SEECdemo/service/api/DeploymentApiImplTest.java

@@ -4,6 +4,7 @@ import nju.seec.SEECdemo.logic.api.k8s.DeploymentApi;
 import nju.seec.SEECdemo.logic.api.k8s.model.Container;
 import nju.seec.SEECdemo.logic.api.k8s.model.ContainerPort;
 import nju.seec.SEECdemo.logic.api.k8s.model.Deployment;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -23,16 +24,17 @@ public class DeploymentApiImplTest {
     @Autowired
     private DeploymentApi deploymentApi;
 
-    @Test
-    public void testCreate() {
-        Deployment deployment = new Deployment();
+    private Deployment deployment;
+    @Before
+    public void before() {
+        deployment = new Deployment();
         deployment.setName("test");
-        deployment.setNamespace("demo");
+        deployment.setNamespace("demo2");
         deployment.setReplicas(1);
 
         Container container = new Container();
         container.setName("container");
-        container.setImage("10.1.1.243:18082/container_demo:2.0");
+        container.setImage("10.1.1.243:18082/container_demo:1.0");
         ContainerPort containerPort = new ContainerPort();
         containerPort.setPort(8081);
         containerPort.setProtocol("TCP");
@@ -41,10 +43,36 @@ public class DeploymentApiImplTest {
         container.setPorts(ports);
 
         deployment.setLabels(Collections.singletonMap(APPLICATION_LABEL, "test"));
-        deployment.setImagePullSecrets(Collections.singletonList("regcred"));
+        deployment.setImagePullSecrets(Collections.singletonList("docker-registry-secret"));
 
         deployment.setContainers(Collections.singletonList(container));
+    }
+
+
+    @Test
+    public void testCreate() {
+        deploymentApi.deleteIfExist("demo2", "test");
 
         deploymentApi.createSync(deployment);
     }
+
+    @Test
+    public void testCreateAsync() {
+        deploymentApi.deleteIfExist("demo2", "test");
+        deploymentApi.createAsync(deployment);
+    }
+
+    @Test
+    public void testCreate_WrongRegistrySecret() {
+    }
+
+    @Test
+    public void testDelete() {
+        deploymentApi.delete("demo", "test");
+    }
+
+    @Test
+    public void testDeleteIfExist() {
+        deploymentApi.deleteIfExist("demo", "test");
+    }
 }

+ 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<>();

+ 27 - 1
src/test/java/nju/seec/SEECdemo/service/api/SecretApiImpl.java → src/test/java/nju/seec/SEECdemo/service/api/SecretApiImplTest.java

@@ -4,15 +4,19 @@ package nju.seec.SEECdemo.service.api;
 import io.kubernetes.client.apis.AppsV1Api;
 import io.kubernetes.client.apis.CoreV1Api;
 import nju.seec.SEECdemo.logic.api.k8s.SecretApi;
+import nju.seec.SEECdemo.logic.api.k8s.util.SecretTypeEnum;
+import nju.seec.SEECdemo.logic.api.k8s.vo.SecretVO;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.util.List;
+
 @RunWith(SpringRunner.class)
 @SpringBootTest
-public class SecretApiImpl {
+public class SecretApiImplTest {
 
     @Autowired
     private AppsV1Api appsV1Api;
@@ -115,4 +119,26 @@ public class SecretApiImpl {
         }*/
 
     }
+
+    @Test
+    public void testGetSecret() {
+        SecretVO v1Secret = secretApi.getSecretByName("group24", "regcred");
+        System.out.println(v1Secret);
+    }
+
+    @Test
+    public void testGetSecretByType() {
+        List<SecretVO> v1Secret = secretApi.getSecretListByType("demo", SecretTypeEnum.REGISTRY);
+        v1Secret.stream().forEach((v1Secret1 -> {
+            v1Secret1.getData().values().forEach((stringEntry -> {
+                System.out.println(stringEntry);
+            }));
+        }));
+        System.out.println(v1Secret);
+    }
+
+    @Test
+    public void testCreate() {
+        secretApi.createPrivateRegistrySecret("demo2", "10.1.1.243:18082", "admin", "admin123");
+    }
 }

+ 0 - 48
src/test/java/nju/seec/SEECdemo/service/api/SecretApiTest.java

@@ -1,48 +0,0 @@
-package nju.seec.SEECdemo.service.api;
-
-import io.kubernetes.client.models.V1Secret;
-import nju.seec.SEECdemo.logic.api.k8s.SecretApi;
-import nju.seec.SEECdemo.logic.api.k8s.util.SecretTypeEnum;
-import nju.seec.SEECdemo.logic.api.k8s.vo.SecretVO;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.test.context.junit4.SpringRunner;
-
-import java.nio.charset.StandardCharsets;
-import java.util.List;
-
-/**
- * author: rale
- * createdAt: 1/2/19
- */
-@RunWith(SpringRunner.class)
-@SpringBootTest
-public class SecretApiTest {
-
-    @Autowired
-    private SecretApi secretApi;
-
-    @Test
-    public void testGetSecret() {
-        SecretVO v1Secret = secretApi.getSecretByName("group24", "regcred");
-        System.out.println(v1Secret);
-    }
-
-    @Test
-    public void testGetSecretByType() {
-        List<SecretVO> v1Secret = secretApi.getSecretListByType("demo", SecretTypeEnum.REGISTRY);
-        v1Secret.stream().forEach((v1Secret1 -> {
-            v1Secret1.getData().values().forEach((stringEntry -> {
-                System.out.println(stringEntry);
-            }));
-        }));
-        System.out.println(v1Secret);
-    }
-
-    @Test
-    public void testCreate() {
-        secretApi.createPrivateRegistrySecret("demo2", "10.1.1.243:18082", "admin", "admin123");
-    }
-}