|
|
@@ -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
|