|
|
@@ -2,20 +2,29 @@ package nju.seec.SEECdemo.logic.api.k8s.impl;
|
|
|
|
|
|
import io.kubernetes.client.ApiException;
|
|
|
import io.kubernetes.client.apis.CoreV1Api;
|
|
|
+import io.kubernetes.client.models.V1DeleteOptions;
|
|
|
+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.vo.Namespace;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
+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.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.vo.ApiResult;
|
|
|
|
|
|
-/**
|
|
|
- * author: rale
|
|
|
- * createdAt: 12/22/18
|
|
|
- */
|
|
|
+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;
|
|
|
+
|
|
|
@Service
|
|
|
public class NamespaceApiImpl implements NamespaceApi{
|
|
|
|
|
|
@@ -25,26 +34,193 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
private CoreV1Api coreV1Api;
|
|
|
|
|
|
/**
|
|
|
- * @todo 此处没有明确项目的LimitRange
|
|
|
* @param namespace
|
|
|
* @return
|
|
|
* @see NamespaceApi#create(Namespace)
|
|
|
*/
|
|
|
@Override
|
|
|
- public ApiResult<Void> create(Namespace namespace) {
|
|
|
+ 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());
|
|
|
+ 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 delete(String name) {
|
|
|
+ try {
|
|
|
+ deleteNamespace(name);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "Namespace删除失败,name={}, response={}", name , e.getResponseBody());
|
|
|
+ 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) {
|
|
|
+ //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() {
|
|
|
+ try {
|
|
|
+ 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 namespaces;
|
|
|
} catch (ApiException e) {
|
|
|
- LoggerUtil.error(logger, e, "Namespace创建失败,namespace={}, v1Namespace={}", namespace, v1Namespace);
|
|
|
- ApiResult<Void> result = new ApiResult<>();
|
|
|
- result.setSuccess(false);
|
|
|
- result.setErrorCode(e.getCode());
|
|
|
- return result;
|
|
|
- }
|
|
|
- ApiResult<Void> result = new ApiResult<>();
|
|
|
- result.setSuccess(true);
|
|
|
- return result;
|
|
|
+ LoggerUtil.error(logger, e, "获取全部Namespace异常, response={}", e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Namespace> getByLabels(Map<String, String> labels) {
|
|
|
+ try {
|
|
|
+ String labelSelector = LabelSelector.toJsonString(labels);
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ true,
|
|
|
+ labelSelector,
|
|
|
+ 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异常, labels={}, response={}", labels, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Namespace> getByLabel(String key, String value){
|
|
|
+ return getByLabels(Collections.singletonMap(key, value));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Namespace> getByLabelSelector(LabelSelector labelSelector) {
|
|
|
+ try {
|
|
|
+ String selector = labelSelector.toJsonString();
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ 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 v1Namespace != null;
|
|
|
+ }catch (ApiException e) {
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
}
|