Kaynağa Gözat

小范围修改原来的实现
添加websocket依赖

raledong 7 yıl önce
ebeveyn
işleme
c19411e6f1

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

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

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

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

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