|
|
@@ -4,11 +4,10 @@ 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.V1ObjectMeta;
|
|
|
-import io.kubernetes.client.models.V1Secret;
|
|
|
-import io.kubernetes.client.models.V1SecretList;
|
|
|
+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.util.LoggerUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
@@ -30,13 +29,13 @@ public class SecretApiImpl implements SecretApi {
|
|
|
private CoreV1Api coreV1Api;
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult createGenericSecret(String namespace, String name, Map<String, byte[]> data) {
|
|
|
+ public V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data) throws K8sApiException {
|
|
|
String type = "Opaque";
|
|
|
return createGenericSecret(namespace, name, data, type);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult createGenericSecret(String namespace, String name, Map<String, byte[]> data, String type) {
|
|
|
+ public V1Secret createGenericSecret(String namespace, String name, Map<String, byte[]> data, String type) throws K8sApiException {
|
|
|
|
|
|
//Secret默认创建的Type为Opaque,base64编码格式的Secret,用来储存密码、密钥等。
|
|
|
|
|
|
@@ -51,20 +50,29 @@ public class SecretApiImpl implements SecretApi {
|
|
|
v1Secret.setType(type);
|
|
|
|
|
|
try {
|
|
|
- V1Secret createdSecret = coreV1Api.createNamespacedSecret("mjj-test", v1Secret, "OK");
|
|
|
- return new ApiResult<V1Secret>(createdSecret);
|
|
|
+ return coreV1Api.createNamespacedSecret("mjj-test", v1Secret, "OK");
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Secret创建失败, Secret={}, response={}", v1Secret, e.getResponseBody());
|
|
|
- ApiResult<Void> result = new ApiResult<>();
|
|
|
- result.setSuccess(false);
|
|
|
- result.setErrorCode(e.getCode());
|
|
|
- return result;
|
|
|
+ if (e.getCode() == K8sApiException.NAMESPACE_NOT_EXIST) {
|
|
|
+ throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
|
|
|
+ } else if (e.getCode() == K8sApiException.SECRET_ALREADY_EXIST) {
|
|
|
+ throw new K8sApiException(K8sApiException.SECRET_ALREADY_EXIST);
|
|
|
+ }else{
|
|
|
+ throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult createPrivateRegistrySecret(String namespace, String server, String user, String password) {
|
|
|
+ public V1Secret createPrivateRegistrySecret(String namespace, String server, String user, String password) throws K8sApiException {
|
|
|
+ //dockerSecret 统一的名称
|
|
|
+ String name = "docker-registry-secret";
|
|
|
+ return createPrivateRegistrySecret(namespace, name, server, user, password);
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public V1Secret createPrivateRegistrySecret(String namespace, String name, String server, String user, String password) throws K8sApiException {
|
|
|
Map<String, byte[]> data = new HashMap<>();
|
|
|
|
|
|
String dockerConfigJson =
|
|
|
@@ -74,46 +82,64 @@ public class SecretApiImpl implements SecretApi {
|
|
|
data.put(".dockerconfigjson", dockerConfigJson.getBytes());
|
|
|
|
|
|
//dockerSecret 统一的名称
|
|
|
- String name = "docker-registry-secret";
|
|
|
-
|
|
|
- String tpye = "kubernetes.io/dockerconfigjson";
|
|
|
|
|
|
- return createGenericSecret(namespace, name, data, tpye);
|
|
|
+ String type = "kubernetes.io/dockerconfigjson";
|
|
|
|
|
|
+ return createGenericSecret(namespace, name, data, type);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult getSecretList(String namespace) {
|
|
|
+ public List<V1Secret> getSecretList(String namespace) throws K8sApiException {
|
|
|
try {
|
|
|
V1SecretList v1SecretList = coreV1Api.listNamespacedSecret(namespace, "OK", "", "",false, "", 200, "", 10, false);
|
|
|
- List<V1Secret> secretList = v1SecretList.getItems();
|
|
|
- return new ApiResult<List<V1Secret>>(secretList);
|
|
|
+ return v1SecretList.getItems();
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Secret列表获取失败, response={}", e.getResponseBody());
|
|
|
- ApiResult<Void> result = new ApiResult<>();
|
|
|
- result.setSuccess(false);
|
|
|
- result.setErrorCode(e.getCode());
|
|
|
- return result;
|
|
|
+ if (e.getCode() == K8sApiException.NAMESPACE_NOT_EXIST) {
|
|
|
+ throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
|
|
|
+ } else {
|
|
|
+ throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<V1Secret> getSecretListByType(String namespace, String 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;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult getSecret(String namespace, String name) {
|
|
|
- ApiResult apiResult = getSecretList(namespace);
|
|
|
- if (apiResult.getErrorCode() != 0) {
|
|
|
- return apiResult;
|
|
|
- } else {
|
|
|
- List<V1Secret> secretList = (List<V1Secret>) apiResult.getValue();
|
|
|
- for (V1Secret secret : secretList) {
|
|
|
- V1ObjectMeta meta = secret.getMetadata();
|
|
|
- if (meta.getName().equals(name)) {
|
|
|
- return new ApiResult<V1Secret>(secret);
|
|
|
- }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteSecretByName(String namespace, String name) throws K8sApiException {
|
|
|
+ V1DeleteOptions body = new V1DeleteOptions();
|
|
|
+ try {
|
|
|
+ //暂时没有用到deleteNamespacedSecret()中的其他参数。
|
|
|
+ V1Status v1Status = coreV1Api.deleteNamespacedSecret(name, namespace, body, "", 5, false, "");
|
|
|
+ } catch (ApiException e) {
|
|
|
+ if (e.getCode() == K8sApiException.SECRET_NOT_FOUND) {
|
|
|
+ throw new K8sApiException(K8sApiException.SECRET_NOT_FOUND);
|
|
|
+ } else {
|
|
|
+ throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
}
|
|
|
- ApiResult<Void> result = new ApiResult<>();
|
|
|
- result.setSuccess(false);
|
|
|
- result.setErrorCode(ApiResult.NOT_EXIST);
|
|
|
- return result;
|
|
|
}
|
|
|
}
|
|
|
}
|