|
|
@@ -10,15 +10,16 @@ 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.dto.ResourceQuotaDTO;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.model.ResourceQuota;
|
|
|
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.logic.api.k8s.exception.K8sApiException.ALREADY_EXIST;
|
|
|
+import static nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException.NOT_FOUND;
|
|
|
import static nju.seec.SEECdemo.util.Constants.FOREGROUND_PROPAGATION_POLICY;
|
|
|
import static nju.seec.SEECdemo.util.Constants.PRETTY_FORMAT;
|
|
|
|
|
|
@@ -33,21 +34,8 @@ public class ResourceQuotaImpl implements ResourceQuotaApi {
|
|
|
@Autowired
|
|
|
private NamespaceApi namespaceApi;
|
|
|
|
|
|
- /**
|
|
|
- * @see ResourceQuotaApi#create(nju.seec.SEECdemo.logic.api.k8s.dto.ResourceQuotaDTO)
|
|
|
- * @param resourceQuota
|
|
|
- * @return
|
|
|
- */
|
|
|
@Override
|
|
|
- public void create(ResourceQuotaDTO 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);
|
|
|
- }
|
|
|
-
|
|
|
+ public void create(ResourceQuota resourceQuota) throws K8sApiException{
|
|
|
try {
|
|
|
V1ResourceQuota v1ResourceQuota = resourceQuota.toV1ResourceQuota();
|
|
|
coreV1Api.createNamespacedResourceQuota(
|
|
|
@@ -57,72 +45,54 @@ public class ResourceQuotaImpl implements ResourceQuotaApi {
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "创建resource quota失败, resourceQuota={}, response={}",
|
|
|
resourceQuota, e.getResponseBody());
|
|
|
- throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ if (e.getCode() == NOT_FOUND) {
|
|
|
+ throw new K8sApiException(NOT_FOUND, "命名空间不存在");
|
|
|
+ }else if (e.getCode() == ALREADY_EXIST) {
|
|
|
+ throw new K8sApiException(ALREADY_EXIST, "资源配额已经存在");
|
|
|
+ }else {
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LoggerUtil.error(logger, e, "创建resource quota失败, resourceQuota={}", resourceQuota);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @see ResourceQuotaApi#update(ResourceQuotaDTO)
|
|
|
- * @param resourceQuota
|
|
|
- * @return
|
|
|
- */
|
|
|
@Override
|
|
|
- public void update(ResourceQuotaDTO 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);
|
|
|
- }
|
|
|
+ public void delete(String namespace, String name) throws K8sApiException{
|
|
|
try {
|
|
|
- coreV1Api.patchNamespacedResourceQuota(
|
|
|
- resourceQuota.getNamespace(),
|
|
|
- resourceQuota.getNamespace(),
|
|
|
- resourceQuota.toV1ResourceQuota(),
|
|
|
- PRETTY_FORMAT);
|
|
|
+
|
|
|
+ deleteResourceQuota(namespace, name);
|
|
|
+
|
|
|
} catch (ApiException e) {
|
|
|
- LoggerUtil.error(logger, e, "更新Resource Quota失败, resourceQuota={}, response={}", resourceQuota, e.getResponseBody());
|
|
|
- throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ LoggerUtil.error(logger, e, "删除resource quota失败,namespace={}, name={}, response={}",
|
|
|
+ namespace, name, e.getResponseBody());
|
|
|
+ if (e.getCode() == NOT_FOUND) {
|
|
|
+ throw new K8sApiException(NOT_FOUND, "资源配额不存在");
|
|
|
+ }else {
|
|
|
+ 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);
|
|
|
- }
|
|
|
+ public void deleteIfExists(String namespace, String name) {
|
|
|
+ try {
|
|
|
|
|
|
- V1DeleteOptions deleteOptions = new V1DeleteOptionsBuilder()
|
|
|
- .withApiVersion(ResourceQuota.API_VERSION)
|
|
|
- .withApiVersion(ResourceQuota.KIND)
|
|
|
- .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
- .build();
|
|
|
+ deleteResourceQuota(namespace, name);
|
|
|
|
|
|
- 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;
|
|
|
+ if (e.getCode() != NOT_FOUND) {
|
|
|
+ LoggerUtil.error(logger, e, "删除resource quota失败,namespace={}, name={}, response={}",
|
|
|
+ namespace, name, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LoggerUtil.error(logger, e, "删除resource quota异常,namespace={}, name={}", namespace, name);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ResourceQuota getByName(String namespace, String name) throws K8sApiException {
|
|
|
- if (exist(namespace, name)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
try {
|
|
|
|
|
|
V1ResourceQuotaList resourceQuotaList = coreV1Api.listNamespacedResourceQuota(namespace,
|
|
|
@@ -137,10 +107,15 @@ public class ResourceQuotaImpl implements ResourceQuotaApi {
|
|
|
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;
|
|
|
+ if (e.getCode() != NOT_FOUND) {
|
|
|
+ LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, name={}, response={}",
|
|
|
+ namespace,
|
|
|
+ name,
|
|
|
+ e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -159,10 +134,34 @@ public class ResourceQuotaImpl implements ResourceQuotaApi {
|
|
|
null);
|
|
|
return resourceQuotaList.getItems().size() > 0;
|
|
|
} catch (ApiException e) {
|
|
|
- LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, response={}", namespace, e.getResponseBody());
|
|
|
- return false;
|
|
|
+ if (e.getCode() != NOT_FOUND) {
|
|
|
+ LoggerUtil.error(logger, e, "查找resource quota失败, namespace={}, name={}, response={}",
|
|
|
+ namespace,
|
|
|
+ name,
|
|
|
+ e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LoggerUtil.error(logger, e, "查找resource quota异常, namespace={}, name={}",
|
|
|
+ namespace,
|
|
|
+ name);
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ private void deleteResourceQuota(String namespace, String name) throws ApiException{
|
|
|
+ V1DeleteOptions deleteOptions = new V1DeleteOptionsBuilder()
|
|
|
+ .withApiVersion(ResourceQuota.API_VERSION)
|
|
|
+ .withApiVersion(ResourceQuota.KIND)
|
|
|
+ .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
+ .build();
|
|
|
+ coreV1Api.deleteNamespacedResourceQuota(
|
|
|
+ name,
|
|
|
+ namespace,
|
|
|
+ deleteOptions,
|
|
|
+ PRETTY_FORMAT,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ FOREGROUND_PROPAGATION_POLICY);
|
|
|
+ }
|
|
|
}
|