|
|
@@ -7,19 +7,20 @@ 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.dto.NamespaceDTO;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.vo.Namespace;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.model.LabelSelector;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.model.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;
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
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.METADATA_NAME_SELECTOR;
|
|
|
import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
|
|
|
@@ -35,74 +36,84 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
/**
|
|
|
* @param namespace
|
|
|
* @return
|
|
|
- * @see NamespaceApi#create(nju.seec.SEECdemo.logic.api.k8s.dto.NamespaceDTO)
|
|
|
+ * @see NamespaceApi#create(Namespace)
|
|
|
*/
|
|
|
@Override
|
|
|
- public void create(NamespaceDTO namespace) throws K8sApiException{
|
|
|
- if (exists(namespace.getName())) {
|
|
|
- throw new K8sApiException(K8sApiException.NAMESPACE_ALREADY_EXIST);
|
|
|
- }
|
|
|
+ public Namespace create(Namespace namespace) {
|
|
|
V1Namespace v1Namespace = namespace.toV1Namespace();
|
|
|
try {
|
|
|
- coreV1Api.createNamespace(v1Namespace, "true");
|
|
|
+ V1Namespace result = coreV1Api.createNamespace(v1Namespace, PRETTY_FORMAT);
|
|
|
+ return result == null ? null : new Namespace(result);
|
|
|
} catch (ApiException e) {
|
|
|
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);
|
|
|
}
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param name
|
|
|
+ * @throws K8sApiException
|
|
|
+ */
|
|
|
@Override
|
|
|
- public void deleteByName(String name) throws K8sApiException{
|
|
|
- if (!exists(name)) {
|
|
|
- throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
|
|
|
- }
|
|
|
+ public void delete(String name) {
|
|
|
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) {
|
|
|
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.warn(logger, e, "k8s swagger bug");
|
|
|
+ LoggerUtil.error(logger, e, "Namespace删除异常, name={}", name);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- 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);
|
|
|
+ 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) {
|
|
|
+ //k8s 由swagger导致的bug
|
|
|
+ LoggerUtil.error(logger, e, "Namespace删除异常, name={}", name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Namespace getByName(String name) {
|
|
|
+ try {
|
|
|
+ V1Namespace v1Namespace = coreV1Api.readNamespace(
|
|
|
+ name,
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ null);
|
|
|
+ return v1Namespace == null ? null : new Namespace(v1Namespace);
|
|
|
+ }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;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Namespace> getAll() throws K8sApiException{
|
|
|
+ public List<Namespace> getAll() {
|
|
|
try {
|
|
|
V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
PRETTY_FORMAT,
|
|
|
@@ -119,19 +130,14 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
return namespaces;
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "获取全部Namespace异常, response={}", e.getResponseBody());
|
|
|
- throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Namespace> getByLabels(Map<String, String> labels) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<Namespace> getByLabel(String key, String value) throws K8sApiException{
|
|
|
try {
|
|
|
- String labelSelector = buildLabelSelector(key, value);
|
|
|
+ String labelSelector = LabelSelector.toJsonString(labels);
|
|
|
V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
PRETTY_FORMAT,
|
|
|
null,
|
|
|
@@ -146,46 +152,75 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
LoggerUtil.info(logger,"result={}", namespaces);
|
|
|
return namespaces;
|
|
|
} catch (ApiException e) {
|
|
|
- LoggerUtil.error(logger, e, "根据标签获取Namespace异常, key={}, value={}, response={}", key, value, e.getResponseBody());
|
|
|
- throw new K8sApiException(K8sApiException.SYSTEM_ERROR);
|
|
|
+ LoggerUtil.error(logger, e, "根据标签获取Namespace异常, labels={}, response={}", labels, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean exists(String name) {
|
|
|
- if (StringUtils.isBlank(name)) throw new K8sApiException(K8sApiException.ILLEGAL_PARAMETER, "名称不能为空");
|
|
|
+ public List<Namespace> getByLabel(String key, String value){
|
|
|
+ return getByLabels(Collections.singletonMap(key, value));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Namespace> getByLabelSelector(LabelSelector labelSelector) {
|
|
|
try {
|
|
|
- V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(PRETTY_FORMAT,
|
|
|
+ String selector = labelSelector.toJsonString();
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
+ PRETTY_FORMAT,
|
|
|
null,
|
|
|
- METADATA_NAME_SELECTOR + name,
|
|
|
null,
|
|
|
+ true,
|
|
|
+ selector,
|
|
|
null,
|
|
|
null,
|
|
|
null,
|
|
|
+ null);
|
|
|
+ List<Namespace> namespaces = v1NamespaceList.getItems().stream().map(Namespace::new).collect(Collectors.toList());
|
|
|
+ LoggerUtil.info(logger,"result={}", namespaces);
|
|
|
+ return namespaces;
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "根据标签获取Namespace异常, labelSelector={}, response={}", labelSelector, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean exists(String name) {
|
|
|
+ try {
|
|
|
+ V1Namespace v1Namespace = coreV1Api.readNamespace(
|
|
|
+ name,
|
|
|
+ PRETTY_FORMAT,
|
|
|
null,
|
|
|
null);
|
|
|
- return v1NamespaceList.getItems().size() > 0;
|
|
|
+ return v1Namespace != null;
|
|
|
}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;
|
|
|
}
|
|
|
|
|
|
|
|
|
- private String buildLabelSelector(String key, String value) {
|
|
|
- return key + "=" + value;
|
|
|
- }
|
|
|
-
|
|
|
- private String buildLabelSelector(Map<String, String> labels) {
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- for (Map.Entry<String, String> entry : labels.entrySet()) {
|
|
|
- result.append(entry.getKey());
|
|
|
- result.append("=");
|
|
|
- result.append(entry.getValue());
|
|
|
- result.append(",");
|
|
|
- }
|
|
|
- return result.toString();
|
|
|
+ /**
|
|
|
+ * 删除命名空间,执行错误会抛出一切异常
|
|
|
+ * @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);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|