|
|
@@ -0,0 +1,108 @@
|
|
|
+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.V1LimitRange;
|
|
|
+import io.kubernetes.client.models.V1LimitRangeList;
|
|
|
+import io.kubernetes.client.models.V1Status;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.LimitRangeApi;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.vo.LimitRange;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+@Service
|
|
|
+public class LimitRangeApiImpl implements LimitRangeApi {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CoreV1Api coreV1Api;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public V1LimitRange toLimitRange(String namespace, String name, Map<String, Integer> _default, Map<String, Integer> defaultRequest, Map<String, Integer> max, Map<String, Integer> min) {
|
|
|
+ LimitRange limitRange = new LimitRange();
|
|
|
+ limitRange.setName(name);
|
|
|
+ limitRange.setNamespace(namespace);
|
|
|
+ limitRange.set_default(_default);
|
|
|
+ limitRange.setDefault_request(defaultRequest);
|
|
|
+ limitRange.setMax(max);
|
|
|
+ limitRange.setMin(min);
|
|
|
+ return limitRange.toV1LimitRange();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public V1LimitRange createLimitRange(String namespace, V1LimitRange body) throws K8sApiException {
|
|
|
+
|
|
|
+ try {
|
|
|
+ return coreV1Api.createNamespacedLimitRange(namespace, body, "OK");
|
|
|
+ } catch (ApiException e) {
|
|
|
+ //namespace NotFound 404
|
|
|
+ //Already Exist 409
|
|
|
+ //422 if default value greater than max value, or min value greater than defaultRequest value
|
|
|
+ switch (e.getCode()) {
|
|
|
+ case K8sApiException.NOT_FOUND:
|
|
|
+ throw new K8sApiException(K8sApiException.NOT_FOUND);
|
|
|
+ case K8sApiException.ALREADY_EXIST:
|
|
|
+ throw new K8sApiException(K8sApiException.ALREADY_EXIST);
|
|
|
+ case K8sApiException.INVALID_VALUE:
|
|
|
+ throw new K8sApiException(K8sApiException.INVALID_VALUE);
|
|
|
+ default:
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteLimitRange(String namespace, String name) throws K8sApiException {
|
|
|
+ V1DeleteOptions body = new V1DeleteOptions();
|
|
|
+ try {
|
|
|
+ //暂时没有用到deleteNamespacedSecret()中的其他参数。
|
|
|
+ V1Status v1Status = coreV1Api.deleteNamespacedLimitRange(name, namespace, body, "", 5, false, "");
|
|
|
+ } catch (ApiException e) {
|
|
|
+ if (e.getCode() == K8sApiException.NOT_FOUND) {
|
|
|
+ throw new K8sApiException(K8sApiException.NOT_FOUND);
|
|
|
+ } else {
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<V1LimitRange> getLimitRangeList(String namespace) throws K8sApiException {
|
|
|
+ try {
|
|
|
+ V1LimitRangeList limitRangeList = coreV1Api.listNamespacedLimitRange(namespace, "OK", "", "", false, "", 5, "", 5, false);
|
|
|
+ List<V1LimitRange> result = limitRangeList.getItems();
|
|
|
+ if (result.isEmpty()) {
|
|
|
+ throw new K8sApiException(K8sApiException.NOT_FOUND);
|
|
|
+ } else {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ } catch (ApiException e) {
|
|
|
+ //如果namespace不存在也不会抛出异常,会返回一个空的list
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public V1LimitRange getLimitRangeByName(String namespace, String name) throws K8sApiException {
|
|
|
+ List<V1LimitRange> list = getLimitRangeList(namespace);
|
|
|
+ for (V1LimitRange limitRange : list) {
|
|
|
+ if (limitRange.getMetadata().getName().equals(name)) {
|
|
|
+ return limitRange;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new K8sApiException(K8sApiException.NOT_FOUND);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public V1LimitRange updateLimitRange(String namespace, String name, V1LimitRange body) throws K8sApiException {
|
|
|
+ //暂时没研究清楚Api参数,该方法实现为删除并重新创建
|
|
|
+ deleteLimitRange(namespace, name);
|
|
|
+ return createLimitRange(namespace, body);
|
|
|
+ }
|
|
|
+}
|