|
@@ -0,0 +1,165 @@
|
|
|
|
|
+package cn.seecoder.api.k8s.impl;
|
|
|
|
|
+
|
|
|
|
|
+import cn.seecoder.api.k8s.LimitRangeApi;
|
|
|
|
|
+import cn.seecoder.api.k8s.exception.K8sApiException;
|
|
|
|
|
+import cn.seecoder.api.k8s.model.K8sObjectRequest;
|
|
|
|
|
+import cn.seecoder.api.k8s.model.LabelSelector;
|
|
|
|
|
+import cn.seecoder.api.k8s.model.LimitRange;
|
|
|
|
|
+import io.kubernetes.client.openapi.ApiException;
|
|
|
|
|
+import io.kubernetes.client.openapi.apis.CoreV1Api;
|
|
|
|
|
+import io.kubernetes.client.openapi.models.V1DeleteOptions;
|
|
|
|
|
+import io.kubernetes.client.openapi.models.V1DeleteOptionsBuilder;
|
|
|
|
|
+import io.kubernetes.client.openapi.models.V1LimitRange;
|
|
|
|
|
+import io.kubernetes.client.openapi.models.V1LimitRangeList;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+import static cn.seecoder.api.k8s.K8sConstants.FOREGROUND_PROPAGATION_POLICY;
|
|
|
|
|
+import static cn.seecoder.api.k8s.K8sConstants.PRETTY_FORMAT;
|
|
|
|
|
+import static cn.seecoder.api.k8s.exception.K8sApiException.*;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class LimitRangeApiImpl implements LimitRangeApi {
|
|
|
|
|
+
|
|
|
|
|
+ private final CoreV1Api coreV1Api;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ public LimitRangeApiImpl(CoreV1Api coreV1Api) {
|
|
|
|
|
+ this.coreV1Api = coreV1Api;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public LimitRange create(LimitRange limitRange) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ V1LimitRange v1LimitRange = limitRange.toK8sObject();
|
|
|
|
|
+ v1LimitRange = coreV1Api.createNamespacedLimitRange(limitRange.getNamespace(), v1LimitRange, PRETTY_FORMAT, null, null);
|
|
|
|
|
+ return v1LimitRange == null ? null : new LimitRange(v1LimitRange);
|
|
|
|
|
+ } catch (ApiException e) {
|
|
|
|
|
+ log.error("limit range创建失败, limit range={}, response={}", limitRange, e.getResponseBody());
|
|
|
|
|
+ if (e.getCode() == ALREADY_EXIST) {
|
|
|
|
|
+ throw new K8sApiException(ALREADY_EXIST, "limit range已经存在");
|
|
|
|
|
+ }else if (e.getCode() == NOT_FOUND){
|
|
|
|
|
+ throw new K8sApiException(NOT_FOUND, "Namespace不存在");
|
|
|
|
|
+ }else {
|
|
|
|
|
+ throw K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public LimitRange update(LimitRange limitRange) {
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ V1LimitRange v1LimitRange = getV1LimitRange(limitRange.getNamespace(), limitRange.getName());
|
|
|
|
|
+ limitRange.merge(v1LimitRange);
|
|
|
|
|
+ v1LimitRange = coreV1Api.replaceNamespacedLimitRange(limitRange.getName(), limitRange.getNamespace(), v1LimitRange, PRETTY_FORMAT, null, null);
|
|
|
|
|
+ return v1LimitRange == null ? null : new LimitRange(v1LimitRange);
|
|
|
|
|
+ } catch (ApiException e) {
|
|
|
|
|
+ log.error("LimitRange替换失败, limit range={}, response={}", limitRange, e.getResponseBody());
|
|
|
|
|
+ if (e.getCode() == NOT_FOUND){
|
|
|
|
|
+ throw new K8sApiException(NOT_FOUND, "LimitRange不存在");
|
|
|
|
|
+ }else {
|
|
|
|
|
+ throw K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LimitRange替换异常, limit range={}", limitRange);
|
|
|
|
|
+ throw K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void delete(LimitRange limitRange) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
|
|
|
|
|
+ .withApiVersion(LimitRange.API_VERSION)
|
|
|
|
|
+ .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ coreV1Api.deleteNamespacedService(
|
|
|
|
|
+ limitRange.getName(),
|
|
|
|
|
+ limitRange.getNamespace(),
|
|
|
|
|
+ PRETTY_FORMAT,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ FOREGROUND_PROPAGATION_POLICY,
|
|
|
|
|
+ v1DeleteOptions);
|
|
|
|
|
+ }catch (ApiException e) {
|
|
|
|
|
+ log.error("LimitRange删除失败,namespace={}, name={}, response={}",
|
|
|
|
|
+ limitRange.getNamespace(), limitRange.getName(), e.getResponseBody());
|
|
|
|
|
+ if (e.getCode() == NOT_FOUND) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }catch (Exception e) {
|
|
|
|
|
+ log.error("LimitRange删除异常,namespace={}, name={}, response={}", limitRange.getNamespace(), limitRange.getName(),e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<LimitRange> getByCondition(K8sObjectRequest request) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (request.getName() != null) {
|
|
|
|
|
+ V1LimitRange obj = coreV1Api.readNamespacedLimitRange(
|
|
|
|
|
+ request.getName(),
|
|
|
|
|
+ request.getNamespace(),
|
|
|
|
|
+ PRETTY_FORMAT,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null);
|
|
|
|
|
+ return obj == null ? Collections.emptyList() : Collections.singletonList(new LimitRange(obj));
|
|
|
|
|
+ }
|
|
|
|
|
+ LabelSelector selector = null;
|
|
|
|
|
+ if (request.getSelector() != null) {
|
|
|
|
|
+ selector = request.getSelector();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getLabels() != null) {
|
|
|
|
|
+ if (selector == null) {
|
|
|
|
|
+ selector = new LabelSelector();
|
|
|
|
|
+ }
|
|
|
|
|
+ selector.addAll(request.getLabels());
|
|
|
|
|
+ }
|
|
|
|
|
+ V1LimitRangeList objList = coreV1Api.listNamespacedLimitRange(
|
|
|
|
|
+ request.getNamespace(),
|
|
|
|
|
+ PRETTY_FORMAT,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ selector.toJsonString(),
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null);
|
|
|
|
|
+ return objList.getItems().stream().map(LimitRange::new).collect(Collectors.toList());
|
|
|
|
|
+ } catch (ApiException e) {
|
|
|
|
|
+ log.error("查找pod失败,namespace = {}, response={}",
|
|
|
|
|
+ request.getNamespace(), e.getResponseBody());
|
|
|
|
|
+ if (e.getCode() == NOT_FOUND) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }else {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("查找pod失败,namespace = {}, name={}",
|
|
|
|
|
+ request.getNamespace(),request.getName());
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private V1LimitRange getV1LimitRange(String namespace, String name) throws ApiException {
|
|
|
|
|
+ return coreV1Api.readNamespacedLimitRange(
|
|
|
|
|
+ name,
|
|
|
|
|
+ namespace,
|
|
|
|
|
+ PRETTY_FORMAT,
|
|
|
|
|
+ null,
|
|
|
|
|
+ null);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|