|
|
@@ -7,13 +7,15 @@ import io.kubernetes.client.models.V1DeleteOptionsBuilder;
|
|
|
import io.kubernetes.client.models.V1Namespace;
|
|
|
import io.kubernetes.client.models.V1NamespaceList;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.model.Namespace;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.model.LabelSelector;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.model.Namespace;
|
|
|
import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -37,10 +39,11 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
* @see NamespaceApi#create(Namespace)
|
|
|
*/
|
|
|
@Override
|
|
|
- public void create(Namespace namespace) throws K8sApiException{
|
|
|
+ public Namespace create(Namespace namespace) {
|
|
|
V1Namespace v1Namespace = namespace.toV1Namespace();
|
|
|
try {
|
|
|
- coreV1Api.createNamespace(v1Namespace, "true");
|
|
|
+ V1Namespace result = coreV1Api.createNamespace(v1Namespace, PRETTY_FORMAT);
|
|
|
+ return result == null ? null : new Namespace(result);
|
|
|
} catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "Namespace创建失败,namespace={}, v1Namespace={}, response={}", namespace, v1Namespace, e.getResponseBody());
|
|
|
if (e.getCode() == ALREADY_EXIST) {
|
|
|
@@ -51,6 +54,7 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
} catch (Exception e) {
|
|
|
LoggerUtil.error(logger, e, "Namespace创建异常, namespace={}, v1Namespace={}", namespace, v1Namespace);
|
|
|
}
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -59,7 +63,7 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
* @throws K8sApiException
|
|
|
*/
|
|
|
@Override
|
|
|
- public void delete(String name) throws K8sApiException{
|
|
|
+ public void delete(String name) {
|
|
|
try {
|
|
|
deleteNamespace(name);
|
|
|
} catch (ApiException e) {
|
|
|
@@ -91,20 +95,14 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Namespace getByName(String name) throws K8sApiException{
|
|
|
+ public Namespace getByName(String name) {
|
|
|
try {
|
|
|
- V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
+ V1Namespace v1Namespace = coreV1Api.readNamespace(
|
|
|
+ name,
|
|
|
PRETTY_FORMAT,
|
|
|
null,
|
|
|
- METADATA_NAME_SELECTOR + name,
|
|
|
- null,
|
|
|
- null,
|
|
|
- null,
|
|
|
- null,
|
|
|
- null,
|
|
|
null);
|
|
|
- List<Namespace> result = v1NamespaceList.getItems().stream().limit(1).map(Namespace::new).collect(Collectors.toList());
|
|
|
- return result.get(0);
|
|
|
+ return v1Namespace == null ? null : new Namespace(v1Namespace);
|
|
|
}catch (ApiException e) {
|
|
|
LoggerUtil.error(logger, e, "查询Namespace失败,name={}, response={}", name, e.getResponseBody());
|
|
|
if (e.getCode() != NOT_FOUND) {
|
|
|
@@ -115,7 +113,7 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Namespace> getAll() throws K8sApiException{
|
|
|
+ public List<Namespace> getAll() {
|
|
|
try {
|
|
|
V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
PRETTY_FORMAT,
|
|
|
@@ -138,13 +136,8 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
|
|
|
@Override
|
|
|
public List<Namespace> getByLabels(Map<String, String> labels) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<Namespace> getByLabel(String key, String value) throws K8sApiException{
|
|
|
try {
|
|
|
- String labelSelector = buildLabelSelector(key, value);
|
|
|
+ String labelSelector = LabelSelector.toJsonString(labels);
|
|
|
V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
PRETTY_FORMAT,
|
|
|
null,
|
|
|
@@ -159,24 +152,48 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
LoggerUtil.info(logger,"result={}", namespaces);
|
|
|
return namespaces;
|
|
|
} catch (ApiException e) {
|
|
|
- LoggerUtil.error(logger, e, "根据标签获取Namespace异常, key={}, value={}, response={}", key, value, e.getResponseBody());
|
|
|
+ LoggerUtil.error(logger, e, "根据标签获取Namespace异常, labels={}, response={}", labels, e.getResponseBody());
|
|
|
throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean exists(String name) {
|
|
|
+ public List<Namespace> getByLabel(String key, String value){
|
|
|
+ return getByLabels(Collections.singletonMap(key, value));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Namespace> getByLabelSelector(LabelSelector labelSelector) {
|
|
|
try {
|
|
|
- V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(PRETTY_FORMAT,
|
|
|
+ String selector = labelSelector.toJsonString();
|
|
|
+ V1NamespaceList v1NamespaceList = coreV1Api.listNamespace(
|
|
|
+ PRETTY_FORMAT,
|
|
|
null,
|
|
|
- METADATA_NAME_SELECTOR + name,
|
|
|
null,
|
|
|
+ true,
|
|
|
+ selector,
|
|
|
null,
|
|
|
null,
|
|
|
null,
|
|
|
+ null);
|
|
|
+ List<Namespace> namespaces = v1NamespaceList.getItems().stream().map(Namespace::new).collect(Collectors.toList());
|
|
|
+ LoggerUtil.info(logger,"result={}", namespaces);
|
|
|
+ return namespaces;
|
|
|
+ } catch (ApiException e) {
|
|
|
+ LoggerUtil.error(logger, e, "根据标签获取Namespace异常, labelSelector={}, response={}", labelSelector, e.getResponseBody());
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean exists(String name) {
|
|
|
+ try {
|
|
|
+ V1Namespace v1Namespace = coreV1Api.readNamespace(
|
|
|
+ name,
|
|
|
+ PRETTY_FORMAT,
|
|
|
null,
|
|
|
null);
|
|
|
- return v1NamespaceList.getItems().size() > 0;
|
|
|
+ return v1Namespace != null;
|
|
|
}catch (ApiException e) {
|
|
|
if (e.getCode() != NOT_FOUND) {
|
|
|
LoggerUtil.error(logger, e, "查询命名空间失败, name={}, response={}", name, e.getResponseBody());
|
|
|
@@ -206,19 +223,4 @@ public class NamespaceApiImpl implements NamespaceApi{
|
|
|
FOREGROUND_PROPAGATION_POLICY);
|
|
|
}
|
|
|
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
}
|