瀏覽代碼

添加查询应用状态的接口

raledong 7 年之前
父節點
當前提交
2a14d6bfb2

+ 6 - 0
src/main/java/nju/seec/SEECdemo/logic/api/k8s/model/Deployment.java

@@ -4,10 +4,12 @@ import io.kubernetes.client.models.*;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
+import nju.seec.SEECdemo.util.DateUtil;
 import org.hibernate.validator.constraints.Range;
 import org.springframework.data.annotation.ReadOnlyProperty;
 
 import javax.validation.constraints.NotEmpty;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -43,6 +45,9 @@ public class Deployment {
     @ReadOnlyProperty
     private DeploymentStatus status;
 
+    @ReadOnlyProperty
+    private LocalDateTime createdAt;
+
     public Deployment(V1Deployment v1Deployment) {
         this.name = v1Deployment.getMetadata().getName();
         this.namespace = v1Deployment.getMetadata().getNamespace();
@@ -54,6 +59,7 @@ public class Deployment {
         this.labels = v1Deployment.getMetadata().getLabels();
         this.annotations = v1Deployment.getMetadata().getAnnotations();
         this.status = new DeploymentStatus(v1Deployment.getStatus());
+        this.createdAt = DateUtil.fromJodaDateTime(v1Deployment.getMetadata().getCreationTimestamp());
     }
 
     public V1Deployment toV1Deployment(){

+ 9 - 0
src/main/java/nju/seec/SEECdemo/logic/service/ApplicationService.java

@@ -1,5 +1,6 @@
 package nju.seec.SEECdemo.logic.service;
 
+import nju.seec.SEECdemo.logic.vo.ApplicationStatusVO;
 import nju.seec.SEECdemo.logic.vo.ApplicationVO;
 import nju.seec.SEECdemo.web.dto.ApplicationDTO;
 
@@ -42,4 +43,12 @@ public interface ApplicationService {
      * @param applicationDTO
      */
     ApplicationVO deploy(ApplicationDTO applicationDTO);
+
+    /**
+     * 查询当前应用的状态
+     * @param projectName
+     * @param name
+     * @return
+     */
+    ApplicationStatusVO getStatus(String projectName, String name);
 }

+ 37 - 17
src/main/java/nju/seec/SEECdemo/logic/service/impl/ApplicationServiceImpl.java

@@ -2,12 +2,14 @@ package nju.seec.SEECdemo.logic.service.impl;
 
 import nju.seec.SEECdemo.logic.api.k8s.*;
 import nju.seec.SEECdemo.logic.api.k8s.model.Deployment;
+import nju.seec.SEECdemo.logic.api.k8s.model.DeploymentStatus;
 import nju.seec.SEECdemo.logic.api.k8s.model.Ingress;
 import nju.seec.SEECdemo.logic.api.k8s.model.Service;
 import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
 import nju.seec.SEECdemo.logic.api.k8s.util.SecretTypeEnum;
 import nju.seec.SEECdemo.logic.api.k8s.vo.SecretVO;
 import nju.seec.SEECdemo.logic.service.ApplicationService;
+import nju.seec.SEECdemo.logic.vo.ApplicationStatusVO;
 import nju.seec.SEECdemo.logic.vo.ApplicationVO;
 import nju.seec.SEECdemo.util.LoggerUtil;
 import nju.seec.SEECdemo.web.dto.ApplicationDTO;
@@ -58,6 +60,10 @@ public class ApplicationServiceImpl implements ApplicationService{
 
     @Override
     public ApplicationVO getByName(String projectName, String name) {
+        Deployment deployment = deploymentApi.get(projectName, name);
+        if (deployment == null){
+            //抛出异常
+        }
         return null;
     }
 
@@ -86,23 +92,7 @@ public class ApplicationServiceImpl implements ApplicationService{
             ingressApi.create(ingress);
 
             //构建返回数据
-            ApplicationVO applicationVO = new ApplicationVO();
-            applicationVO.setName(applicationDTO.getName());
-            applicationVO.setProjectName(applicationDTO.getProjectName());
-            if (ingress.getHttpRules() != null) {
-                applicationVO.setUrl(
-                        ingress.getHttpRules().stream()
-                                .map(rule -> ingress.getHttpHost() + rule).collect(Collectors.toList())
-                );
-
-            }
-
-            try {
-                Thread.sleep(100000);
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
-            return applicationVO;
+            return buildApplicationVO(applicationDTO, deployment, ingress);
         } catch (K8sApiException e) {
             //抛出创建失败异常
             deploymentApi.deleteIfExist(applicationDTO.getProjectName(), applicationDTO.getName());
@@ -112,6 +102,13 @@ public class ApplicationServiceImpl implements ApplicationService{
         return null;
     }
 
+    @Override
+    public ApplicationStatusVO getStatus(String projectName, String name) {
+        Deployment deployment = deploymentApi.get(projectName, name);
+        if (deployment == null) return null;//抛出异常
+        return buildApplicationStatusVO(deployment);
+    }
+
     private Deployment buildDeployment(ApplicationDTO applicationDTO) {
         Deployment deployment = applicationDTO.toDeployment();
 
@@ -171,4 +168,27 @@ public class ApplicationServiceImpl implements ApplicationService{
         ingress.setHttpRules(detailDTOS);
         return ingress;
     }
+
+    private ApplicationVO buildApplicationVO(ApplicationDTO applicationDTO, Deployment deployment,  Ingress ingress) {
+        ApplicationVO applicationVO = new ApplicationVO();
+        applicationVO.setName(applicationDTO.getName());
+        applicationVO.setProjectName(applicationDTO.getProjectName());
+        if (ingress.getHttpRules() != null) {
+            applicationVO.setUrl(
+                    ingress.getHttpRules().stream()
+                            .map(rule -> ingress.getHttpHost() + rule).collect(Collectors.toList())
+            );
+
+        }
+        ApplicationStatusVO applicationStatusVO = buildApplicationStatusVO(deployment);
+        applicationVO.setStatus(applicationStatusVO);
+        return applicationVO;
+    }
+
+    private ApplicationStatusVO buildApplicationStatusVO(Deployment deployment) {
+        ApplicationStatusVO applicationStatusVO = new ApplicationStatusVO();
+        applicationStatusVO.setReadyReplicas(deployment.getStatus().getReadyReplicas());
+        applicationStatusVO.setDesiredReplicas(deployment.getReplicas());
+        return applicationStatusVO;
+    }
 }

+ 22 - 0
src/main/java/nju/seec/SEECdemo/logic/vo/ApplicationStatusVO.java

@@ -0,0 +1,22 @@
+package nju.seec.SEECdemo.logic.vo;
+
+import lombok.Data;
+
+/**
+ * author: rale
+ * createdAt: 1/17/19
+ */
+@Data
+public class ApplicationStatusVO {
+
+    /**
+     * 就绪数
+     */
+    private int readyReplicas;
+
+    /**
+     * 期待数
+     */
+    private int desiredReplicas;
+
+}

+ 5 - 0
src/main/java/nju/seec/SEECdemo/logic/vo/ApplicationVO.java

@@ -2,7 +2,9 @@ package nju.seec.SEECdemo.logic.vo;
 
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+import nju.seec.SEECdemo.logic.api.k8s.model.Deployment;
 
+import java.time.LocalDate;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -20,4 +22,7 @@ public class ApplicationVO {
     //传递回额外的信息
     private Map<String, Object> extra = new HashMap<>();
 
+    private LocalDate createdAt;
+
+    private ApplicationStatusVO status;
 }

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

@@ -1,6 +1,8 @@
 package nju.seec.SEECdemo.service;
 
 import nju.seec.SEECdemo.logic.service.ApplicationService;
+import nju.seec.SEECdemo.logic.vo.ApplicationStatusVO;
+import nju.seec.SEECdemo.logic.vo.ApplicationVO;
 import nju.seec.SEECdemo.web.dto.ApplicationDTO;
 import nju.seec.SEECdemo.web.dto.ContainerDTO;
 import nju.seec.SEECdemo.web.dto.PortDTO;
@@ -50,4 +52,10 @@ public class ApplicationServiceImplTest {
     public void testDelete() {
         applicationService.delete("demo", "test");
     }
+
+    @Test
+    public void testGetStatus() {
+        ApplicationStatusVO status = applicationService.getStatus("demo2", "test");
+        System.out.println(status);
+    }
 }