|
|
@@ -7,9 +7,10 @@ import io.kubernetes.client.models.V1DeleteOptionsBuilder;
|
|
|
import io.kubernetes.client.models.V1Namespace;
|
|
|
import io.kubernetes.client.models.V1NamespaceList;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.vo.ApiResult;
|
|
|
+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 org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -18,91 +19,120 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
-import static nju.seec.SEECdemo.logic.api.k8s.util.Constants.PRETTY_FORMAT;
|
|
|
+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.PRETTY_FORMAT;
|
|
|
|
|
|
-/**
|
|
|
- * author: rale
|
|
|
- * createdAt: 12/22/18
|
|
|
- */
|
|
|
@Service
|
|
|
public class NamespaceApiImpl implements NamespaceApi{
|
|
|
|
|
|
private Logger logger = LoggerUtil.getLogger(NamespaceApi.class);
|
|
|
|
|
|
- private static final String FOREGROUND_PROPAGATION_POLICY = "Foreground";
|
|
|
@Autowired
|
|
|
private CoreV1Api coreV1Api;
|
|
|
|
|
|
/**
|
|
|
- * @todo 此处没有明确项目的LimitRange
|
|
|
* @param namespace
|
|
|
* @return
|
|
|
* @see NamespaceApi#create(Namespace)
|
|
|
*/
|
|
|
@Override
|
|
|
- public ApiResult<Void> create(Namespace namespace) {
|
|
|
+ public void create(Namespace namespace) throws K8sApiException{
|
|
|
+ if (exists(namespace.getName())) {
|
|
|
+ throw new K8sApiException(K8sApiException.NAMESPACE_ALREADY_EXIST);
|
|
|
+ }
|
|
|
V1Namespace v1Namespace = namespace.toV1Namespace();
|
|
|
try {
|
|
|
coreV1Api.createNamespace(v1Namespace, "true");
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Namespace创建失败,namespace={}, v1Namespace={}", namespace, v1Namespace);
|
|
|
- return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
+ throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
}
|
|
|
- ApiResult<Void> result = new ApiResult<>();
|
|
|
- result.setSuccess(true);
|
|
|
- return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult<Void> createAsync(Namespace namespace) {
|
|
|
- return null;
|
|
|
+ public void createAsync(Namespace namespace) throws K8sApiException{
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult<Void> deleteByName(String name) {
|
|
|
+ public void deleteByName(String name) throws K8sApiException{
|
|
|
+ if (!exists(name)) {
|
|
|
+ throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
|
|
|
+ }
|
|
|
try {
|
|
|
V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
|
|
|
.withApiVersion(Namespace.VERSION)
|
|
|
.withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
.build();
|
|
|
- coreV1Api.deleteNamespace(name, v1DeleteOptions, PRETTY_FORMAT,
|
|
|
- null, null, FOREGROUND_PROPAGATION_POLICY);
|
|
|
+ coreV1Api.deleteNamespace(
|
|
|
+ name,
|
|
|
+ v1DeleteOptions,
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ FOREGROUND_PROPAGATION_POLICY);
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Namespace删除失败,name={}, response={}", name , e.getResponseBody());
|
|
|
- return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
} catch (Exception e) {
|
|
|
+ //k8s 由swagger导致的bug
|
|
|
LoggerUtil.warn(logger, e, "k8s swagger bug");
|
|
|
- return new ApiResult<>(null);
|
|
|
}
|
|
|
- return new ApiResult<>(null);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult<Namespace> getByName(String name) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult<List<Namespace>> getAll() {
|
|
|
+ public List<Namespace> getAll() throws K8sApiException{
|
|
|
try {
|
|
|
- V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(PRETTY_FORMAT, null, null,
|
|
|
- true, null, null, null, null, null);
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ true,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null);
|
|
|
List<Namespace> namespaces = v1NamespaceList.getItems().stream().map(Namespace::new).collect(Collectors.toList());
|
|
|
LoggerUtil.info(logger,"result={}", namespaces);
|
|
|
- return new ApiResult<>(namespaces);
|
|
|
+ return namespaces;
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "获取全部Namespace异常, response={}", e.getResponseBody());
|
|
|
- return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
+ throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult<List<Namespace>> getByLabels(Map<String, String> labels) {
|
|
|
+ public List<Namespace> getByLabels(Map<String, String> labels) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ApiResult<List<Namespace>> getByLabel(String key, String value) {
|
|
|
+ public List<Namespace> getByLabel(String key, String value) throws K8sApiException{
|
|
|
try {
|
|
|
String labelSelector = buildLabelSelector(key, value);
|
|
|
V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
@@ -117,13 +147,34 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
null);
|
|
|
List<Namespace> namespaces = v1NamespaceList.getItems().stream().map(Namespace::new).collect(Collectors.toList());
|
|
|
LoggerUtil.info(logger,"result={}", namespaces);
|
|
|
- return new ApiResult<>(namespaces);
|
|
|
+ return namespaces;
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "根据标签获取Namespace异常, key={}, value={}, response={}", key, value, e.getResponseBody());
|
|
|
- return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
+ throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean exists(String name) throws K8sApiException {
|
|
|
+ if (StringUtils.isBlank(name)) throw new K8sApiException(K8sApiException.ILLEGAL_PARAMETER, "名称不能为空");
|
|
|
+ try {
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ METADATA_NAME_SELECTOR + name,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null);
|
|
|
+ return v1NamespaceList.getItems().size() > 0;
|
|
|
+ }catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "查询命名空间失败, name={}, response={}", name, e.getResponseBody());
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
private String buildLabelSelector(String key, String value) {
|
|
|
return key + "=" + value;
|
|
|
}
|
|
|
@@ -138,4 +189,6 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
}
|
|
|
return result.toString();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|