|
|
@@ -2,7 +2,10 @@ 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;
|
|
|
@@ -10,7 +13,12 @@ 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;
|
|
|
+
|
|
|
+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;
|
|
|
|
|
|
/**
|
|
|
* author: rale
|
|
|
@@ -21,6 +29,7 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
|
|
|
private Logger logger = LoggerUtil.getLogger(NamespaceApi.class);
|
|
|
|
|
|
+ private static final String BACKGROUND_PROPAGATION_POLICY = "Background";
|
|
|
@Autowired
|
|
|
private CoreV1Api coreV1Api;
|
|
|
|
|
|
@@ -37,14 +46,69 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
coreV1Api.createNamespace(v1Namespace, "true");
|
|
|
} 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;
|
|
|
+ return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
}
|
|
|
ApiResult<Void> result = new ApiResult<>();
|
|
|
result.setSuccess(true);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public ApiResult<Void> createAsync(Namespace namespace) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ApiResult<Void> deleteByName(String name) {
|
|
|
+ try {
|
|
|
+ V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
|
|
|
+ .withApiVersion(Namespace.VERSION)
|
|
|
+ .withPropagationPolicy(BACKGROUND_PROPAGATION_POLICY)
|
|
|
+ .build();
|
|
|
+ coreV1Api.deleteNamespace(name, v1DeleteOptions, PRETTY_FORMAT,
|
|
|
+ null, null, BACKGROUND_PROPAGATION_POLICY);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "Namespace删除失败,name={}, response={}", name , e.getResponseBody());
|
|
|
+ return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
+ }
|
|
|
+ return new ApiResult<>(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ApiResult<Namespace> getByName(String name) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ApiResult<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 new ApiResult<>(namespaces);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "获取全部Namespace异常, response={}", e.getResponseBody());
|
|
|
+ return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ApiResult<List<Namespace>> getByLabels(Map<String, String> labels) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ApiResult<List<Namespace>> getByLabel(String key, String value) {
|
|
|
+ try {
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(PRETTY_FORMAT, null, null, true,key+":"+value, 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);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "根据标签获取Namespace异常, key={}, value={}", key, value);
|
|
|
+ return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|