|
|
@@ -2,211 +2,164 @@ package nju.seec.SEECdemo.logic.api.k8s.impl;
|
|
|
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.ResourceQuotaApi;
|
|
|
|
|
|
-//
|
|
|
-//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.V1ResourceQuotaList;
|
|
|
-//import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
|
|
|
-//import nju.seec.SEECdemo.logic.api.k8s.ResourceQuotaApi;
|
|
|
-//import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
-//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.vo.ResourceQuota;
|
|
|
-//import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
-//import org.slf4j.Logger;
|
|
|
-//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-//import org.springframework.stereotype.Service;
|
|
|
-//import org.springframework.util.CollectionUtils;
|
|
|
-//
|
|
|
-//
|
|
|
-//import java.awt.*;
|
|
|
-//import java.util.List;
|
|
|
-//import java.util.stream.Collectors;
|
|
|
-//
|
|
|
-//import static nju.seec.SEECdemo.util.Constants.FOREGROUND_PROPAGATION_POLICY;
|
|
|
-//import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
|
|
|
-//
|
|
|
-//@Service
|
|
|
+
|
|
|
+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.V1ResourceQuotaList;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.vo.ResourceQuota;
|
|
|
+import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+
|
|
|
+import static nju.seec.SEECdemo.util.Constants.FOREGROUND_PROPAGATION_POLICY;
|
|
|
+import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
|
|
|
+
|
|
|
+@Service
|
|
|
public class ResourceQuotaImpl implements ResourceQuotaApi {
|
|
|
-//
|
|
|
-// private static final Logger logger = LoggerUtil.getLogger(ResourceQuotaApi.class);
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// private CoreV1Api coreV1Api;
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// private NamespaceApi namespaceApi;
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * @see ResourceQuotaApi#create(ResourceQuota)
|
|
|
-// * @param resourceQuota
|
|
|
-// * @return
|
|
|
-// */
|
|
|
-// @Override
|
|
|
-// public void create(ResourceQuota resourceQuota) throws K8sApiException{
|
|
|
-// ApiResult<Namespace> namespaceRequest = namespaceApi.getByName(resourceQuota.getName());
|
|
|
-// //Namespace不存在
|
|
|
-// if (namespaceRequest.getErrorCode() == ApiResult.NOT_EXIST) {
|
|
|
-// return new ApiResult<>( ApiResult.NOT_EXIST, "该命名空间不存在");
|
|
|
-// }
|
|
|
-// ApiResult<Boolean> resourceQuotaExistRequest = this.exist(resourceQuota.getNamespace(), resourceQuota.getName());
|
|
|
-// if (!resourceQuotaExistRequest.isSuccess()) {
|
|
|
-// return new ApiResult<>(ApiResult.SYSTEM_ERROR, "resource quota查询失败");
|
|
|
-// }
|
|
|
-//
|
|
|
-// //该资源限额已经存在,直接更新资源限额
|
|
|
-// if (resourceQuotaExistRequest.getValue()) {
|
|
|
-// return update(resourceQuota);
|
|
|
-// }
|
|
|
-//
|
|
|
-// try {
|
|
|
-// coreV1Api.createNamespacedResourceQuota(
|
|
|
-// resourceQuota.getNamespace(),
|
|
|
-// resourceQuota.toV1ResourceQuota(),
|
|
|
-// PRETTY_FORMAT);
|
|
|
-// } catch (ApiException e) {
|
|
|
-// LoggerUtil.error(logger, e, "创建resource quota失败, resourceQuota={}, response={}",
|
|
|
-// resourceQuota, e.getResponseBody());
|
|
|
-// return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
-// }
|
|
|
-// return new ApiResult<>(null);
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * @see ResourceQuotaApi#update(ResourceQuota)
|
|
|
-// * @param resourceQuota
|
|
|
-// * @return
|
|
|
-// */
|
|
|
-// @Override
|
|
|
-// public ApiResult<Void> update(ResourceQuota resourceQuota) {
|
|
|
-// ApiResult<Namespace> namespaceRequest = namespaceApi.getByName(resourceQuota.getName());
|
|
|
-// //Namespace不存在
|
|
|
-// if (namespaceRequest.getErrorCode() == ApiResult.NOT_EXIST) {
|
|
|
-// return new ApiResult<>( ApiResult.NOT_EXIST, "该命名空间不存在");
|
|
|
-// }
|
|
|
-//
|
|
|
-// ApiResult<Boolean> resourceQuotaExistRequest = this.exist(resourceQuota.getNamespace(), resourceQuota.getName());
|
|
|
-// if (!resourceQuotaExistRequest.isSuccess()) {
|
|
|
-// return new ApiResult<>(ApiResult.SYSTEM_ERROR, "resource quota查询失败");
|
|
|
-// }
|
|
|
-// if (!resourceQuotaExistRequest.getValue()) {
|
|
|
-// return new ApiResult<>(ApiResult.NOT_EXIST, "名称为" + resourceQuota.getName() + "的资源限额不存在");
|
|
|
-// }
|
|
|
-//
|
|
|
-// try {
|
|
|
-// coreV1Api.patchNamespacedResourceQuota(
|
|
|
-// resourceQuota.getNamespace(),
|
|
|
-// resourceQuota.getNamespace(),
|
|
|
-// resourceQuota.toV1ResourceQuota(),
|
|
|
-// PRETTY_FORMAT);
|
|
|
-// } catch (ApiException e) {
|
|
|
-// LoggerUtil.error(logger, e, "更新Resource Quota失败, resourceQuota={}, response={}", resourceQuota, e.getResponseBody());
|
|
|
-// return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
-// }
|
|
|
-// return new ApiResult<>(null);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public ApiResult<Void> delete(String namespace, String name) {
|
|
|
-// ApiResult<Namespace> namespaceRequest = namespaceApi.getByName(namespace);
|
|
|
-// //系统错误
|
|
|
-// if (!namespaceRequest.isSuccess()) {
|
|
|
-// return new ApiResult<>(namespaceRequest.getErrorCode(), namespaceRequest.getErrorDescription());
|
|
|
-// }
|
|
|
-// //Namespace不存在
|
|
|
-// if (namespaceRequest.getErrorCode() == ApiResult.NOT_EXIST) {
|
|
|
-// return new ApiResult<>( ApiResult.NOT_EXIST, "该命名空间不存在");
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// ApiResult<Boolean> resourceQuotaExistRequest = this.exist(namespace, name);
|
|
|
-// //系统错误
|
|
|
-// if (!resourceQuotaExistRequest.isSuccess()) {
|
|
|
-// return new ApiResult<>(ApiResult.SYSTEM_ERROR, "resource quota查询失败");
|
|
|
-// }
|
|
|
-// //该资源限额不存在
|
|
|
-// if (!resourceQuotaExistRequest.getValue()) {
|
|
|
-// return new ApiResult<>(ApiResult.NOT_EXIST, "该资源限额不存在");
|
|
|
-// }
|
|
|
-//
|
|
|
-// V1DeleteOptions deleteOptions = new V1DeleteOptionsBuilder()
|
|
|
-// .withApiVersion(ResourceQuota.API_VERSION)
|
|
|
-// .withApiVersion(ResourceQuota.KIND)
|
|
|
-// .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
-// .build();
|
|
|
-//
|
|
|
-// try {
|
|
|
-// coreV1Api.deleteNamespacedResourceQuota(
|
|
|
-// name,
|
|
|
-// namespace,
|
|
|
-// deleteOptions,
|
|
|
-// PRETTY_FORMAT,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// FOREGROUND_PROPAGATION_POLICY);
|
|
|
-// } catch (ApiException e) {
|
|
|
-// LoggerUtil.error(logger, e, "删除resource quota异常,namespace={}, name={}, response={}",
|
|
|
-// namespace, name, e.getResponseBody());
|
|
|
-// }
|
|
|
-// return new ApiResult<>(null);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public ResourceQuota getByName(String namespace, String name) throws K8sApiException {
|
|
|
-// return null;
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public boolean exist(String namespace, String name) throws K8sApiException {
|
|
|
-// try {
|
|
|
-//
|
|
|
-// V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
|
|
|
-// PRETTY_FORMAT,
|
|
|
-// null,
|
|
|
-// "metadata.name="+name,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null);
|
|
|
-// return resourceQuotaList.getItems().size() > 0;
|
|
|
-// } catch (ApiException e) {
|
|
|
-// LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, response={}", namespace, e.getResponseBody());
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * @see ResourceQuotaApi#getByNamespace(String)
|
|
|
-// * @param namespace
|
|
|
-// * @return
|
|
|
-// */
|
|
|
-// @Override
|
|
|
-// public ApiResult<ResourceQuota> getByNamespace(String namespace) {
|
|
|
-// try {
|
|
|
-// V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
|
|
|
-// PRETTY_FORMAT,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null,
|
|
|
-// null);
|
|
|
-// if (resourceQuotaList != null && !CollectionUtils.isEmpty(resourceQuotaList.getItems())) {
|
|
|
-// List<ResourceQuota> result = resourceQuotaList.getItems().stream().map(ResourceQuota::new).collect(Collectors.toList());
|
|
|
-// return new ApiResult<>(result.get(0));
|
|
|
-// }
|
|
|
-// } catch (ApiException e) {
|
|
|
-// LoggerUtil.error(logger, e, "查找Resource Quota失败, namespace={}, response={}", namespace, e.getResponseBody());
|
|
|
-// return new ApiResult<>(e.getCode(), e.getMessage());
|
|
|
-// }
|
|
|
-// return new ApiResult<>(null);
|
|
|
-// }
|
|
|
-//
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerUtil.getLogger(ResourceQuotaApi.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CoreV1Api coreV1Api;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private NamespaceApi namespaceApi;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see ResourceQuotaApi#create(ResourceQuota)
|
|
|
+ * @param resourceQuota
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void create(ResourceQuota resourceQuota) throws K8sApiException{
|
|
|
+ if (!namespaceApi.exists(resourceQuota.getNamespace())) {
|
|
|
+ throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.exist(resourceQuota.getNamespace(), resourceQuota.getName())) {
|
|
|
+ throw new K8sApiException(K8sApiException.RESOURCE_QUOTA_ALREADY_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ coreV1Api.createNamespacedResourceQuota(
|
|
|
+ resourceQuota.getNamespace(),
|
|
|
+ resourceQuota.toV1ResourceQuota(),
|
|
|
+ PRETTY_FORMAT);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "创建resource quota失败, resourceQuota={}, response={}",
|
|
|
+ resourceQuota, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see ResourceQuotaApi#update(ResourceQuota)
|
|
|
+ * @param resourceQuota
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void update(ResourceQuota resourceQuota) throws K8sApiException{
|
|
|
+ if (!namespaceApi.exists(resourceQuota.getNamespace())) {
|
|
|
+ throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!this.exist(resourceQuota.getNamespace(), resourceQuota.getName())) {
|
|
|
+ throw new K8sApiException(K8sApiException.RESOURCE_QUOTA_NOT_EXIST);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ coreV1Api.patchNamespacedResourceQuota(
|
|
|
+ resourceQuota.getNamespace(),
|
|
|
+ resourceQuota.getNamespace(),
|
|
|
+ resourceQuota.toV1ResourceQuota(),
|
|
|
+ PRETTY_FORMAT);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "更新Resource Quota失败, resourceQuota={}, response={}", resourceQuota, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(String namespace, String name) throws K8sApiException{
|
|
|
+ if (!namespaceApi.exists(namespace)) {
|
|
|
+ throw new K8sApiException(K8sApiException.NAMESPACE_NOT_EXIST);
|
|
|
+ }
|
|
|
+ if (!this.exist(namespace, name)) {
|
|
|
+ throw new K8sApiException(K8sApiException.RESOURCE_QUOTA_NOT_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ V1DeleteOptions deleteOptions = new V1DeleteOptionsBuilder()
|
|
|
+ .withApiVersion(ResourceQuota.API_VERSION)
|
|
|
+ .withApiVersion(ResourceQuota.KIND)
|
|
|
+ .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ try {
|
|
|
+ coreV1Api.deleteNamespacedResourceQuota(
|
|
|
+ name,
|
|
|
+ namespace,
|
|
|
+ deleteOptions,
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ FOREGROUND_PROPAGATION_POLICY);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "删除resource quota异常,namespace={}, name={}, response={}",
|
|
|
+ namespace, name, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResourceQuota getByName(String namespace, String name) throws K8sApiException {
|
|
|
+ if (exist(namespace, name)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+
|
|
|
+ V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ "metadata.name="+name,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null);
|
|
|
+ return new ResourceQuota(resourceQuotaList.getItems().get(0));
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, response={}", namespace, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean exist(String namespace, String name) throws K8sApiException {
|
|
|
+ try {
|
|
|
+
|
|
|
+ V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ "metadata.name="+name,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null);
|
|
|
+ return resourceQuotaList.getItems().size() > 0;
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, response={}", namespace, e.getResponseBody());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|