Преглед на файлове

NamespaceApiImpl 删除和查找

raledong преди 7 години
родител
ревизия
f7e2856713

+ 32 - 5
src/main/java/nju/seec/SEECdemo/logic/api/k8s/impl/NamespaceApiImpl.java

@@ -29,7 +29,7 @@ public class NamespaceApiImpl implements NamespaceApi{
 
     private Logger logger = LoggerUtil.getLogger(NamespaceApi.class);
 
-    private static final String BACKGROUND_PROPAGATION_POLICY = "Background";
+    private static final String FOREGROUND_PROPAGATION_POLICY = "Foreground";
     @Autowired
     private CoreV1Api coreV1Api;
 
@@ -63,13 +63,16 @@ public class NamespaceApiImpl implements NamespaceApi{
         try {
             V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
                     .withApiVersion(Namespace.VERSION)
-                    .withPropagationPolicy(BACKGROUND_PROPAGATION_POLICY)
+                    .withPropagationPolicy(FOREGROUND_PROPAGATION_POLICY)
                     .build();
             coreV1Api.deleteNamespace(name, v1DeleteOptions, PRETTY_FORMAT,
-                    null, null, BACKGROUND_PROPAGATION_POLICY);
+                    null, null, FOREGROUND_PROPAGATION_POLICY);
         } catch (ApiException e) {
             LoggerUtil.error(logger, e, "Namespace删除失败,name={}, response={}", name , e.getResponseBody());
             return new ApiResult<>(e.getCode(), e.getMessage());
+        } catch (Exception e) {
+            LoggerUtil.warn(logger, e, "k8s swagger bug");
+            return new ApiResult<>(null);
         }
         return new ApiResult<>(null);
     }
@@ -101,14 +104,38 @@ public class NamespaceApiImpl implements NamespaceApi{
     @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);
+            String labelSelector = buildLabelSelector(key, value);
+            V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
+                    PRETTY_FORMAT,
+                    null,
+                    null,
+                    true,
+                    labelSelector,
+                    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);
+            LoggerUtil.error(logger, e, "根据标签获取Namespace异常, key={}, value={}, response={}", key, value, e.getResponseBody());
             return new ApiResult<>(e.getCode(), e.getMessage());
         }
     }
 
+    private String buildLabelSelector(String key, String value) {
+        return key + "=" + value;
+    }
+
+    private String buildLabelSelector(Map<String, String> labels) {
+        StringBuilder result = new StringBuilder();
+        for (Map.Entry<String, String> entry : labels.entrySet()) {
+            result.append(entry.getKey());
+            result.append("=");
+            result.append(entry.getValue());
+            result.append(",");
+        }
+        return result.toString();
+    }
 }

+ 1 - 1
src/main/java/nju/seec/SEECdemo/logic/api/k8s/vo/Container.java

@@ -13,7 +13,7 @@ import javax.validation.constraints.Pattern;
 import java.util.*;
 import java.util.stream.Collectors;
 
-import static nju.seec.SEECdemo.logic.api.k8s.vo.Constants.NAME_PATTERN;
+import static nju.seec.SEECdemo.logic.api.k8s.util.Constants.NAME_PATTERN;
 
 /**
  * author: rale

+ 1 - 1
src/main/java/nju/seec/SEECdemo/logic/api/k8s/vo/ContainerPort.java

@@ -10,7 +10,7 @@ import org.hibernate.validator.constraints.Range;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Pattern;
 
-import static nju.seec.SEECdemo.logic.api.k8s.vo.Constants.NAME_PATTERN;
+import static nju.seec.SEECdemo.logic.api.k8s.util.Constants.NAME_PATTERN;
 
 /**
  * author: rale

+ 1 - 1
src/main/java/nju/seec/SEECdemo/logic/api/k8s/vo/Deployment.java

@@ -10,7 +10,7 @@ import javax.validation.constraints.NotNull;
 import java.util.*;
 import java.util.stream.Collectors;
 
-import static nju.seec.SEECdemo.logic.api.k8s.vo.Constants.APPLICATION_LABEL;
+import static nju.seec.SEECdemo.logic.api.k8s.util.Constants.APPLICATION_LABEL;
 
 /**
  * author: rale