|
|
@@ -1,14 +1,12 @@
|
|
|
package nju.seec.SEECdemo.logic.api.k8s.impl;
|
|
|
|
|
|
|
|
|
-import com.google.protobuf.Api;
|
|
|
import io.kubernetes.client.ApiException;
|
|
|
import io.kubernetes.client.apis.CoreV1Api;
|
|
|
import io.kubernetes.client.models.*;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.DeploymentApi;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.SecretApi;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.vo.ApiResult;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.util.SecretTypeEnum;
|
|
|
import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -19,6 +17,8 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
|
|
|
+
|
|
|
|
|
|
@Service
|
|
|
public class SecretApiImpl implements SecretApi {
|
|
|
@@ -30,12 +30,11 @@ public class SecretApiImpl implements SecretApi {
|
|
|
|
|
|
@Override
|
|
|
public V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data) throws K8sApiException {
|
|
|
- String type = "Opaque";
|
|
|
- return createGenericSecret(namespace, name, data, type);
|
|
|
+ return createGenericSecret(namespace, name, data, SecretTypeEnum.Generic);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data, String type) throws K8sApiException {
|
|
|
+ public V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data, SecretTypeEnum type) throws K8sApiException {
|
|
|
|
|
|
//Secret默认创建的Type为Opaque,base64编码格式的Secret,用来储存密码、密钥等。
|
|
|
|
|
|
@@ -47,10 +46,10 @@ public class SecretApiImpl implements SecretApi {
|
|
|
v1Secret.setData(data);
|
|
|
v1Secret.setKind("Secret");
|
|
|
v1Secret.setMetadata(meta);
|
|
|
- v1Secret.setType(type);
|
|
|
+ v1Secret.setType(type.getValue());
|
|
|
|
|
|
try {
|
|
|
- return coreV1Api.createNamespacedSecret("mjj-test", v1Secret, "OK");
|
|
|
+ return coreV1Api.createNamespacedSecret(namespace, v1Secret, PRETTY_FORMAT);
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Secret创建失败, Secret={}, response={}", v1Secret, e.getResponseBody());
|
|
|
switch (e.getCode()) {
|
|
|
@@ -77,22 +76,28 @@ public class SecretApiImpl implements SecretApi {
|
|
|
Map<String, byte[]> data = new HashMap<>();
|
|
|
|
|
|
String dockerConfigJson =
|
|
|
- "{\"auths\":{\"" + server +"\":{\"username\":\"" + user +
|
|
|
+ "{\"auths\":{\"" + server + "\":{\"username\":\"" + user +
|
|
|
"\",\"password\":\"" + password +
|
|
|
"\",\"auth\":\"" + user + ":" + password + "\"}}}";
|
|
|
data.put(".dockerconfigjson", dockerConfigJson.getBytes());
|
|
|
|
|
|
- //dockerSecret 统一的名称
|
|
|
-
|
|
|
- String type = "kubernetes.io/dockerconfigjson";
|
|
|
-
|
|
|
- return createGenericSecret(namespace, name, data, type);
|
|
|
+ return createGenericSecret(namespace, name, data, SecretTypeEnum.REGISTRY);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<V1Secret> getSecretList(String namespace) throws K8sApiException {
|
|
|
try {
|
|
|
- V1SecretList v1SecretList = coreV1Api.listNamespacedSecret(namespace, "OK", "", "",false, "", 200, "", 10, false);
|
|
|
+ V1SecretList v1SecretList = coreV1Api.listNamespacedSecret(
|
|
|
+ namespace,
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ false,
|
|
|
+ "",
|
|
|
+ 200,
|
|
|
+ "",
|
|
|
+ 10,
|
|
|
+ false);
|
|
|
return v1SecretList.getItems();
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Secret列表获取失败, response={}", e.getResponseBody());
|
|
|
@@ -105,7 +110,7 @@ public class SecretApiImpl implements SecretApi {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<V1Secret> getSecretListByType(String namespace, String type) throws K8sApiException {
|
|
|
+ public List<V1Secret> getSecretListByType(String namespace, SecretTypeEnum type) throws K8sApiException {
|
|
|
List<V1Secret> secretList = getSecretList(namespace);
|
|
|
List<V1Secret> resultList = new ArrayList<>();
|
|
|
for (V1Secret secret : secretList) {
|
|
|
@@ -134,7 +139,7 @@ public class SecretApiImpl implements SecretApi {
|
|
|
V1DeleteOptions body = new V1DeleteOptions();
|
|
|
try {
|
|
|
//暂时没有用到deleteNamespacedSecret()中的其他参数。
|
|
|
- V1Status v1Status = coreV1Api.deleteNamespacedSecret(name, namespace, body, "", 5, false, "");
|
|
|
+ V1Status v1Status = coreV1Api.deleteNamespacedSecret(name, namespace, body, PRETTY_FORMAT, 5, false, "");
|
|
|
} catch (ApiException e) {
|
|
|
if (e.getCode() == K8sApiException.NOT_FOUND) {
|
|
|
throw new K8sApiException(K8sApiException.NOT_FOUND);
|
|
|
@@ -144,3 +149,4 @@ public class SecretApiImpl implements SecretApi {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|