Quellcode durchsuchen

refactor SecretApi

raledong vor 7 Jahren
Ursprung
Commit
7370e6bdb3

+ 8 - 7
src/main/java/nju/seec/SEECdemo/logic/api/k8s/SecretApi.java

@@ -6,6 +6,7 @@ import io.kubernetes.client.models.V1Secret;
 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.ApiResult;
+import nju.seec.SEECdemo.logic.api.k8s.vo.SecretVO;
 
 import javax.xml.transform.Result;
 import java.util.List;
@@ -20,7 +21,7 @@ public interface SecretApi {
      * @param data
      * @return ApiResult
      */
-    V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data) throws K8sApiException;
+    SecretVO createGenericSecret(String namespace, String name, Map<String, byte[]> data) throws K8sApiException;
 
     /**
      * 创建通用密钥,自己定义Secret的type,如Service Account、Opaque、kubernetes.io/dockerconfigjson.
@@ -30,7 +31,7 @@ public interface SecretApi {
      * @param type
      * @return ApiResult
      */
-    V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data, SecretTypeEnum type) throws K8sApiException;
+    SecretVO createGenericSecret(String namespace, String name, Map<String, byte[]> data, SecretTypeEnum type) throws K8sApiException;
 
     /**
      * 在namespace下生成私有仓库server的密钥
@@ -40,7 +41,7 @@ public interface SecretApi {
      * @param user
      * @param password
      */
-    V1Secret createPrivateRegistrySecret(String namespace, String server, String user, String password) throws K8sApiException;
+    SecretVO createPrivateRegistrySecret(String namespace, String server, String user, String password) throws K8sApiException;
 
 
     /**
@@ -52,13 +53,13 @@ public interface SecretApi {
      * @param user
      * @param password
      */
-    V1Secret createPrivateRegistrySecret(String namespace, String name, String server, String user, String password) throws K8sApiException;
+    SecretVO createPrivateRegistrySecret(String namespace, String name, String server, String user, String password) throws K8sApiException;
 
     /**
      * 获取指定namespace下的Secret列表
      * @param namespace
      */
-    List<V1Secret> getSecretList(String namespace) throws K8sApiException;
+    List<SecretVO> getSecretList(String namespace) throws K8sApiException;
 
     /**
      * 获取指定namespace下的指定type的Secret列表
@@ -66,7 +67,7 @@ public interface SecretApi {
      * @param namespace
      * @param type
      */
-    List<V1Secret> getSecretListByType(String namespace, SecretTypeEnum type) throws K8sApiException;
+    List<SecretVO> getSecretListByType(String namespace, SecretTypeEnum type) throws K8sApiException;
 
 
     /**
@@ -75,7 +76,7 @@ public interface SecretApi {
      * @param namespace
      * @param name
      */
-    V1Secret getSecretByName(String namespace, String name) throws K8sApiException;
+    SecretVO getSecretByName(String namespace, String name) throws K8sApiException;
 
     /**
      * 删除指定namespace下指定名字的Secret

+ 21 - 26
src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/SecretApiImpl.java

@@ -7,7 +7,9 @@ import io.kubernetes.client.models.*;
 import nju.seec.SEECdemo.logic.api.k8s.SecretApi;
 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.util.LoggerUtil;
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -16,6 +18,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
 
@@ -29,12 +32,12 @@ public class SecretApiImpl implements SecretApi {
     private CoreV1Api coreV1Api;
 
     @Override
-    public V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data) throws K8sApiException {
+    public SecretVO createGenericSecret(String namespace, String name, Map<String, byte[]> data) throws K8sApiException {
         return createGenericSecret(namespace, name, data, SecretTypeEnum.Generic);
     }
 
     @Override
-    public V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data, SecretTypeEnum type) throws K8sApiException {
+    public SecretVO createGenericSecret(String namespace, String name, Map<String, byte[]> data, SecretTypeEnum type) throws K8sApiException {
 
         //Secret默认创建的Type为Opaque,base64编码格式的Secret,用来储存密码、密钥等。
 
@@ -49,7 +52,7 @@ public class SecretApiImpl implements SecretApi {
         v1Secret.setType(type.getValue());
 
         try {
-            return coreV1Api.createNamespacedSecret(namespace, v1Secret, PRETTY_FORMAT);
+            return new SecretVO(coreV1Api.createNamespacedSecret(namespace, v1Secret, PRETTY_FORMAT));
         } catch (ApiException e) {
             LoggerUtil.error(logger, e, "Secret创建失败, Secret={}, response={}", v1Secret, e.getResponseBody());
             switch (e.getCode()) {
@@ -64,7 +67,7 @@ public class SecretApiImpl implements SecretApi {
     }
 
     @Override
-    public V1Secret createPrivateRegistrySecret(String namespace, String server, String user, String password) throws K8sApiException {
+    public SecretVO createPrivateRegistrySecret(String namespace, String server, String user, String password) throws K8sApiException {
         //dockerSecret 统一的名称
         String name = "docker-registry-secret";
         return createPrivateRegistrySecret(namespace, name, server, user, password);
@@ -72,7 +75,7 @@ public class SecretApiImpl implements SecretApi {
     }
 
     @Override
-    public V1Secret createPrivateRegistrySecret(String namespace, String name, String server, String user, String password) throws K8sApiException {
+    public SecretVO createPrivateRegistrySecret(String namespace, String name, String server, String user, String password) throws K8sApiException {
         Map<String, byte[]> data = new HashMap<>();
 
         String dockerConfigJson =
@@ -85,7 +88,7 @@ public class SecretApiImpl implements SecretApi {
     }
 
     @Override
-    public List<V1Secret> getSecretList(String namespace) throws K8sApiException {
+    public List<SecretVO> getSecretList(String namespace) throws K8sApiException {
         try {
             V1SecretList v1SecretList = coreV1Api.listNamespacedSecret(
                     namespace,
@@ -98,7 +101,7 @@ public class SecretApiImpl implements SecretApi {
                     "",
                     10,
                     false);
-            return v1SecretList.getItems();
+            return v1SecretList.getItems().stream().map(SecretVO::new).collect(Collectors.toList());
         } catch (ApiException e) {
             LoggerUtil.error(logger, e, "Secret列表获取失败, response={}", e.getResponseBody());
             if (e.getCode() == K8sApiException.NAMESPACE_NOT_EXIST) {
@@ -110,28 +113,20 @@ public class SecretApiImpl implements SecretApi {
     }
 
     @Override
-    public List<V1Secret> getSecretListByType(String namespace, SecretTypeEnum type) throws K8sApiException {
-        List<V1Secret> secretList = getSecretList(namespace);
-        List<V1Secret> resultList = new ArrayList<>();
-        for (V1Secret secret : secretList) {
-            String secretType = secret.getType();
-            if (type.equals(secretType)) {
-                resultList.add(secret);
-            }
-        }
-        return resultList;
+    public List<SecretVO> getSecretListByType(String namespace, SecretTypeEnum type) throws K8sApiException {
+        return getSecretList(namespace)
+                .stream()
+                .filter(secretVO -> type == secretVO.getSecretType())
+                .collect(Collectors.toList());
     }
 
     @Override
-    public V1Secret getSecretByName(String namespace, String name) throws K8sApiException {
-        List<V1Secret> secretList = getSecretList(namespace);
-        for (V1Secret secret : secretList) {
-            V1ObjectMeta meta = secret.getMetadata();
-            if (meta.getName().equals(name)) {
-                return secret;
-            }
-        }
-        throw new K8sApiException(K8sApiException.NOT_FOUND);
+    public SecretVO getSecretByName(String namespace, String name) throws K8sApiException {
+        return getSecretList(namespace)
+                .stream()
+                .filter(secretVO -> StringUtils.equals(secretVO.getName(), name))
+                .findAny()
+                .get();
     }
 
     @Override

+ 9 - 2
src/test/java/nju/seec/SEECdemo/service/api/SecretApiTest.java

@@ -3,12 +3,14 @@ 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;
 
 /**
@@ -24,13 +26,18 @@ public class SecretApiTest {
 
     @Test
     public void getSecret() {
-        V1Secret v1Secret = secretApi.getSecretByName("group24", "regcred");
+        SecretVO v1Secret = secretApi.getSecretByName("group24", "regcred");
         System.out.println(v1Secret);
     }
 
     @Test
     public void getSecretByType() {
-        List<V1Secret> v1Secret = secretApi.getSecretListByType("demo", SecretTypeEnum.Generic);
+        List<SecretVO> v1Secret = secretApi.getSecretListByType("demo", SecretTypeEnum.Generic);
+        v1Secret.stream().forEach((v1Secret1 -> {
+            v1Secret1.getData().values().forEach((stringEntry -> {
+                System.out.println(stringEntry);
+            }));
+        }));
         System.out.println(v1Secret);
     }
 }