|
@@ -7,11 +7,9 @@ import io.kubernetes.client.models.V1DeleteOptionsBuilder;
|
|
|
import io.kubernetes.client.models.V1Namespace;
|
|
import io.kubernetes.client.models.V1Namespace;
|
|
|
import io.kubernetes.client.models.V1NamespaceList;
|
|
import io.kubernetes.client.models.V1NamespaceList;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
|
|
import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.dto.NamespaceDTO;
|
|
|
|
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.model.Namespace;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.vo.Namespace;
|
|
|
|
|
import nju.seec.SEECdemo.util.LoggerUtil;
|
|
import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -20,6 +18,7 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
+import static nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException.*;
|
|
|
import static nju.seec.SEECdemo.util.Constants.FOREGROUND_PROPAGATION_POLICY;
|
|
import static nju.seec.SEECdemo.util.Constants.FOREGROUND_PROPAGATION_POLICY;
|
|
|
import static nju.seec.SEECdemo.util.Constants.METADATA_NAME_SELECTOR;
|
|
import static nju.seec.SEECdemo.util.Constants.METADATA_NAME_SELECTOR;
|
|
|
import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
|
|
import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
|
|
@@ -35,67 +34,81 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
/**
|
|
/**
|
|
|
* @param namespace
|
|
* @param namespace
|
|
|
* @return
|
|
* @return
|
|
|
- * @see NamespaceApi#create(nju.seec.SEECdemo.logic.api.k8s.dto.NamespaceDTO)
|
|
|
|
|
|
|
+ * @see NamespaceApi#create(Namespace)
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
- public void create(NamespaceDTO namespace) throws K8sApiException{
|
|
|
|
|
- if (exists(namespace.getName())) {
|
|
|
|
|
- throw new K8sApiException(K8sApiException.NAMESPACE_ALREADY_EXIST);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public void create(Namespace namespace) throws K8sApiException{
|
|
|
V1Namespace v1Namespace = namespace.toV1Namespace();
|
|
V1Namespace v1Namespace = namespace.toV1Namespace();
|
|
|
try {
|
|
try {
|
|
|
coreV1Api.createNamespace(v1Namespace, "true");
|
|
coreV1Api.createNamespace(v1Namespace, "true");
|
|
|
} catch (ApiException e) {
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Namespace创建失败,namespace={}, v1Namespace={}, response={}", namespace, v1Namespace, e.getResponseBody());
|
|
LoggerUtil.error(logger, e, "Namespace创建失败,namespace={}, v1Namespace={}, response={}", namespace, v1Namespace, e.getResponseBody());
|
|
|
- throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
|
|
|
|
+ if (e.getCode() == ALREADY_EXIST) {
|
|
|
|
|
+ throw new K8sApiException(K8sApiException.NAMESPACE_ALREADY_EXIST);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ throw K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ LoggerUtil.error(logger, e, "Namespace创建异常, namespace={}, v1Namespace={}", namespace, v1Namespace);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param name
|
|
|
|
|
+ * @throws K8sApiException
|
|
|
|
|
+ */
|
|
|
@Override
|
|
@Override
|
|
|
- public void deleteByName(String name) throws K8sApiException{
|
|
|
|
|
- if (!exists(name)) {
|
|
|
|
|
- throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public void delete(String name) throws K8sApiException{
|
|
|
try {
|
|
try {
|
|
|
- V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
|
|
|
|
|
- .withApiVersion(Namespace.VERSION)
|
|
|
|
|
- .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
|
|
- .build();
|
|
|
|
|
- coreV1Api.deleteNamespace(
|
|
|
|
|
- name,
|
|
|
|
|
- v1DeleteOptions,
|
|
|
|
|
- PRETTY_FORMAT,
|
|
|
|
|
- null,
|
|
|
|
|
- null,
|
|
|
|
|
- FOREGROUND_PROPAGATION_POLICY);
|
|
|
|
|
|
|
+ deleteNamespace(name);
|
|
|
} catch (ApiException e) {
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Namespace删除失败,name={}, response={}", name , e.getResponseBody());
|
|
LoggerUtil.error(logger, e, "Namespace删除失败,name={}, response={}", name , e.getResponseBody());
|
|
|
- throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
|
|
|
|
+ if (e.getCode() == NOT_FOUND) {
|
|
|
|
|
+ throw K8s_NAMESPACE_NOT_EXIST;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ throw K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ //k8s 由swagger导致的bug
|
|
|
|
|
+ LoggerUtil.error(logger, e, "Namespace删除异常, name={}", name);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void deleteIfExists(String name) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ deleteNamespace(name);
|
|
|
|
|
+ } catch (ApiException e) {
|
|
|
|
|
+ //只打印系统执行一场
|
|
|
|
|
+ if (e.getCode() != NOT_FOUND) {
|
|
|
|
|
+ LoggerUtil.error(logger, e, "Namespace删除失败,name={}, response={}", name , e.getResponseBody());
|
|
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
//k8s 由swagger导致的bug
|
|
//k8s 由swagger导致的bug
|
|
|
- LoggerUtil.warn(logger, e, "k8s swagger bug");
|
|
|
|
|
|
|
+ LoggerUtil.error(logger, e, "Namespace删除异常, name={}", name);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Namespace getByName(String name) throws K8sApiException{
|
|
public Namespace getByName(String name) throws K8sApiException{
|
|
|
- if (exists(name)){
|
|
|
|
|
- try {
|
|
|
|
|
- V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
|
|
- PRETTY_FORMAT,
|
|
|
|
|
- null,
|
|
|
|
|
- METADATA_NAME_SELECTOR + name,
|
|
|
|
|
- null,
|
|
|
|
|
- null,
|
|
|
|
|
- null,
|
|
|
|
|
- null,
|
|
|
|
|
- null,
|
|
|
|
|
- null);
|
|
|
|
|
- List<Namespace> result = v1NamespaceList.getItems().stream().limit(1).map(Namespace::new).collect(Collectors.toList());
|
|
|
|
|
- return result.get(0);
|
|
|
|
|
- }catch (ApiException e) {
|
|
|
|
|
- LoggerUtil.error(logger, e, "查询Namespace失败,name={}, response={}", name, e.getResponseBody());
|
|
|
|
|
- throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
|
|
+ PRETTY_FORMAT,
|
|
|
|
|
+ null,
|
|
|
|
|
+ METADATA_NAME_SELECTOR + name,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null);
|
|
|
|
|
+ List<Namespace> result = v1NamespaceList.getItems().stream().limit(1).map(Namespace::new).collect(Collectors.toList());
|
|
|
|
|
+ return result.get(0);
|
|
|
|
|
+ }catch (ApiException e) {
|
|
|
|
|
+ LoggerUtil.error(logger, e, "查询Namespace失败,name={}, response={}", name, e.getResponseBody());
|
|
|
|
|
+ if (e.getCode() != NOT_FOUND) {
|
|
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return null;
|
|
return null;
|
|
@@ -119,7 +132,7 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
return namespaces;
|
|
return namespaces;
|
|
|
} catch (ApiException e) {
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "获取全部Namespace异常, response={}", e.getResponseBody());
|
|
LoggerUtil.error(logger, e, "获取全部Namespace异常, response={}", e.getResponseBody());
|
|
|
- throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
|
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -147,13 +160,12 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
return namespaces;
|
|
return namespaces;
|
|
|
} catch (ApiException e) {
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "根据标签获取Namespace异常, key={}, value={}, response={}", key, value, e.getResponseBody());
|
|
LoggerUtil.error(logger, e, "根据标签获取Namespace异常, key={}, value={}, response={}", key, value, e.getResponseBody());
|
|
|
- throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
|
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public boolean exists(String name) {
|
|
public boolean exists(String name) {
|
|
|
- if (StringUtils.isBlank(name)) throw new K8sApiException(K8sApiException.ILLEGAL_PARAMETER, "名称不能为空");
|
|
|
|
|
try {
|
|
try {
|
|
|
V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(PRETTY_FORMAT,
|
|
V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(PRETTY_FORMAT,
|
|
|
null,
|
|
null,
|
|
@@ -166,12 +178,34 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
null);
|
|
null);
|
|
|
return v1NamespaceList.getItems().size() > 0;
|
|
return v1NamespaceList.getItems().size() > 0;
|
|
|
}catch (ApiException e) {
|
|
}catch (ApiException e) {
|
|
|
- LoggerUtil.error(logger, e, "查询命名空间失败, name={}, response={}", name, e.getResponseBody());
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ if (e.getCode() != NOT_FOUND) {
|
|
|
|
|
+ LoggerUtil.error(logger, e, "查询命名空间失败, name={}, response={}", name, e.getResponseBody());
|
|
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除命名空间,执行错误会抛出一切异常
|
|
|
|
|
+ * @param name
|
|
|
|
|
+ * @throws ApiException
|
|
|
|
|
+ */
|
|
|
|
|
+ private void deleteNamespace(String name) throws ApiException{
|
|
|
|
|
+ V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
|
|
|
|
|
+ .withApiVersion(Namespace.VERSION)
|
|
|
|
|
+ .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ coreV1Api.deleteNamespace(
|
|
|
|
|
+ name,
|
|
|
|
|
+ v1DeleteOptions,
|
|
|
|
|
+ PRETTY_FORMAT,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ FOREGROUND_PROPAGATION_POLICY);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private String buildLabelSelector(String key, String value) {
|
|
private String buildLabelSelector(String key, String value) {
|
|
|
return key + "=" + value;
|
|
return key + "=" + value;
|
|
|
}
|
|
}
|
|
@@ -187,5 +221,4 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
return result.toString();
|
|
return result.toString();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|