diff --git a/hango-api-plane-server/pom.xml b/hango-api-plane-server/pom.xml index 68f329f9..b84e959f 100644 --- a/hango-api-plane-server/pom.xml +++ b/hango-api-plane-server/pom.xml @@ -120,7 +120,7 @@ io.fabric8 kubernetes-model-gatewayapi - 6.2.0 + 6.6.2 io.fabric8 diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/configuration/ApiPlaneAutoBaseConfiguration.java b/hango-api-plane-server/src/main/java/org/hango/cloud/configuration/ApiPlaneAutoBaseConfiguration.java index eb73eb97..d1a767d0 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/configuration/ApiPlaneAutoBaseConfiguration.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/configuration/ApiPlaneAutoBaseConfiguration.java @@ -9,7 +9,6 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; import com.google.common.collect.ImmutableList; import com.hubspot.jackson.datatype.protobuf.ProtobufModule; -import org.hango.cloud.configuration.ext.MeshConfig; import org.hango.cloud.util.freemarker.AutoRemoveDirective; import org.hango.cloud.util.freemarker.IgnoreDirective; import org.hango.cloud.util.freemarker.IndentationDirective; @@ -21,7 +20,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.PropertySource; import org.springframework.http.client.BufferingClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.InterceptingClientHttpRequestFactory; @@ -36,7 +34,6 @@ @Configuration -@PropertySource(value = {"classpath:mesh-config.properties"}) public class ApiPlaneAutoBaseConfiguration { @Autowired @@ -99,9 +96,4 @@ void configureFreemarkerConfig() throws IOException { freemarkerConfig.setSharedVariable("supply", new SupplyDirective()); freemarkerConfig.setSharedVariable("autoremove", new AutoRemoveDirective()); } - - @Bean - MeshConfig meshConfig() { - return new MeshConfig(); - } } \ No newline at end of file diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/configuration/ext/MeshConfig.java b/hango-api-plane-server/src/main/java/org/hango/cloud/configuration/ext/MeshConfig.java deleted file mode 100644 index 45100964..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/configuration/ext/MeshConfig.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.hango.cloud.configuration.ext; - -import org.springframework.beans.factory.annotation.Value; - -public class MeshConfig { - - @Value("${meshProjectKey:nsf.skiff.netease.com/project}") - private String projectKey; - @Value("${meshVersionKey:nsf.skiff.netease.com/version}") - private String versionKey; - @Value("${meshAppKey:nsf.skiff.netease.com/app}") - private String appKey; - @Value("${selectorAppKey:nsf.skiff.netease.com/app}") - private String selectorAppKey; - @Value("${trafficMarkLabel:nsf.skiff.netease.com/mark}") - private String trafficMarkLabel; - - public String getSelectorAppKey() { - return selectorAppKey; - } - - public void setSelectorAppKey(String selectorAppKey) { - this.selectorAppKey = selectorAppKey; - } - - public String getProjectKey() { - return projectKey; - } - - public void setProjectKey(String projectKey) { - this.projectKey = projectKey; - } - - public String getVersionKey() { - return versionKey; - } - - public void setVersionKey(String versionKey) { - this.versionKey = versionKey; - } - - public String getAppKey() { - return appKey; - } - - public void setAppKey(String appKey) { - this.appKey = appKey; - } - - public String getTrafficMarkLabel() { - return trafficMarkLabel; - } - - public void setTrafficMarkLabel(String trafficMarkLabel) { - this.trafficMarkLabel = trafficMarkLabel; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/AbstractConfigManagerSupport.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/AbstractConfigManagerSupport.java index f15db37e..6994649f 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/AbstractConfigManagerSupport.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/AbstractConfigManagerSupport.java @@ -30,8 +30,6 @@ protected void update(ConfigStore configStore, List resources, if (latest instanceof EmptyResource && pack.hasSubtracter()) { // 若latest标识为emptyResource, 则进行subtract操作 merged = pack.getSubtracter().subtract(old); - } else if (pack.hasMerger()) { - merged = pack.getMerger().merge(old, latest); } else { merged = modelEngine.merge(old, latest); } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/ConfigStore.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/ConfigStore.java index c271cc3f..2ee913eb 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/ConfigStore.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/ConfigStore.java @@ -21,6 +21,8 @@ public interface ConfigStore { List get(String kind, String namespace); + List get(String kind); + List get(String kind, String namespace, Map labels); } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/GlobalConfig.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/GlobalConfig.java index d87b9d49..4e9ee861 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/GlobalConfig.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/GlobalConfig.java @@ -4,16 +4,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; -import java.util.Arrays; -import java.util.List; - -import static org.hango.cloud.util.Const.VAILD_REGISTRY; - -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import static org.hango.cloud.util.Const.VAILD_REGISTRY; @@ -29,7 +20,7 @@ public class GlobalConfig { @Value("${apiPlaneVersion}") private String apiPlaneVersion; - @Value("${istioRev:gw-1.12}") + @Value("${istioRev:gw-stable}") private String istioRev; @Value("${telnet.connect.timeout:3000}") @@ -41,6 +32,23 @@ public class GlobalConfig { @Value("${registry:all}") private String registry; + //项目隔离标识 + @Value("${projectCode:skiff.netease.com/project}") + private String projectCode; + + //ingress controller标识,和istio保持一致 + @Value("${ingressClass:hango}") + private String ingressClass; + + //自定义插件代码挂载cm 名称 + @Value("${pluginContentConfigName:hango-rider-plugin}") + private String pluginContentConfigName; + + //自定义插件schema挂载cm 名称 + @Value("${pluginSchemaConfigName:hango-plugin-schema}") + private String pluginSchemaConfigName; + + private final String ALL = "all"; @Value("${ignorePlugins:#{null}}") @@ -85,4 +93,21 @@ public List getRegistryList() { } return Arrays.asList(registry.split(",")); } + + public String getProjectCode() { + return projectCode; + } + + public String getIngressClass() { + return ingressClass; + } + + public String getPluginContentConfigName() { + return pluginContentConfigName; + } + + public String getPluginSchemaConfigName() { + return pluginSchemaConfigName; + } + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/IstioModelEngine.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/IstioModelEngine.java index e12e3540..d1e5b08d 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/IstioModelEngine.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/IstioModelEngine.java @@ -1,5 +1,6 @@ package org.hango.cloud.core; +import io.fabric8.kubernetes.api.model.HasMetadata; import org.hango.cloud.core.editor.ResourceType; import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.k8s.K8sResourceGenerator; @@ -7,9 +8,7 @@ import org.hango.cloud.core.k8s.operator.IntegratedResourceOperator; import org.hango.cloud.util.exception.ApiPlaneException; import org.hango.cloud.util.exception.ExceptionConst; -import org.hango.cloud.util.function.Merger; import org.hango.cloud.util.function.Subtracter; -import io.fabric8.kubernetes.api.model.HasMetadata; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -69,34 +68,24 @@ protected HasMetadata str2HasMetadata(String str) { } protected List generateK8sPack(List raws) { - return generateK8sPack(raws, null, null, r -> r, this::str2HasMetadata, hsm -> hsm); + return generateK8sPack(raws, null, r -> r, this::str2HasMetadata, hsm -> hsm); } - protected List generateK8sPack(List raws, Function transFun) { - return generateK8sPack(raws, null, null, r -> r, transFun, hsm -> hsm); - } protected List generateK8sPack(List raws, Subtracter subtracter) { - return generateK8sPack(raws, null, subtracter, r -> r, this::str2HasMetadata, hsm -> hsm); + return generateK8sPack(raws, subtracter, r -> r, this::str2HasMetadata, hsm -> hsm); } - protected List generateK8sPack(List raws, Merger merger, Subtracter subtracter, Function transFun) { - return generateK8sPack(raws, merger, subtracter, r -> r, transFun, hsm -> hsm); - } - - protected List generateK8sPack(List raws, Merger merger, Subtracter subtracter, Function preFun, Function transFun) { - return generateK8sPack(raws, merger, subtracter, preFun, transFun, hsm -> hsm); + protected List generateK8sPack(List raws, Subtracter subtracter, Function transFun) { + return generateK8sPack(raws, subtracter, r -> r, transFun, hsm -> hsm); } protected List generateK8sPack(List raws, Subtracter subtracter, Function preFun, Function postFun) { - return generateK8sPack(raws, null, subtracter, preFun, this::str2HasMetadata, postFun); + return generateK8sPack(raws, subtracter, preFun, this::str2HasMetadata, postFun); } - protected List generateK8sPack(List raws, Function preFun, Function postFun) { - return generateK8sPack(raws, null, null, preFun, this::str2HasMetadata, postFun); - } - protected List generateK8sPack(List raws, Merger merger, Subtracter subtracter, + protected List generateK8sPack(List raws, Subtracter subtracter, Function preFun, Function transFun, Function postFun) { if (CollectionUtils.isEmpty(raws)) { @@ -106,7 +95,7 @@ protected List generateK8sPack(List raws, Merger merger return raws.stream().map(r -> preFun.apply(r)) .map(r -> transFun.apply(r)) .map(hsm -> postFun.apply(hsm)) - .map(hsm -> new K8sResourcePack(hsm, merger, subtracter)) + .map(hsm -> new K8sResourcePack(hsm, subtracter)) .collect(Collectors.toList()); } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/editor/ResourceGenerator.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/editor/ResourceGenerator.java index ac767817..b4dd3345 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/editor/ResourceGenerator.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/editor/ResourceGenerator.java @@ -8,7 +8,6 @@ import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Predicate; import org.hango.cloud.util.exception.ApiPlaneException; -import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.util.List; @@ -274,9 +273,6 @@ public static String yaml2json(String yaml) { return yaml2json(yaml, defaultContext); } - public static String json2yaml(String json) { - return json2yaml(json, defaultContext); - } public static String obj2json(Object obj) { return obj2json(obj, defaultContext); @@ -286,15 +282,6 @@ public static T json2obj(String json, Class type) { return json2obj(json, type, defaultContext); } - public static T yaml2obj(String yaml, Class type) { - return yaml2obj(yaml, type, defaultContext); - } - - public static String prettyJson(String json) { - if (StringUtils.isEmpty(json)) return json; - return prettyJson(json, defaultContext); - } - public static String yaml2json(String yaml, EditorContext editorContext) { try { Object obj = editorContext.yamlMapper().readValue(yaml, Object.class); @@ -329,16 +316,4 @@ public static T json2obj(String json, Class type, EditorContext editorCon throw new ApiPlaneException(e.getMessage(), e); } } - - public static T yaml2obj(String yaml, Class type, EditorContext editorContext) { - return json2obj(yaml2json(yaml, editorContext), type, editorContext); - } - - public static String prettyJson(String json, EditorContext editorContext) { - try { - return editorContext.jsonMapper().writerWithDefaultPrettyPrinter().writeValueAsString(json2obj(json, Object.class, editorContext)); - } catch (JsonProcessingException e) { - throw new ApiPlaneException(e.getMessage(), e); - } - } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/envoy/EnvoyHttpClient.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/envoy/EnvoyHttpClient.java index 977f6b25..02f4a8f4 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/envoy/EnvoyHttpClient.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/envoy/EnvoyHttpClient.java @@ -1,31 +1,32 @@ package org.hango.cloud.core.envoy; import com.google.common.collect.ImmutableMap; +import io.fabric8.kubernetes.api.model.*; +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.util.Strings; import org.hango.cloud.core.editor.ResourceGenerator; import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; -import org.hango.cloud.core.k8s.KubernetesClient; +import org.hango.cloud.core.k8s.K8sClient; import org.hango.cloud.meta.EndpointHealth; import org.hango.cloud.meta.HealthServiceSubset; import org.hango.cloud.meta.ServiceHealth; import org.hango.cloud.util.exception.ApiPlaneException; import org.hango.cloud.util.exception.ExceptionConst; -import io.fabric8.kubernetes.api.model.Pod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import java.util.*; import java.util.function.Function; import java.util.function.Predicate; -import java.util.regex.Pattern; import java.util.stream.Collectors; +import static org.hango.cloud.util.exception.ExceptionConst.ENVOY_SERVICE_NON_EXIST; + @Component public class EnvoyHttpClient { @@ -35,7 +36,8 @@ public class EnvoyHttpClient { private RestTemplate restTemplate; @Autowired - private KubernetesClient client; + private K8sClient client; + @Value(value = "${gatewayNamespace:gateway-system}") private String gatewayNamespace; @@ -48,34 +50,171 @@ public class EnvoyHttpClient { private static final String GET_CLUSTER_HEALTH_JSON = "/clusters?format=json"; - private static final String SUBSET_PATTERN = ".+\\|\\d+\\|.+\\|.*"; private static final String HEALTHY = "HEALTHY"; private static final String UNHEALTHY = "UNHEALTHY"; - private String getEnvoyUrl() { - return getEnvoyUrlByLabels(ImmutableMap.of("app", gatewayName)); - } + private static final String OPERATOR_IN = "In"; + private static final String OPERATOR_NOT_IN = "NotIn"; + private static final String OPERATOR_EXISTS = "Exists"; + private static final String OPERATOR_DOES_NOT_EXIST = "DoesNotExist"; + private static final String OPERATOR_GT = "Gt"; + private static final String OPERATOR_LT = "Lt"; + public static final String INTERNAL_IP = "InternalIP"; + + /** + * 获取envoy pod ip + * @param gateway: gwClusterName + * @return ip:19000 + */ private String getEnvoyUrl(String gateway) { - return getEnvoyUrlByLabels(ImmutableMap.of("gw_cluster", gateway)); + Pod envoyPod = getEnvoyPod(gateway); + return String.format("http://%s:19000", envoyPod.getStatus().getPodIP()); + } + + /** + * 获取envoy pod + */ + public Pod getEnvoyPod(String gwClusterName){ + List podList = client.getPods(gatewayNamespace, ImmutableMap.of("gw_cluster", gwClusterName)); + if (CollectionUtils.isEmpty(podList)) { + throw new ApiPlaneException(ExceptionConst.ENVOY_POD_NON_EXIST); + } + Pod runningPod = podList.stream().filter(e -> e.getStatus().getPhase().equals("Running")).findFirst().orElse(null); + if (runningPod == null){ + throw new ApiPlaneException(ExceptionConst.ENVOY_POD_NON_EXIST); + } + return runningPod; + } + + /** + * 获取envoy service + * 1.基于gwclusterName获取 envoy pod + * 2.从envoy pod 获取 app label + * 3.基于app label 获取envoy service + */ + public List getEnvoyServiceList(String gwClusterName){ + Pod envoyPod = getEnvoyPod(gwClusterName); + String app = envoyPod.getMetadata().getLabels().get("app"); + if (StringUtils.isEmpty(app)){ + throw new ApiPlaneException(ENVOY_SERVICE_NON_EXIST); + } + return client.getServices(gatewayNamespace, ImmutableMap.of("app", gatewayName)); + } + + /** + * 获取envoy可以调度的node ip,会基于nodeAffinity做node过滤 + * ex: + * nodeAffinity: + * requiredDuringSchedulingIgnoredDuringExecution: + * nodeSelectorTerms: + * - matchExpressions: + * - key: skiff/apigw-gateway + * operator: In + * values: + * - "true" + * + * 匹配node label中skiff/apigw-gateway=true的节点 + */ + public List getEnvoySchedulableNodeAddress(String gwClusterName) { + List node = client.getNode(); + List nodeSelectorTerm = getEnvoyNodeSelectorTerm(gwClusterName); + return node.stream().filter(o -> isSchedulable(o, nodeSelectorTerm)).map(this::parseNodeAddress).collect(Collectors.toList()); + } + + private List getEnvoyNodeSelectorTerm(String gwClusterName){ + Pod pod = getEnvoyPod(gwClusterName); + Affinity affinity = pod.getSpec().getAffinity(); + if (affinity == null){ + return new ArrayList<>(); + } + NodeAffinity nodeAffinity = affinity.getNodeAffinity(); + if (nodeAffinity == null){ + return new ArrayList<>(); + } + NodeSelector requiredSelector = nodeAffinity.getRequiredDuringSchedulingIgnoredDuringExecution(); + if (requiredSelector == null){ + return new ArrayList<>(); + } + return requiredSelector.getNodeSelectorTerms() + .stream() + .flatMap(term -> term.getMatchExpressions().stream()) + .collect(Collectors.toList()); } - private String getEnvoyUrlByLabels(Map labels) { - if (!StringUtils.isEmpty(envoyUrl)) return envoyUrl; - //envoy service暂时未暴露管理端口,直接拿pod ip - List envoyPods = client.getObjectList(K8sResourceEnum.Pod.name(), gatewayNamespace, labels); - if (CollectionUtils.isEmpty(envoyPods)) throw new ApiPlaneException(ExceptionConst.ENVOY_POD_NON_EXIST); - Optional healthPod = envoyPods.stream() - .filter(e -> e.getStatus().getPhase().equals("Running")) - .map(e -> "http://" + e.getStatus().getPodIP()) - .findFirst(); - if (!healthPod.isPresent()) throw new ApiPlaneException(ExceptionConst.ENVOY_POD_NON_EXIST); - //fixed port - int port = 19000; - return healthPod.get() + ":" + port; + /** + * 基于k8s操作符匹配节点信息 + * 操作符包括:[In,NotIn,Exists,DoesNotExist,Gt,Lt] + * @param node 节点 + * @param nodeSelectorRequirements nodeselector匹配条件 + * @return 是否允许调度 + */ + @SuppressWarnings("java:S3776") + private boolean isSchedulable(Node node, List nodeSelectorRequirements){ + for (NodeSelectorRequirement requirement : nodeSelectorRequirements) { + String key = requirement.getKey(); + String operator = requirement.getOperator(); + List values = requirement.getValues(); + + Map labels = node.getMetadata().getLabels(); + String nodeValue = labels.get(key); + + if (nodeValue == null) { + return false; + } + switch (operator) { + case OPERATOR_IN: + if (!values.contains(nodeValue)) { + return false; + } + break; + case OPERATOR_NOT_IN: + if (values.contains(nodeValue)) { + return false; + } + break; + case OPERATOR_EXISTS: + // 如果节点的标签值不为空,则符合要求 + break; + case OPERATOR_DOES_NOT_EXIST: + // 如果节点的标签值为空,则符合要求 + return false; + case OPERATOR_GT: + String max = Collections.max(values); + if (nodeValue.compareTo(max) <= 0) { + return false; + } + break; + case OPERATOR_LT: + String min = Collections.min(values); + if (nodeValue.compareTo(min) >= 0) { + return false; + } + break; + default: + // 如果操作符不是 In、NotIn、Exists、DoesNotExist、Gt 或 Lt,则不符合要求 + return false; + } + } + return true; + } + + private String parseNodeAddress(Node node){ + List addresses = node.getStatus().getAddresses(); + if (CollectionUtils.isEmpty(addresses)){ + return Strings.EMPTY; + } + for (NodeAddress address : addresses) { + if (address.getType().equals(INTERNAL_IP)){ + return address.getAddress(); + } + } + return Strings.EMPTY; } + + @SuppressWarnings("java:S3776") public List getServiceHealth(Function nameHandler, Predicate filter, String gateway) { Map> healthMap = new HashMap<>(); @@ -83,11 +222,10 @@ public List getServiceHealth(Function nameFunction = nameHandler == null ? n -> new HealthServiceSubset(n) : nameHandler; + Function nameFunction = nameHandler == null ? HealthServiceSubset::new : nameHandler; Predicate serviceFilter = filter == null ? n -> true : filter; if (CollectionUtils.isEmpty(endpoints)) return Collections.emptyList(); - endpoints.stream() - .forEach(e -> { + endpoints.forEach(e -> { ResourceGenerator gen = ResourceGenerator.newInstance(e, ResourceType.OBJECT); String serviceName = gen.getValue("$.name"); @@ -107,7 +245,7 @@ public List getServiceHealth(Function new ArrayList()).add(eh); + healthMap.computeIfAbsent(serviceSubset.getHost(), v -> new ArrayList<>()).add(eh); } }); @@ -115,7 +253,7 @@ public List getServiceHealth(Function { ServiceHealth sh = new ServiceHealth(); Map mergedEhs = new HashMap<>(); - ehs.stream().forEach(eh -> { + ehs.forEach(eh -> { if (mergedEhs.containsKey(eh)) { // 有任何一个实例不健康,则整体不健康 if (Objects.equals(eh.getStatus(), UNHEALTHY) @@ -143,15 +281,12 @@ private String healthStatus(ResourceGenerator gen, int index) { Boolean failedActive = gen.getValue(String.format("$.host_statuses[%d].health_status.failed_active_health_check", index)); Boolean failedOutlier = gen.getValue(String.format("$.host_statuses[%d].health_status.failed_outlier_check", index)); - boolean active = failedActive == null ? false : true; - boolean outlier = failedOutlier == null ? false : true; + boolean active = failedActive != null; + boolean outlier = failedOutlier != null; return !(active || outlier) ? HEALTHY : UNHEALTHY; } - private boolean isSubset(String name) { - return Pattern.matches(SUBSET_PATTERN, name); - } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/GatewayIstioModelEngine.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/GatewayIstioModelEngine.java index 34be6956..3df98333 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/GatewayIstioModelEngine.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/GatewayIstioModelEngine.java @@ -17,7 +17,6 @@ import org.hango.cloud.core.k8s.subtracter.GatewayPluginNormalSubtracter; import org.hango.cloud.core.k8s.subtracter.GatewayVirtualServiceSubtracter; import org.hango.cloud.core.plugin.FragmentHolder; -import org.hango.cloud.core.plugin.FragmentWrapper; import org.hango.cloud.core.template.TemplateTranslator; import org.hango.cloud.k8s.K8sTypes; import org.hango.cloud.k8s.K8sTypes.VirtualService; @@ -112,21 +111,12 @@ public List translate(API api, boolean simple) { logger.info("{}{} start translate k8s resource", LogConstant.TRANSLATE_LOG_NOTE, LogConstant.ROUTE_LOG_NOTE); List resourcePacks = new ArrayList<>(); api.setNamespace(globalConfig.getResourceNamespace()); - List vsFragments = new ArrayList<>(); - // 渲染VS上的插件片段(当前仅有Match插件) - if (!StringUtils.isEmpty(api.getPlugins())) { - vsFragments = renderPlugins(api.getPlugins()).stream() - .map(FragmentHolder::getVirtualServiceFragment) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - } if (NumberUtils.INTEGER_ZERO.equals(api.getCustomDefaultRespCode())){ api.setCustomDefaultRespCode(globalConfig.getCustomDefaultRespCode()); } List rawVirtualServices = renderTwiceModelProcessor .process(API_VIRTUAL_SERVICE, api, - new PortalVirtualServiceAPIDataHandler( - defaultModelProcessor, vsFragments, simple)); + new PortalVirtualServiceAPIDataHandler(defaultModelProcessor, simple)); logger.info("{}{} start to generate and add k8s resource", LogConstant.TRANSLATE_LOG_NOTE, LogConstant.ROUTE_LOG_NOTE); @@ -215,14 +205,13 @@ private List configureEnvoyPlugin(RawResourceContainer rawResou // 插件配置放在GatewayPlugin的CRD上 List rawGatewayPlugins = renderTwiceModelProcessor.process(GATEWAY_PLUGIN, plugin, new GatewayPluginDataHandler( - rawResourceContainer.getVirtualServices(), globalConfig.getResourceNamespace())); + rawResourceContainer.getGatewayPlugins(), globalConfig.getResourceNamespace())); // 当插件传入为空时,生成空的GatewayPlugin,删除时使用 DynamicGatewayPluginSupplier dynamicGatewayPluginSupplier = new DynamicGatewayPluginSupplier(plugin.getGateway(), plugin.getRouteId(), "%s-%s"); resourcePacks.addAll(generateK8sPack(rawGatewayPlugins, - null, new GatewayPluginNormalSubtracter(), new DynamicResourceGenerator(dynamicGatewayPluginSupplier))); logger.info("{}{} raw GatewayPlugin CRDs added ok", @@ -276,7 +265,7 @@ private List renderPlugins(List pluginList) { } List plugins = pluginList.stream() - .filter(p -> !StringUtils.isEmpty(p)) + .filter(StringUtils::hasText) .collect(Collectors.toList()); return pluginService.processPlugin(plugins, new ServiceInfo()); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/RawResourceContainer.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/RawResourceContainer.java index 67fab512..b6ebf724 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/RawResourceContainer.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/RawResourceContainer.java @@ -9,8 +9,6 @@ public class RawResourceContainer { - List virtualServices = new ArrayList<>(); - List sharedConfigs = new ArrayList<>(); List gatewayPlugins = new ArrayList<>(); List smartLimiters = new ArrayList<>(); @@ -18,12 +16,6 @@ public void add(FragmentHolder holder) { if (holder == null) return; - if (holder.getVirtualServiceFragment() != null) { - virtualServices.add(holder.getVirtualServiceFragment()); - } - if (holder.getSharedConfigFragment() != null) { - sharedConfigs.add(holder.getSharedConfigFragment()); - } if (holder.getGatewayPluginsFragment() != null) { gatewayPlugins.add(holder.getGatewayPluginsFragment()); } @@ -35,16 +27,9 @@ public void add(FragmentHolder holder) { public void add(List holders) { if (CollectionUtils.isEmpty(holders)) return; - holders.stream().forEach(h -> add(h)); + holders.forEach(this::add); } - public List getVirtualServices() { - return virtualServices; - } - - public List getSharedConfigs() { - return sharedConfigs; - } public List getGatewayPlugins() { return gatewayPlugins; diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/APIDataHandler.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/APIDataHandler.java index 0c3d3a05..b7025b36 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/APIDataHandler.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/APIDataHandler.java @@ -8,7 +8,6 @@ import org.hango.cloud.k8s.K8sTypes; import org.hango.cloud.meta.API; import org.hango.cloud.meta.CRDMetaEnum; -import org.hango.cloud.meta.PairMatch; import org.hango.cloud.meta.UriMatch; import org.hango.cloud.meta.dto.DubboInfoDto; import org.hango.cloud.util.CommonUtil; @@ -19,7 +18,6 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -56,7 +54,6 @@ public TemplateParams handleApi(API api) { .put(API_LOADBALANCER, api.getLoadBalancer()) .put(API_REQUEST_URIS, uriMatchMeta.getUri()) .put(VIRTUAL_SERVICE_URL_MATCH, uriMatchMeta.getUriMatch()) - .put(API_MATCH_PLUGINS, api.getPlugins()) .put(API_METHODS, methods) .put(API_RETRIES, api.getRetries()) .put(API_PRESERVE_HOST, api.getPreserveHost()) @@ -86,8 +83,6 @@ public TemplateParams handleApi(API api) { .put(SERVICE_INFO_API_REQUEST_URIS, getOrDefault(uriMatchMeta.getUri(), ".*")) .put(SERVICE_INFO_VIRTUAL_SERVICE_HOST_HEADERS, getOrDefault(hostHeaders, ".*")) .put(VIRTUAL_SERVICE_REQUEST_HEADERS, api.getRequestOperation()) - .put(VIRTUAL_SERVICE_VIRTUAL_CLUSTER_NAME, api.getVirtualClusterName()) - .put(VIRTUAL_SERVICE_VIRTUAL_CLUSTER_HEADERS, getVirtualClusterHeaders(api)) .put(VIRTUAL_SERVICE_RESP_EXCEPTION_CODE, api.getCustomDefaultRespCode()) ; @@ -181,48 +176,9 @@ private String getMethods(API api) { return String.join("|", api.getMethods()); } - private List getVirtualClusterHeaders(API api){ - List virtualClusterHeaders = new ArrayList<>(); - if (StringUtils.isEmpty(api.getVirtualClusterName())){ - return virtualClusterHeaders; - } - //headers - if (api.getHeaders() != null) { - virtualClusterHeaders.addAll(api.getHeaders()); - } - //构造method - String methods = getMethods(api); - if (!StringUtils.isEmpty(methods)) { - virtualClusterHeaders.add(new PairMatch(":method", methods, "regex")); - } - //构造path - if (CollectionUtils.isEmpty(api.getVirtualClusterHeaders())) { - virtualClusterHeaders.add(new PairMatch(":path", getVirtualClusterUris(api), "regex")); - }else { - //支持前端传入:path,匹配query - virtualClusterHeaders.addAll(api.getVirtualClusterHeaders()); - } - //authority - String authority = produceHostHeaders(api); - if (!StringUtils.isEmpty(authority)) { - virtualClusterHeaders.add(new PairMatch(":authority", authority, "regex")); - } - return virtualClusterHeaders; - } abstract List doHandle(TemplateParams tp, API api); - String getVirtualClusterUris(API api){ - final StringBuffer suffix = new StringBuffer(); - if (api.getUriMatch().equals(UriMatch.prefix) || api.getUriMatch().equals(UriMatch.exact)) { - suffix.append(".*"); - } - String uri = String.join("|", api.getRequestUris().stream() - .map(u -> u + suffix.toString()) - .collect(Collectors.toList())); - return StringEscapeUtils.escapeJava(uri); - } - UriMatchMeta getUris(API api) { //only one path,return String uri; diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/BaseVirtualServiceAPIDataHandler.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/BaseVirtualServiceAPIDataHandler.java index b3b0a9eb..a2f90fd2 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/BaseVirtualServiceAPIDataHandler.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/BaseVirtualServiceAPIDataHandler.java @@ -1,8 +1,8 @@ package org.hango.cloud.core.gateway.handler; +import org.apache.commons.lang3.StringUtils; import org.hango.cloud.core.gateway.processor.ModelProcessor; -import org.hango.cloud.core.plugin.FragmentWrapper; import org.hango.cloud.core.template.TemplateParams; import org.hango.cloud.meta.API; import org.hango.cloud.meta.Endpoint; @@ -11,13 +11,14 @@ import org.hango.cloud.util.PriorityUtil; import org.hango.cloud.util.exception.ApiPlaneException; import org.hango.cloud.util.exception.ExceptionConst; -import org.apache.commons.lang3.StringUtils; import java.util.*; import java.util.stream.Collectors; import static org.hango.cloud.core.template.TemplateConst.*; + +@SuppressWarnings("java:S3740") public class BaseVirtualServiceAPIDataHandler extends APIDataHandler { static final String API_VIRTUAL_SERVICE_MATCH = "gateway/api/virtualServiceMatch"; @@ -29,7 +30,6 @@ public class BaseVirtualServiceAPIDataHandler extends APIDataHandler { static final String API_VIRTUAL_SERVICE_MIRROR = "gateway/api/virtualServiceMirror"; ModelProcessor subModelProcessor; - List fragments; List endpoints; boolean simple; @@ -37,9 +37,8 @@ public BaseVirtualServiceAPIDataHandler(ModelProcessor modelProcessor) { this.subModelProcessor = modelProcessor; } - public BaseVirtualServiceAPIDataHandler(ModelProcessor subModelProcessor, List fragments, List endpoints, boolean simple) { + public BaseVirtualServiceAPIDataHandler(ModelProcessor subModelProcessor, List endpoints, boolean simple) { this.subModelProcessor = subModelProcessor; - this.fragments = fragments; this.endpoints = endpoints; this.simple = simple; } @@ -47,14 +46,6 @@ public BaseVirtualServiceAPIDataHandler(ModelProcessor subModelProcessor, List doHandle(TemplateParams baseParams, API api) { - // 插件分为match、api、host三个级别 - List matchPlugins = new ArrayList<>(); - // api下的插件 可以根据插件划分 - Map> apiPlugins = new HashMap<>(); - List hostPlugins = new ArrayList<>(); - - HandlerUtil.distributePlugins(fragments, matchPlugins, apiPlugins, hostPlugins); - int pluginPriority = calculatePluginPriority(api, baseParams.get(VIRTUAL_SERVICE_MATCH_PRIORITY)); String matchYaml = produceMatch(baseParams); @@ -66,13 +57,12 @@ List doHandle(TemplateParams baseParams, API api) { .setParent(baseParams) .put(VIRTUAL_SERVICE_MATCH_YAML, matchYaml) .put(VIRTUAL_SERVICE_MATCH_PRIORITY_YAML, matchPriorityYaml) - .put(API_MATCH_PLUGINS, matchPlugins) .put(VIRTUAL_SERVICE_HTTP_RETRY_YAML, httpRetryYaml) .put(VIRTUAL_SERVICE_PLUGIN_MATCH_PRIORITY, pluginPriority) .put(SERVICE_INFO_VIRTUAL_SERVICE_PLUGIN_MATCH_PRIORITY, pluginPriority) .put(VIRTUAL_SERVICE_MIRROR_YAML,mirrorYaml); - List collect = api.getGateways().stream() + return api.getGateways().stream() .map(gw -> { String subset = buildVirtualServiceSubsetName(api.getService(), api.getName(), gw); String route = produceRoute(api, endpoints, subset); @@ -81,7 +71,6 @@ List doHandle(TemplateParams baseParams, API api) { .setParent(vsParams) .put(VERSION, api.getVersion()) .put(GATEWAY_NAME, buildGatewayName(api.getService(), gw)) - .put(VIRTUAL_GATEWAY_CODE, api.getVirtualClusterName()) .put(VIRTUAL_SERVICE_NAME, HandlerUtil.buildVirtualServiceName(api.getName(), api.getProjectId(), gw)) .put(VIRTUAL_SERVICE_SUBSET_NAME, subset) .put(VIRTUAL_SERVICE_ROUTE_YAML, route) @@ -92,8 +81,6 @@ List doHandle(TemplateParams baseParams, API api) { return tmpParams; }) .collect(Collectors.toList()); - - return collect; } String productExtra(TemplateParams params) { diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/GatewayPluginDataHandler.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/GatewayPluginDataHandler.java index 83db0500..2d1fd111 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/GatewayPluginDataHandler.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/GatewayPluginDataHandler.java @@ -32,12 +32,12 @@ public List handle(GatewayPlugin plugin) { plugin.setPort(80); } List params = new ArrayList<>(); - Map> gatewayPluginMap = HandlerUtil.getGatewayPlugins(fragments); + List gatewayPlugins = HandlerUtil.getGatewayPlugins(fragments); TemplateParams gatewayPluginParams = TemplateParams.instance() .put(TemplateConst.VERSION, plugin.getVersion()) .put(TemplateConst.GATEWAY_PLUGIN_GATEWAYS, getGatewayName(plugin)) .put(TemplateConst.GATEWAY_PLUGIN_NAME, HandlerUtil.getGatewayPluginName(plugin)) - .put(TemplateConst.GATEWAY_PLUGIN_PLUGINS, gatewayPluginMap); + .put(TemplateConst.GATEWAY_PLUGIN_PLUGINS, gatewayPlugins); // 路由和全局插件模板渲染数据区分填充 diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/PortalDestinationRuleServiceDataHandler.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/PortalDestinationRuleServiceDataHandler.java index 835b0edb..aeb12feb 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/PortalDestinationRuleServiceDataHandler.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/PortalDestinationRuleServiceDataHandler.java @@ -1,25 +1,24 @@ package org.hango.cloud.core.gateway.handler; -import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; import org.hango.cloud.core.template.TemplateParams; +import org.hango.cloud.k8s.K8sTypes; +import org.hango.cloud.meta.CRDMetaEnum; import org.hango.cloud.meta.Service; import org.hango.cloud.meta.dto.LocalitySettingDTO; import org.hango.cloud.meta.dto.PortalServiceConnectionPoolDTO; import org.hango.cloud.util.CommonUtil; import org.hango.cloud.util.Const; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.springframework.util.CollectionUtils; import java.util.Arrays; +import java.util.Iterator; import java.util.List; +import java.util.Map; import static org.hango.cloud.core.template.TemplateConst.*; public class PortalDestinationRuleServiceDataHandler extends ServiceDataHandler { - private static final Logger logger = LoggerFactory.getLogger(PortalDestinationRuleServiceDataHandler.class); - - private static YAMLMapper yamlMapper; @Override List doHandle(TemplateParams tp, Service service) { @@ -110,6 +109,32 @@ List doHandle(TemplateParams tp, Service service) { params.put(DESTINATION_RULE_TCP_CONNECTION_POOL_MAX_CONNECTIONS, portalServiceTcpConnectionPoolDTO.getMaxConnections()); } - return Arrays.asList(params); + return Arrays.asList(handleServiceMetaMap(service, params)); + } + + /** + * 处理DestinationRule metadata 数据 + * + * @param service 上层输入的service数据 + * @param tp 模板参数 + * @return TemplateParams + */ + private TemplateParams handleServiceMetaMap(Service service, TemplateParams tp) { + if (CollectionUtils.isEmpty(service.getMetaMap())) { + return tp; + } + Iterator>> iterator = service.getMetaMap().entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry> entry = iterator.next(); + CRDMetaEnum metaEnum = CRDMetaEnum.get(K8sTypes.DestinationRule.class, entry.getKey()); + switch (metaEnum) { + case DESTINATION_RULE_STATS_META: + tp.put(metaEnum.getTemplateName(), entry.getValue()); + break; + default: + break; + } + } + return tp; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/PortalVirtualServiceAPIDataHandler.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/PortalVirtualServiceAPIDataHandler.java index d7d7673d..c6a7e44f 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/PortalVirtualServiceAPIDataHandler.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/handler/PortalVirtualServiceAPIDataHandler.java @@ -1,7 +1,6 @@ package org.hango.cloud.core.gateway.handler; import org.hango.cloud.core.gateway.processor.ModelProcessor; -import org.hango.cloud.core.plugin.FragmentWrapper; import org.hango.cloud.core.template.TemplateParams; import org.hango.cloud.meta.API; import org.hango.cloud.meta.Endpoint; @@ -15,14 +14,15 @@ import static org.hango.cloud.core.template.TemplateConst.VIRTUAL_SERVICE_DESTINATIONS; +@SuppressWarnings("java:S3740") public class PortalVirtualServiceAPIDataHandler extends BaseVirtualServiceAPIDataHandler { public PortalVirtualServiceAPIDataHandler(ModelProcessor modelProcessor){ super(modelProcessor); }; - public PortalVirtualServiceAPIDataHandler(ModelProcessor subModelProcessor, List fragments, boolean simple) { - super(subModelProcessor, fragments, Collections.emptyList(), simple); + public PortalVirtualServiceAPIDataHandler(ModelProcessor subModelProcessor, boolean simple) { + super(subModelProcessor, Collections.emptyList(), simple); } @Override @@ -41,7 +41,7 @@ String produceRoute(API api, List endpoints, String subset) { param.put("weight", service.getWeight()); param.put("subset", StringUtils.isEmpty(service.getSubset()) ? service.getCode() + "-" + gateway : service.getSubset()); - Integer port = -1; + int port = -1; String host = decorateHost(service.getCode()); if (Const.PROXY_SERVICE_TYPE_DYNAMIC.equals(service.getType())) { @@ -58,8 +58,7 @@ String produceRoute(API api, List endpoints, String subset) { destinations.add(param); } - String destinationStr = subModelProcessor.process(API_VIRTUAL_SERVICE_ROUTE, TemplateParams.instance().put(VIRTUAL_SERVICE_DESTINATIONS, destinations)); - return destinationStr; + return subModelProcessor.process(API_VIRTUAL_SERVICE_ROUTE, TemplateParams.instance().put(VIRTUAL_SERVICE_DESTINATIONS, destinations)); } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/GatewayConfigManager.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/GatewayConfigManager.java index b0450ece..5b6466f3 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/GatewayConfigManager.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/GatewayConfigManager.java @@ -1,5 +1,6 @@ package org.hango.cloud.core.gateway.service; +import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.HasMetadata; import org.hango.cloud.core.ConfigManager; import org.hango.cloud.meta.*; @@ -48,6 +49,21 @@ public interface GatewayConfigManager extends ConfigManager { */ HasMetadata getConfig(String kind, String name); + /** + * 获取资源 + */ + HasMetadata getConfig(String kind, String namespace, String name); + + /** + * 获取资源列表,基于rev进行过滤 + */ + List getConfigListWithRev(String kind); + + /** + * 获取资源列表,基于rev进行过滤 + */ + List getConfigList(String kind); + /** * 更新插件优先级 * @param pluginOrder @@ -95,4 +111,15 @@ public interface GatewayConfigManager extends ConfigManager { List generateEnvoyConfigObjectPatch(IpSourceEnvoyFilterDTO ipSourceEnvoyFilterDTO); + + /** + * 更新k8s服务 + */ + void updateK8sService(io.fabric8.kubernetes.api.model.Service service); + + + /** + * 更新Configmap资源 + */ + void updateConfig(ConfigMap configMap); } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/DefaultResourceManager.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/DefaultResourceManager.java index 7ccdfce0..9c28756d 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/DefaultResourceManager.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/DefaultResourceManager.java @@ -22,6 +22,7 @@ import static org.hango.cloud.util.Const.*; + @Component public class DefaultResourceManager implements ResourceManager { @@ -131,6 +132,7 @@ private boolean namespaceFilter(String hostName){ * @return 是否满足所有过滤器条件 */ private boolean isMatchFilter(Endpoint endpoint, Map> kindFilters) { + if (!isMatchFilter(endpoint, kindFilters, PREFIX_LABEL)) { return false; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/GatewayConfigManagerImpl.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/GatewayConfigManagerImpl.java index 1b5eeeff..740fe202 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/GatewayConfigManagerImpl.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/GatewayConfigManagerImpl.java @@ -1,5 +1,6 @@ package org.hango.cloud.core.gateway.service.impl; +import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.HasMetadata; import me.snowdrop.istio.api.networking.v1alpha3.ServiceEntry; import org.hango.cloud.core.AbstractConfigManagerSupport; @@ -14,6 +15,7 @@ import org.hango.cloud.core.k8s.K8sResourcePack; import org.hango.cloud.core.k8s.event.K8sResourceDeleteNotificationEvent; import org.hango.cloud.core.k8s.subtracter.ServiceEntryEndpointsSubtracter; +import org.hango.cloud.core.template.TemplateConst; import org.hango.cloud.k8s.K8sTypes; import org.hango.cloud.meta.*; import org.hango.cloud.meta.dto.GrpcEnvoyFilterDTO; @@ -25,10 +27,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; -import java.util.Comparator; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; public class GatewayConfigManagerImpl extends AbstractConfigManagerSupport implements @@ -135,6 +134,22 @@ public HasMetadata getConfig(String kind, String name) { return configStore.get(kind, null, name); } + @Override + public HasMetadata getConfig(String kind, String namespace, String name) { + return configStore.get(kind, namespace, name); + } + + @Override + public List getConfigListWithRev(String kind) { + List hasMetadata = configStore.get(kind); + return hasMetadata.stream().filter(this::revFilter).collect(Collectors.toList()); + } + + @Override + public List getConfigList(String kind) { + return configStore.get(kind); + } + @Override public void updateConfig(PluginOrder pluginOrder) { List resources = modelEngine.translate(pluginOrder); @@ -160,6 +175,7 @@ public void deleteConfig(IstioGateway istioGateway) { update(resources); } + @Override public void updateConfig(EnvoyFilterOrder envoyFilterOrder) { List resources = modelEngine.translate(envoyFilterOrder); @@ -188,6 +204,18 @@ public void updateConfig(Secret secret) { update(resources); } + @Override + public void updateK8sService(io.fabric8.kubernetes.api.model.Service service) { + K8sResourcePack resource = new K8sResourcePack(service); + update(Collections.singletonList(resource)); + } + + @Override + public void updateConfig(ConfigMap configMap) { + K8sResourcePack resource = new K8sResourcePack(configMap); + update(Collections.singletonList(resource)); + } + private void delete(List resources, Subtracter fun) { delete(resources, (i1, i2) -> 0, fun, configStore, modelEngine); } @@ -202,4 +230,9 @@ private void delete(List resources) { protected void deleteNotification(HasMetadata i) { eventPublisher.publishEvent(new K8sResourceDeleteNotificationEvent(i)); } + + private boolean revFilter(HasMetadata hasMetadata){ + Map labels = hasMetadata.getMetadata().getLabels(); + return !CollectionUtils.isEmpty(labels) && Objects.equals(globalConfig.getIstioRev(), labels.get(TemplateConst.LABLE_ISTIO_REV)); + } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/K8sConfigStore.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/K8sConfigStore.java index e1edd9e3..54662066 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/K8sConfigStore.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/gateway/service/impl/K8sConfigStore.java @@ -109,6 +109,11 @@ public List get(String kind, String namespace) { return client.getObjectList(kind, namespace); } + @Override + public List get(String kind) { + return client.getObjectList(kind, null); + } + @Override public List get(String kind, String namespace, Map labels) { if (StringUtils.isEmpty(namespace)) { diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sClient.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sClient.java index 2bb86bcf..14fb8c80 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sClient.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sClient.java @@ -1,8 +1,6 @@ package org.hango.cloud.core.k8s; -import io.fabric8.kubernetes.api.model.Doneable; -import io.fabric8.kubernetes.api.model.HasMetadata; -import io.fabric8.kubernetes.api.model.KubernetesResourceList; +import io.fabric8.kubernetes.api.model.*; import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; @@ -16,6 +14,7 @@ import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -46,11 +45,17 @@ public void init(){ public > List getCustomResources(CustomResource customResource, String namespace, Map labels){ NonNamespaceOperation, Resource>> operataion = getOperataion(customResource, namespace); + if (operataion == null){ + return new ArrayList<>(); + } return operataion.withLabels(labels).list().getItems(); } public > T getCustomResource(CustomResource customResource, String namespace, String name){ NonNamespaceOperation, Resource>> operataion = getOperataion(customResource, namespace); + if (operataion == null){ + return null; + } return operataion.withName(name).get(); } @@ -64,7 +69,7 @@ public > NonNamespace } - private CustomResourceDefinition getCustomResourceDefinition(String name){ + private CustomResourceDefinition getCustomResourceDefinition(String name) { try { return client.customResourceDefinitions().withName(name).get(); } catch (Exception e) { @@ -72,4 +77,22 @@ private CustomResourceDefinition getCustomResourceDefinition(String name){ return null; } } + + public List getServices(String namespaces, Map labels){ + return client.services().inNamespace(namespaces).withLabels(labels).list().getItems(); + } + + public List getPods(String namespaces, Map labels){ + return client.pods().inNamespace(namespaces).withLabels(labels).list().getItems(); + } + + + public ConfigMap getConfigMap(String namespaces, String name){ + return client.configMaps().inNamespace(namespaces).withName(name).get(); + } + + public List getNode(){ + return client.nodes().list().getItems(); + } + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sResourceEnum.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sResourceEnum.java index 5fd9b6e0..05aa0c3d 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sResourceEnum.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sResourceEnum.java @@ -2,12 +2,13 @@ import com.google.common.collect.ImmutableMap; import io.fabric8.kubernetes.api.model.*; -import io.fabric8.kubernetes.api.model.apps.*; +import io.fabric8.kubernetes.api.model.apps.Deployment; +import io.fabric8.kubernetes.api.model.apps.DeploymentList; +import io.fabric8.kubernetes.api.model.networking.v1.IngressList; +import io.fabric8.kubernetes.api.model.apps.Deployment; +import io.fabric8.kubernetes.api.model.apps.DeploymentList; import io.fabric8.kubernetes.client.utils.URLUtils; -import me.snowdrop.istio.api.authentication.v1alpha1.Policy; -import me.snowdrop.istio.api.authentication.v1alpha1.PolicyList; import me.snowdrop.istio.api.networking.v1alpha3.*; -import me.snowdrop.istio.api.rbac.v1alpha1.*; import org.apache.commons.lang3.StringUtils; import org.hango.cloud.k8s.K8sTypes; import org.hango.cloud.util.exception.ApiPlaneException; @@ -27,172 +28,111 @@ public enum K8sResourceEnum { VirtualService( K8sTypes.VirtualService.class, K8sTypes.VirtualServiceList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/virtualservices")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/virtualservices")), /** DestinationRule resource */ DestinationRule( K8sTypes.DestinationRule.class, K8sTypes.DestinationRuleList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/destinationrules")), - /** DestinationRule resource */ - ServiceRole( - ServiceRole.class, - ServiceRoleList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/rbac.istio.io/v1alpha1/namespaces/%s/serviceroles")), - /** DestinationRule resource */ - ServiceRoleBinding( - ServiceRoleBinding.class, - ServiceRoleBindingList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/rbac.istio.io/v1alpha1/namespaces/%s/servicerolebindings")), - /** DestinationRule resource */ - Policy( - Policy.class, - PolicyList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/authentication.istio.io/v1alpha1/namespaces/%s/policies")), - /** DestinationRule resource */ - ServiceAccount( - ServiceAccount.class, - ServiceAccountList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/v1/namespaces/%s/serviceaccounts")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/destinationrules")), /** DestinationRule resource */ Gateway( Gateway.class, GatewayList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/gateways")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/gateways")), /** DestinationRule resource */ Pod( Pod.class, PodList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/v1/namespaces/%s/pods")), - /** DestinationRule resource */ - ClusterRbacConfig( - RbacConfig.class, - RbacConfigList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/rbac.istio.io/v1alpha1/clusterrbacconfigs")), - /** DestinationRule resource */ - RbacConfig( - RbacConfig.class, - RbacConfigList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/rbac.istio.io/v1alpha1/clusterrbacconfigs")), + ImmutableMap.of(K8sVersion.V1_17_0, "/api/v1/namespaces/%s/pods")), + /** DestinationRule resource */ SharedConfig( SharedConfig.class, SharedConfigList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/sharedconfigs")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/sharedconfigs")), /** DestinationRule resource */ ServiceEntry( ServiceEntry.class, ServiceEntryList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/serviceentries")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/serviceentries")), /** DestinationRule resource */ PluginManager( K8sTypes.PluginManager.class, K8sTypes.PluginManagerList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/microservice.slime.io/v1alpha1/namespaces/%s/pluginmanagers")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/microservice.slime.io/v1alpha1/namespaces/%s/pluginmanagers")), /** DestinationRule resource */ EnvoyFilter( K8sTypes.EnvoyFilter.class, K8sTypes.EnvoyFilterList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/envoyfilters")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/envoyfilters")), /** DestinationRule resource */ Deployment( Deployment.class, DeploymentList.class, - ImmutableMap.of( - K8sVersion.V1_11_0, "/apis/extensions/v1beta1/namespaces/%s/deployments", - K8sVersion.V1_17_0, "/apis/apps/v1/namespaces/%s/deployments" - )), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/apps/v1/namespaces/%s/deployments")), /** DestinationRule resource */ Endpoints( Endpoints.class, EndpointsList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/v1/namespaces/%s/endpoints/")), - /** DestinationRule resource */ - DaemonSet( - DaemonSet.class, - DaemonSetList.class, - ImmutableMap.of( - K8sVersion.V1_11_0, "/apis/extensions/v1beta1/namespaces/%s/daemonsets", - K8sVersion.V1_17_0, "/apis/apps/v1/namespaces/%s/daemonsets" - )), + ImmutableMap.of(K8sVersion.V1_17_0, "/api/v1/namespaces/%s/endpoints/")), + /** DestinationRule resource */ Service( Service.class, ServiceList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/v1/namespaces/%s/services/")), - /** DestinationRule resource */ - StatefulSet( - StatefulSet.class, - StatefulSetList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/apps/v1/namespaces/%s/statefulsets/")), - /** DestinationRule resource */ - ReplicaSet( - ReplicaSet.class, - ReplicaSetList.class, - ImmutableMap.of( - K8sVersion.V1_11_0, "/apis/extensions/v1beta1/namespaces/%s/replicasets/", - K8sVersion.V1_17_0, "/apis/apps/v1/namespaces/%s/replicasets/" - )), + ImmutableMap.of(K8sVersion.V1_17_0, "/api/v1/namespaces/%s/services/")), + /** DestinationRule resource */ VersionManager( VersionManager.class, VersionManagerList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/versionmanagers")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/versionmanagers")), /** DestinationRule resource */ GlobalConfig( GlobalConfig.class, GlobalConfigList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/globalconfigs") + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.istio.io/v1alpha3/globalconfigs") ), /** DestinationRule resource */ NameSpace( Namespace.class, NamespaceList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/v1/namespaces/%s")), + ImmutableMap.of(K8sVersion.V1_17_0, "/api/v1/namespaces/%s")), /** DestinationRule resource */ EnvoyPlugin( K8sTypes.EnvoyPlugin.class, K8sTypes.EnvoyPluginList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/microservice.slime.io/v1alpha1/namespaces/%s/envoyplugins")), - /** DestinationRule resource */ - MixerUrlPattern( - MixerUrlPattern.class, - MixerUrlPatternList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/mixerurlpatterns")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/microservice.slime.io/v1alpha1/namespaces/%s/envoyplugins")), /** DestinationRule resource */ ConfigMap( ConfigMap.class, ConfigMapList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/v1/namespaces/%s/configmaps")), + ImmutableMap.of(K8sVersion.V1_17_0, "/api/v1/namespaces/%s/configmaps")), /** DestinationRule resource */ SmartLimiter( K8sTypes.SmartLimiter.class, K8sTypes.SmartLimiterList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/microservice.slime.io/v1alpha2/namespaces/%s/smartlimiters")), - /** DestinationRule resource */ - Sidecar( - me.snowdrop.istio.api.networking.v1alpha3.Sidecar.class, - SidecarList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/apis/networking.istio.io/v1alpha3/namespaces/%s/sidecars")), + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/microservice.slime.io/v1alpha2/namespaces/%s/smartlimiters")), Secret( Secret.class, SecretList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/v1/namespaces/%s/secrets")), + ImmutableMap.of(K8sVersion.V1_17_0, "/api/v1/namespaces/%s/secrets")), /** Ingress resource */ Ingress( - io.fabric8.kubernetes.api.model.extensions.Ingress.class, - io.fabric8.kubernetes.api.model.extensions.IngressList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/extensions/v1beta1/namespaces/%s/ingresses")), + io.fabric8.kubernetes.api.model.networking.v1.Ingress.class, + IngressList.class, + ImmutableMap.of(K8sVersion.V1_17_0, "/apis/networking.k8s.io/v1/namespaces/%s/ingresses")), //####### Gateway API resource ########### KubernetesGateway( io.fabric8.kubernetes.api.model.gatewayapi.v1beta1.Gateway.class, io.fabric8.kubernetes.api.model.gatewayapi.v1beta1.GatewayList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/gateway.networking.k8s.io/v1beta1/namespaces/%s/gateways")), + ImmutableMap.of(K8sVersion.V1_17_0, "/api/gateway.networking.k8s.io/v1beta1/namespaces/%s/gateways")), HTTPRoute( io.fabric8.kubernetes.api.model.gatewayapi.v1beta1.HTTPRoute.class, io.fabric8.kubernetes.api.model.gatewayapi.v1beta1.HTTPRouteList.class, - ImmutableMap.of(K8sVersion.V1_11_0, "/api/gateway.networking.k8s.io/v1beta1/namespaces/%s/httproutes")) + ImmutableMap.of(K8sVersion.V1_17_0, "/api/gateway.networking.k8s.io/v1beta1/namespaces/%s/httproutes")) ; @@ -208,7 +148,7 @@ public enum K8sResourceEnum { if (StringUtils.isNotEmpty(currentK8sVersion)) { currentVersion = new K8sVersion(currentK8sVersion); } else { - currentVersion = K8sVersion.V1_11_0; + currentVersion = K8sVersion.V1_17_0; } K8sVersion closedVersion = select(selfLinkMap.keySet(), currentVersion); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sResourcePack.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sResourcePack.java index ff9963aa..12a9aded 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sResourcePack.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sResourcePack.java @@ -1,8 +1,7 @@ package org.hango.cloud.core.k8s; -import org.hango.cloud.util.function.Merger; -import org.hango.cloud.util.function.Subtracter; import io.fabric8.kubernetes.api.model.HasMetadata; +import org.hango.cloud.util.function.Subtracter; /** * k8s资源 + 操作工具 @@ -11,7 +10,6 @@ public class K8sResourcePack { private HasMetadata resource; - private Merger merger; private Subtracter subtracter; @@ -19,16 +17,11 @@ public K8sResourcePack(HasMetadata resource) { this.resource = resource; } - public K8sResourcePack(HasMetadata resource, Merger merger, Subtracter subtracter) { + public K8sResourcePack(HasMetadata resource, Subtracter subtracter) { this.resource = resource; - this.merger = merger; this.subtracter = subtracter; } - public boolean hasMerger() { - return this.merger != null; - } - public boolean hasSubtracter() { return this.subtracter != null; } @@ -41,13 +34,6 @@ public void setResource(HasMetadata resource) { this.resource = resource; } - public Merger getMerger() { - return merger; - } - - public void setMerger(Merger merger) { - this.merger = merger; - } public Subtracter getSubtracter() { return subtracter; diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sVersion.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sVersion.java index 918caf70..752865cc 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sVersion.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/K8sVersion.java @@ -13,7 +13,6 @@ public class K8sVersion implements Comparable { // 例如v1.1.0 private static final Pattern FORMATTER = Pattern.compile("^v(\\d{0,3})\\.(\\d{0,3})\\.(\\d{0,3})$"); - public static final K8sVersion V1_11_0 = new K8sVersion("v1.11.0"); public static final K8sVersion V1_17_0 = new K8sVersion("v1.17.0"); private final String version; diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/KubernetesClient.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/KubernetesClient.java index 920310d9..0c1e3f2f 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/KubernetesClient.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/KubernetesClient.java @@ -1,11 +1,10 @@ package org.hango.cloud.core.k8s; +import io.fabric8.kubernetes.client.Config; +import okhttp3.OkHttpClient; import org.hango.cloud.core.editor.EditorContext; import org.hango.cloud.core.editor.ResourceType; import org.hango.cloud.core.k8s.http.DefaultK8sHttpClient; -import io.fabric8.kubernetes.api.model.KubernetesResourceList; -import io.fabric8.kubernetes.client.Config; -import okhttp3.OkHttpClient; import org.springframework.util.StringUtils; import java.util.List; @@ -58,18 +57,6 @@ public List getObjectList(String kind, String namespace) { return getObjectList(url); } - /** - * @param kind 资源类型 - * @param namespace 命名空间 - * @param name 资源名 - * @param labels 标签 - * @param 返回资源类型 - * @return 资源对象 - */ - public T getObject(String kind, String namespace, String name, Map labels) { - String url = getUrlWithLabels(kind, namespace, name, labels); - return getObject(url); - } /** * @param kind 资源类型 @@ -115,15 +102,6 @@ public List getObjectList(String url) { return gen.object(resourceEnum.mappingListType()).getItems(); } - public KubernetesResourceList getListObject(String kind, String namespace) { - String url = getUrl(kind, namespace); - String obj = getWithNull(url); - if (StringUtils.isEmpty(obj)) return null; - - K8sResourceGenerator gen = K8sResourceGenerator.newInstance(obj, ResourceType.JSON); - K8sResourceEnum resourceEnum = K8sResourceEnum.getItem(gen.getKind()); - return gen.object(resourceEnum.mappingListType()); - } /** * 创建或更新资源 diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/DynamicSmartLimiterSupplier.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/DynamicSmartLimiterSupplier.java deleted file mode 100644 index fc76b607..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/DynamicSmartLimiterSupplier.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.hango.cloud.core.k8s.empty; - -import org.hango.cloud.k8s.K8sTypes; - -import java.util.function.Supplier; - -public class DynamicSmartLimiterSupplier implements Supplier { - - private String gw; - private String name; - private String format; - - public DynamicSmartLimiterSupplier(String gw, String name, String format) { - this.gw = gw; - this.name = name; - this.format = format; - } - - @Override - public K8sTypes.SmartLimiter get() { - String realName = String.format(format, name, gw); - return new EmptySmartLimiter(realName); - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptyConfigMap.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptyConfigMap.java deleted file mode 100644 index 69901dfd..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptyConfigMap.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.hango.cloud.core.k8s.empty; - -import io.fabric8.kubernetes.api.model.ConfigMap; -import io.fabric8.kubernetes.api.model.HasMetadata; -import io.fabric8.kubernetes.api.model.ObjectMeta; - -public class EmptyConfigMap extends ConfigMap implements HasMetadata, EmptyResource { - - private ObjectMeta om; - - public EmptyConfigMap(String name) { - ObjectMeta tom = new ObjectMeta(); - tom.setName(name); - this.om = tom; - } - - public EmptyConfigMap(String name, String namespace) { - ObjectMeta om = new ObjectMeta(); - om.setName(name); - om.setNamespace(namespace); - this.om = om; - } - - @Override - public ObjectMeta getMetadata() { - return om; - } - - - - -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptyGatewayPlugin.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptyGatewayPlugin.java index fb183983..1a449e24 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptyGatewayPlugin.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptyGatewayPlugin.java @@ -8,13 +8,6 @@ public class EmptyGatewayPlugin extends K8sTypes.EnvoyPlugin implements HasMetad private ObjectMeta om; - public EmptyGatewayPlugin(String name, String namespace) { - ObjectMeta tom = new ObjectMeta(); - tom.setName(name); - tom.setNamespace(namespace); - this.om = tom; - } - public EmptyGatewayPlugin(String name) { ObjectMeta tom = new ObjectMeta(); tom.setName(name); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptySmartLimiter.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptySmartLimiter.java deleted file mode 100644 index 8d880d17..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/empty/EmptySmartLimiter.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.hango.cloud.core.k8s.empty; - -import io.fabric8.kubernetes.api.model.HasMetadata; -import io.fabric8.kubernetes.api.model.ObjectMeta; -import org.hango.cloud.k8s.K8sTypes; - -public class EmptySmartLimiter extends K8sTypes.SmartLimiter implements HasMetadata, EmptyResource { - - private ObjectMeta om; - - public EmptySmartLimiter(String name, String namespace) { - ObjectMeta tom = new ObjectMeta(); - tom.setName(name); - tom.setNamespace(namespace); - this.om = tom; - } - - public EmptySmartLimiter(String name) { - ObjectMeta tom = new ObjectMeta(); - tom.setName(name); - this.om = tom; - } - - @Override - public ObjectMeta getMetadata() { - return om; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/event/NotifyRateLimitServerEvent.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/event/NotifyRateLimitServerEvent.java deleted file mode 100644 index b6c01450..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/event/NotifyRateLimitServerEvent.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.hango.cloud.core.k8s.event; - -import org.springframework.context.ApplicationEvent; - -/** - * 该事件用于rate limit server 的configmap更新后,提醒修改rate limit server的annotation - * - **/ -public class NotifyRateLimitServerEvent extends ApplicationEvent { - - public NotifyRateLimitServerEvent(RlsInfo rlsInfo) { - super(rlsInfo); - } - -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/event/RlsInfo.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/event/RlsInfo.java deleted file mode 100644 index 69e11607..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/event/RlsInfo.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.hango.cloud.core.k8s.event; - -import java.util.Map; - -public class RlsInfo { - - Map labels; - String clusterId; - String namespace; - String key; - String val; - - public RlsInfo(Map labels, String clusterId, String namespace, String key, String val) { - this.labels = labels; - this.clusterId = clusterId; - this.namespace = namespace; - this.key = key; - this.val = val; - } - - public Map getLabels() { - return labels; - } - - public String getClusterId() { - return clusterId; - } - - public String getNamespace() { - return namespace; - } - - public String getKey() { - return key; - } - - public String getVal() { - return val; - } - - @Override - public String toString() { - return "RlsInfo{" + - "labels=" + labels + - ", clusterId='" + clusterId + '\'' + - ", namespace='" + namespace + '\'' + - ", key='" + key + '\'' + - ", val='" + val + '\'' + - '}'; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/http/DefaultK8sHttpClient.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/http/DefaultK8sHttpClient.java index 49a298b9..d3b7f184 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/http/DefaultK8sHttpClient.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/http/DefaultK8sHttpClient.java @@ -165,8 +165,7 @@ public String getUrl(String kind, String namespace) { } public String getUrl(String kind, String namespace, String name) { - K8sResourceEnum resourceEnum = K8sResourceEnum.get(kind); - return URLUtils.pathJoin(resourceEnum.selfLink(config.getMasterUrl(), namespace), name); + return URLUtils.pathJoin(getUrl(kind, namespace), name); } public String getUrlWithLabels(String url, Map labels) { @@ -185,11 +184,6 @@ public String getUrlWithLabels(String kind, String namespace, Map labels) { - String url = getUrl(kind, name, namespace); - return getUrlWithLabels(url, labels); - } - @Override public String getWithNull(String url) { Request.Builder requestBuilder = new Request.Builder().get().url(url); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/GatewayRateLimitConfigMapMerger.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/GatewayRateLimitConfigMapMerger.java deleted file mode 100644 index ee67428f..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/GatewayRateLimitConfigMapMerger.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.hango.cloud.core.k8s.merger; - -import org.hango.cloud.meta.ConfigMapRateLimit; -import org.hango.cloud.util.function.Equals; - -import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class GatewayRateLimitConfigMapMerger extends RateLimitConfigMapMerger { - - @Override - Equals getDescriptorEquals() { - - return (ot, nt) -> { - String oldVal = ot.getValue(); - String newVal = nt.getValue(); - - //eg. Service[httpbin]-User[none]-Gateway[gw]-Api[httpbin]-Id[08638e47-48db-43bc-9c21-07ef892b5494] - // 当Api[]和Gateway[]中的值分别相等时,才认为两者相当 - Pattern pattern = Pattern.compile("(Service.*)-(User.*)-(Gateway.*)-(Api.*)-(Id.*)"); - Matcher oldMatcher = pattern.matcher(oldVal); - Matcher newMatcher = pattern.matcher(newVal); - if (oldMatcher.find() && newMatcher.find()) { - return Objects.equals(oldMatcher.group(3), newMatcher.group(3)) && - Objects.equals(oldMatcher.group(4), newMatcher.group(4)); - } - return false; - }; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/RateLimitConfigMapMerger.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/RateLimitConfigMapMerger.java deleted file mode 100644 index cab74d39..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/RateLimitConfigMapMerger.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.hango.cloud.core.k8s.merger; - -import org.hango.cloud.meta.ConfigMapRateLimit; -import org.hango.cloud.util.CommonUtil; -import org.hango.cloud.util.function.Equals; -import org.hango.cloud.util.function.Merger; -import io.fabric8.kubernetes.api.model.ConfigMap; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; - -import java.util.List; -import java.util.Map; - -/** - * for ratelimit server config map - */ -public abstract class RateLimitConfigMapMerger implements Merger { - - private static final Logger logger = LoggerFactory.getLogger(RateLimitConfigMapMerger.class); - - @Override - public ConfigMap merge(ConfigMap old, ConfigMap latest) { - - if (latest == null || CollectionUtils.isEmpty(latest.getData())) return old; - if (old == null || CollectionUtils.isEmpty(old.getData())) return latest; - - Map.Entry oldConfig = old.getData().entrySet().stream().findFirst().get(); - Map.Entry latestConfig = latest.getData().entrySet().stream().findFirst().get(); - - // k8s上的非数组,本地渲染的为数组 - ConfigMapRateLimit oldCmrl = str2RateLimitConfig(oldConfig.getValue()); - ConfigMapRateLimit latestCmrl = str2RateLimitConfig(latestConfig.getValue()); - - if (oldCmrl == null) return latest; - if (latestCmrl == null) return old; - - List mergedDescriptors = CommonUtil.mergeList( - oldCmrl.getDescriptors(), latestCmrl.getDescriptors(), getDescriptorEquals()); - - //对descriptors、domain进行覆盖 - oldCmrl.setDescriptors(mergedDescriptors); - oldCmrl.setDomain(latestCmrl.getDomain()); - - String finalConfig = limitConfig2Str(oldCmrl); - if (!StringUtils.isEmpty(finalConfig)) { - oldConfig.setValue(finalConfig); - } - return old; - } - - abstract Equals getDescriptorEquals(); - - private String limitConfig2Str(ConfigMapRateLimit cmrl) { - return CommonUtil.obj2yaml(cmrl); - } - - private ConfigMapRateLimit str2RateLimitConfig(String str) { - return CommonUtil.yaml2Obj(str, ConfigMapRateLimit.class); - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/SmartLimiterMerger.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/SmartLimiterMerger.java deleted file mode 100644 index 1aff6a4e..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/merger/SmartLimiterMerger.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.hango.cloud.core.k8s.merger; - -import org.hango.cloud.util.function.Merger; -import com.netease.slime.api.microservice.v1alpha1.SmartLimiter; -import org.springframework.util.CollectionUtils; - -public class SmartLimiterMerger implements Merger { - - @Override - public SmartLimiter merge(SmartLimiter old, SmartLimiter latest) { - - if (isNull(latest)) { - old.setSpec(null); - return old; - } - if (isNull(old)) return latest; - - old.setSpec(latest.getSpec()); - return old; - } - - private boolean isNull(SmartLimiter sl) { - - return sl == null || - sl.getSpec() == null || - sl.getSpec().getRatelimitConfig() == null || - sl.getSpec().getRatelimitConfig().getRateLimitConf() == null || - CollectionUtils.isEmpty(sl.getSpec().getRatelimitConfig().getRateLimitConf().getDescriptors()); - } - -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/ConfigMapOperator.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/ConfigMapOperator.java index 48271d59..cf59ac35 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/ConfigMapOperator.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/ConfigMapOperator.java @@ -8,7 +8,7 @@ public class ConfigMapOperator implements k8sResourceOperator { @Override public ConfigMap merge(ConfigMap old, ConfigMap fresh) { - throw new UnsupportedOperationException(); + return fresh; } @Override diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/DestinationRuleOperator.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/DestinationRuleOperator.java index a42c2558..4be05051 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/DestinationRuleOperator.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/DestinationRuleOperator.java @@ -1,5 +1,6 @@ package org.hango.cloud.core.k8s.operator; +import com.google.protobuf.Struct; import io.fabric8.kubernetes.api.model.ObjectMeta; import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.k8s.K8sTypes; @@ -10,6 +11,7 @@ import org.springframework.util.StringUtils; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; @@ -33,6 +35,8 @@ public K8sTypes.DestinationRule merge(K8sTypes.DestinationRule old, K8sTypes.Des if (!StringUtils.isEmpty(freshSpec.getHost())){ builder.setHost(freshSpec.getHost()); } + builder.clearMetadata(); + builder.putAllMetadata(fresh.getSpec().getMetadataMap()); List subList = mergeList(oldSpec.getSubsetsList(), freshSpec.getSubsetsList(), new SubsetEquals()); if (!CollectionUtils.isEmpty(subList)){ diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/GlobalConfigOperator.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/GlobalConfigOperator.java deleted file mode 100644 index 4483e338..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/GlobalConfigOperator.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.hango.cloud.core.k8s.operator; - -import org.hango.cloud.configuration.ext.MeshConfig; -import org.hango.cloud.core.k8s.K8sResourceEnum; -import me.snowdrop.istio.api.networking.v1alpha3.GlobalConfig; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; - -@Component -public class GlobalConfigOperator implements k8sResourceOperator { - @Autowired - private MeshConfig meshConfig; - - @Override - public GlobalConfig merge(GlobalConfig old, GlobalConfig fresh) { - //do nothing - return fresh; - } - - @Override - public GlobalConfig subtract(GlobalConfig old, String value) { - //do nothing - return old; - } - - @Override - public boolean adapt(String name) { - return K8sResourceEnum.GlobalConfig.name().equals(name); - } - - @Override - public boolean isUseless(GlobalConfig globalConfig) { - return globalConfig == null || - StringUtils.isEmpty(globalConfig.getApiVersion()) || - globalConfig.getSpec() == null || - globalConfig.getSpec().getBody() == null; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/KubernetesServiceOperator.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/KubernetesServiceOperator.java new file mode 100644 index 00000000..a862f286 --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/KubernetesServiceOperator.java @@ -0,0 +1,35 @@ +package org.hango.cloud.core.k8s.operator; + +import io.fabric8.kubernetes.api.model.Service; +import org.apache.commons.collections.CollectionUtils; +import org.hango.cloud.core.k8s.K8sResourceEnum; +import org.springframework.stereotype.Component; + +/** + * @Author zhufengwei + * @Date 2023/3/13 + */ +@Component +public class KubernetesServiceOperator implements k8sResourceOperator { + @Override + public Service merge(Service old, Service fresh) { + old.getSpec().setPorts(fresh.getSpec().getPorts()); + return old; + } + + @Override + public Service subtract(Service old, String value) { + //do nothing + return old; + } + + @Override + public boolean adapt(String name) { + return K8sResourceEnum.Service.name().equals(name); + } + + @Override + public boolean isUseless(Service service) { + return service == null || service.getSpec() == null || CollectionUtils.isEmpty(service.getSpec().getPorts()); + } +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/SidecarOperator.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/SidecarOperator.java deleted file mode 100644 index 3f4cfcdf..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/SidecarOperator.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.hango.cloud.core.k8s.operator; - -import org.hango.cloud.core.k8s.K8sResourceEnum; -import me.snowdrop.istio.api.networking.v1alpha3.Sidecar; -import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; - -import java.util.List; -import java.util.stream.Collectors; - -@Component -public class SidecarOperator implements k8sResourceOperator { - - /** - * 增量增加hosts - * @param old - * @param fresh - * @return - */ - @Override - public Sidecar merge(Sidecar old, Sidecar fresh) { - - if (isUseless(fresh)) return old; - if (isUseless(old)) return fresh; - - List freshHosts = fresh.getSpec().getEgress().get(0).getHosts(); - List oldHosts = old.getSpec().getEgress().get(0).getHosts(); - - oldHosts.addAll(freshHosts); - List finalHosts = oldHosts.stream().distinct().collect(Collectors.toList()); - old.getSpec().getEgress().get(0).setHosts(finalHosts); - return old; - } - - - @Override - public Sidecar subtract(Sidecar old, String value) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean adapt(String name) { - return K8sResourceEnum.Sidecar.name().equals(name); - } - - @Override - public boolean isUseless(Sidecar sidecar) { - return sidecar == null || - sidecar.getSpec() == null || - CollectionUtils.isEmpty(sidecar.getSpec().getEgress()) || - sidecar.getSpec().getEgress().get(0) == null || - CollectionUtils.isEmpty(sidecar.getSpec().getEgress().get(0).getHosts()); - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/VersionManagerOperator.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/VersionManagerOperator.java deleted file mode 100644 index 4b8db1b6..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/operator/VersionManagerOperator.java +++ /dev/null @@ -1,178 +0,0 @@ -package org.hango.cloud.core.k8s.operator; - -import org.hango.cloud.configuration.ext.MeshConfig; -import org.hango.cloud.core.k8s.K8sResourceEnum; -import org.hango.cloud.meta.IptablesConfig; -import org.hango.cloud.meta.PodStatus; -import org.hango.cloud.meta.PodVersion; -import org.hango.cloud.util.function.Equals; -import me.snowdrop.istio.api.networking.v1alpha3.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; - - -@Component -public class VersionManagerOperator implements k8sResourceOperator { - - @Autowired - private MeshConfig meshConfig; - - @Override - public VersionManager merge(VersionManager old, VersionManager fresh) { - VersionManager versionManager = new VersionManagerBuilder(old).build(); - List oldSpecList = old.getSpec().getSidecarVersionSpec(); - List latestSpecList = fresh.getSpec().getSidecarVersionSpec(); - Map oldLabel = old.getMetadata().getLabels(); - Map freshLabel = fresh.getMetadata().getLabels(); - Map mergedLabel = mergeMap(oldLabel, freshLabel, (o, n) -> o.equals(n)); - versionManager.getSpec().setSidecarVersionSpec(mergeList(oldSpecList, latestSpecList, new SidecarVersionSpecEquals())); - versionManager.getSpec().setStatus(old.getSpec().getStatus()); - //versionManager.getMetadata().setResourceVersion(old.getMetadata().getResourceVersion()); - versionManager.getMetadata().setResourceVersion(null); - versionManager.getMetadata().setLabels(mergedLabel); - return versionManager; - } - - private class SidecarVersionSpecEquals implements Equals { - @Override - public boolean apply(SidecarVersionSpec np, SidecarVersionSpec op) { - boolean isEqual = Objects.equals(op.getSelector(), np.getSelector()); - if (isEqual) { - mergeToNewSidecarVersionSpec(np, op); - } - return isEqual; - } - - private void mergeToNewSidecarVersionSpec(SidecarVersionSpec np, SidecarVersionSpec op) { - if (StringUtils.isEmpty(np.getIptablesParams()) && StringUtils.isEmpty(np.getIptablesDetail())) { - np.setIptablesParams(op.getIptablesParams()); - np.setIptablesDetail(op.getIptablesDetail()); - } - if (np.getExpectedVersion() == null) { - np.setExpectedVersion(op.getExpectedVersion()); - } - } - } - - @Override - public boolean adapt(String name) { - return K8sResourceEnum.VersionManager.name().equals(name); - } - - @Override - public VersionManager subtract(VersionManager old, String value) { - // TODO - return old; - } - - - @Override - public boolean isUseless(VersionManager versionManager) { - return versionManager.getSpec() == null || - StringUtils.isEmpty(versionManager.getApiVersion()) || - CollectionUtils.isEmpty(versionManager.getSpec().getSidecarVersionSpec()); - } - - public List getPodVersion(PodVersion podVersion, VersionManager versionmanager) { - - List resultList = new ArrayList<>(); - Status state = versionmanager.getSpec().getStatus(); - if (state == null || CollectionUtils.isEmpty(state.getPodVersionStatus())) { - return resultList; - } - - List versionList = state.getPodVersionStatus(); - if (CollectionUtils.isEmpty(versionList)) { - return resultList; - } - List podList = podVersion.getPodNames(); - - for (String need : podList) { - for (PodVersionStatus had : versionList) { - if (had.getPodName().equals(need)) { - resultList.add( - new PodStatus(had.getPodName(), - had.getCurrentVersion(), - had.getExpectedVersion(), - had.getLastUpdateTime(), - had.getStatusCode(), - had.getStatusMessage())); - break; - } - } - } - return resultList; - } - - public IptablesConfig getIptablesConfigOfApp(VersionManager vm, String appName) { - List specs = vm.getSpec().getSidecarVersionSpec(); - if (specs == null || appName == null) { - return null; - } - return specs.stream() - .filter(spec -> { - if (!(spec.getSelector() instanceof ViaLabelSelectorSelector)) { - return false; - } - ViaLabelSelectorSelector selector = (ViaLabelSelectorSelector) spec.getSelector(); - return selector.getViaLabelSelector() != null && - selector.getViaLabelSelector().getLabels() != null && - appName.equals(selector.getViaLabelSelector().getLabels().get(meshConfig.getSelectorAppKey())); - }) - .map(spec -> IptablesConfig.readFromJson(spec.getIptablesDetail())) - .findAny() - .orElse(null); - } - - public String getExpectedVersion (VersionManager versionmanager, String workLoadType, String workLoadName ) { - - List specList = versionmanager.getSpec().getSidecarVersionSpec(); - SidecarVersionSpec.Selector target; - switch (workLoadType) { - case "Deployment" : - ViaDeployment deploy = new ViaDeployment(); - deploy.setName(workLoadName); - ViaDeploymentSelector deploySelector = new ViaDeploymentSelector(); - deploySelector.setViaDeployment(deploy); - target = deploySelector; - break; - case "StatefulSet" : - ViaStatefulSet statefulSet = new ViaStatefulSet(); - statefulSet.setName(workLoadName); - ViaStatefulSetSelector statefulSetSelector = new ViaStatefulSetSelector(); - statefulSetSelector.setViaStatefulSet(statefulSet); - target = statefulSetSelector; - break; - case "Service" : - ViaService service = new ViaService(); - service.setName(workLoadName); - ViaServiceSelector serviceSelector = new ViaServiceSelector(); - serviceSelector.setViaService(service); - target = serviceSelector; - break; - case "LabelSelector" : - System.out.println("查询类型暂时不支持"); - return null; - default : - System.out.println("查询类型不存在"); - return null; - - } - - for (SidecarVersionSpec spec : specList) { - SidecarVersionSpec.Selector selector = spec.getSelector(); - if (selector.equals(target)) { - return spec.getExpectedVersion(); - } - } - - return null; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/subtracter/GatewayRateLimitConfigMapSubtracter.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/subtracter/GatewayRateLimitConfigMapSubtracter.java deleted file mode 100644 index 1551bbe2..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/subtracter/GatewayRateLimitConfigMapSubtracter.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.hango.cloud.core.k8s.subtracter; - -import org.hango.cloud.core.editor.PathExpressionEnum; - -public class GatewayRateLimitConfigMapSubtracter extends RateLimitConfigMapSubtracter { - - private String gateway; - private String api; - - public GatewayRateLimitConfigMapSubtracter(String gateway, String api) { - this.gateway = gateway; - this.api = api; - } - - @Override - public String getPath() { - return PathExpressionEnum.REMOVE_GATEWAY_RATELIMIT_CONFIGMAP_BY_VALUE.translate(gateway, api); - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/subtracter/RateLimitConfigMapSubtracter.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/subtracter/RateLimitConfigMapSubtracter.java deleted file mode 100644 index f2c5876f..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/k8s/subtracter/RateLimitConfigMapSubtracter.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.hango.cloud.core.k8s.subtracter; - -import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.meta.ConfigMapRateLimit; -import org.hango.cloud.util.CommonUtil; -import org.hango.cloud.util.function.Subtracter; -import io.fabric8.kubernetes.api.model.ConfigMap; - -import java.util.Map; -import java.util.Optional; - -public abstract class RateLimitConfigMapSubtracter implements Subtracter { - - @Override - public ConfigMap subtract(ConfigMap configMap) { - - if (configMap == null || configMap.getData() == null) return configMap; - - Optional> firstEntry = configMap.getData().entrySet().stream().findFirst(); - if (!firstEntry.isPresent()) return configMap; - ConfigMapRateLimit rateLimitConfig = CommonUtil.yaml2Obj(firstEntry.get().getValue(), ConfigMapRateLimit.class); - ResourceGenerator gen = ResourceGenerator.newInstance(rateLimitConfig, ResourceType.OBJECT); - gen.removeElement(getPath()); - firstEntry.get().setValue(gen.yamlString()); - return configMap; - } - - public abstract String getPath(); -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentHolder.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentHolder.java index 87b89774..4fa8bf71 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentHolder.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentHolder.java @@ -4,33 +4,12 @@ import java.util.List; public class FragmentHolder { - private FragmentWrapper virtualServiceFragment; - - // ratelimit插件的configmap - private FragmentWrapper sharedConfigFragment; private FragmentWrapper gatewayPluginsFragment; - // ratelimit插件的smartLimiter private List smartLimiterFragment = new ArrayList<>(); - public FragmentWrapper getVirtualServiceFragment() { - return virtualServiceFragment; - } - - public void setVirtualServiceFragment(FragmentWrapper virtualServiceFragment) { - this.virtualServiceFragment = virtualServiceFragment; - } - - public FragmentWrapper getSharedConfigFragment() { - return sharedConfigFragment; - } - - public void setSharedConfigFragment(FragmentWrapper sharedConfigFragment) { - this.sharedConfigFragment = sharedConfigFragment; - } - public FragmentWrapper getGatewayPluginsFragment() { return gatewayPluginsFragment; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentTypeEnum.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentTypeEnum.java index 89f31775..d8cd7563 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentTypeEnum.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentTypeEnum.java @@ -1,11 +1,7 @@ package org.hango.cloud.core.plugin; public enum FragmentTypeEnum { - VS_HOST, - - VS_API, - - VS_MATCH, + ENVOY_PLUGIN, SMART_LIMIT, diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentWrapper.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentWrapper.java index bf87bfa3..28233b4c 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentWrapper.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/FragmentWrapper.java @@ -1,20 +1,8 @@ package org.hango.cloud.core.plugin; -import org.hango.cloud.core.k8s.K8sResourceEnum; - public class FragmentWrapper { - private K8sResourceEnum resourceType; private FragmentTypeEnum fragmentType; private String content; - private String xUserId; - - public K8sResourceEnum getResourceType() { - return resourceType; - } - - public void setResourceType(K8sResourceEnum resourceType) { - this.resourceType = resourceType; - } public FragmentTypeEnum getFragmentType() { return fragmentType; @@ -32,24 +20,11 @@ public void setContent(String content) { this.content = content; } - public String getXUserId() { - return xUserId; - } - - public void setXUserId(String xUserId) { - this.xUserId = xUserId; - } public static class Builder { - private K8sResourceEnum resourceType; private FragmentTypeEnum fragmentType; private String content; - private String xUserId; - public Builder withResourceType(K8sResourceEnum resourceType) { - this.resourceType = resourceType; - return this; - } public Builder withFragmentType(FragmentTypeEnum fragmentType) { this.fragmentType = fragmentType; @@ -61,17 +36,10 @@ public Builder withContent(String content) { return this; } - public Builder withXUserId(String id) { - this.xUserId = id; - return this; - } - public FragmentWrapper build() { FragmentWrapper wrapper = new FragmentWrapper(); - wrapper.setResourceType(resourceType); wrapper.setFragmentType(fragmentType); wrapper.setContent(content); - wrapper.setXUserId(xUserId); return wrapper; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AbstractSchemaProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AbstractSchemaProcessor.java index 639ccabb..cf43142f 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AbstractSchemaProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AbstractSchemaProcessor.java @@ -1,11 +1,9 @@ package org.hango.cloud.core.plugin.processor; +import org.apache.commons.lang3.StringUtils; import org.hango.cloud.core.editor.EditorContext; -import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.editor.ResourceType; import org.hango.cloud.meta.ServiceInfo; import org.hango.cloud.util.exception.ApiPlaneException; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -98,47 +96,6 @@ protected String getServiceName(ServiceInfo serviceInfo) { return serviceInfo.getServiceName(); } - protected String createMatch(ResourceGenerator rg, ServiceInfo info, String xUserId) { - ResourceGenerator match = ResourceGenerator.newInstance("[{}]", ResourceType.JSON, editorContext); - // 添加默认的字段 - match.createOrUpdateJson("$[0]", "uri", String.format("{\"regex\":\"(?:%s.*)\"}", info.getUri())); - match.createOrUpdateJson("$[0]", "method", String.format("{\"regex\":\"%s\"}", info.getMethod())); - match.createOrUpdateJson("$[0]", "headers", String.format("{\":authority\":{\"regex\":\"%s\"}}", info.getHosts())); - - if (rg.contain("$.matcher")) { - int length = rg.getValue("$.matcher.length()"); - for (int i = 0; i < length; i++) { - String sourceType = rg.getValue(String.format("$.matcher[%d].source_type", i)); - String leftValue = rg.getValue(String.format("$.matcher[%d].left_value", i)); - String op = rg.getValue(String.format("$.matcher[%d].op", i)); - String rightValue = rg.getValue(String.format("$.matcher[%d].right_value", i)); - - switch (sourceType) { - case "Args": - match.createOrUpdateJson("$[0]", "queryParams", String.format("{\"%s\":{\"regex\":\"%s\"}}", leftValue, getRegexByOp(op, rightValue))); - break; - case "Header": - match.createOrUpdateJson("$[0].headers", leftValue, String.format("{\"regex\":\"%s\"}", getRegexByOp(op, rightValue))); - break; - case "Cookie": - match.createOrUpdateJson("$[0].headers", "Cookie", String.format("{\"regex\":\".*(?:;|^)%s=%s(?:;|$).*\"}", leftValue, getRegexByOp(op, rightValue))); - break; - case "User-Agent": - match.createOrUpdateJson("$[0].headers", "User-Agent", String.format("{\"regex\":\"%s\"}", getRegexByOp(op, rightValue))); - break; - case "URI": - match.createOrUpdateJson("$[0].headers", ":path", String.format("{\"regex\":\"%s\"}", getRegexByOp(op, rightValue))); - break; - case "Host": - match.createOrUpdateJson("$[0].headers", ":authority", String.format("{\"regex\":\"%s\"}", getRegexByOp(op, rightValue))); - break; - default: - throw new ApiPlaneException("Unsupported match : " + sourceType); - } - } - } - return match.jsonString(); - } protected String getRegexByOp(String op, String value) { switch (op) { @@ -218,24 +175,5 @@ protected boolean nonNull(Object... items) { return true; } - protected String getAndDeleteXUserId(ResourceGenerator rg) { - if (rg.contain("$.x_user_id")) { - String xUserId = rg.getValue("$.x_user_id"); - rg.removeElement("$.x_user_id"); - return xUserId; - } - return null; - } - - protected Integer getPriority(ResourceGenerator rg) { - return rg.getValue("$.priority", Integer.class); - } - protected T getOrDefault(T obj, T defaultVal) { - if (Objects.isNull(obj)) { - return defaultVal; - } else { - return obj; - } - } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AggregateExtensionProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AggregateExtensionProcessor.java index d30c6c18..8d3622af 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AggregateExtensionProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AggregateExtensionProcessor.java @@ -1,25 +1,18 @@ package org.hango.cloud.core.plugin.processor; -import com.google.common.collect.Lists; import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; -import org.hango.cloud.core.plugin.FragmentTypeEnum; -import org.hango.cloud.core.plugin.FragmentWrapper; import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.meta.PluginMapping; import org.hango.cloud.meta.ServiceInfo; -import org.hango.cloud.util.CommonUtil; -import org.apache.commons.collections.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; -import java.util.*; -import java.util.stream.Collectors; +import java.util.Objects; + +import static org.hango.cloud.util.Const.LUA; @Component public class AggregateExtensionProcessor extends AbstractSchemaProcessor implements SchemaProcessor { @@ -35,26 +28,22 @@ public String getName() { public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { PluginGenerator rg = PluginGenerator.newInstance(plugin); FragmentHolder holder; - String kind = rg.getValue("$.kind", String.class); - PluginMapping mapping = PluginMapping.getBymappingName(kind); + String kind = getKind(rg); + PluginMapping mapping = PluginMapping.getBymappingName(getKind(rg)); holder = getProcessor(mapping.getProcessorName()).process(plugin,serviceInfo); if (StringUtils.hasText(mapping.getName())){ if (PluginMapping.ianus_router.getMappingName().equals(kind)) { coverToExtensionPlugin(holder, mapping.getName(), true, "ROOT"); } else { - coverToExtensionPlugin(holder, mapping.getName()); + coverToExtensionPlugin(holder, mapping.getName(), false, null); } } return holder; } - public void coverToExtensionPlugin(FragmentHolder holder, String name) { - coverToExtensionPlugin(holder, name, false, null); - } - - private void coverToExtensionPlugin(FragmentHolder holder, String name, boolean directPatch, String field) { - if (Objects.nonNull(holder.getVirtualServiceFragment())) { - PluginGenerator source = PluginGenerator.newInstance(holder.getVirtualServiceFragment().getContent(), ResourceType.YAML); + public void coverToExtensionPlugin(FragmentHolder holder, String name, boolean directPatch, String field) { + if (Objects.nonNull(holder.getGatewayPluginsFragment())) { + PluginGenerator source = PluginGenerator.newInstance(holder.getGatewayPluginsFragment().getContent(), ResourceType.YAML); PluginGenerator builder = PluginGenerator.newInstance(String.format("{\"name\":\"%s\"}", name)); builder.createOrUpdateJson("$", "inline", "{}"); @@ -65,78 +54,16 @@ private void coverToExtensionPlugin(FragmentHolder holder, String name, boolean builder.createOrUpdateJson("$.inline", "directPatch", "true"); builder.createOrUpdateJson("$.inline", "fieldPatchTo", StringUtils.isEmpty(field) ? "route" : field); } - holder.getVirtualServiceFragment().setContent(builder.yamlString()); + holder.getGatewayPluginsFragment().setContent(builder.yamlString()); logger.info("Extension plugin: [{}]", builder.yamlString()); } } - - @Override - public List process(List plugins, ServiceInfo serviceInfo) { - List holders = Lists.newArrayList(); - List luaPlugins = plugins.stream().filter(CommonUtil::isLuaPlugin).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(luaPlugins)) { - List luaHolder = getProcessor("RestyProcessor").process(luaPlugins, serviceInfo); - holders.addAll(luaHolder); - } - - List notLuaPlugins = plugins.stream().filter(item -> !CommonUtil.isLuaPlugin(item)).collect(Collectors.toList()); - - List notLuaHolder = notLuaPlugins.stream() - .map(plugin -> process(plugin, serviceInfo)) - .collect(Collectors.toList()); - holders.addAll(notLuaHolder); - - // 根据租户将插件分类 - MultiValueMap xUserMap = new LinkedMultiValueMap<>(); - // 一个租户下最多配置一个限流插件 - Map sharedConfigMap = new LinkedHashMap<>(); - Map> smartLimiterMap = new LinkedHashMap<>(); - holders.forEach(holder -> { - FragmentWrapper wrapper = holder.getVirtualServiceFragment(); - FragmentWrapper sharedConfig = holder.getSharedConfigFragment(); - List smartLimiterList = holder.getSmartLimiterFragment(); - if (wrapper == null) return; - String xUserId = wrapper.getXUserId(); - String xUser; - if (StringUtils.isEmpty(xUserId)) { - xUser = "NoneUser"; - } else { - xUser = xUserId; - } - xUserMap.add(xUser, wrapper); - if (Objects.nonNull(sharedConfig)) { - sharedConfigMap.put(xUser, wrapper); - } - if (CollectionUtils.isNotEmpty(smartLimiterList)) { - smartLimiterMap.put(xUser, smartLimiterList); - } - - }); - List ret = new ArrayList<>(); - for (Map.Entry> userMap : xUserMap.entrySet()) { - PluginGenerator builder = PluginGenerator.newInstance("{\"ext\":[]}"); - for (FragmentWrapper wrapper : userMap.getValue()) { - PluginGenerator source = PluginGenerator.newInstance(wrapper.getContent(), ResourceType.YAML); - builder.addJsonElement("$.ext", source.jsonString()); - } - String xUserId = "NoneUser".equals(userMap.getKey()) ? null : userMap.getKey(); - FragmentHolder holder = new FragmentHolder(); - FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withContent(builder.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_API) - .withXUserId(xUserId) - .build(); - holder.setVirtualServiceFragment(wrapper); - if (sharedConfigMap.containsKey(userMap.getKey())) { - holder.setSharedConfigFragment(sharedConfigMap.get(userMap.getKey())); - } - if (smartLimiterMap.containsKey(userMap.getKey())) { - holder.setSmartLimiterFragment(smartLimiterMap.get(userMap.getKey())); - } - ret.add(holder); + private String getKind(PluginGenerator rg){ + String type = rg.getValue("$.type", String.class); + if (LUA.equals(type)){ + return LUA; } - return ret; + return rg.getValue("$.kind", String.class); } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AggregateGatewayPluginProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AggregateGatewayPluginProcessor.java index 35966daa..445ab629 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AggregateGatewayPluginProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/AggregateGatewayPluginProcessor.java @@ -4,15 +4,22 @@ import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.meta.ServiceInfo; import org.hango.cloud.util.CommonUtil; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; -import java.util.*; +import java.util.List; import java.util.stream.Collectors; @Component public class AggregateGatewayPluginProcessor extends AbstractSchemaProcessor implements SchemaProcessor { + @Autowired + AggregateExtensionProcessor aggregateExtensionProcessor; + + @Autowired + LuaProcessor luaProcessor; + @Override public String getName() { return "AggregateGatewayPluginProcessor"; @@ -20,20 +27,7 @@ public String getName() { @Override public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { - FragmentHolder holder = getProcessor("AggregateExtensionProcessor").process(plugin, serviceInfo); - convertToGatewayPlugin(holder); - return holder; - } - - private void convertToGatewayPlugin(FragmentHolder holder) { - if (Objects.nonNull(holder.getVirtualServiceFragment())) { - holder.setGatewayPluginsFragment(holder.getVirtualServiceFragment()); - return; - } - if (Objects.nonNull(holder.getSharedConfigFragment())) { - holder.setGatewayPluginsFragment(holder.getSharedConfigFragment()); - return; - } + return aggregateExtensionProcessor.process(plugin, serviceInfo); } @Override @@ -42,8 +36,7 @@ public List process(List plugins, ServiceInfo serviceInf List luaPlugins = plugins.stream().filter(CommonUtil::isLuaPlugin).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(luaPlugins)) { - List luaHolder = getProcessor("RestyProcessor").process(luaPlugins, serviceInfo); - luaHolder.forEach(this::convertToGatewayPlugin); + List luaHolder = luaProcessor.process(luaPlugins, serviceInfo); ret.addAll(luaHolder); } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/BodyReWriteProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/BodyReWriteProcessor.java index c857dad0..1e357efe 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/BodyReWriteProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/BodyReWriteProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -61,12 +60,10 @@ public final FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(pluginConfig.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CacheProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CacheProcessor.java index fbff5e4f..33e80526 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CacheProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CacheProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -172,12 +171,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CircuitBreakerProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CircuitBreakerProcessor.java index 8106d628..e54ceb97 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CircuitBreakerProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CircuitBreakerProcessor.java @@ -1,15 +1,19 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; +import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.lang3.StringUtils; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.meta.ServiceInfo; -import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; +/** + * 熔断插件 + * example: + {"rt":{"consecutiveSlowRequests":"2","rtThreshold":"1"},"breakType":["RTCircuitbreaker","ErrorPercentCircuitbreaker"],"response":{"headers":[{"_formTableKey":1685340549421,"value":"ccc","key":"aaa"},{"_formTableKey":1685340549421,"value":"eee","key":"ddd"}],"code":"456","body":"aasdadasdagsDBSDFDSAD"},"kind":"circuit-breaker","errorPercent":{"minRequestAmount":"2","errorPercentThreshold":"1"},"breakDuration":"10","lookbackDuration":"6"} + */ @Component public class CircuitBreakerProcessor extends AbstractSchemaProcessor implements SchemaProcessor { @Override @@ -25,12 +29,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { buildConfig(source, builder); FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } @@ -59,29 +61,29 @@ private void buildResponse(PluginGenerator source, PluginGenerator builder) { } private void buildConfig(PluginGenerator source, PluginGenerator builder) { - if (!source.contain("$.config")) return; - if (source.contain("$.config.consecutive_slow_requests")) { - Long consecutive_slow_requests = Long.valueOf(source.getValue("$.config.consecutive_slow_requests")); + if (!source.contain("$.breakType")) return; + if (source.contain("$.rt.consecutiveSlowRequests")) { + Long consecutive_slow_requests = Long.valueOf(source.getValue("$.rt.consecutiveSlowRequests")); builder.createOrUpdateValue("$", "consecutive_slow_requests", consecutive_slow_requests); } - if (source.contain("$.config.average_response_time")) { - Double average_response_time = Double.valueOf(source.getValue("$.config.average_response_time")); + if (source.contain("$.rt.rtThreshold")) { + Double average_response_time = Double.valueOf(source.getValue("$.rt.rtThreshold")); builder.createOrUpdateValue("$", "average_response_time", average_response_time + "s"); } - if (source.contain("$.config.min_request_amount")) { - Long min_request_amount = Long.valueOf(source.getValue("$.config.min_request_amount")); + if (source.contain("$.errorPercent.minRequestAmount")) { + Long min_request_amount = Long.valueOf(source.getValue("$.errorPercent.minRequestAmount")); builder.createOrUpdateValue("$", "min_request_amount", min_request_amount); } - if (source.contain("$.config.error_percent_threshold")) { - Double error_percent_threshold = Double.valueOf(source.getValue("$.config.error_percent_threshold")); + if (source.contain("$.errorPercent.errorPercentThreshold")) { + Double error_percent_threshold = Double.valueOf(source.getValue("$.errorPercent.errorPercentThreshold")); builder.createOrUpdateJson("$", "error_percent_threshold", String.format("{\"value\":%s}", error_percent_threshold)); } - if (source.contain("$.config.break_duration")) { - Double break_duration = Double.valueOf(source.getValue("$.config.break_duration")); + if (source.contain("$.breakDuration")) { + Double break_duration = Double.valueOf(source.getValue("$.breakDuration")); builder.createOrUpdateValue("$", "break_duration", break_duration + "s"); } - if (source.contain("$.config.lookback_duration")) { - Double lookback_duration = Double.valueOf(source.getValue("$.config.lookback_duration")); + if (source.contain("$.lookbackDuration")) { + Double lookback_duration = Double.valueOf(source.getValue("$.lookbackDuration")); builder.createOrUpdateValue("$", "lookback_duration", lookback_duration + "s"); } builder.createOrUpdateValue("$", "wait_body", "true"); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CorsProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CorsProcessor.java index 1477c4b8..4cd67030 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CorsProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/CorsProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -57,12 +56,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { } FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/DefaultProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/DefaultProcessor.java index 6d1e6104..83ee1f62 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/DefaultProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/DefaultProcessor.java @@ -1,10 +1,9 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; +import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.meta.ServiceInfo; import org.springframework.stereotype.Component; @@ -22,16 +21,14 @@ public String getName() { @Override public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { - ResourceGenerator source = ResourceGenerator.newInstance(plugin); + PluginGenerator source = PluginGenerator.newInstance(plugin); if(source.contain("$.kind"))source.removeElement("$.kind"); FragmentHolder holder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) .withContent(source.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_API) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .build(); - holder.setVirtualServiceFragment(wrapper); + holder.setGatewayPluginsFragment(wrapper); return holder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/DynamicDowngradeProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/DynamicDowngradeProcessor.java index bd526700..dff9b958 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/DynamicDowngradeProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/DynamicDowngradeProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -40,12 +39,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/FlowLimitProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/FlowLimitProcessor.java index ef3c0fc6..f5cafc21 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/FlowLimitProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/FlowLimitProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -23,12 +22,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(rg)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(ret.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/IpRestrictionProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/IpRestrictionProcessor.java index 101df83a..6224fc18 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/IpRestrictionProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/IpRestrictionProcessor.java @@ -1,10 +1,9 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.plugin.PluginGenerator; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; +import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.meta.ServiceInfo; import org.springframework.stereotype.Component; @@ -34,12 +33,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { } FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(rg)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(ret.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/JsonpProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/JsonpProcessor.java index 3e1c849e..37113708 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/JsonpProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/JsonpProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -22,12 +21,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { PluginGenerator builder = PluginGenerator.newInstance(String.format("{\"callback\":\"%s\"}", callback)); FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LocalCacheProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LocalCacheProcessor.java index eb947a37..d570358a 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LocalCacheProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LocalCacheProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -176,12 +175,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LocalLimitProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LocalLimitProcessor.java index 266a2a8a..0d5e9756 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LocalLimitProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LocalLimitProcessor.java @@ -1,7 +1,6 @@ package org.hango.cloud.core.plugin.processor; import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -9,7 +8,10 @@ import org.hango.cloud.meta.ServiceInfo; import org.springframework.stereotype.Component; -import java.util.*; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; @Component public class LocalLimitProcessor extends AbstractSchemaProcessor implements SchemaProcessor { @@ -37,12 +39,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LuaProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LuaProcessor.java index 5a278d5e..3c9955ef 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LuaProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/LuaProcessor.java @@ -1,26 +1,25 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; +import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.meta.ServiceInfo; -import org.hango.cloud.util.exception.ApiPlaneException; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; @Component +@SuppressWarnings("java:S1602") public class LuaProcessor extends AbstractSchemaProcessor implements SchemaProcessor { + + @Autowired + private AggregateExtensionProcessor extensionProcessor; + @Override public String getName() { return "LuaProcessor"; @@ -28,46 +27,23 @@ public String getName() { @Override public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { - FragmentHolder fragmentHolder = new FragmentHolder(); - FragmentWrapper wrapper; - PluginGenerator rg = PluginGenerator.newInstance("{\"resty\":{\"plugins\":[]}}"); - rg.addJsonElement("$.resty.plugins", plugin); - String level = rg.getValue("$.resty.plugins[0].level"); - String xUserId = rg.getValue("$.resty.plugins[0].x_user_id"); - rg.removeElement("$.resty.plugins[0].level"); - rg.removeElement("$.resty.plugins[0].kind"); - rg.removeElement("$.resty.plugins[0].version"); - rg.removeElement("$.resty.plugins[0].x_user_id"); - switch (level) { - case "host": - wrapper = new FragmentWrapper.Builder() - .withContent(rg.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_HOST) - .withXUserId(xUserId) - .build(); - break; - case "api": - wrapper = new FragmentWrapper.Builder() - .withContent(rg.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_API) - .withXUserId(xUserId) - .build(); - break; - case "match": - wrapper = new FragmentWrapper.Builder() - .withContent(rg.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_MATCH) - .withXUserId(xUserId) - .build(); - break; - default: - throw new ApiPlaneException("Unsupported Lua plugin level:" + level); + PluginGenerator source = PluginGenerator.newInstance(plugin); + PluginGenerator builder = PluginGenerator.newInstance("{}", ResourceType.JSON); + String kind = source.getValue("$.kind", String.class); + Object config = source.getValue("$.config"); + builder.createOrUpdateValue("$", "config", config); + if ("trace".equals(kind)) { + kind = "neTraceFileLog"; } - fragmentHolder.setVirtualServiceFragment(wrapper); - return fragmentHolder; + builder.createOrUpdateValue("$", "name", kind); + + FragmentHolder holder = new FragmentHolder(); + FragmentWrapper wrapper = new FragmentWrapper.Builder() + .withContent(builder.yamlString()) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) + .build(); + holder.setGatewayPluginsFragment(wrapper); + return holder; } @Override @@ -76,56 +52,22 @@ public List process(List plugins, ServiceInfo serviceInf .map(plugin -> process(plugin, serviceInfo)) .collect(Collectors.toList()); - List ret = new ArrayList<>(); - - List hostLuas = new ArrayList<>(); - List apiLuas = new ArrayList<>(); - List matchLuas = new ArrayList<>(); - Object[][] wrapperMap = new Object[][]{ - new Object[]{hostLuas, FragmentTypeEnum.VS_HOST}, - new Object[]{apiLuas, FragmentTypeEnum.VS_API}, - new Object[]{matchLuas, FragmentTypeEnum.VS_MATCH} - }; - // 根据 host api match级别分类lua插件 - for (Object[] item : wrapperMap) { - item[0] = holders.stream() - .filter(holder -> holder.getVirtualServiceFragment().getFragmentType().equals(item[1])) - .map(FragmentHolder::getVirtualServiceFragment) - .collect(Collectors.toList()); - - List luas = (List) item[0]; - if (CollectionUtils.isEmpty(luas)) continue; - // 根据租户分类lua插件 - MultiValueMap userLuaMap = new LinkedMultiValueMap<>(); - luas.forEach(lua -> { - String xUserId = lua.getXUserId(); - if (StringUtils.isEmpty(xUserId)) { - userLuaMap.add("NoneUser", lua); - } else { - userLuaMap.add(xUserId, lua); - } - }); - - for (Map.Entry> luaMap : userLuaMap.entrySet()) { - PluginGenerator rg = PluginGenerator.newInstance("{\"resty\":{\"plugins\":[]}}"); - luaMap.getValue().forEach(lua -> rg.addElement("$.resty.plugins", - PluginGenerator.newInstance(lua.getContent(), ResourceType.YAML).getValue("$.resty.plugins[0]"))); + PluginGenerator builder = PluginGenerator.newInstance("{\"plugins\":[]}"); + holders.forEach(item -> { + builder.addElement("$.plugins", + PluginGenerator.newInstance(item.getGatewayPluginsFragment().getContent(), ResourceType.YAML).getValue("$")); + }); - FragmentHolder holder = new FragmentHolder(); - FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withContent(rg.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType((FragmentTypeEnum) item[1]) - .build(); - if (!"NoneUser".equals(luaMap.getKey())) { - wrapper.setXUserId(luaMap.getKey()); - } - holder.setVirtualServiceFragment(wrapper); - ret.add(holder); - } - - } + List ret = new ArrayList<>(); + FragmentHolder holder = new FragmentHolder(); + FragmentWrapper wrapper = new FragmentWrapper.Builder() + .withContent(builder.yamlString()) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) + .build(); + holder.setGatewayPluginsFragment(wrapper); + extensionProcessor.coverToExtensionPlugin(holder, "proxy.filters.http.rider", false, null); + ret.add(holder); return ret; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/ParameterValidateProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/ParameterValidateProcessor.java index fe2658b5..c8156712 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/ParameterValidateProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/ParameterValidateProcessor.java @@ -3,7 +3,6 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -83,11 +82,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { PluginGenerator pluginGenerator = transformDataToYamlBuilder(source); FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(pluginGenerator.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RateLimitProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RateLimitProcessor.java deleted file mode 100644 index 367db7d4..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RateLimitProcessor.java +++ /dev/null @@ -1,208 +0,0 @@ -package org.hango.cloud.core.plugin.processor; - -import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; -import org.hango.cloud.core.plugin.FragmentHolder; -import org.hango.cloud.core.plugin.FragmentTypeEnum; -import org.hango.cloud.core.plugin.FragmentWrapper; -import org.hango.cloud.core.plugin.PluginGenerator; -import org.hango.cloud.meta.ServiceInfo; -import org.hango.cloud.util.exception.ApiPlaneException; -import org.apache.commons.lang3.StringUtils; -import org.springframework.stereotype.Component; - -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -@Component -public class RateLimitProcessor extends AbstractSchemaProcessor implements SchemaProcessor { - @Override - public String getName() { - return "RateLimitProcessor"; - } - - @Override - public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { - //todo: XUser - FragmentHolder holder = new FragmentHolder(); - PluginGenerator total = PluginGenerator.newInstance(plugin, ResourceType.JSON, editorContext); - String xUserId = getAndDeleteXUserId(total); - - List limits = total.getValue("$.limit_by_list"); - - PluginGenerator rateLimitGen = PluginGenerator.newInstance("{\"rate_limits\":[]}"); - PluginGenerator shareConfigGen = PluginGenerator.newInstance("{\"domain\":\"qingzhou\",\"descriptors\":[]}"); - - limits.forEach(limit -> { - PluginGenerator rg = PluginGenerator.newInstance(limit, ResourceType.OBJECT, editorContext); - // 频控计算的不同维度,例如second, minute, hour, day(month, year暂时不支持) - getUnits(rg).forEach((unit, duration) -> { - String descriptorId; - if (rg.contain("$.limit_id")) { - descriptorId = rg.getValue("$.limit_id", String.class); - } else { - // 根据每个limit的整个string + unit单位 + unit值做hash - descriptorId = "hash:" + Objects.hash(limit, unit, duration); - } - String headerDescriptor = getHeaderDescriptor(serviceInfo, xUserId, descriptorId); - rateLimitGen.addJsonElement("$.rate_limits", createRateLimits(rg, serviceInfo, headerDescriptor, null)); - shareConfigGen.addJsonElement("$.descriptors", createShareConfig(rg, serviceInfo, headerDescriptor, unit, duration)); - }); - }); - holder.setSharedConfigFragment( - new FragmentWrapper.Builder() - .withXUserId(xUserId) - .withFragmentType(FragmentTypeEnum.OTHERS) - .withResourceType(K8sResourceEnum.SharedConfig) - .withContent(shareConfigGen.yamlString()) - .build() - ); - holder.setVirtualServiceFragment( - new FragmentWrapper.Builder() - .withXUserId(xUserId) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) - .withContent(rateLimitGen.yamlString()) - .build() - ); - return holder; - } - - private String createRateLimits(PluginGenerator rg, ServiceInfo serviceInfo, String headerDescriptor, String xUserId) { - PluginGenerator vs = PluginGenerator.newInstance("{\"stage\":0,\"actions\":[]}"); - - int length = 0; - if (rg.contain("$.pre_condition")) { - length = rg.getValue("$.pre_condition.length()"); - } - // 如果condition数量为0,则使用generic_key,否则使用header_value_match - if (length == 0) { - vs.addJsonElement("$.actions", - String.format("{\"generic_key\":{\"descriptor_value\":\"%s\"}}", headerDescriptor)); - } else { - vs.addJsonElement("$.actions", - String.format("{\"header_value_match\":{\"headers\":[],\"descriptor_value\":\"%s\"}}", headerDescriptor)); - String matchHeader = getMatchHeader(rg, "", "$.identifier_extractor"); - for (int i = 0; i < length; i++) { - String operator = rg.getValue(String.format("$.pre_condition[%d].operator", i)); - String rightValue = rg.getValue(String.format("$.pre_condition[%d].right_value", i)); - boolean invertMatch = false; - if (rg.contain(String.format("$.pre_condition[%d].custom_extractor", i))) { - matchHeader = getMatchHeader(rg, matchHeader, String.format("$.pre_condition[%d].custom_extractor", i)); - } - if ("true".equalsIgnoreCase(rg.getValue(String.format("$.pre_condition[%d].invert", i), String.class))) { - invertMatch = true; - } - - String expression; - switch (operator) { - case "≈": - expression = escapeBackSlash(rightValue); - vs.addJsonElement("$.actions[0].header_value_match.headers", - String.format(safe_regex_with_invert_string_match, matchHeader, expression, invertMatch)); - break; - case "!≈": - expression = escapeBackSlash(rightValue); - vs.addJsonElement("$.actions[0].header_value_match.headers", - String.format(safe_regex_with_invert_string_match, matchHeader, expression, !invertMatch)); - break; - case "=": - expression = rightValue; - vs.addJsonElement("$.actions[0].header_value_match.headers", - String.format(exact_invert_string_match, matchHeader, expression, invertMatch)); - break; - case "!=": - expression = rightValue; - vs.addJsonElement("$.actions[0].header_value_match.headers", - String.format(exact_invert_string_match, matchHeader, expression, !invertMatch)); - break; - case "present": - vs.addJsonElement("$.actions[0].header_value_match.headers", - String.format("{\"name\":\"%s\",\"present_match\":true,\"invert_match\":%s}", matchHeader, invertMatch)); - break; - default: - throw new ApiPlaneException(String.format("Unsupported $.config.limit_by_list.pre_condition.operator: %s", operator)); - } - } - } - if (length == 0 && rg.contain("$.identifier_extractor") && !StringUtils.isEmpty(rg.getValue("$.identifier_extractor", String.class))) { - String matchHeader = getMatchHeader(rg, "", "$.identifier_extractor"); - String descriptorKey = String.format("WithoutValueHeader[%s]", matchHeader); - vs.addJsonElement("$.actions", String.format("{\"request_headers\":{\"header_name\":\"%s\",\"descriptor_key\":\"%s\"}}", matchHeader, descriptorKey)); - } - return vs.jsonString(); - } - - private String createShareConfig(PluginGenerator rg, ServiceInfo serviceInfo, String headerDescriptor, String unit, Long duration) { - PluginGenerator shareConfig; - int length = 0; - if (rg.contain("$.pre_condition")) { - length = rg.getValue("$.pre_condition.length()"); - } - if (length == 0 && rg.contain("$.identifier_extractor") && !StringUtils.isEmpty(rg.getValue("$.identifier_extractor", String.class))) { - String matchHeader = getMatchHeader(rg, "", "$.identifier_extractor"); - String descriptorKey = String.format("WithoutValueHeader[%s]", matchHeader); - shareConfig = PluginGenerator.newInstance(String.format("{\"key\":\"generic_key\",\"value\":\"%s\",\"descriptors\":[{\"key\":\"%s\",\"rate_limit\":{\"unit\":\"%s\",\"requests_per_unit\":%d}}]}", - headerDescriptor, - descriptorKey, - unit, - duration - )); - } else if (length == 0) { - shareConfig = PluginGenerator.newInstance(String.format("{\"key\":\"generic_key\",\"value\":\"%s\",\"rate_limit\":{\"unit\":\"%s\",\"requests_per_unit\":%d}}", - headerDescriptor, - unit, - duration - )); - } else { - shareConfig = PluginGenerator.newInstance(String.format("{\"key\":\"header_match\",\"value\":\"%s\",\"rate_limit\":{\"unit\":\"%s\",\"requests_per_unit\":%d}}", - headerDescriptor, - unit, - duration - )); - } - return shareConfig.jsonString(); - } - - private String getMatchHeader(PluginGenerator rg, String defaultVal, String path) { - if (!rg.contain(path)) return defaultVal; - String extractor = rg.getValue(path, String.class); - - String matchHeader; - Matcher matcher = Pattern.compile("Header\\[(.*)\\]").matcher(extractor); - if (matcher.find()) { - matchHeader = matcher.group(1); - } else { - throw new ApiPlaneException(String.format("Unsupported %s: %s", path, extractor)); - } - return matchHeader; - } - - private Map getUnits(PluginGenerator rg) { - Map ret = new LinkedHashMap<>(); - String[][] map = new String[][]{ - {"$.second", "SECOND"}, - {"$.minute", "MINUTE"}, - {"$.hour", "HOUR"}, - {"$.day", "DAY"} - }; - for (String[] obj : map) { - Long duration = rg.getValue(obj[0], Long.class); - if (rg.contain(obj[0]) && Objects.nonNull(duration)) { - ret.put(obj[1], duration); - } - } - return ret; - } - - private String getHeaderDescriptor(ServiceInfo serviceInfo, String user, String id) { - if (StringUtils.isBlank(user)) { - user = "none"; - } - return String.format("Service[%s]-User[%s]-Gateway[%s]-Api[%s]-Id[%s]", getServiceName(serviceInfo), user, getGateway(serviceInfo), getApiName(serviceInfo), id); - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RedisCacheProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RedisCacheProcessor.java index b2843885..b4c276eb 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RedisCacheProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RedisCacheProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -175,12 +174,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RestyProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RestyProcessor.java deleted file mode 100644 index 60acf2bf..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RestyProcessor.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.hango.cloud.core.plugin.processor; - -import org.hango.cloud.core.plugin.PluginGenerator; -import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; -import org.hango.cloud.core.plugin.FragmentHolder; -import org.hango.cloud.core.plugin.FragmentTypeEnum; -import org.hango.cloud.core.plugin.FragmentWrapper; -import org.hango.cloud.meta.ServiceInfo; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -@Component -public class RestyProcessor extends AbstractSchemaProcessor implements SchemaProcessor { - - @Autowired - private AggregateExtensionProcessor extensionProcessor; - - @Override - public String getName() { - return "RestyProcessor"; - } - - @Override - public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { - PluginGenerator source = PluginGenerator.newInstance(plugin); - PluginGenerator builder = PluginGenerator.newInstance("{}", ResourceType.JSON); - String kind = source.getValue("$.kind", String.class); - Object config = source.getValue("$.config"); - builder.createOrUpdateValue("$", "config", config); - if ("trace".equals(kind)) { - kind = "neTraceFileLog"; - } - builder.createOrUpdateValue("$", "name", kind); - - FragmentHolder holder = new FragmentHolder(); - FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withContent(builder.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_API) - .build(); - holder.setVirtualServiceFragment(wrapper); - return holder; - } - - @Override - public List process(List plugins, ServiceInfo serviceInfo) { - List holders = plugins.stream() - .map(plugin -> process(plugin, serviceInfo)) - .collect(Collectors.toList()); - - PluginGenerator builder = PluginGenerator.newInstance("{\"plugins\":[]}"); - holders.forEach(item -> { - builder.addElement("$.plugins", - PluginGenerator.newInstance(item.getVirtualServiceFragment().getContent(), ResourceType.YAML).getValue("$")); - }); - - - List ret = new ArrayList<>(); - FragmentHolder holder = new FragmentHolder(); - FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withContent(builder.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_API) - .build(); - holder.setVirtualServiceFragment(wrapper); - extensionProcessor.coverToExtensionPlugin(holder, "proxy.filters.http.rider"); - ret.add(holder); - return ret; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RewriteProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RewriteProcessor.java index c4df41de..aa1b3f61 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RewriteProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RewriteProcessor.java @@ -1,7 +1,6 @@ package org.hango.cloud.core.plugin.processor; import org.apache.commons.lang3.StringUtils; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -46,12 +45,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { PluginGenerator builder = PluginGenerator.newInstance(String.format(formatter, rewriteRegex, target)); FragmentHolder holder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) .withContent(builder.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_API) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .build(); - holder.setVirtualServiceFragment(wrapper); + holder.setGatewayPluginsFragment(wrapper); return holder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RouteProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RouteProcessor.java index 9e181d6e..558114a8 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RouteProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/RouteProcessor.java @@ -2,7 +2,6 @@ import org.hango.cloud.core.editor.ResourceGenerator; import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -38,12 +37,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { builder.createOrUpdateValue("$.direct_response.body", "inline_string", rg.getValue("$.action.return_target.body", String.class)); FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/SmartLimiterProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/SmartLimiterProcessor.java index 5e874e37..1dd62d7a 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/SmartLimiterProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/SmartLimiterProcessor.java @@ -1,7 +1,6 @@ package org.hango.cloud.core.plugin.processor; import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -16,9 +15,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -import static org.hango.cloud.util.constant.PluginConstant.CLUSTER_GROUP_LIMITER; -import static org.hango.cloud.util.constant.PluginConstant.CLUSTER_LIMITER; -import static org.hango.cloud.util.constant.PluginConstant.LOCAL_LIMITER; +import static org.hango.cloud.util.constant.PluginConstant.*; /** * SmartLimiter处理器(转换本地限流和集群限流插件) @@ -72,9 +69,7 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { } FragmentWrapper smartLimiterWrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) .withFragmentType(FragmentTypeEnum.SMART_LIMIT) - .withResourceType(K8sResourceEnum.SmartLimiter) .withContent(limitAction.yamlString()) .build(); smartLimiterActionList.add(smartLimiterWrapper); @@ -85,6 +80,7 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { return fragmentHolder; } + private PluginGenerator generateHeaderMatch(String limitPluginType, Map limitStrategy) { PluginGenerator headerMatch = null; List headerMatchList = (List) limitStrategy.get("headers"); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/SoapJsonTranscoderProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/SoapJsonTranscoderProcessor.java index 3730964b..30e10549 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/SoapJsonTranscoderProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/SoapJsonTranscoderProcessor.java @@ -1,7 +1,6 @@ package org.hango.cloud.core.plugin.processor; import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -21,11 +20,9 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder holder = new FragmentHolder(); PluginGenerator total = PluginGenerator.newInstance(plugin, ResourceType.JSON, editorContext); total.removeElement("$.kind"); - holder.setVirtualServiceFragment( + holder.setGatewayPluginsFragment( new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(total)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(total.yamlString()) .build() ); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/StaticDowngradeProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/StaticDowngradeProcessor.java index b04fc2e5..1d648d04 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/StaticDowngradeProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/StaticDowngradeProcessor.java @@ -1,12 +1,11 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; +import org.apache.commons.lang3.StringEscapeUtils; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.meta.ServiceInfo; -import org.apache.commons.lang3.StringEscapeUtils; import org.springframework.stereotype.Component; import java.util.List; @@ -14,11 +13,16 @@ /** * 静态降级插件 + * example: + * {"requestHost":{"match_type":"exact_match","value":"abc.com"},"requestHeaders":[{"requestHeaderKey":"aaa","_formTableKey":1685342272493,"match_type":"exact_match","requestHeaderValue":"bbb"}],"responseHeaders":[{"responseHeaderKey":"ccc","responseHeaderValue":"ddd","_formTableKey":1685342278064,"match_type":"exact_match"}],"downgradeResponseHeaders":[],"kind":"static-downgrade","requestMethod":{"requestMethodList":["GET","POST"]},"downgradeResponseBody":"qa test rsp","downgradeResponseCode":"200","requestSwitch":true,"requestPath":{"match_type":"exact_match","value":"/get"},"responseCode":{"match_type":"exact_match","value":"500"}} * **/ @Component public class StaticDowngradeProcessor extends AbstractSchemaProcessor implements SchemaProcessor { + private static String RQX_HEADERS = "$.downgrade_rqx.headers"; + private static String SAFE_REGEX_MATCH = "safe_regex_match"; + private static String DOWNGRADE_RPX_HEADERS = "$.downgrade_rpx.headers"; @Override public String getName() { return "StaticDowngradeProcessor"; @@ -28,96 +32,108 @@ public String getName() { public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { PluginGenerator source = PluginGenerator.newInstance(plugin); PluginGenerator builder = PluginGenerator.newInstance("{\"downgrade_rpx\":{\"headers\":[]},\"static_response\":{\"http_status\":0}}"); - if (source.contain("$.condition.request")) { - builder.createOrUpdateJson("$", "downgrade_rqx", "{\"headers\":[]}"); + buildRequestPluginGenerator(source,builder); + buildResponsePluginGenerator(source,builder); + FragmentHolder fragmentHolder = new FragmentHolder(); + FragmentWrapper wrapper = new FragmentWrapper.Builder() + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) + .withContent(builder.yamlString()) + .build(); + fragmentHolder.setGatewayPluginsFragment(wrapper); + return fragmentHolder; + } + private PluginGenerator buildRequestPluginGenerator(PluginGenerator source,PluginGenerator builder){ + buildRequestHeaderPluginGenerator(source,builder); + buildRequestHostPluginGenerator(source,builder); + if (source.contain("$.requestMethod")) { + List method = source.getValue("$.requestMethod.requestMethodList", List.class); + if (nonNull(method)) { + if (method.size() == 1) { + builder.addJsonElement(RQX_HEADERS, String.format(exact_string_match, ":method", method.get(0))); + } else if (method.size() > 1) { + builder.addJsonElement(RQX_HEADERS, String.format(safe_regex_string_match, ":method", String.join("|", method))); + } + } } - if (source.contain("$.condition.request.headers")) { - List> headers = source.getValue("$.condition.request.headers", List.class); + if (source.contain("$.requestPath")) { + String matchType = source.getValue("$.requestPath.match_type", String.class); + String path = source.getValue("$.requestPath.value", String.class); + if (nonNull(matchType, path)) { + if (SAFE_REGEX_MATCH.equals(matchType)) { + builder.addJsonElement(RQX_HEADERS, String.format(safe_regex_string_match, ":path", path)); + } else { + builder.addJsonElement(RQX_HEADERS, String.format(exact_string_match, ":path", path)); + } + } + } + return builder; + } + private PluginGenerator buildRequestHeaderPluginGenerator(PluginGenerator source,PluginGenerator builder){ + builder.createOrUpdateJson("$", "downgrade_rqx", "{\"headers\":[]}"); + if (source.contain("$.requestHeaders")) { + List> headers = source.getValue("$.requestHeaders", List.class); headers.forEach(item -> { String matchType = item.get("match_type"); - String headerKey = item.get("headerKey"); - String headerValue = item.get("value"); + String headerKey = item.get("requestHeaderKey"); + String headerValue = item.get("requestHeaderValue"); if (haveNull(matchType, headerKey, headerValue)) return; - if ("safe_regex_match".equals(matchType)) { - builder.addJsonElement("$.downgrade_rqx.headers", String.format(safe_regex_string_match, headerKey, headerValue)); + if (SAFE_REGEX_MATCH.equals(matchType)) { + builder.addJsonElement(RQX_HEADERS, String.format(safe_regex_string_match, headerKey, headerValue)); } else { - builder.addJsonElement("$.downgrade_rqx.headers", String.format(exact_string_match, headerKey, headerValue)); + builder.addJsonElement(RQX_HEADERS, String.format(exact_string_match, headerKey, headerValue)); } }); } - if (source.contain("$.condition.request.host")) { - String matchType = source.getValue("$.condition.request.host.match_type", String.class); - String host = source.getValue("$.condition.request.host.value", String.class); + return builder; + } + private PluginGenerator buildRequestHostPluginGenerator(PluginGenerator source,PluginGenerator builder){ + if (source.contain("$.requestHost")) { + String matchType = source.getValue("$.requestHost.match_type", String.class); + String host = source.getValue("$.requestHost.value", String.class); if (nonNull(matchType, host)) { - if ("safe_regex_match".equals(matchType)) { - builder.addJsonElement("$.downgrade_rqx.headers", String.format(safe_regex_string_match, ":authority", host)); - } else { - builder.addJsonElement("$.downgrade_rqx.headers", String.format(exact_string_match, ":authority", host)); - } - } - } - if (source.contain("$.condition.request.method")) { - List method = source.getValue("$.condition.request.method", List.class); - if (nonNull(method)) { - if (method.size() == 1) { - builder.addJsonElement("$.downgrade_rqx.headers", String.format(exact_string_match, ":method", method.get(0))); - } else if (method.size() > 1) { - builder.addJsonElement("$.downgrade_rqx.headers", String.format(safe_regex_string_match, ":method", String.join("|", method))); - } - } - } - if (source.contain("$.condition.request.path")) { - String matchType = source.getValue("$.condition.request.path.match_type", String.class); - String path = source.getValue("$.condition.request.path.value", String.class); - if (nonNull(matchType, path)) { - if ("safe_regex_match".equals(matchType)) { - builder.addJsonElement("$.downgrade_rqx.headers", String.format(safe_regex_string_match, ":path", path)); + if (SAFE_REGEX_MATCH.equals(matchType)) { + builder.addJsonElement(RQX_HEADERS, String.format(safe_regex_string_match, ":authority", host)); } else { - builder.addJsonElement("$.downgrade_rqx.headers", String.format(exact_string_match, ":path", path)); + builder.addJsonElement(RQX_HEADERS, String.format(exact_string_match, ":authority", host)); } } } - if (source.contain("$.condition.response.headers")) { - List> headers = source.getValue("$.condition.response.headers", List.class); + return builder; + } + private PluginGenerator buildResponsePluginGenerator(PluginGenerator source,PluginGenerator builder){ + if (source.contain("$.responseHeaders")) { + List> headers = source.getValue("$.responseHeaders", List.class); headers.forEach(item -> { String matchType = item.get("match_type"); - String headerKey = item.get("headerKey"); - String headerValue = item.get("value"); + String headerKey = item.get("responseHeaderKey"); + String headerValue = item.get("responseHeaderValue"); if (haveNull(matchType, headerKey, headerValue)) return; - if ("safe_regex_match".equals(matchType)) { - builder.addJsonElement("$.downgrade_rpx.headers", String.format(safe_regex_string_match, headerKey, headerValue)); + if (SAFE_REGEX_MATCH.equals(matchType)) { + builder.addJsonElement(DOWNGRADE_RPX_HEADERS, String.format(safe_regex_string_match, headerKey, headerValue)); } else { - builder.addJsonElement("$.downgrade_rpx.headers", String.format(exact_string_match, headerKey, headerValue)); + builder.addJsonElement(DOWNGRADE_RPX_HEADERS, String.format(exact_string_match, headerKey, headerValue)); } }); } - if (source.contain("$.condition.response.code")) { - String matchType = source.getValue("$.condition.response.code.match_type", String.class); - String code = source.getValue("$.condition.response.code.value", String.class); - if (nonNull(code)) { - builder.addJsonElement("$.downgrade_rpx.headers", String.format(safe_regex_string_match, ":status", code+"|")); + if (source.contain("$.responseCode")) { + String matchType = source.getValue("$.responseCode.match_type", String.class); + String code = source.getValue("$.responseCode.value", String.class); + if (nonNull(matchType,code)) { + builder.addJsonElement(DOWNGRADE_RPX_HEADERS, String.format(safe_regex_string_match, ":status", code+"|")); } } - builder.updateValue("$.static_response.http_status", source.getValue("$.response.code", Integer.class)); - if (source.contain("$.response.headers")) { + builder.updateValue("$.static_response.http_status", source.getValue("$.downgradeResponseCode", Integer.class)); + if (source.contain("$.downgradeResponseHeaders")) { builder.createOrUpdateJson("$.static_response", "headers", "[]"); - Map headers = source.getValue("$.response.headers", Map.class); - for (Map.Entry entry : headers.entrySet()) { - builder.addJsonElement("$.static_response.headers", String.format("{\"key\":\"%s\",\"value\":\"%s\"}", entry.getKey(), entry.getValue())); + List> headers = source.getValue("$.downgradeResponseHeaders", List.class); + for (Map entry : headers) { + builder.addJsonElement("$.static_response.headers", String.format("{\"key\":\"%s\",\"value\":\"%s\"}", entry.get("downgradeResponseHeaderKey"), entry.get("downgradeResponseHeaderValue"))); } } - if (source.contain("$.response.body")) { - String body = StringEscapeUtils.escapeJava(source.getValue("$.response.body", String.class)); + if (source.contain("$.downgradeResponseBody")) { + String body = StringEscapeUtils.escapeJava(source.getValue("$.downgradeResponseBody", String.class)); builder.createOrUpdateJson("$.static_response", "body", String.format("{\"inline_string\": \"%s\"}", body)); } - FragmentHolder fragmentHolder = new FragmentHolder(); - FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) - .withContent(builder.yamlString()) - .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); - return fragmentHolder; + return builder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/TraceProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/TraceProcessor.java index b4ff59d6..24b97ae7 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/TraceProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/TraceProcessor.java @@ -1,11 +1,10 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; +import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.meta.ServiceInfo; import org.springframework.stereotype.Component; @@ -25,12 +24,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder holder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) .withContent(builder.yamlString()) - .withResourceType(K8sResourceEnum.VirtualService) - .withFragmentType(FragmentTypeEnum.VS_API) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .build(); - holder.setVirtualServiceFragment(wrapper); + holder.setGatewayPluginsFragment(wrapper); return holder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/TransformProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/TransformProcessor.java index e25fcdb4..f23493d5 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/TransformProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/TransformProcessor.java @@ -1,7 +1,6 @@ package org.hango.cloud.core.plugin.processor; import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -58,12 +57,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { buildPath(source, builder); FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/WafProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/WafProcessor.java index 12cf4342..a8129f05 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/WafProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/WafProcessor.java @@ -1,6 +1,6 @@ package org.hango.cloud.core.plugin.processor; -import org.hango.cloud.core.k8s.K8sResourceEnum; +import org.apache.commons.collections.CollectionUtils; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -53,11 +53,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } @@ -159,6 +158,10 @@ private static Map extractedConfig(List> con private boolean getWafPluginSwitch(PluginGenerator source, String wafPluginKey) { List pluginSwitch = source.getValue("$.wafRule.." + wafPluginKey); - return pluginSwitch.get(0); + if (CollectionUtils.isEmpty(pluginSwitch)){ + return false; + } + Boolean result = pluginSwitch.get(0); + return result != null && result; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/AuthProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/AuthProcessor.java index 93941a78..8f359212 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/AuthProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/AuthProcessor.java @@ -1,14 +1,13 @@ package org.hango.cloud.core.plugin.processor.auth; import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; +import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.core.plugin.processor.AbstractSchemaProcessor; import org.hango.cloud.core.plugin.processor.SchemaProcessor; import org.hango.cloud.meta.ServiceInfo; -import org.springframework.stereotype.Component; import java.util.List; import java.util.Objects; @@ -25,8 +24,8 @@ public abstract class AuthProcessor extends AbstractSchemaProcessor implements S @Override public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { - ResourceGenerator source = ResourceGenerator.newInstance(plugin); - ResourceGenerator builder = ResourceGenerator.newInstance("{\"need_authorization\":false, \"missing_auth_allow\":\"false\"}"); + PluginGenerator source = PluginGenerator.newInstance(plugin); + PluginGenerator builder = PluginGenerator.newInstance("{\"need_authorization\":false, \"missing_auth_allow\":\"false\"}"); String kind = source.getValue("$.kind", String.class); switch (kind) { @@ -66,12 +65,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { } FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/BasicRbacProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/BasicRbacProcessor.java index 02dd1a42..5458c587 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/BasicRbacProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/BasicRbacProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor.auth; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -8,7 +7,6 @@ import org.hango.cloud.core.plugin.processor.AbstractSchemaProcessor; import org.hango.cloud.core.plugin.processor.SchemaProcessor; import org.hango.cloud.meta.ServiceInfo; - import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; @@ -17,6 +15,7 @@ import static org.hango.cloud.util.Const.*; + /** * 基础鉴权插件处理器 * @@ -58,12 +57,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(resultBuilder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } @@ -78,14 +75,9 @@ private String convertToEnvoyFilter(String authType) { if (StringUtils.isEmpty(authType)) { throw new RuntimeException("基础鉴权插件转换异常,authType为空!"); } - String filter; - switch (authType) { - case AUTH_JWKS: - filter = JWT_FILTER; - break; - default: - throw new RuntimeException("没有对应的filter类型,authType:" + authType); + if (AUTH_JWKS.equals(authType)){ + return JWT_FILTER; } - return filter; + throw new RuntimeException("没有对应的filter类型,authType:" + authType); } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/JwtAuthProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/JwtAuthProcessor.java index e8e9126d..e3473cf8 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/JwtAuthProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/JwtAuthProcessor.java @@ -1,6 +1,5 @@ package org.hango.cloud.core.plugin.processor.auth; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -71,12 +70,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/PreviousVersionAuthProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/PreviousVersionAuthProcessor.java index 93c6e65c..99f164f3 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/PreviousVersionAuthProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/auth/PreviousVersionAuthProcessor.java @@ -1,10 +1,10 @@ package org.hango.cloud.core.plugin.processor.auth; import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; +import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.core.plugin.processor.AbstractSchemaProcessor; import org.hango.cloud.core.plugin.processor.SchemaProcessor; import org.hango.cloud.meta.ServiceInfo; @@ -47,8 +47,8 @@ public String getName() { @Override public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { - ResourceGenerator source = ResourceGenerator.newInstance(plugin); - ResourceGenerator builder = ResourceGenerator.newInstance("{\"need_authorization\":\"false\", \"failure_auth_allow\":\"false\"}"); + PluginGenerator source = PluginGenerator.newInstance(plugin); + PluginGenerator builder = PluginGenerator.newInstance("{\"need_authorization\":\"false\", \"failure_auth_allow\":\"false\"}"); String authType = source.getValue("$.authnType", String.class); if (AKSK_AUTHN_TYPE.equals(authType)) { builder.createOrUpdateJson("$", AKSK_AUTHN_TYPE, "{}"); @@ -75,12 +75,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { } FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(source)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/HeaderRestrictionProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/HeaderRestrictionProcessor.java index b7ba32fa..1a4faa55 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/HeaderRestrictionProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/HeaderRestrictionProcessor.java @@ -4,15 +4,14 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import net.minidev.json.JSONArray; import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.core.plugin.processor.AbstractSchemaProcessor; import org.hango.cloud.meta.ServiceInfo; -import net.minidev.json.JSONArray; import org.springframework.stereotype.Component; import java.util.Objects; @@ -77,12 +76,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { } FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(rg)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(PluginGenerator.newInstance("{\"config\":{}}").createOrUpdateJson("$", "config", ret.jsonString()).yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/ResponseHeaderRewriteProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/ResponseHeaderRewriteProcessor.java index 78f1e5fb..bd372ae0 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/ResponseHeaderRewriteProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/ResponseHeaderRewriteProcessor.java @@ -3,14 +3,13 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.hango.cloud.core.k8s.K8sResourceEnum; +import net.minidev.json.JSONArray; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; import org.hango.cloud.core.plugin.PluginGenerator; import org.hango.cloud.core.plugin.processor.AbstractSchemaProcessor; import org.hango.cloud.meta.ServiceInfo; -import net.minidev.json.JSONArray; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -57,12 +56,10 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { FragmentHolder fragmentHolder = new FragmentHolder(); FragmentWrapper wrapper = new FragmentWrapper.Builder() - .withXUserId(getAndDeleteXUserId(pluginInfo)) - .withFragmentType(FragmentTypeEnum.VS_API) - .withResourceType(K8sResourceEnum.VirtualService) + .withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN) .withContent(builder.yamlString()) .build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/TrafficMarkProcessor.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/TrafficMarkProcessor.java index 629476df..cb3192ce 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/TrafficMarkProcessor.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/plugin/processor/header/TrafficMarkProcessor.java @@ -2,7 +2,6 @@ import com.google.gson.*; import net.minidev.json.JSONArray; -import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.core.plugin.FragmentHolder; import org.hango.cloud.core.plugin.FragmentTypeEnum; import org.hango.cloud.core.plugin.FragmentWrapper; @@ -64,8 +63,8 @@ public FragmentHolder process(String plugin, ServiceInfo serviceInfo) { PluginGenerator builder = transformDataToYamlBuilder(pluginInfo); FragmentHolder fragmentHolder = new FragmentHolder(); - FragmentWrapper wrapper = new FragmentWrapper.Builder().withXUserId(getAndDeleteXUserId(pluginInfo)).withFragmentType(FragmentTypeEnum.VS_API).withResourceType(K8sResourceEnum.VirtualService).withContent(builder.yamlString()).build(); - fragmentHolder.setVirtualServiceFragment(wrapper); + FragmentWrapper wrapper = new FragmentWrapper.Builder().withFragmentType(FragmentTypeEnum.ENVOY_PLUGIN).withContent(builder.yamlString()).build(); + fragmentHolder.setGatewayPluginsFragment(wrapper); return fragmentHolder; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/core/template/TemplateConst.java b/hango-api-plane-server/src/main/java/org/hango/cloud/core/template/TemplateConst.java index 972b4404..5a6be80a 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/core/template/TemplateConst.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/core/template/TemplateConst.java @@ -64,8 +64,6 @@ public interface TemplateConst { String VIRTUAL_SERVICE_RETRY_PER_TIMEOUT = "t_http_retry_perTryTimeout"; String VIRTUAL_SERVICE_RETRY_RETRY_ON = "t_http_retry_retryOn"; String VIRTUAL_SERVICE_REQUEST_HEADERS = "t_virtual_service_request_headers"; - String VIRTUAL_SERVICE_VIRTUAL_CLUSTER_NAME = "t_virtual_service_virtual_cluster_name"; - String VIRTUAL_SERVICE_VIRTUAL_CLUSTER_HEADERS = "t_virtual_service_virtual_cluster_headers"; String VIRTUAL_SERVICE_MIRROR_SERVICE = "t_virtual_service_mirror_service"; String VIRTUAL_SERVICE_MIRROR_PORT = "t_virtual_service_mirror_port"; String VIRTUAL_SERVICE_MIRROR_SUBSET = "t_virtual_service_mirror_subset"; @@ -77,6 +75,9 @@ public interface TemplateConst { String VIRTUAL_SERVICE_RESP_EXCEPTION_CODE ="t_virtual_service_resp_exception_code"; + String DESTINATION_RULE_METADATA_HUB = "t_destination_rule_metadata_hub"; + + /** * ServiceInfo @@ -90,20 +91,6 @@ public interface TemplateConst { String SERVICE_INFO_VIRTUAL_SERVICE_HOST_HEADERS = "t_service_info_virtual_service_host_headers"; String SERVICE_INFO_VIRTUAL_SERVICE_PLUGIN_MATCH_PRIORITY = "t_service_info_virtual_service_plugin_match_priority"; - /** - * MATCH级別插件 - */ - String API_MATCH_PLUGINS = "t_api_match_plugins"; - - /** - * API级别插件 - */ - String API_API_PLUGINS = "t_api_api_plugins"; - - /** - * HOST级别插件 - */ - String API_HOST_PLUGINS = "t_api_host_plugins"; /** * api请求uri @@ -199,8 +186,6 @@ public interface TemplateConst { * Gateway **/ String GATEWAY_NAME = "t_gateway_name"; - - String VIRTUAL_GATEWAY_CODE = "t_virtual_gateway_code"; String GATEWAY_NS = "t_gateway_ns"; String GATEWAY_HOSTS = "t_gateway_hosts"; String GATEWAY_HTTP_10 = "t_gateway_http_10"; @@ -248,14 +233,11 @@ public interface TemplateConst { * GatewayPlugin */ String GATEWAY_PLUGIN_NAME = "t_gateway_plugin_name"; - String GATEWAY_CLUSTER_NAME = "t_gateway_cluster_name"; String GATEWAY_PLUGIN_NAMESPACE = "t_gateway_plugin_namespace"; String GATEWAY_PLUGIN_GATEWAYS = "t_gateway_plugin_gateways"; String GATEWAY_PLUGIN_HOSTS = "t_gateway_plugin_hosts"; - String GATEWAY_PLUGIN_SERVICES = "t_gateway_plugin_services"; String GATEWAY_PLUGIN_PLUGINS = "t_gateway_plugin_plugins"; String GATEWAY_PLUGIN_ROUTE = "t_gateway_plugin_route"; - String GATEWAY_PLUGIN_USERS = "t_gateway_plugin_users"; /** diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/mcp/McpConfigStore.java b/hango-api-plane-server/src/main/java/org/hango/cloud/mcp/McpConfigStore.java index cbee6d92..711bced0 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/mcp/McpConfigStore.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/mcp/McpConfigStore.java @@ -88,6 +88,16 @@ public List get(String kind, String namespace) { return ret; } + @Override + public List get(String kind) { + if (isUnSupportedType(kind)) return new ArrayList<>(); + List ret = new ArrayList<>(); + String collection = getCollectionByKind(kind); + List rss = resourceDao.list(collection); + rss.forEach(rs -> ret.add(toHasMetadata(rs))); + return ret; + } + @Override public List get(String kind, String namespace, Map labels) { if (isUnSupportedType(kind)) return new ArrayList<>(); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/API.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/API.java index 218ce0ba..3525b8a8 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/API.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/API.java @@ -154,17 +154,6 @@ public class API extends CommonModel { */ private RequestOperation requestOperation; - - /** - * virtualCluster name - */ - private String virtualClusterName; - - /** - * virtualCluster headers - */ - private List virtualClusterHeaders; - /** * 流量镜像配置 */ @@ -511,21 +500,6 @@ public void setRequestOperation(RequestOperation requestOperation) { this.requestOperation = requestOperation; } - public String getVirtualClusterName() { - return virtualClusterName; - } - - public void setVirtualClusterName(String virtualClusterName) { - this.virtualClusterName = virtualClusterName; - } - - public List getVirtualClusterHeaders() { - return virtualClusterHeaders; - } - - public void setVirtualClusterHeaders(List virtualClusterHeaders) { - this.virtualClusterHeaders = virtualClusterHeaders; - } public Map getMetaMap() { return metaMap; diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ApiOption.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ApiOption.java deleted file mode 100644 index 6763906e..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ApiOption.java +++ /dev/null @@ -1,120 +0,0 @@ -package org.hango.cloud.meta; - -import com.fasterxml.jackson.annotation.JsonProperty; - - -public class ApiOption { - - @JsonProperty(value = "ExtractMethod") - private Boolean extractMethod; - /** - * 负载均衡 - */ - @JsonProperty(value = "LoadBalancer") - private String loadBalancer; - - /** - * 请求是否幂等 - */ - @JsonProperty(value = "Idempotent") - private Boolean Idempotent; - - /** - * 保留原始host - */ - @JsonProperty(value = "PreserveHost") - private Boolean preserveHost = true; - - /** - * 重试次数 - */ - @JsonProperty(value = "Retries") - private Integer retries = 5; - - @JsonProperty(value = "ConnectTimeout") - private Long connectTimeout; - - /** - * 上下游超时时间(发送、读取) - */ - @JsonProperty(value = "IdleTimeout") - private Long IdleTimeout; - - @JsonProperty(value = "HttpsOnly") - private Boolean httpsOnly; - - @JsonProperty(value = "HttpIfTerminated") - private Boolean httpIfTerminated; - - public Boolean getExtractMethod() { - return extractMethod; - } - - public void setExtractMethod(Boolean extractMethod) { - this.extractMethod = extractMethod; - } - - public String getLoadBalancer() { - return loadBalancer; - } - - public void setLoadBalancer(String loadBalancer) { - this.loadBalancer = loadBalancer; - } - - public Boolean getIdempotent() { - return Idempotent; - } - - public void setIdempotent(Boolean idempotent) { - Idempotent = idempotent; - } - - public Boolean getPreserveHost() { - return preserveHost; - } - - public void setPreserveHost(Boolean preserveHost) { - this.preserveHost = preserveHost; - } - - public Integer getRetries() { - return retries; - } - - public void setRetries(Integer retries) { - this.retries = retries; - } - - public Long getConnectTimeout() { - return connectTimeout; - } - - public void setConnectTimeout(Long connectTimeout) { - this.connectTimeout = connectTimeout; - } - - public Long getIdleTimeout() { - return IdleTimeout; - } - - public void setIdleTimeout(Long idleTimeout) { - IdleTimeout = idleTimeout; - } - - public Boolean getHttpsOnly() { - return httpsOnly; - } - - public void setHttpsOnly(Boolean httpsOnly) { - this.httpsOnly = httpsOnly; - } - - public Boolean getHttpIfTerminated() { - return httpIfTerminated; - } - - public void setHttpIfTerminated(Boolean httpIfTerminated) { - this.httpIfTerminated = httpIfTerminated; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/CRDMetaEnum.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/CRDMetaEnum.java index 6bf5140f..6846f087 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/CRDMetaEnum.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/CRDMetaEnum.java @@ -27,6 +27,9 @@ public enum CRDMetaEnum { }), VIRTUAL_SERVICE_DUBBO_META(K8sTypes.VirtualService.class, "DubboMeta", TemplateConst.VIRTUAL_SERVICE_DUBBO, new TypeReference() { }), + + DESTINATION_RULE_STATS_META(K8sTypes.DestinationRule.class, "StatsMeta", TemplateConst.DESTINATION_RULE_METADATA_HUB, new TypeReference>() { + }), ; diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/IptablesConfig.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/IptablesConfig.java deleted file mode 100644 index c0718189..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/IptablesConfig.java +++ /dev/null @@ -1,142 +0,0 @@ -package org.hango.cloud.meta; - -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.IOException; -import java.util.List; -import java.util.stream.Collectors; - -/** - * Created by 张武(zhangwu@corp.netease.com) at 2020/3/27 - */ -public class IptablesConfig { - - private static final ObjectMapper MAPPER = new ObjectMapper(); - private boolean enableOutbound; - private List outboundIps; - private List excludeOutboundIps; - private List outboundPorts; - - private boolean enableInbound; - private List inboundPorts; - - public IptablesConfig(){} - - public IptablesConfig(boolean enableOutbound, List outboundIps, List excludeOutboundIps, List outboundPorts, boolean enableInbound, List inboundPorts) { - this.enableOutbound = enableOutbound; - this.outboundIps = outboundIps; - this.excludeOutboundIps = excludeOutboundIps; - this.outboundPorts = outboundPorts; - this.enableInbound = enableInbound; - this.inboundPorts = inboundPorts; - } - - public boolean isEnableOutbound() { - return enableOutbound; - } - - public void setEnableOutbound(boolean enableOutbound) { - this.enableOutbound = enableOutbound; - } - - public List getOutboundIps() { - return outboundIps; - } - - public void setOutboundIps(List outboundIps) { - this.outboundIps = outboundIps; - } - - public List getOutboundPorts() { - return outboundPorts; - } - - public void setOutboundPorts(List outboundPorts) { - this.outboundPorts = outboundPorts; - } - - public boolean isEnableInbound() { - return enableInbound; - } - - public void setEnableInbound(boolean enableInbound) { - this.enableInbound = enableInbound; - } - - public List getInboundPorts() { - return inboundPorts; - } - - public void setInboundPorts(List inboundPorts) { - this.inboundPorts = inboundPorts; - } - - public List getExcludeOutboundIps() { - return excludeOutboundIps; - } - - public void setExcludeOutboundIps(List excludeOutboundIps) { - this.excludeOutboundIps = excludeOutboundIps; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("{"); - sb.append(asJson("enableOutbound")) - .append(": ") - .append(enableOutbound); - appendList(sb, "outboundIps", outboundIps); - appendList(sb, "excludeOutboundIps", excludeOutboundIps); - appendList(sb, "outboundPorts", outboundPorts); - sb.append(", ") - .append(asJson("enableInbound")) - .append(": ") - .append(enableInbound); - appendList(sb, "inboundPorts", inboundPorts); - return sb.append('}').toString(); - } - - private void appendList(StringBuilder sb, String key, List values) { - if (values != null) { - sb.append(", ") - .append(asJson(key)) - .append(": ") - .append(asJson(values)); - } - } - - private String asJson(List strings) { - return strings.stream() - .map(this::asJson) - .collect(Collectors.joining(", ", "[", "]")); - } - - private String asJson(String obj) { - return String.format("\"%s\"", obj); - } - - public static IptablesConfig readFromJson(String json) { - if (json == null) { - return null; - } - try { - IptablesConfig result = MAPPER.readValue(json, IptablesConfig.class); - result.setOutboundPorts(convertPorts(result.getOutboundPorts())); - result.setInboundPorts(convertPorts(result.getInboundPorts())); - return result; - } catch (IOException e) { - return null; - } - } - - private static List convertPorts(List ports) { - if (ports == null) { - return null; - } else { - return ports.stream() - .map(p -> p.replaceAll(":", "-")) - .collect(Collectors.toList()); - } - } - -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginConfigInfo.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginConfigInfo.java new file mode 100644 index 00000000..35cc8b36 --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginConfigInfo.java @@ -0,0 +1,97 @@ +package org.hango.cloud.meta; + +/** + * @Author zhufengwei + * @Date 2023/7/3 + */ +public class PluginConfigInfo { + + /** 插件名称 */ + private String name; + + /** 插件显示名称 */ + private String displayName; + + /** 插件配置文件的路径 */ + private String schema; + + /** 插件的描述 */ + private String description; + + /** 插件作用范围 */ + private String pluginScope; + + /** 插件使用说明 */ + private String instructionForUse; + + /** 插件分类键 */ + private String categoryKey; + + /** 插件分类名称 */ + private String categoryName; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public String getSchema() { + return schema; + } + + public void setSchema(String schema) { + this.schema = schema; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + + public String getPluginScope() { + return pluginScope; + } + + public void setPluginScope(String pluginScope) { + this.pluginScope = pluginScope; + } + + public String getInstructionForUse() { + return instructionForUse; + } + + public void setInstructionForUse(String instructionForUse) { + this.instructionForUse = instructionForUse; + } + + public String getCategoryKey() { + return categoryKey; + } + + public void setCategoryKey(String categoryKey) { + this.categoryKey = categoryKey; + } + + public String getCategoryName() { + return categoryName; + } + + public void setCategoryName(String categoryName) { + this.categoryName = categoryName; + } +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginListenType.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginListenType.java deleted file mode 100644 index 45059798..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginListenType.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.hango.cloud.meta; - -/** - * @author zhangbj - * @version 1.0 - * @Type - * @Desc - * @date 2022/11/18 - */ -@SuppressWarnings("java:S115") -public enum PluginListenType { - - Outbound("Outbound",0), - - Inbound("Inbound",1), - - Gateway("Gateway",2); - private String listenType; - - private Integer listenTypeValue; - - PluginListenType(String listenType, Integer listenTypeValue) { - this.listenType = listenType; - this.listenTypeValue = listenTypeValue; - } - - public String getListenType() { - return listenType; - } - - public Integer getListenTypeValue() { - return listenTypeValue; - } - - public static String getListenType(Integer listenTypeValue) { - for (PluginListenType value : values()) { - if (value.listenType.equals(listenTypeValue)) { - return value.listenType; - } - } - return null; - } - - -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginMapping.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginMapping.java index 167c1f0f..ce1a24c6 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginMapping.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PluginMapping.java @@ -42,14 +42,14 @@ public enum PluginMapping { soap_json_transcoder("soap-json-transcoder","proxy.filters.http.soapjsontranscoder","SoapJsonTranscoderProcessor"), ianus_router("ianus-router","envoy.filters.http.fault","RouteProcessor"), waf("waf","proxy.filters.http.waf","WafProcessor"), - trace("trace","proxy.filters.http.rider","RestyProcessor"), + trace("trace","proxy.filters.http.rider","LuaProcessor"), request_body_rewrite("request-body-rewrite","proxy.filters.http.request_body_transformation","RequestBodyReWriteProcessor"), response_body_rewrite("response-body-rewrite","proxy.filters.http.response_body_transformation","ResponseBodyReWriteProcessor"), - + parameter_validate("parameters-validate","proxy.filters.http.parameter_validate","ParameterValidateProcessor"), //默认处理 - resty("resty","proxy.filters.http.rider","RestyProcessor"), - parameter_validate("parameters-validate","proxy.filters.http.parameter_validate","ParameterValidateProcessor") + lua("lua","proxy.filters.http.rider","LuaProcessor") + ; /** @@ -91,6 +91,18 @@ public static PluginMapping getBymappingName(String mappingName){ return value; } } - return PluginMapping.resty; + return PluginMapping.lua; + } + + public static PluginMapping getPluin(String kind, String type){ + if ("lua".equals(type)) { + return PluginMapping.lua; + } + for (PluginMapping value : values()) { + if (value.getMappingName().equals(kind)) { + return value; + } + } + return null; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PodStatus.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PodStatus.java deleted file mode 100644 index 1d4aed5e..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PodStatus.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.hango.cloud.meta; - -import com.fasterxml.jackson.annotation.JsonProperty; - - -public class PodStatus { - - @JsonProperty(value = "PodName") - private String podName; - - @JsonProperty(value = "CurrentVersion") - private String currentVersion; - - @JsonProperty(value = "ExpectedVersion") - private String expectedVersion; - - @JsonProperty(value = "LastUpdateTime") - private String lastUpdateTime; - - @JsonProperty(value = "StatusCode") - private Integer statusCode; - - @JsonProperty(value = "StatusMessage") - private String statusMessage; - - public PodStatus(String podName, String currentVersion, String expectedVersion ,String lastUpdateTime, Integer statusCode, String statusMessage) { - this.podName = podName; - this.currentVersion = currentVersion; - this.expectedVersion = expectedVersion; - this.lastUpdateTime = lastUpdateTime; - this.statusCode = statusCode; - this.statusMessage = statusMessage; - } - - public PodStatus(String podName) { - this.podName = podName; - } - - public String getPodName() { - return podName; - } - - public void setPodName(String podName) { - this.podName = podName; - } - - public String getCurrentVersion() { - return currentVersion; - } - - public void setCurrentVersion(String currentVersion) { - this.currentVersion = currentVersion; - } - - public String getLastUpdateTime() { - return lastUpdateTime; - } - - public void setLastUpdateTime(String lastUpdateTime) { - this.lastUpdateTime = lastUpdateTime; - } - - public Integer getStatusCode() { - return statusCode; - } - - public void setStatusCode(Integer statusCode) { - this.statusCode = statusCode; - } - - public String getStatusMessage() { - return statusMessage; - } - - public void setStatusMessage(String statusMessage) { - this.statusMessage = statusMessage; - } - - public String getExpectedVersion() { - return expectedVersion; - } - - public void setExpectedVersion(String expectedVersion) { - this.expectedVersion = expectedVersion; - } - -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PodVersion.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PodVersion.java deleted file mode 100644 index 6a4d00f0..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/PodVersion.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.hango.cloud.meta; - -import com.fasterxml.jackson.annotation.JsonProperty; -import org.hibernate.validator.constraints.NotEmpty; - -import java.util.List; - -public class PodVersion { - - @NotEmpty(message = "clusterId") - @JsonProperty(value = "ClusterId") - private String clusterId; - - @NotEmpty(message = "namespace") - @JsonProperty(value = "Namespace") - private String namespace; - - @NotEmpty(message = "podnames") - @JsonProperty(value = "PodNames") - private List podNames; - - public String getClusterId() { - return clusterId; - } - - public void setClusterId(String clusterId) { - this.clusterId = clusterId; - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } - - public List getPodNames() { - return podNames; - } - - public void setPodNames(List podNames) { - this.podNames = podNames; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/RateLimitPlugin.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/RateLimitPlugin.java deleted file mode 100644 index 06defbf7..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/RateLimitPlugin.java +++ /dev/null @@ -1,169 +0,0 @@ -package org.hango.cloud.meta; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import java.util.ArrayList; -import java.util.List; - - -public class RateLimitPlugin { - - private String kind = "mesh-rate-limiting"; - - @JsonProperty(value = "limit_by_list") - private List rules = new ArrayList<>(); - - public static class Rule { - @JsonProperty(value = "pre_condition") - private List preConditions; - - private String type; - - private String when; - - private String then; - - private Integer day; - - private Integer hour; - - private Integer minute; - - private Integer second; - - public List getPreConditions() { - return preConditions; - } - - public void setPreConditions(List preConditions) { - this.preConditions = preConditions; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Integer getDay() { - return day; - } - - public void setDay(Integer day) { - this.day = day; - } - - public Integer getHour() { - return hour; - } - - public void setHour(Integer hour) { - this.hour = hour; - } - - public Integer getMinute() { - return minute; - } - - public void setMinute(Integer minute) { - this.minute = minute; - } - - public Integer getSecond() { - return second; - } - - public void setSecond(Integer second) { - this.second = second; - } - - public String getWhen() { - return when; - } - - public void setWhen(String when) { - this.when = when; - } - - public String getThen() { - return then; - } - - public void setThen(String then) { - this.then = then; - } - } - - - public static class PreCondition { - - /** - * 格式必须为Header[$header] - */ - @JsonProperty(value = "custom_extractor") - private String customExtractor; - - /** - * present, =, ≈, !≈, != - */ - private String operator; - - /** - * 条件反转,默认false - */ - private Boolean invert = false; - - @JsonProperty(value = "right_value") - private String rightValue; - - public String getCustomExtractor() { - return customExtractor; - } - - public void setCustomExtractor(String customExtractor) { - this.customExtractor = customExtractor; - } - - public String getOperator() { - return operator; - } - - public void setOperator(String operator) { - this.operator = operator; - } - - public Boolean getInvert() { - return invert; - } - - public void setInvert(Boolean invert) { - this.invert = invert; - } - - public String getRightValue() { - return rightValue; - } - - public void setRightValue(String rightValue) { - this.rightValue = rightValue; - } - } - - public String getKind() { - return kind; - } - - public void setKind(String kind) { - this.kind = kind; - } - - public List getRules() { - return rules; - } - - public void setRules(List rules) { - this.rules = rules; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/Service.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/Service.java index 5d4c58a8..42df3e6c 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/Service.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/Service.java @@ -1,9 +1,11 @@ package org.hango.cloud.meta; +import com.fasterxml.jackson.annotation.JsonProperty; import org.hango.cloud.meta.dto.LocalitySettingDTO; import org.hango.cloud.meta.dto.PortalServiceConnectionPoolDTO; import java.util.List; +import java.util.Map; public class Service extends CommonModel { @@ -164,6 +166,17 @@ public class Service extends CommonModel { */ private Long version; + /** + * meta数据传输集 + * Map + * mata_type meta类型 + * + * @see CRDMetaEnum + * mata_type: 服务meta数据类型 + * meta_data: 服务meta + */ + private Map> metaMap; + public String getCode() { return code; } @@ -420,6 +433,14 @@ public void setVersion(Long version) { this.version = version; } + public Map> getMetaMap() { + return metaMap; + } + + public void setMetaMap(Map> metaMap) { + this.metaMap = metaMap; + } + public static class ServiceLoadBalancer { private String simple; private Integer slowStartWindow; diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ServiceSubset.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ServiceSubset.java index d214034e..0b75b497 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ServiceSubset.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ServiceSubset.java @@ -1,11 +1,14 @@ package org.hango.cloud.meta; +import com.fasterxml.jackson.annotation.JsonProperty; import org.hango.cloud.meta.dto.PortalHealthCheckDTO; import org.hango.cloud.meta.dto.PortalOutlierDetectionDTO; import org.hango.cloud.meta.dto.PortalServiceConnectionPoolDTO; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.springframework.util.CollectionUtils; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -20,6 +23,15 @@ public class ServiceSubset { private List staticAddrs; + /** + * + * + * 服务meta label 数据map + * mata_type: 服务meta数据类型 + * meta_data: 服务meta数据值,转换为Map + */ + private Map metaLabelMap; + public String getName() { return name; } @@ -52,6 +64,14 @@ public void setStaticAddrs(List staticAddrs) { this.staticAddrs = staticAddrs; } + public Map getMetaLabelMap() { + return metaLabelMap; + } + + public void setMetaLabelMap(Map metaLabelMap) { + this.metaLabelMap = metaLabelMap; + } + public static class TrafficPolicy { /** * 负载均衡策略 diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/TrafficMarkConfigDto.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/TrafficMarkConfigDto.java deleted file mode 100644 index 691f5ea0..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/TrafficMarkConfigDto.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.hango.cloud.meta; - -import java.util.Map; -import java.util.Set; - -/** - * Created by 张武(zhangwu@corp.netease.com) at 2021/5/31 - */ -public class TrafficMarkConfigDto { - - private Set serviceNames; - private Set enabledMarks; - private Map> mappings; - - public Set getServiceNames() { - return serviceNames; - } - - public void setServiceNames(Set serviceNames) { - this.serviceNames = serviceNames; - } - - public Set getEnabledMarks() { - return enabledMarks; - } - - public void setEnabledMarks(Set enabledMarks) { - this.enabledMarks = enabledMarks; - } - - public Map> getMappings() { - return mappings; - } - - public void setMappings(Map> mappings) { - this.mappings = mappings; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ValidateResult.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ValidateResult.java deleted file mode 100644 index 10d89916..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ValidateResult.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.hango.cloud.meta; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import java.util.ArrayList; -import java.util.List; - -/** - * @auther wupenghuai@corp.netease.com - * @date 2020/3/9 - **/ -public class ValidateResult { - @JsonProperty("pass") - private boolean pass = false; - @JsonProperty("items") - private List items = new ArrayList<>(); - - public boolean isPass() { - return pass; - } - - public void setPass(boolean pass) { - this.pass = pass; - } - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ViolationItem.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ViolationItem.java deleted file mode 100644 index 789e64fa..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/ViolationItem.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.hango.cloud.meta; - -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * @auther wupenghuai@corp.netease.com - * @date 2020/3/9 - **/ -public class ViolationItem { - @JsonProperty("kind") - private String kind; - @JsonProperty("namespace") - private String namespace; - @JsonProperty("name") - private String name; - @JsonProperty("message") - private String message; - @JsonProperty("validator") - private String validator; - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public String getKind() { - return kind; - } - - public void setKind(String kind) { - this.kind = kind; - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValidator() { - return validator; - } - - public void setValidator(String validator) { - this.validator = validator; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/WhiteList.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/WhiteList.java deleted file mode 100644 index 1214d19c..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/WhiteList.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.hango.cloud.meta; - -/** - * @auther wupenghuai@corp.netease.com - * @date 2019/7/25 - **/ -public class WhiteList { - - private String service; - private String namespace; - - public void setService(String service) { - this.service = service; - } - - public String getService() { - return service; - } - - public String getFullService() { - return String.format("%s.%s.svc.cluster.local", service, namespace); - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } - - public String getSourcesNamespace() { - return getNamespace(); - } - -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ConfigMapDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ConfigMapDTO.java new file mode 100644 index 00000000..c65cd383 --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ConfigMapDTO.java @@ -0,0 +1,53 @@ +package org.hango.cloud.meta.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * @Author zhufengwei + * @Date 2023/7/3 + */ +public class ConfigMapDTO{ + /** + * ConfigMap名称 + */ + @JsonProperty(value = "Name") + private String name; + + + /** + * ConfigMap数据key + */ + @JsonProperty(value = "ContentKey") + private String contentKey; + + /** + * ConfigMap数据 + */ + @JsonProperty(value = "ContentValue") + private String contentValue; + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getContentKey() { + return contentKey; + } + + public void setContentKey(String contentKey) { + this.contentKey = contentKey; + } + + public String getContentValue() { + return contentValue; + } + + public void setContentValue(String contentValue) { + this.contentValue = contentValue; + } +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/CustomPluginDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/CustomPluginDTO.java new file mode 100644 index 00000000..47deb030 --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/CustomPluginDTO.java @@ -0,0 +1,69 @@ +package org.hango.cloud.meta.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import org.hibernate.validator.constraints.NotEmpty; + +import javax.validation.constraints.Pattern; + +/** + * @Author zhufengwei + * @Date 2023/7/3 + */ +public class CustomPluginDTO { + + /** 插件的名称: uri-restriction */ + @NotEmpty(message = "PluginName can not be null") + @JsonProperty("PluginName") + private String pluginName; + + + /** 插件的类型 wasm/lua */ + @NotEmpty(message = "Language can not be null") + @Pattern(regexp = "wasm|lua", message = "Language must be wasm or lua") + @JsonProperty("Language") + private String language; + + /** 插件的内容 */ + @NotEmpty(message = "PluginContent can not be null") + @JsonProperty("PluginContent") + private String pluginContent; + + /** 插件schema */ + @NotEmpty(message = "Schema can not be null") + @JsonProperty("Schema") + private String schema; + + public String getSchema() { + return schema; + } + + public void setSchema(String schema) { + this.schema = schema; + } + + public String getPluginName() { + return pluginName; + } + + public void setPluginName(String pluginName) { + this.pluginName = pluginName; + } + + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getPluginContent() { + return pluginContent; + } + + public void setPluginContent(String pluginContent) { + this.pluginContent = pluginContent; + } + +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/EnvoyServiceDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/EnvoyServiceDTO.java new file mode 100644 index 00000000..13bc4c55 --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/EnvoyServiceDTO.java @@ -0,0 +1,59 @@ +package org.hango.cloud.meta.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import javax.validation.constraints.NotNull; +import java.util.List; + +/** + * @Author zhufengwei + * @Date 2023/3/13 + */ +public class EnvoyServiceDTO { + + @NotNull + @JsonProperty("GwClusterName") + private String gwClusterName; + + @JsonProperty("ServiceType") + private String serviceType; + + + @JsonProperty("Ports") + private List ports; + + @JsonProperty("Ips") + private List ips; + + public String getGwClusterName() { + return gwClusterName; + } + + public void setGwClusterName(String gwClusterName) { + this.gwClusterName = gwClusterName; + } + + public List getPorts() { + return ports; + } + + public void setPorts(List ports) { + this.ports = ports; + } + + public String getServiceType() { + return serviceType; + } + + public void setServiceType(String serviceType) { + this.serviceType = serviceType; + } + + public List getIps() { + return ips; + } + + public void setIps(List ips) { + this.ips = ips; + } +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/EnvoyServicePortDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/EnvoyServicePortDTO.java new file mode 100644 index 00000000..205fa43a --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/EnvoyServicePortDTO.java @@ -0,0 +1,54 @@ +package org.hango.cloud.meta.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * @Author zhufengwei + * @Date 2023/3/13 + */ +public class EnvoyServicePortDTO { + @JsonProperty("Name") + private String name; + + @JsonProperty("Port") + private Integer port; + + @JsonProperty("Protocol") + private String protocol; + + @JsonProperty("NodePort") + private Integer nodePort; + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; + } + + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public Integer getNodePort() { + return nodePort; + } + + public void setNodePort(Integer nodePort) { + this.nodePort = nodePort; + } +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/HTTPIngressPathDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/HTTPIngressPathDTO.java new file mode 100644 index 00000000..2bd46eed --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/HTTPIngressPathDTO.java @@ -0,0 +1,71 @@ +package org.hango.cloud.meta.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * @Author zhufengwei + * @Date 2023/5/25 + * ingress dto + * - backend: + * serviceName: httpbin + * servicePort: 80 + * path: /status + * pathType: Prefix + */ +public class HTTPIngressPathDTO { + /** + * path + */ + @JsonProperty("Path") + private String path; + + /** + * 匹配方式 + */ + @JsonProperty("PathType") + private String pathType; + + /** + * 名称 + */ + @JsonProperty("ServiceName") + private String serviceName; + + /** + * 名称 + */ + @JsonProperty("ServicePort") + private Integer servicePort; + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getPathType() { + return pathType; + } + + public void setPathType(String pathType) { + this.pathType = pathType; + } + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public Integer getServicePort() { + return servicePort; + } + + public void setServicePort(Integer servicePort) { + this.servicePort = servicePort; + } +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/IngressDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/IngressDTO.java new file mode 100644 index 00000000..6ed9225e --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/IngressDTO.java @@ -0,0 +1,83 @@ +package org.hango.cloud.meta.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +/** + * @Author zhufengwei + * @Date 2023/5/25 + */ +public class IngressDTO { + /** + * 名称 + */ + @JsonProperty("Name") + private String name; + + /** + * 命名空间 + */ + @JsonProperty("Namespace") + private String namespace; + + + /** + * 所属项目id + */ + @JsonProperty("ProjectCode") + private String projectCode; + + /** + * 所属项目id + */ + @JsonProperty("IngressRule") + private List ingressRuleDTOS; + + + /** + * 配置详细内容 + */ + @JsonProperty("Content") + private String content; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getProjectCode() { + return projectCode; + } + + public void setProjectCode(String projectCode) { + this.projectCode = projectCode; + } + + public List getIngressRuleDTOS() { + return ingressRuleDTOS; + } + + public void setIngressRuleDTOS(List ingressRuleDTOS) { + this.ingressRuleDTOS = ingressRuleDTOS; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/IngressRuleDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/IngressRuleDTO.java new file mode 100644 index 00000000..89a19de8 --- /dev/null +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/IngressRuleDTO.java @@ -0,0 +1,40 @@ +package org.hango.cloud.meta.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +/** + * @Author zhufengwei + * @Date 2023/5/25 + */ +public class IngressRuleDTO { + /** + * 域名 + */ + @JsonProperty("Host") + private String host; + + + /** + * 域名 + */ + @JsonProperty("HTTPRules") + private List httpRuleValueDTOS; + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public List getHttpRuleValueDTOS() { + return httpRuleValueDTOS; + } + + public void setHttpRuleValueDTOS(List httpRuleValueDTOS) { + this.httpRuleValueDTOS = httpRuleValueDTOS; + } +} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PluginOrderDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PluginOrderDTO.java index 8deccf39..9a8ea6a1 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PluginOrderDTO.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PluginOrderDTO.java @@ -1,10 +1,8 @@ package org.hango.cloud.meta.dto; import com.fasterxml.jackson.annotation.JsonProperty; -import org.hibernate.validator.constraints.NotEmpty; import java.util.List; -import java.util.Map; public class PluginOrderDTO { @@ -14,24 +12,32 @@ public class PluginOrderDTO { @JsonProperty(value = "Name") private String name; + + @JsonProperty(value = "GwCluster") + private String gwCluster; + /** * plugin manager 名称 */ @JsonProperty(value = "GatewayKind") private String gatewayKind; - @JsonProperty(value = "GatewayLabels") - private Map gatewayLabels; + /** + * plm对应端口 + */ + @JsonProperty(value = "Port") + private Integer port; + @JsonProperty(value = "Plugins") private List plugins; - public Map getGatewayLabels() { - return gatewayLabels; + public String getGwCluster() { + return gwCluster; } - public void setGatewayLabels(Map gatewayLabels) { - this.gatewayLabels = gatewayLabels; + public void setGwCluster(String gwCluster) { + this.gwCluster = gwCluster; } public List getPlugins() { @@ -54,6 +60,14 @@ public String getGatewayKind() { return gatewayKind; } + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; + } + public void setGatewayKind(String gatewayKind) { this.gatewayKind = gatewayKind; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PluginOrderItemDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PluginOrderItemDTO.java index 5ef87a19..a8d5e37b 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PluginOrderItemDTO.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PluginOrderItemDTO.java @@ -1,10 +1,10 @@ package org.hango.cloud.meta.dto; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import org.hango.cloud.util.Const; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; public class PluginOrderItemDTO { @@ -16,17 +16,26 @@ public class PluginOrderItemDTO { @NotNull(message = "name") private String name; + @JsonProperty("subName") + private String subName; + @JsonProperty("port") @NotNull(message = "port") private Integer port; @JsonProperty("inline") - @JsonInclude(JsonInclude.Include.NON_EMPTY) private Object inline; + @JsonProperty("rider") + private Object rider; + @JsonProperty("listenerType") private String listenerType = Const.GATEWAY; + @JsonProperty("operate") + @Pattern(regexp = "update|delete", message = "operate must be update or delete") + private String operate; + public String getListenerType() { return listenerType; } @@ -67,4 +76,27 @@ public void setInline(Object inline) { this.inline = inline; } + public Object getRider() { + return rider; + } + + public void setRider(Object rider) { + this.rider = rider; + } + + public String getOperate() { + return operate; + } + + public void setOperate(String operate) { + this.operate = operate; + } + + public String getSubName() { + return subName; + } + + public void setSubName(String subName) { + this.subName = subName; + } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PortalAPIDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PortalAPIDTO.java index e87b46d8..b3e5ab19 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PortalAPIDTO.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PortalAPIDTO.java @@ -112,13 +112,6 @@ public class PortalAPIDTO { @Valid private RequestOperationDTO requestOperation; - /** - * VirtualCluster配置 - */ - @JsonProperty(value = "VirtualCluster") - @Valid - private VirtualClusterDTO virtualClusterDTO; - /** * 流量镜像配置 */ @@ -328,13 +321,6 @@ public void setRequestOperation(RequestOperationDTO requestOperation) { this.requestOperation = requestOperation; } - public VirtualClusterDTO getVirtualClusterDTO() { - return virtualClusterDTO; - } - - public void setVirtualClusterDTO(VirtualClusterDTO virtualClusterDTO) { - this.virtualClusterDTO = virtualClusterDTO; - } public Map getMetaMap() { return metaMap; diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PortalServiceDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PortalServiceDTO.java index f25b41c7..365ef1fb 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PortalServiceDTO.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/PortalServiceDTO.java @@ -2,11 +2,13 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import org.hango.cloud.meta.CRDMetaEnum; import org.hibernate.validator.constraints.NotEmpty; import javax.validation.Valid; import javax.validation.constraints.Pattern; import java.util.List; +import java.util.Map; @JsonInclude(JsonInclude.Include.NON_NULL) public class PortalServiceDTO { @@ -61,6 +63,18 @@ public class PortalServiceDTO { @JsonProperty(value = "Version") private Long version; + /** + * meta数据传输集 + * Map + * mata_type meta类型 + * + * @see CRDMetaEnum + * mata_type: 服务meta数据类型 + * meta_data: 服务meta + */ + @JsonProperty(value = "MetaMap") + private Map> metaMap; + public String getCode() { return code; } @@ -140,4 +154,12 @@ public Long getVersion() { public void setVersion(Long version) { this.version = version; } + + public Map> getMetaMap() { + return metaMap; + } + + public void setMetaMap(Map> metaMap) { + this.metaMap = metaMap; + } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ServiceSubsetDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ServiceSubsetDTO.java index ba848387..e622dad5 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ServiceSubsetDTO.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ServiceSubsetDTO.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +import org.hango.cloud.meta.CRDMetaEnum; import org.hibernate.validator.constraints.NotEmpty; import javax.validation.Valid; @@ -26,6 +27,18 @@ public class ServiceSubsetDTO { @JsonProperty(value = "StaticAddrList") private List staticAddrs; + /** + * meta数据传输集 + * Map + * mata_type meta类型 + * + * @see CRDMetaEnum + * mata_type: 服务meta数据类型 + * meta_data: 服务meta + */ + @JsonProperty(value = "MetaMap") + private Map> metaMap; + public String getName() { return name; } @@ -58,6 +71,14 @@ public void setStaticAddrs(List staticAddrs) { this.staticAddrs = staticAddrs; } + public Map> getMetaMap() { + return metaMap; + } + + public void setMetaMap(Map> metaMap) { + this.metaMap = metaMap; + } + @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ValidateResultDTO.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ValidateResultDTO.java deleted file mode 100644 index 026f2a7d..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/ValidateResultDTO.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.hango.cloud.meta.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import org.hango.cloud.meta.ViolationItem; - -import java.util.ArrayList; -import java.util.List; - - -public class ValidateResultDTO { - - @JsonProperty("Pass") - private boolean pass = false; - - @JsonProperty("Items") - private List items = new ArrayList<>(); - - public boolean isPass() { - return pass; - } - - public void setPass(boolean pass) { - this.pass = pass; - } - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthInfoDto.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthInfoDto.java deleted file mode 100644 index 649c9ea7..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthInfoDto.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.hango.cloud.meta.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import javax.validation.constraints.NotNull; -import java.util.List; - - -public class WhiteListV2AuthInfoDto { - - @JsonProperty("Service") - @NotNull(message = "service") - private String service; - - @JsonProperty("DefaultPolicy") - @NotNull(message = "default policy") - private String defaultPolicy; - - /** - * 当且仅当删除规则的时候使用这个字段 - */ - @JsonProperty("RuleName") - private String ruleName; - - @JsonProperty("AuthRules") - @NotNull(message = "auth rules") - private List authRules; - - public String getService() { - return service; - } - - public void setService(String service) { - this.service = service; - } - - public String getDefaultPolicy() { - return defaultPolicy; - } - - public void setDefaultPolicy(String defaultPolicy) { - this.defaultPolicy = defaultPolicy; - } - - public String getRuleName() { - return ruleName; - } - - public void setRuleName(String ruleName) { - this.ruleName = ruleName; - } - - public List getAuthRules() { - return authRules; - } - - public void setAuthRules(List authRules) { - this.authRules = authRules; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthRuleDto.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthRuleDto.java deleted file mode 100644 index bfcbb71b..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthRuleDto.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.hango.cloud.meta.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import javax.validation.constraints.NotNull; - - -public class WhiteListV2AuthRuleDto { - - @JsonProperty("RuleName") - @NotNull(message = "rule name") - private String ruleName; - - @JsonProperty("Enabled") - @NotNull(message = "rule status") - private Boolean enabled; - - @JsonProperty("MatchType") - @NotNull(message = "match type") - private String matchType; - - @JsonProperty("MatchApis") - @NotNull(message = "match apis") - private String matchApis; - - @JsonProperty("MatchConditions") - @NotNull(message = "match conditions") - private String matchConditions; - - public String getRuleName() { - return ruleName; - } - - public Boolean getEnabled() { - return enabled; - } - - public void setEnabled(Boolean enabled) { - this.enabled = enabled; - } - - public void setRuleName(String ruleName) { - this.ruleName = ruleName; - } - - public String getMatchType() { - return matchType; - } - - public void setMatchType(String matchType) { - this.matchType = matchType; - } - - public String getMatchApis() { - return matchApis; - } - - public void setMatchApis(String matchApis) { - this.matchApis = matchApis; - } - - public String getMatchConditions() { - return matchConditions; - } - - public void setMatchConditions(String matchConditions) { - this.matchConditions = matchConditions; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthStatusDto.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthStatusDto.java deleted file mode 100644 index 39d4621d..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/dto/WhiteListV2AuthStatusDto.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.hango.cloud.meta.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import javax.validation.constraints.NotNull; -import java.util.List; - - -public class WhiteListV2AuthStatusDto { - - @JsonProperty("Service") - @NotNull(message = "service") - private String service; - - @JsonProperty("AuthOn") - @NotNull(message = "auth status") - private Boolean authOn; - - @JsonProperty("DefaultPolicy") - @NotNull(message = "default policy") - private String defaultPolicy; - - @JsonProperty("AuthRules") - @NotNull(message = "auth rules") - private List authRules; - - public String getService() { - return service; - } - - public void setService(String service) { - this.service = service; - } - - public Boolean getAuthOn() { - return authOn; - } - - public void setAuthOn(Boolean authOn) { - this.authOn = authOn; - } - - public String getDefaultPolicy() { - return defaultPolicy; - } - - public void setDefaultPolicy(String defaultPolicy) { - this.defaultPolicy = defaultPolicy; - } - - public List getAuthRules() { - return authRules; - } - - public void setAuthRules(List authRules) { - this.authRules = authRules; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Destination.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Destination.java deleted file mode 100644 index 3834c4d2..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Destination.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.hango.cloud.meta.template; - - - -public class Destination { - - private String subset; - - private Integer weight; - - private String host; - - private Integer port; - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public Integer getPort() { - return port; - } - - public void setPort(Integer port) { - this.port = port; - } - - public String getSubset() { - return subset; - } - - public void setSubset(String subset) { - this.subset = subset; - } - - public Integer getWeight() { - return weight; - } - - public void setWeight(Integer weight) { - this.weight = weight; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Header.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Header.java deleted file mode 100644 index 251a1c02..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Header.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.hango.cloud.meta.template; - -import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonAnySetter; - -import java.util.HashMap; -import java.util.Map; - - -public class Header { - - /** - * header key值 - */ - private String header; - - private Map headerMatch = new HashMap<>(); - - public String getHeader() { - return header; - } - - public void setHeader(String header) { - this.header = header; - } - - public void setHeaderMatch(Map headerMatch) { - this.headerMatch = headerMatch; - } - - @JsonAnySetter - public void setHeaderMatch(String type, String header) { - this.headerMatch.put(type, header); - } - - @JsonAnyGetter - public Map getHeaderMatch() { - return headerMatch; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Metadata.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Metadata.java deleted file mode 100644 index 6576e005..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Metadata.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.hango.cloud.meta.template; - -import javax.validation.constraints.NotNull; - - -public class Metadata { - - @NotNull(message = "metadata name") - private String name; - - @NotNull(message = "metadata namespace") - private String namespace; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } - - - public static final class MetadataBuilder { - private String name; - private String namespace; - - private MetadataBuilder() { - } - - public static MetadataBuilder aMetadata() { - return new MetadataBuilder(); - } - - public MetadataBuilder withName(String name) { - this.name = name; - return this; - } - - public MetadataBuilder withNamespace(String namespace) { - this.namespace = namespace; - return this; - } - - public Metadata build() { - Metadata metadata = new Metadata(); - metadata.setName(name); - metadata.setNamespace(namespace); - return metadata; - } - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/NsfExtra.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/NsfExtra.java deleted file mode 100644 index 65eeea1a..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/NsfExtra.java +++ /dev/null @@ -1,290 +0,0 @@ -package org.hango.cloud.meta.template; - -import javax.validation.Valid; -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; -import javax.validation.constraints.Pattern; -import java.util.List; - - -public class NsfExtra { - - private String host; - - private UrlMatch uri; - - private List
headers; - - private List destinations; - - @Min(value = 0, message = "percent") - @Max(value = 100, message = "percent") - private Integer percent; - - private String fixedDelay; - - private Integer httpStatus; - - private Integer consecutiveErrors = 5; - - private String interval = "10s"; - - private String baseEjectionTime = "30s"; - - @Min(value = 1, message = "maxEjectionPercent") - @Max(value = 100, message = "maxEjectionPercent") - private Integer maxEjectionPercent = 10; - - @Pattern(regexp = "(ROUND_ROBIN|LEASE_CONN|RANDOM|PASSTHROUGH)", message = "simple") - private String simple; - - private List hosts; - - @Valid - private List targetList; - - @Min(value = 0, message = "outWeight") - @Max(value = 100, message = "outWeight") - private Integer outWeight; - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public List getDestinations() { - return destinations; - } - - public void setDestinations(List destinations) { - this.destinations = destinations; - } - - public Integer getPercent() { - return percent; - } - - public void setPercent(Integer percent) { - this.percent = percent; - } - - public String getFixedDelay() { - return fixedDelay; - } - - public void setFixedDelay(String fixedDelay) { - this.fixedDelay = fixedDelay; - } - - public Integer getHttpStatus() { - return httpStatus; - } - - public void setHttpStatus(Integer httpStatus) { - this.httpStatus = httpStatus; - } - - public Integer getConsecutiveErrors() { - return consecutiveErrors; - } - - public void setConsecutiveErrors(Integer consecutiveErrors) { - this.consecutiveErrors = consecutiveErrors; - } - - public String getInterval() { - return interval; - } - - public void setInterval(String interval) { - this.interval = interval; - } - - public String getBaseEjectionTime() { - return baseEjectionTime; - } - - public void setBaseEjectionTime(String baseEjectionTime) { - this.baseEjectionTime = baseEjectionTime; - } - - public Integer getMaxEjectionPercent() { - return maxEjectionPercent; - } - - public void setMaxEjectionPercent(Integer maxEjectionPercent) { - this.maxEjectionPercent = maxEjectionPercent; - } - - public String getSimple() { - return simple; - } - - public void setSimple(String simple) { - this.simple = simple; - } - - public List getHosts() { - return hosts; - } - - public void setHosts(List hosts) { - this.hosts = hosts; - } - - public List getTargetList() { - return targetList; - } - - public void setTargetList(List targetList) { - this.targetList = targetList; - } - - public UrlMatch getUri() { - return uri; - } - - public void setUri(UrlMatch uri) { - this.uri = uri; - } - - public List
getHeaders() { - return headers; - } - - public void setHeaders(List
headers) { - this.headers = headers; - } - - public Integer getOutWeight() { - return outWeight; - } - - public void setOutWeight(Integer outWeight) { - this.outWeight = outWeight; - } - - - public static final class NsfExtraBuilder { - private String host; - private UrlMatch uri; - private List
headers; - private List destinations; - private Integer percent; - private String fixedDelay; - private Integer httpStatus; - private Integer consecutiveErrors; - private String interval; - private String baseEjectionTime; - private Integer maxEjectionPercent; - private String simple; - private List hosts; - private List targetList; - private Integer outWeight; - - private NsfExtraBuilder() { - } - - public static NsfExtraBuilder aNsfExtra() { - return new NsfExtraBuilder(); - } - - public NsfExtraBuilder withHost(String host) { - this.host = host; - return this; - } - - public NsfExtraBuilder withUri(UrlMatch uri) { - this.uri = uri; - return this; - } - - public NsfExtraBuilder withHeaders(List
headers) { - this.headers = headers; - return this; - } - - public NsfExtraBuilder withDestinations(List destinations) { - this.destinations = destinations; - return this; - } - - public NsfExtraBuilder withPercent(Integer percent) { - this.percent = percent; - return this; - } - - public NsfExtraBuilder withFixedDelay(String fixedDelay) { - this.fixedDelay = fixedDelay; - return this; - } - - public NsfExtraBuilder withHttpStatus(Integer httpStatus) { - this.httpStatus = httpStatus; - return this; - } - - public NsfExtraBuilder withConsecutiveErrors(Integer consecutiveErrors) { - this.consecutiveErrors = consecutiveErrors; - return this; - } - - public NsfExtraBuilder withInterval(String interval) { - this.interval = interval; - return this; - } - - public NsfExtraBuilder withBaseEjectionTime(String baseEjectionTime) { - this.baseEjectionTime = baseEjectionTime; - return this; - } - - public NsfExtraBuilder withMaxEjectionPercent(Integer maxEjectionPercent) { - this.maxEjectionPercent = maxEjectionPercent; - return this; - } - - public NsfExtraBuilder withSimple(String simple) { - this.simple = simple; - return this; - } - - public NsfExtraBuilder withHosts(List hosts) { - this.hosts = hosts; - return this; - } - - public NsfExtraBuilder withTargetList(List targetList) { - this.targetList = targetList; - return this; - } - - public NsfExtraBuilder withOutWeight(Integer outWeight) { - this.outWeight = outWeight; - return this; - } - - public NsfExtra build() { - NsfExtra nsfExtra = new NsfExtra(); - nsfExtra.setHost(host); - nsfExtra.setUri(uri); - nsfExtra.setHeaders(headers); - nsfExtra.setDestinations(destinations); - nsfExtra.setPercent(percent); - nsfExtra.setFixedDelay(fixedDelay); - nsfExtra.setHttpStatus(httpStatus); - nsfExtra.setConsecutiveErrors(consecutiveErrors); - nsfExtra.setInterval(interval); - nsfExtra.setBaseEjectionTime(baseEjectionTime); - nsfExtra.setMaxEjectionPercent(maxEjectionPercent); - nsfExtra.setSimple(simple); - nsfExtra.setHosts(hosts); - nsfExtra.setTargetList(targetList); - nsfExtra.setOutWeight(outWeight); - return nsfExtra; - } - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/RouteMatch.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/RouteMatch.java deleted file mode 100644 index b9445ac9..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/RouteMatch.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.hango.cloud.meta.template; - - -public class RouteMatch { - - -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Service.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Service.java deleted file mode 100644 index 58ac241f..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/Service.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.hango.cloud.meta.template; - -import org.hibernate.validator.constraints.NotEmpty; - - -public class Service { - - @NotEmpty(message = "name") - private String name; - - @NotEmpty(message = "namespace") - private String namespace; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/ServiceMeshTemplate.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/ServiceMeshTemplate.java deleted file mode 100644 index 8b41f8a8..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/ServiceMeshTemplate.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.hango.cloud.meta.template; - -import org.hango.cloud.util.validator.annotation.ConditionalTemplate; -import org.hibernate.validator.constraints.NotEmpty; - -import javax.validation.Valid; -import javax.validation.constraints.NotNull; - - -@ConditionalTemplate(templateName = "whiteList", required = {"outWeight", "targetList"}) -@ConditionalTemplate(templateName = "cloudShuttle", required = {"outWeight", "targetList"}) -@ConditionalTemplate(templateName = "faultInject", required = {"host", "percent", "fixedDelay", "httpStatus", "destinations"}) -@ConditionalTemplate(templateName = "loadBalancing", required = {"host", "simple"}) -public class ServiceMeshTemplate { - - @NotEmpty(message = "template") - private String nsfTemplate; - - @Valid - @NotNull(message = "nsfExtra") - private NsfExtra nsfExtra; - - @Valid - @NotNull(message = "metadata") - private Metadata metadata; - - private Boolean update; - - public String getNsfTemplate() { - return nsfTemplate; - } - - public void setNsfTemplate(String nsfTemplate) { - this.nsfTemplate = nsfTemplate; - } - - public NsfExtra getNsfExtra() { - return nsfExtra; - } - - public void setNsfExtra(NsfExtra nsfExtra) { - this.nsfExtra = nsfExtra; - } - - public Metadata getMetadata() { - return metadata; - } - - public void setMetadata(Metadata metadata) { - this.metadata = metadata; - } - - public Boolean getUpdate() { - return update; - } - - public void setUpdate(Boolean update) { - this.update = update; - } - - public static final class ServiceMeshTemplateBuilder { - private String nsfTemplate; - private NsfExtra nsfExtra; - private Metadata metadata; - - private ServiceMeshTemplateBuilder() { - } - - public static ServiceMeshTemplateBuilder aServiceMeshTemplate() { - return new ServiceMeshTemplateBuilder(); - } - - public ServiceMeshTemplateBuilder withNsfTemplate(String nsfTemplate) { - this.nsfTemplate = nsfTemplate; - return this; - } - - public ServiceMeshTemplateBuilder withNsfExtra(NsfExtra nsfExtra) { - this.nsfExtra = nsfExtra; - return this; - } - - public ServiceMeshTemplateBuilder withMetadata(Metadata metadata) { - this.metadata = metadata; - return this; - } - - public ServiceMeshTemplate build() { - ServiceMeshTemplate serviceMeshTemplate = new ServiceMeshTemplate(); - serviceMeshTemplate.setNsfTemplate(nsfTemplate); - serviceMeshTemplate.setNsfExtra(nsfExtra); - serviceMeshTemplate.setMetadata(metadata); - return serviceMeshTemplate; - } - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/UrlMatch.java b/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/UrlMatch.java deleted file mode 100644 index 085c82f1..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/meta/template/UrlMatch.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.hango.cloud.meta.template; - - -import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonAnySetter; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - - - -public class UrlMatch implements Serializable { - - private Map urlMatch = new HashMap<>(); - - @JsonAnySetter - public void setUrlMatch(String type, String url) { - this.urlMatch.put(type, url); - } - - @JsonAnyGetter - public Map getUrlMatch() { - return urlMatch; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/service/GatewayService.java b/hango-api-plane-server/src/main/java/org/hango/cloud/service/GatewayService.java index eea76174..e7e8cf5d 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/service/GatewayService.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/service/GatewayService.java @@ -69,4 +69,41 @@ public interface GatewayService { void updateIstioGateway(PortalIstioGatewayDTO portalGateway); void deleteIstioGateway(PortalIstioGatewayDTO portalGateway); + + /** + * 获取envoy proxy service ep 信息 + * 返回格式为[ip1:port1,ip2:port2,..] + * clusterIp: 空列表 + * nodeport: ip为envoy所在节点的ip,port为nodeport + * loadbalance: ip为loadbalance的外部地址,port为监听端口 + * hostnetwork: ip为网关pod所在节点的ip地址,port为监听端口。 + */ + List getEnvoyAddress(String gwClusterName); + + /** + * 下发plm资源时校验端口是否冲突 + * @return + */ + boolean pluginOrderPortCheck(PluginOrderDTO pluginOrderDto); + + + /** + * 发布configmap资源 + * @return + */ + boolean publishConfigMap(ConfigMapDTO configMapDTO); + + + + /** + * 发布自定义插件 + */ + boolean publishCustomPlugin(CustomPluginDTO customPluginDTO); + + /** + * 删除自定义插件 + */ + boolean deleteCustomPlugin(String pluginName, String language); + + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/service/KubernetesGatewayService.java b/hango-api-plane-server/src/main/java/org/hango/cloud/service/KubernetesGatewayService.java index 60db1958..7f0d0847 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/service/KubernetesGatewayService.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/service/KubernetesGatewayService.java @@ -1,6 +1,7 @@ package org.hango.cloud.service; import io.fabric8.kubernetes.api.model.gatewayapi.v1beta1.HTTPRoute; +import org.hango.cloud.meta.dto.IngressDTO; import org.hango.cloud.meta.dto.KubernetesGatewayDTO; import java.util.List; @@ -11,8 +12,25 @@ */ public interface KubernetesGatewayService { + /** + * 获取k8s gateway资源 + * @param gateway + * @return + */ List getKubernetesGateway(String gateway); + /** + * 获取gateay api中的httproute资源 + * @param gateway + * @return + */ List getHTTPRoute(String gateway); + /** + * 获取ingress列表 + * @param namespace 命名空间 + * @param name ingress name + */ + List getIngress(String namespace, String name); + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/service/PluginService.java b/hango-api-plane-server/src/main/java/org/hango/cloud/service/PluginService.java index 92dfdd13..67e65d60 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/service/PluginService.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/service/PluginService.java @@ -14,10 +14,20 @@ * @date 2019/8/2 **/ public interface PluginService { + + /** + * 查询插件 + */ Plugin getPlugin(String name); + /** + * 查询全量插件 + */ Map getPlugins(); + /** + * 查询schema + */ String getSchema(String path); List processPlugin(List plugins, ServiceInfo serviceInfo); diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/service/ValidateService.java b/hango-api-plane-server/src/main/java/org/hango/cloud/service/ValidateService.java deleted file mode 100644 index 994cf047..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/service/ValidateService.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.hango.cloud.service; - -import org.hango.cloud.meta.ValidateResult; -import io.fabric8.kubernetes.api.model.HasMetadata; - -import java.util.List; - -/** - * @auther wupenghuai@corp.netease.com - * @date 2020/3/5 - **/ -public interface ValidateService { - ValidateResult validate(List resources); -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/GatewayServiceImpl.java b/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/GatewayServiceImpl.java index 88c81034..800f2075 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/GatewayServiceImpl.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/GatewayServiceImpl.java @@ -1,17 +1,24 @@ package org.hango.cloud.service.impl; import freemarker.template.Configuration; -import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.api.model.*; +import me.snowdrop.istio.api.networking.v1alpha3.Gateway; +import me.snowdrop.istio.api.networking.v1alpha3.Port; +import me.snowdrop.istio.api.networking.v1alpha3.Server; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.commons.lang3.math.NumberUtils; import org.hango.cloud.core.GlobalConfig; +import org.hango.cloud.core.envoy.EnvoyHttpClient; import org.hango.cloud.core.gateway.service.GatewayConfigManager; import org.hango.cloud.core.gateway.service.ResourceManager; import org.hango.cloud.core.istio.PilotHttpClient; +import org.hango.cloud.core.k8s.K8sClient; import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.k8s.K8sTypes; +import org.hango.cloud.meta.Secret; import org.hango.cloud.meta.*; import org.hango.cloud.meta.dto.*; import org.hango.cloud.service.GatewayService; @@ -26,6 +33,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.util.CollectionUtils; import java.util.*; @@ -33,6 +41,8 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import static org.hango.cloud.util.Const.RIDER_PLUGIN; + public class GatewayServiceImpl implements GatewayService { @@ -46,6 +56,22 @@ public class GatewayServiceImpl implements GatewayService { private static final Pattern DUBBO_INFO_PATTRERN = Pattern.compile("^(\\S*) (\\S*)\\((\\S*)\\)$"); private static final Pattern DUBBO_TELNET_RETURN_PATTERN = Pattern.compile("[\\s\\S]*?\\(as (provider|consumer)\\):"); public static final String GW_CLUSTER = "gw_cluster"; + //插件路径 + public static final String PLUGIN_CONTENT_PATH = "var/lib/istio/data"; + + public static final String CUSTOM_PLUGIN_CONFIG = "custom-plugin-config.json"; + + + public static final String HOST_NETWORK = "HostNetwork"; + + public static final String CLUSTER_IP = "ClusterIP"; + + public static final String NODE_PORT = "NodePort"; + + public static final String LOAD_BALANCER = "LoadBalancer"; + + @Value(value = "${gatewayName:gateway-proxy}") + private String gatewayName; private ResourceManager resourceManager; @@ -59,6 +85,12 @@ public class GatewayServiceImpl implements GatewayService { @Autowired private PilotHttpClient pilotHttpClient; + @Autowired + private EnvoyHttpClient envoyHttpClient; + + @Autowired + private K8sClient k8sClient; + @Autowired private PluginService pluginService; @@ -336,6 +368,9 @@ public PluginOrderDTO getPluginOrder(PluginOrderDTO pluginOrderDto) { @Override public void updatePluginOrder(PluginOrderDTO pluginOrderDto) { + if (CollectionUtils.isEmpty(pluginOrderDto.getPlugins())){ + return; + } //查询plm资源 PluginOrderDTO pluginManager = getPluginManager(pluginOrderDto.getName()); if (CollectionUtils.isEmpty(pluginManager.getPlugins())){ @@ -352,9 +387,32 @@ public void updatePluginOrder(PluginOrderDTO pluginOrderDto) { public void publishPluginOrder(PluginOrderDTO pluginOrderDto) { PluginOrderDTO pluginOrder = pluginService.getPluginOrderTemplate(pluginOrderDto.getGatewayKind()); pluginOrder.setName(pluginOrderDto.getName()); + pluginOrder.getPlugins().forEach(o -> o.setPort(pluginOrderDto.getPort())); + pluginOrder.setGwCluster(pluginOrderDto.getGwCluster()); configManager.updateConfig(Trans.pluginOrderDTO2PluginOrder(pluginOrder)); } + /** + * 校验相同端口的plm资源中是否已经下发 + */ + @Override + public boolean pluginOrderPortCheck(PluginOrderDTO pluginOrderDto){ + List pluginManagerList = getPluginManagerList(pluginOrderDto.getGwCluster()); + if (CollectionUtils.isEmpty(pluginManagerList)){ + return true; + } + for (PluginOrderDTO pluginOrderDTO : pluginManagerList) { + List plugins = pluginOrderDTO.getPlugins(); + if (CollectionUtils.isEmpty(plugins)){ + continue; + } + if (plugins.get(0).getPort().equals(pluginOrderDto.getPort())){ + return false; + } + } + return true; + } + @Override public void deletePluginOrder(PluginOrderDTO pluginOrderDTO) { @@ -364,12 +422,18 @@ public void deletePluginOrder(PluginOrderDTO pluginOrderDTO) { @Override public void updateIstioGateway(PortalIstioGatewayDTO portalGateway) { - configManager.updateConfig(Trans.portalGW2GW(portalGateway)); + IstioGateway istioGateway = Trans.portalGW2GW(portalGateway); + configManager.updateConfig(istioGateway); + //联动创建envoy service端口 + createEnvoyServicePort(istioGateway); } @Override public void deleteIstioGateway(PortalIstioGatewayDTO portalGateway) { - configManager.deleteConfig(Trans.portalGW2GW(portalGateway)); + IstioGateway istioGateway = Trans.portalGW2GW(portalGateway); + //联动删除envoy service端口 + deleteEnvoyServicePort(istioGateway); + configManager.deleteConfig(istioGateway); } @@ -528,14 +592,249 @@ public PluginOrderDTO getPluginManager(String name){ return pluginOrderDTO; } + public List getPluginManagerList(String gwCluster){ + List config = configManager.getConfigListWithRev(K8sResourceEnum.PluginManager.name()); + if (CollectionUtils.isEmpty(config)){ + return new ArrayList<>(); + } + return config.stream().map(o -> Trans.trans((K8sTypes.PluginManager) o)).filter(o -> gwCluster.equals(o.getGwCluster())).collect(Collectors.toList()); + } + + private void processPluginOrder(PluginOrderDTO pluginOrderDto, List updatePluginList) { if (CollectionUtils.isEmpty(updatePluginList)) { return; } - Map> updatePluginMap = updatePluginList.stream().collect(Collectors.groupingBy(PluginOrderItemDTO::getName)); + List plugins = pluginOrderDto.getPlugins(); + for (PluginOrderItemDTO pluginOrderItemDTO : updatePluginList) { + String pluginName = RIDER_PLUGIN.equals(pluginOrderItemDTO.getName()) ? pluginOrderItemDTO.getSubName() : pluginOrderItemDTO.getName(); + if ("delete".equals(pluginOrderItemDTO.getOperate())){ + //删除item + plugins.removeIf(plugin -> Trans.getPluginName(plugin).equals(pluginName)); + continue; + } + PluginOrderItemDTO source = plugins.stream().filter(plugin -> Trans.getPluginName(plugin).equals(pluginName)).findFirst().orElse(null); + if (source == null){ + //不存在item,新增 + plugins.add(pluginOrderItemDTO); + }else { + //存在item,更新 + plugins = plugins.stream().filter(plugin -> !Trans.getPluginName(plugin).equals(pluginName)).collect(Collectors.toList()); + plugins.add(processorPluginItem(source, pluginOrderItemDTO)); + } + } + pluginOrderDto.setPlugins(plugins); + } + + private PluginOrderItemDTO processorPluginItem(PluginOrderItemDTO source, PluginOrderItemDTO target){ + if (source == null){ + return target; + } + source.setEnable(target.getEnable()); + if (target.getInline() != null){ + source.setInline(target.getInline()); + } + if (target.getRider() != null){ + source.setRider(target.getRider()); + } + return source; + } + + + + private void createEnvoyServicePort(IstioGateway istioGateway){ + List envoyServiceList = envoyHttpClient.getEnvoyServiceList(istioGateway.getGwCluster()); + List servers = istioGateway.getServers(); + if (CollectionUtils.isEmpty(envoyServiceList) || CollectionUtils.isEmpty(servers)){ + return; + } + for (Service envoyService : envoyServiceList) { + boolean needCreate = Boolean.FALSE; + List servicePortList = envoyService.getSpec().getPorts(); + List ports = servicePortList.stream().map(ServicePort::getPort).collect(Collectors.toList()); + for (IstioGatewayServer server : servers) { + if (ports.contains(server.getNumber())){ + //当前端口已存在,不添加 + continue; + } + ServicePort servicePort = new ServicePort(); + String protocol = Const.PROTOCOL_UDP.equalsIgnoreCase(server.getProtocol()) ? Const.PROTOCOL_UDP : Const.PROTOCOL_TCP; + servicePort.setProtocol(protocol); + servicePort.setPort(server.getNumber()); + servicePort.setName(server.getProtocol().toLowerCase() + server.getNumber()); + servicePortList.add(servicePort); + needCreate = Boolean.TRUE; + } + if (needCreate){ + configManager.updateK8sService(envoyService); + } + + } + } + + private void deleteEnvoyServicePort(IstioGateway istioGateway){ + List envoyServiceList = envoyHttpClient.getEnvoyServiceList(istioGateway.getGwCluster()); + if (CollectionUtils.isEmpty(envoyServiceList)){ + return; + } + List deletePort = getDeletePort(istioGateway.getName()); + for (Service service : envoyServiceList) { + List servicePorts = service.getSpec().getPorts().stream().filter(o -> !deletePort.contains(o.getPort())).collect(Collectors.toList()); + service.getSpec().setPorts(servicePorts); + configManager.updateK8sService(service); + } + } + + private List getDeletePort(String name){ + HasMetadata hasMetadata = configManager.getConfig(K8sResourceEnum.Gateway.name(), name); + if (hasMetadata == null){ + return Collections.emptyList(); + } + Gateway gateway = (Gateway)hasMetadata; + List servers = gateway.getSpec().getServers(); + if (CollectionUtils.isEmpty(servers)){ + return Collections.emptyList(); + } + //80端口envoy用于健康检查,默认不删除80端口 + return servers.stream().map(Server::getPort).map(Port::getNumber).distinct().filter(o -> o != 80).collect(Collectors.toList()); + } + + + @Override + public List getEnvoyAddress(String gwClusterName) { + Pod envoyPod = envoyHttpClient.getEnvoyPod(gwClusterName); + if (Boolean.TRUE.equals(envoyPod.getSpec().getHostNetwork())){ + EnvoyServiceDTO serviceDTO = new EnvoyServiceDTO(); + serviceDTO.setGwClusterName(gwClusterName); + serviceDTO.setServiceType(HOST_NETWORK); + serviceDTO.setIps(envoyHttpClient.getEnvoySchedulableNodeAddress(gwClusterName)); + return Collections.singletonList(serviceDTO); + } + List envoyServiceDTOS = new ArrayList<>(); + List services = envoyHttpClient.getEnvoyServiceList(gwClusterName); + for (Service service : services) { + EnvoyServiceDTO serviceDTO = new EnvoyServiceDTO(); + serviceDTO.setGwClusterName(gwClusterName); + serviceDTO.setServiceType(service.getSpec().getType()); + List ports = service.getSpec().getPorts().stream().map(Trans::getPorts).collect(Collectors.toList()); + serviceDTO.setPorts(ports); + serviceDTO.setIps(getIps(serviceDTO.getServiceType(), service.getSpec(), gwClusterName)); + envoyServiceDTOS.add(serviceDTO); + } + return envoyServiceDTOS; + } + + + + private List getIps(String type, ServiceSpec spec, String gwClusterName){ + switch (type){ + case NODE_PORT: + return envoyHttpClient.getEnvoySchedulableNodeAddress(gwClusterName); + case CLUSTER_IP: + return Collections.singletonList(spec.getClusterIP()); + case LOAD_BALANCER: + return spec.getExternalIPs(); + default: + return new ArrayList<>(); + } + } + + @Override + public boolean publishConfigMap(ConfigMapDTO configMapDTO) { + ConfigMap configMap = k8sClient.getConfigMap(globalConfig.getResourceNamespace(), configMapDTO.getName()); + if (configMap == null){ + logger.error("config map not existed, name"); + return false; + } + Map data = configMap.getData(); + if (data == null){ + data = new HashMap<>(); + } + //如果value为空,则需要删除key + if (StringUtils.isBlank(configMapDTO.getContentValue())){ + if (!data.containsKey(configMapDTO.getContentKey())){ + return true; + } + data.remove(configMapDTO.getContentKey()); + }else { + data.put(configMapDTO.getContentKey(), configMapDTO.getContentValue()); + } + configMap.setData(data); + configManager.updateConfig(configMap); + return true; + } + + @Override + public boolean publishCustomPlugin(CustomPluginDTO customPluginDTO) { + //发布自定义代码 + boolean result = publishCustomCode(customPluginDTO.getPluginName(), customPluginDTO.getLanguage(), customPluginDTO.getPluginContent()); + if (!result){ + logger.error("publish custom code failed, pluginName:{}", customPluginDTO.getPluginName()); + return false; + } + //发布schema + result = publishSchema(customPluginDTO); + if (!result){ + logger.error("publish schema failed, pluginName:{}", customPluginDTO.getPluginName()); + return false; + } + return true; + } + + @Override + public boolean deleteCustomPlugin(String pluginName, String language) { + //删除自定义代码 + boolean result = publishCustomCode(pluginName, language, null); + if (!result){ + logger.error("delete custom code failed, pluginName:{}", pluginName); + return false; + } + //删除schema + CustomPluginDTO customPluginDTO = new CustomPluginDTO(); + customPluginDTO.setPluginName(pluginName); + result = publishSchema(customPluginDTO); + if (!result){ + logger.error("delete schema failed, pluginName:{}", pluginName); + return false; + } + return true; + } + + + /** + * 发布自定义代码 + * @param pluginName 插件名称 + * @param language 语言 + * @param code 代码 + * @return 是否发布成功 + */ + private boolean publishCustomCode(String pluginName, String language, String code){ + ConfigMapDTO configMapDTO = new ConfigMapDTO(); + configMapDTO.setName(globalConfig.getPluginContentConfigName()); + //contentKey格式为:pluginName.language,例如:uri-restriction.lua + configMapDTO.setContentKey(Trans.getCustomCodePath(pluginName, language)); + configMapDTO.setContentValue(code); + return publishConfigMap(configMapDTO); + } - pluginOrderDto.getPlugins().stream() - .filter(plugin -> updatePluginMap.containsKey(plugin.getName())) - .forEach(plugin -> plugin.setEnable(updatePluginMap.get(plugin.getName()).get(0).getEnable())); + /** + * 发布schema详情 + * @param customPluginDTO 自定义插件 + * @return 是否发布成功 + */ + private boolean publishSchema(CustomPluginDTO customPluginDTO){ + /** + * 发布schema + */ + ConfigMapDTO configMapDTO = new ConfigMapDTO(); + configMapDTO.setName(globalConfig.getPluginSchemaConfigName()); + configMapDTO.setContentKey(Trans.getSchemaPath(customPluginDTO.getPluginName())); + configMapDTO.setContentValue(customPluginDTO.getSchema()); + boolean result = publishConfigMap(configMapDTO); + if (!result){ + logger.error("publish schema failed, pluginName:{}", customPluginDTO.getPluginName()); + return false; + } + return true; } } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/KubernetesGatewayServiceImpl.java b/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/KubernetesGatewayServiceImpl.java index 4afddb2f..f4013b0a 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/KubernetesGatewayServiceImpl.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/KubernetesGatewayServiceImpl.java @@ -5,11 +5,17 @@ import io.fabric8.kubernetes.api.model.KubernetesResourceList; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.api.model.gatewayapi.v1beta1.*; +import io.fabric8.kubernetes.api.model.networking.v1.*; import org.apache.logging.log4j.util.Strings; import org.hango.cloud.core.GlobalConfig; +import org.hango.cloud.core.gateway.service.GatewayConfigManager; import org.hango.cloud.core.k8s.K8sClient; +import org.hango.cloud.core.k8s.K8sResourceEnum; import org.hango.cloud.k8s.K8sResourceApiEnum; import org.hango.cloud.meta.CustomResource; +import org.hango.cloud.meta.dto.IngressDTO; +import org.hango.cloud.meta.dto.HTTPIngressPathDTO; +import org.hango.cloud.meta.dto.IngressRuleDTO; import org.hango.cloud.meta.dto.KubernetesGatewayDTO; import org.hango.cloud.service.KubernetesGatewayService; import org.slf4j.Logger; @@ -36,7 +42,7 @@ public class KubernetesGatewayServiceImpl implements KubernetesGatewayService { public static final String GATEWAY = "Gateway"; - public static final String PROJECT_KEY = "hango.io/gateway.project"; + public static final String INGRESS_CONTROLLER = "kubernetes.io/ingress.class"; @Autowired private K8sClient k8sClient; @@ -44,6 +50,9 @@ public class KubernetesGatewayServiceImpl implements KubernetesGatewayService { @Autowired private GlobalConfig globalConfig; + @Autowired + private GatewayConfigManager configManager; + @Override public List getKubernetesGateway(String name) { CustomResource customResource = buildCustomResource(KubernetesGateway, Gateway.class, GatewayList.class); @@ -77,6 +86,24 @@ public List getHTTPRoute(String gateway) { return httpRoutes.stream().filter(o -> gatewayFilter(o, gateway)).map(this::convertHTTPRoute).collect(Collectors.toList()); } + @Override + public List getIngress(String namespace, String name) { + //name或ns为空,查询全量配置 + if (!StringUtils.hasText(namespace) || !StringUtils.hasText(name)){ + return configManager.getConfigList(K8sResourceEnum.Ingress.name()).stream() + .filter(this::ingressFilter) + .map(this::convertIngress) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + List targetList = new ArrayList<>(); + HasMetadata config = configManager.getConfig(K8sResourceEnum.Ingress.name(), namespace, name); + if (ingressFilter(config)){ + targetList.add(convertIngress(config)); + } + return targetList; + } + private Map getLabel(){ Map label = new HashMap<>(); label.put(LABLE_ISTIO_REV, globalConfig.getIstioRev()); @@ -101,11 +128,7 @@ private KubernetesGatewayDTO convertGateway(Gateway gateway){ ObjectMeta metadata = gateway.getMetadata(); KubernetesGatewayDTO gatewayDTO = new KubernetesGatewayDTO(); gatewayDTO.setName(metadata.getName()); - Map annotations = metadata.getAnnotations(); - if (annotations.containsKey(PROJECT_KEY)){ - String projectValue = annotations.get(PROJECT_KEY); - gatewayDTO.setProjectId(projectValue); - } + gatewayDTO.setProjectId(getProject(metadata.getAnnotations())); List listeners = gateway.getSpec().getListeners(); if (!CollectionUtils.isEmpty(listeners)){ //目前只支持一个listener @@ -135,6 +158,18 @@ private String getGatewayContent(Gateway gateway){ return obj2yaml(targetGateway); } + private String getIngressContent(Ingress ingress){ + Ingress targetIngress = new Ingress(); + targetIngress.setApiVersion(ingress.getApiVersion()); + targetIngress.setKind(ingress.getKind()); + ObjectMeta targetMetadata = new ObjectMeta(); + targetMetadata.setName(ingress.getMetadata().getName()); + targetMetadata.setNamespace(ingress.getMetadata().getNamespace()); + targetIngress.setMetadata(targetMetadata); + targetIngress.setSpec(ingress.getSpec()); + return obj2yaml(targetIngress); + } + private HTTPRoute convertHTTPRoute(HasMetadata hasMetadata){ if (hasMetadata instanceof HTTPRoute){ HTTPRoute httpRoute = (HTTPRoute) hasMetadata; @@ -151,6 +186,67 @@ private HTTPRoute convertHTTPRoute(HasMetadata hasMetadata){ return null; } + private IngressDTO convertIngress(HasMetadata hasMetadata){ + if (!(hasMetadata instanceof Ingress)){ + return null; + } + ObjectMeta metadata = hasMetadata.getMetadata(); + IngressDTO ingressDTO = new IngressDTO(); + ingressDTO.setName(metadata.getName()); + ingressDTO.setNamespace(metadata.getNamespace()); + ingressDTO.setProjectCode(getProject(metadata.getAnnotations())); + IngressSpec spec = ((Ingress) hasMetadata).getSpec(); + List rules = spec.getRules(); + if (CollectionUtils.isEmpty(rules)){ + return ingressDTO; + } + List ingressRuleDTOS = rules.stream().map(this::convertIngressRule).collect(Collectors.toList()); + ingressDTO.setIngressRuleDTOS(ingressRuleDTOS); + ingressDTO.setContent(getIngressContent((Ingress) hasMetadata)); + return ingressDTO; + } + + private IngressRuleDTO convertIngressRule(IngressRule ingressRule){ + IngressRuleDTO ingressRuleDTO = new IngressRuleDTO(); + String host = ingressRule.getHost(); + if (StringUtils.isEmpty(host)){ + host = "*"; + } + ingressRuleDTO.setHost(host); + HTTPIngressRuleValue http = ingressRule.getHttp(); + if (http == null){ + return ingressRuleDTO; + } + List paths = http.getPaths(); + if (CollectionUtils.isEmpty(paths)){ + return ingressRuleDTO; + } + List httpIngressPathDTOS = paths.stream().map(this::convertHTTPRule).collect(Collectors.toList()); + ingressRuleDTO.setHttpRuleValueDTOS(httpIngressPathDTOS); + return ingressRuleDTO; + } + + private HTTPIngressPathDTO convertHTTPRule(HTTPIngressPath httpIngressPath){ + HTTPIngressPathDTO httpIngressPathDTO = new HTTPIngressPathDTO(); + httpIngressPathDTO.setPath(httpIngressPath.getPath()); + httpIngressPathDTO.setPathType(httpIngressPath.getPathType()); + IngressBackend backend = httpIngressPath.getBackend(); + if (backend == null){ + return httpIngressPathDTO; + } + IngressServiceBackend service = backend.getService(); + if (service == null){ + return httpIngressPathDTO; + } + httpIngressPathDTO.setServiceName(service.getName()); + ServiceBackendPort port = service.getPort(); + if (port == null){ + return httpIngressPathDTO; + } + httpIngressPathDTO.setServicePort(port.getNumber()); + return httpIngressPathDTO; + } + private String obj2yaml(HasMetadata hasMetadata){ try { @@ -161,4 +257,23 @@ private String obj2yaml(HasMetadata hasMetadata){ return Strings.EMPTY; } + private String getProject(Map annotations){ + if (annotations == null){ + return null; + } + return annotations.getOrDefault(globalConfig.getProjectCode(), null); + } + + + private boolean ingressFilter(HasMetadata hasMetadata){ + if (hasMetadata instanceof Ingress) { + Map annotations = hasMetadata.getMetadata().getAnnotations(); + if (annotations == null){ + return false; + } + return globalConfig.getIngressClass().equals(annotations.get(INGRESS_CONTROLLER)); + } + return false; + } + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/PluginServiceImpl.java b/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/PluginServiceImpl.java index 9caf6a6c..e4736032 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/PluginServiceImpl.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/PluginServiceImpl.java @@ -9,8 +9,7 @@ import org.hango.cloud.core.editor.ResourceGenerator; import org.hango.cloud.core.editor.ResourceType; import org.hango.cloud.core.plugin.FragmentHolder; -import org.hango.cloud.core.plugin.PluginInstance; -import org.hango.cloud.core.plugin.processor.SchemaProcessor; +import org.hango.cloud.core.plugin.processor.AggregateGatewayPluginProcessor; import org.hango.cloud.core.template.TemplateUtils; import org.hango.cloud.meta.Plugin; import org.hango.cloud.meta.PluginSupportConfig; @@ -26,8 +25,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import java.util.*; @@ -44,6 +41,8 @@ public class PluginServiceImpl implements PluginService { private static final String PLUGIN_CONFIG = "plugin/%s/plugin-config.json"; + public static final String CUSTOM_PLUGIN_CONFIG = "plugin/custom/%s.json"; + @Value("${pluginConfigEnv:route}") private String env; @@ -60,12 +59,11 @@ public class PluginServiceImpl implements PluginService { ObjectMapper objectMapper; @Autowired - private List processors; + private AggregateGatewayPluginProcessor processor; private static final String PLUGIN_MANAGER_TEMPLATE = "plugin/manager/plugin-manager-template.json"; private static final String PLUGIN_SUPPORT_CONFIG = "plugin/manager/plugin-support-config.json"; - @Override public Plugin getPlugin(String name) { return getPlugin(env, name); @@ -84,9 +82,10 @@ public String getSchema(String path) { return TemplateUtils.getTemplate(path, configuration).toString(); } + @Override public List processPlugin(List plugins, ServiceInfo serviceInfo) { - return processPlugin(env, plugins, serviceInfo); + return processor.process(plugins, serviceInfo); } @Override @@ -132,25 +131,6 @@ public PluginOrderDTO getPluginOrderTemplate(String gatewayKind) { return pluginOrderDTO; } - private List processPlugin(String env, List plugins, ServiceInfo serviceInfo) { - List ret = new ArrayList<>(); - - // 1. classify plugins - List totalPlugin = plugins.stream().map(PluginInstance::new).collect(Collectors.toList()); - MultiValueMap pluginMap = new LinkedMultiValueMap<>(); - totalPlugin.forEach(plugin -> pluginMap.add(getProcessor(getPlugin(env, plugin.getKind()).getProcessor()), plugin)); - - // 2. process plugins - Set processors = pluginMap.keySet(); - for (SchemaProcessor processor : processors) { - List classifiedPlugins = pluginMap.get(processor); - List pluginStrs = classifiedPlugins.stream().map(PluginInstance::jsonString).collect(Collectors.toList()); - logger.info("process multi processor :[{}], jsons :[{}], serviceInfo :[{}]", processor.getName(), pluginStrs, serviceInfo); - ret.addAll(processor.process(pluginStrs, serviceInfo)); - } - - return ret; - } private Plugin getPlugin(String env, String name) { Plugin p = getPlugins(env).get(name); @@ -178,14 +158,4 @@ private String getPluginConfig(String env) { } return TemplateUtils.getTemplate(String.format(PLUGIN_CONFIG, env), configuration).toString(); } - - private SchemaProcessor getProcessor(String name) { - logger.info("get processor:{}", name); - Optional processor = processors.stream().filter(item -> item.getName().equalsIgnoreCase(name)).findAny(); - if (!processor.isPresent()) { - throw new ApiPlaneException("can not resolve the schema processor of name:" + name); - } - return processor.get(); - } - } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/testServiceAuth.http b/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/testServiceAuth.http deleted file mode 100644 index 0359ce7c..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/service/impl/testServiceAuth.http +++ /dev/null @@ -1,19 +0,0 @@ -### -http://localhost:9090/api/istio/rbac?Version=2020-04-10&Action=EnableServiceAuth - -### - -POST http://localhost:9090/api/istio/rbac/v2?Version=2020-04-10&Action=UpdateServiceAuthRule -Content-Type: application/json - -{ - "Service": "sleep3.rbac-test-system", - "AuthRules": [ - { - "RuleName": "test1", - "MatchType": "service", - "MatchApis": "*:/anything/api1;*:/anything/api2;*:/anything/api3;", - "MatchConditions": "sleep1.rbac-test-system" - } - ] -} \ No newline at end of file diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/CommonUtil.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/CommonUtil.java index 54baea87..87aa1c28 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/CommonUtil.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/util/CommonUtil.java @@ -6,12 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; -import io.fabric8.kubernetes.api.model.HasMetadata; import org.apache.commons.lang3.StringUtils; import org.hango.cloud.core.editor.ResourceGenerator; -import org.hango.cloud.core.editor.ResourceType; -import org.hango.cloud.core.k8s.K8sResourceEnum; -import org.hango.cloud.core.k8s.K8sResourceGenerator; import org.hango.cloud.util.exception.ApiPlaneException; import org.hango.cloud.util.function.Equals; import org.slf4j.Logger; @@ -19,8 +15,10 @@ import org.springframework.util.CollectionUtils; import java.io.IOException; -import java.util.*; -import java.util.function.Supplier; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.regex.Pattern; @@ -154,11 +152,6 @@ public static List dropList(List oldL, Object identical, Equals eq) { return result; } - public static HasMetadata json2HasMetadata(String json) { - K8sResourceGenerator gen = K8sResourceGenerator.newInstance(json, ResourceType.JSON); - K8sResourceEnum resourceEnum = K8sResourceEnum.get(gen.getKind()); - return gen.object(resourceEnum.mappingType()); - } public static boolean isLuaPlugin(String plugin) { ResourceGenerator source = ResourceGenerator.newInstance(plugin); @@ -167,32 +160,6 @@ public static boolean isLuaPlugin(String plugin) { return "lua".equals(type) || "trace".equals(kind); } - public static T safelyGet(Supplier getter) { - try { - return getter.get(); - } catch (NullPointerException npe) { - return null; - } - } - - public static Optional safely(Supplier getter) { - return Optional.ofNullable(safelyGet(getter)); - } - - /** - * @param t - * @param - * @return - */ - public static T copy(T t) { - - try { - return (T) objectMapper.readValue(objectMapper.writeValueAsString(t), t.getClass()); - } catch (Exception e) { - logger.warn("copy object failed", e); - throw new RuntimeException(e); - } - } /** * 返回第n次出现的字符串的索引 @@ -221,31 +188,4 @@ public static int xIndexOf(String str, String occur, int order) { return str.indexOf(occur, xIndexOf(str, occur, order-1) + 1); } - /** - * 去除字符串末尾指定字符 - * - * @param remove - * @param origin - * @return - */ - public static String removeEnd(String remove, String origin) { - if (StringUtils.isAnyBlank(remove, origin)) { - return origin; - } - if (!StringUtils.endsWith(origin, remove)) { - return origin; - } - return removeEnd(remove, origin.substring(0, origin.length() - remove.length())); - } - - public static String toJSONString(Object object){ - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.writeValueAsString(object); - } catch (JsonProcessingException e) { - logger.error("parse json error"); - } - return ""; - } - } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/Const.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/Const.java index 39a00afe..1a49addb 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/Const.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/util/Const.java @@ -3,51 +3,42 @@ import java.util.Arrays; import java.util.List; -public interface Const { +public class Const { - String PROXY_SERVICE_TYPE_STATIC = "STATIC"; + public static final String PROXY_SERVICE_TYPE_STATIC = "STATIC"; - String PROXY_SERVICE_TYPE_DYNAMIC = "DYNAMIC"; + public static final String PROXY_SERVICE_TYPE_DYNAMIC = "DYNAMIC"; - String LABEL_NSF_ENV = "nsf-env"; - - String GATEWAY = "Gateway"; + public static final String GATEWAY = "Gateway"; //http 方法 - String GET_METHOD = "GET"; + public static final String GET_METHOD = "GET"; - String POST_METHOD = "POST"; + public static final String POST_METHOD = "POST"; // 默认请求头 - String INTERFACE_CALL_TYPE_INNER = "inner"; - String ACCEPT_LANGUAGE_ZH = "zh"; - - String OPTION_TRUE = "true"; - String SERVICE_TYPE_CONSUL = "Consul"; - String SERVICE_TYPE_K8S = "Kubernetes"; - String SERVICE_TYPE_DUBBO = "Zookeeper"; - String SERVICE_TYPE_EUREKA = "Eureka"; - - String SERVICE_TYPE_NACOS = "Nacos"; - List VAILD_REGISTRY = Arrays.asList(SERVICE_TYPE_CONSUL, SERVICE_TYPE_K8S, SERVICE_TYPE_DUBBO, SERVICE_TYPE_EUREKA,SERVICE_TYPE_NACOS); - - String PROTOCOL_DUBBO = "dubbo"; - String DUBBO_APPLICATION = "application"; - String DUBBO_TCP_PORT = "skiff_dubbo_tcp_port"; - String DUBBO_SERVICE_SUFFIX = ".dubbo"; - /** - * eureka服务endpoint的hosts后缀 - */ - String EUREKA_SERVICE_SUFFIX = ".eureka"; + public static final String INTERFACE_CALL_TYPE_INNER = "inner"; + public static final String ACCEPT_LANGUAGE_ZH = "zh"; + + public static final String OPTION_TRUE = "true"; + public static final String SERVICE_TYPE_CONSUL = "Consul"; + public static final String SERVICE_TYPE_K8S = "Kubernetes"; + public static final String SERVICE_TYPE_DUBBO = "Zookeeper"; + public static final String SERVICE_TYPE_EUREKA = "Eureka"; - String SIDECAR_CONTAINER = "istio-proxy"; + public static final String SERVICE_TYPE_NACOS = "Nacos"; + public static final List VAILD_REGISTRY = Arrays.asList(SERVICE_TYPE_CONSUL, SERVICE_TYPE_K8S, SERVICE_TYPE_DUBBO, SERVICE_TYPE_EUREKA,SERVICE_TYPE_NACOS); - String WORKLOAD_UPDATE_TIME_ANNOTATION = "nsf_workload_update_time"; - String WORKLOAD_OPERATION_TYPE_ANNOTATION = "nsf_workload_operation_type"; - String CONTAINER_STATUS_RUNNING = "Running"; - String CONTAINER_STATUS_WAITING = "Waiting"; - String CONTAINER_STATUS_TERMINATED = "Terminated"; + public static final String PROTOCOL_DUBBO = "dubbo"; + public static final String DUBBO_APPLICATION = "application"; + public static final String DUBBO_TCP_PORT = "skiff_dubbo_tcp_port"; + public static final String DUBBO_SERVICE_SUFFIX = ".dubbo"; + + public static final String PROTOCOL_HTTP = "HTTP"; + public static final String PROTOCOL_HTTPS = "HTTPS"; + public static final String PROTOCOL_TCP = "TCP"; + public static final String PROTOCOL_UDP = "UDP"; /** * 查询服务接口的过滤条件前缀字符 @@ -57,32 +48,57 @@ public interface Const { public static final String PREFIX_ADDRESS = "address_"; public static final String PREFIX_PORT = "port_"; public static final String PREFIX_PROTOCOL = "protocol_"; - String PROJECT_LABEL = "label_projectCode"; + public static final String PROJECT_LABEL = "label_projectCode"; - String PROJECT_CODE = "projectCode"; + public static final String PROJECT_CODE = "projectCode"; - /** - * envoy outbound prefix - */ - String OUTBOUND = "outbound"; /** * 认证类型 */ - String AUTH_JWKS = "Jwks"; + public static final String AUTH_JWKS = "Jwks"; /** * 以下是envoy filter全名 */ - String JWT_FILTER = "envoy.filters.http.jwt_authn"; + public static final String JWT_FILTER = "envoy.filters.http.jwt_authn"; /** * RBAC插件身份信息存储 filter */ - String RBAC_IDENTITY_FILTER = "envoy.filters.http.rbac_identity"; + public static final String RBAC_IDENTITY_FILTER = "envoy.filters.http.rbac_identity"; /** * 项目隔离服务host正则表达式 */ - String SERVICE_PROJECT_ISOLATION_FORMAT = "^.+\\.nsf\\..+\\.(eureka|nacos)$"; + public static final String SERVICE_PROJECT_ISOLATION_FORMAT = "^.+\\.nsf\\..+\\.(eureka|nacos)$"; + + /** + * 插件配置路径前缀 + */ + public static final String PLUGIN_PATH_PREFIX = "plugin/route/"; + + public static final String PLUGIN_MANAGER_TEMPLATE = "pluginmanager-template.json"; + + /** + * 插件处理器 + */ + public static final String PLUGIN_PROCESSOR = "AggregateGatewayPluginProcessor"; + + /** + * 自定义插件默认作者 + */ + public static final String SYSTEM = "system"; + + /** + * 文件 + */ + public static final String FILE = "file"; + + + public static final String LUA = "lua"; + + public static final String RIDER_PLUGIN = "proxy.filters.http.rider"; + + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/HandlerUtil.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/HandlerUtil.java index 913b2b82..0014803c 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/HandlerUtil.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/util/HandlerUtil.java @@ -5,65 +5,29 @@ import org.hango.cloud.meta.GatewayPlugin; import org.hango.cloud.util.constant.PluginConstant; import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.stream.Collectors; public class HandlerUtil { - /** - * 分配插件 - * - * @param fragments - * @param matchPlugins - * @param apiPlugins - * @param hostPlugins - */ - public static void distributePlugins(List fragments, - List matchPlugins, - Map> apiPlugins, - List hostPlugins) { - fragments.stream() - .forEach(f -> { - switch (f.getFragmentType()) { - case VS_MATCH: - matchPlugins.add(f.getContent()); - break; - case VS_API: - String userId = StringUtils.isEmpty(f.getXUserId()) ? - PluginConstant.DEFAULT_USER_ID : f.getXUserId(); - apiPlugins.computeIfAbsent(userId, k -> new ArrayList<>()).add(f.getContent()); - break; - case VS_HOST: - hostPlugins.add(f.getContent()); - break; - default: - } - }); - } - /** * 获取插件Map * * @param fragments CRD片段 - * @return 以用户ID为key,插件CRD文本集合为value的Map对象 */ - public static Map> getGatewayPlugins(List fragments) { + public static List getGatewayPlugins(List fragments) { if (CollectionUtils.isEmpty(fragments)) { - return Collections.EMPTY_MAP; + return new ArrayList<>(); } - - Map> pluginMap = new HashMap<>(); - fragments.stream() - .filter(f -> f.getFragmentType().equals(FragmentTypeEnum.VS_API)) - .forEach(f -> { - String userId = - StringUtils.isEmpty(f.getXUserId()) ? PluginConstant.DEFAULT_USER_ID : f.getXUserId(); - pluginMap.computeIfAbsent(userId, k -> new ArrayList<>()).add(f.getContent()); - }); - return pluginMap; + return fragments.stream() + .filter(f -> f.getFragmentType().equals(FragmentTypeEnum.ENVOY_PLUGIN)) + .map(FragmentWrapper::getContent) + .distinct() + .collect(Collectors.toList()); } /** diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/Trans.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/Trans.java index 9f6ae15b..7f410375 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/Trans.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/util/Trans.java @@ -1,6 +1,9 @@ package org.hango.cloud.util; import com.google.common.collect.ImmutableList; +import com.google.protobuf.Value; +import io.fabric8.kubernetes.api.model.ServicePort; +import org.apache.logging.log4j.util.Strings; import org.hango.cloud.core.editor.ResourceGenerator; import org.hango.cloud.core.editor.ResourceType; import org.hango.cloud.k8s.K8sTypes; @@ -15,14 +18,11 @@ import java.util.*; import java.util.stream.Collectors; - -public class Trans { - - public static final String HTTP = "HTTP"; - public static final String HTTPS = "HTTPS"; - public static final int DEFAULT_PORT = 80; +import static org.hango.cloud.service.impl.GatewayServiceImpl.GW_CLUSTER; +import static org.hango.cloud.util.Const.RIDER_PLUGIN; +public class Trans { public static API portalAPI2API(PortalAPIDTO portalAPI) { API api = new API(); @@ -52,10 +52,6 @@ public static API portalAPI2API(PortalAPIDTO portalAPI) { api.setRetryOn(portalAPI.getHttpRetry().getRetryOn()); } api.setRequestOperation(requestOperationDTO2requestOperation(portalAPI.getRequestOperation())); - if (portalAPI.getVirtualClusterDTO() != null){ - api.setVirtualClusterName(portalAPI.getVirtualClusterDTO().getVirtualClusterName()); - api.setVirtualClusterHeaders(pairsDTO2Pairs(portalAPI.getVirtualClusterDTO().getHeaders())); - } //流量镜像配置 if(portalAPI.getMirrorTrafficDto() != null){ PortalMirrorTrafficDto mirrorTrafficDto = portalAPI.getMirrorTrafficDto(); @@ -69,6 +65,16 @@ public static API portalAPI2API(PortalAPIDTO portalAPI) { return api; } + public static EnvoyServicePortDTO getPorts(ServicePort port){ + EnvoyServicePortDTO envoyServicePortDTO = new EnvoyServicePortDTO(); + envoyServicePortDTO.setName(port.getName()); + envoyServicePortDTO.setProtocol(port.getProtocol()); + envoyServicePortDTO.setPort(port.getPort()); + envoyServicePortDTO.setNodePort(port.getNodePort()); + return envoyServicePortDTO; + } + + private static RequestOperation requestOperationDTO2requestOperation( RequestOperationDTO requestOperation) { if (requestOperation == null) return null; @@ -96,7 +102,7 @@ public static IstioGateway portalGW2GW(PortalIstioGatewayDTO portalGateway) { istioGatewayServer.setNumber(server.getNumber()); istioGatewayServer.setHosts(server.getHosts()); PortalIstioGatewayTLSDTO portalIstioGatewayTLSDTO = server.getPortalIstioGatewayTLSDTO(); - if (HTTPS.equals(server.getProtocol()) && portalIstioGatewayTLSDTO != null){ + if (Const.PROTOCOL_HTTPS.equals(server.getProtocol()) && portalIstioGatewayTLSDTO != null){ IstioGatewayTLS istioGatewayTLS = new IstioGatewayTLS(); istioGatewayTLS.setMode(portalIstioGatewayTLSDTO.getMode()); istioGatewayTLS.setCredentialName(portalIstioGatewayTLSDTO.getCredentialName()); @@ -176,6 +182,7 @@ public static Service portalService2Service(PortalServiceDTO portalService) { s.setServiceTag(portalService.getServiceTag()); s.setSubsets(subsetDTO2Subset(portalService.getSubsets())); + s.setMetaMap(portalService.getMetaMap()); return s; } @@ -219,6 +226,9 @@ private static List subsetDTO2Subset(List subse ss.setName(sd.getName()); ss.setTrafficPolicy(subsetTrafficPolicyDtoTosubsetTrafficPolicy(sd.getTrafficPolicy())); ss.setStaticAddrs(sd.getStaticAddrs()); + if(!CollectionUtils.isEmpty(sd.getMetaMap())){ + ss.setMetaLabelMap(sd.getMetaMap().get(CRDMetaEnum.DESTINATION_RULE_STATS_META.getName())); + } return ss; }) .collect(Collectors.toList()); @@ -247,7 +257,9 @@ private static List subsetDTO2Subset(List subse public static PluginOrder pluginOrderDTO2PluginOrder(PluginOrderDTO pluginOrderDTO) { PluginOrder po = new PluginOrder(); - po.setGatewayLabels(pluginOrderDTO.getGatewayLabels()); + Map gatewayLabels = new HashMap<>(); + gatewayLabels.put(GW_CLUSTER, pluginOrderDTO.getGwCluster()); + po.setGatewayLabels(gatewayLabels); po.setName(pluginOrderDTO.getName()); List orderItems = new ArrayList<>(); List plugins = pluginOrderDTO.getPlugins(); @@ -266,9 +278,32 @@ public static PluginOrder pluginOrderDTO2PluginOrder(PluginOrderDTO pluginOrderD return po; } + public static String getPluginName(PluginOrderItemDTO itemDTO){ + if (itemDTO == null){ + return Strings.EMPTY; + } + String name = itemDTO.getName(); + if (!RIDER_PLUGIN.equals(name)){ + return name; + } + Object inlineObj = itemDTO.getInline(); + if (!(inlineObj instanceof PluginManagerOuterClass.Inline)){ + return Strings.EMPTY; + } + PluginManagerOuterClass.Inline inline = (PluginManagerOuterClass.Inline) inlineObj; + Map fieldsMap = inline.getSettings().getFieldsMap(); + Value plugin = fieldsMap.get("plugin"); + if (plugin == null){ + return Strings.EMPTY; + } + Value pluginName = plugin.getStructValue().getFieldsMap().get("name"); + return pluginName == null ? Strings.EMPTY : pluginName.getStringValue(); + } + public static PluginOrderDTO trans(K8sTypes.PluginManager pluginManager){ PluginOrderDTO dto = new PluginOrderDTO(); - dto.setGatewayLabels(pluginManager.getSpec().getWorkloadLabels()); + Map workloadLabels = pluginManager.getSpec().getWorkloadLabels(); + dto.setGwCluster(workloadLabels.get(GW_CLUSTER)); dto.setPlugins(new ArrayList<>()); List plugins = pluginManager.getSpec().getPluginList(); if (CollectionUtils.isEmpty(plugins)) { @@ -333,14 +368,6 @@ public static API portalDeleteAPI2API(PortalAPIDeleteDTO portalAPI) { return api; } - public static ValidateResultDTO validateResult2ValidateResultDTO(ValidateResult validateResult) { - ValidateResultDTO dto = new ValidateResultDTO(); - dto.setPass(validateResult.isPass()); - if (validateResult.getItems() != null) { - dto.setItems(validateResult.getItems()); - } - return dto; - } /** * 插件DTO对象转业务流转对象 @@ -362,4 +389,13 @@ public static GatewayPlugin pluginDTOToPlugin(GatewayPluginDTO dto) { return gatewayPlugin; } + + public static String getSchemaPath(String pluginName){ + return pluginName + ".json"; + } + + public static String getCustomCodePath(String pluginName, String language){ + return pluginName + "." + language; + } + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/errorcode/ApiPlaneErrorCode.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/errorcode/ApiPlaneErrorCode.java index 062dc047..75c76c85 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/errorcode/ApiPlaneErrorCode.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/util/errorcode/ApiPlaneErrorCode.java @@ -80,5 +80,7 @@ private static ErrorCode workLoadNotInMesh() { public static ErrorCode InvalidMaxRequestsPerConnection = new ErrorCode(ErrorCodeEnum.InvalidMaxRequestsPerConnection); public static ErrorCode InvalidMaxConnections = new ErrorCode(ErrorCodeEnum.InvalidMaxConnections); public static ErrorCode InvalidConnectTimeout = new ErrorCode(ErrorCodeEnum.InvalidConnectTimeout); + public static ErrorCode PluginOrderPortError = new ErrorCode(ErrorCodeEnum.PluginOrderPortError); + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/errorcode/ErrorCodeEnum.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/errorcode/ErrorCodeEnum.java index 22adc1a8..867f688b 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/errorcode/ErrorCodeEnum.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/util/errorcode/ErrorCodeEnum.java @@ -46,6 +46,7 @@ public enum ErrorCodeEnum { NoSuchAPI("NoSuchApi", "No such api:%s.", "没有请求的API:%s", 404), + PluginOrderPortError("PluginOrderPortError", "PluginOrder Port Error", "pluginmanager端口冲突", 400), ConfigAlreadExist("ConfigAlreadyExist", "The configuration you provided is already exist.", "配置已存在", 400), TargetAlreadExist("ConfigAlreadyExist", "The target service for rule is already exist.", "该目标服务已匹配治理规则", 400), diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/exception/ExceptionConst.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/exception/ExceptionConst.java index 4c8b85c0..1f775ae7 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/exception/ExceptionConst.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/util/exception/ExceptionConst.java @@ -1,26 +1,13 @@ package org.hango.cloud.util.exception; -public interface ExceptionConst { - - String ISTIO_POD_NON_EXIST = "Istio pod is non-exist"; - String PILOT_SERVICE_NON_EXIST = "Pilot service is non-exist"; - String GALLEY_SERVICE_NON_EXIST = "Galley service is non-exist"; - String ENVOY_POD_NON_EXIST = "Envoy pod is non-exist"; - String RESOURCE_NON_EXIST = "Resource is non-exist"; - String K8S_SERVICE_NON_EXIST = "k8s service is non-exist"; - String SERVICE_NON_EXIST = "service is non-exist"; - String API_NON_EXIST = "api is non-exist"; - String RESOURCE_KIND_MISMATCH = "Resource kind is mismatch"; - String RESOURCES_DIFF_IDENTITY = "Resources have different identities"; - String GATEWAY_LIST_EMPTY = "gateway list is empty"; - String PROXY_URI_LIST_EMPTY = "proxy uri list is empty"; - - String TARGET_SERVICE_NON_EXIST = "target service is non-exist"; - - String ENDPOINT_LIST_EMPTY = "endpoint list is empty"; - - String UNSUPPORTED_RESOURCE_TYPE = "unsupported resource type"; - - String UNSUPPORTED_OPERATION = "unsupported operation"; +public class ExceptionConst { + + public static String PILOT_SERVICE_NON_EXIST = "Pilot service is non-exist"; + public static String ENVOY_POD_NON_EXIST = "Envoy pod is non-exist"; + public static String ENVOY_SERVICE_NON_EXIST = "Envoy service is non-exist"; + public static String RESOURCE_NON_EXIST = "Resource is non-exist"; + public static String RESOURCES_DIFF_IDENTITY = "Resources have different identities"; + public static String TARGET_SERVICE_NON_EXIST = "target service is non-exist"; + public static String UNSUPPORTED_RESOURCE_TYPE = "unsupported resource type"; } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/ConditionalTemplateValidator.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/ConditionalTemplateValidator.java deleted file mode 100644 index 0e758a7b..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/ConditionalTemplateValidator.java +++ /dev/null @@ -1,85 +0,0 @@ -package org.hango.cloud.util.validator; - -import org.hango.cloud.meta.template.NsfExtra; -import org.hango.cloud.meta.template.ServiceMeshTemplate; -import org.hango.cloud.util.validator.annotation.ConditionalTemplate; -import org.apache.commons.beanutils.BeanUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.util.StringUtils; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.util.Collection; -import java.util.Objects; - -/** - * 用于校验,当模板为template时,所必需的的字段为required数组中的值 - * - * - **/ -public class ConditionalTemplateValidator implements ConstraintValidator { - - private static final Logger logger = LoggerFactory.getLogger(ConditionalTemplateValidator.class); - - private String template; - - private String[] required; - - @Override - public void initialize(ConditionalTemplate constraintAnnotation) { - this.template = constraintAnnotation.templateName(); - this.required = constraintAnnotation.required(); - } - - @Override - public boolean isValid(Object value, ConstraintValidatorContext context) { - - boolean valid = true; - - try { - if (!template.equals(BeanUtils.getProperty(value, "nsfTemplate"))) return true; - if (value instanceof ServiceMeshTemplate) { - ServiceMeshTemplate template = (ServiceMeshTemplate) value; - for (String req : required) { - Field field = NsfExtra.class.getDeclaredField(req); - field.setAccessible(true); - Object requiredProp = field.get(template.getNsfExtra()); - valid = Objects.nonNull(requiredProp) && !StringUtils.isEmpty(requiredProp) && !isObjectEmpty(requiredProp); - if (!valid) { - context.disableDefaultConstraintViolation(); - context.buildConstraintViolationWithTemplate(String.format("parameter %s is missing", req)) - .addConstraintViolation(); - return false; - } - } - } - - } catch (IllegalAccessException e) { - logger.error("Accessor method is not available for class : {}, exception : {}", value.getClass().getName(), e); - return false; - } catch (InvocationTargetException e) { - logger.error("Field or method is not present on class : {}, exception : {}", value.getClass().getName(), e); - return false; - } catch (NoSuchMethodException e) { - logger.error("An exception occurred while accessing class : {}, exception : {}", value.getClass().getName(), e); - return false; - } catch (NoSuchFieldException e) { - logger.error("Field is not present on class : {}, exception : {}", value.getClass().getName(), e); - return false; - } - - return valid; - } - - private boolean isObjectEmpty(Object o) { - - if (o instanceof Collection) { - Collection coll = (Collection) o; - return coll.size() == 0; - } - return false; - } -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/annotation/ConditionalTemplate.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/annotation/ConditionalTemplate.java deleted file mode 100644 index c49d6b26..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/annotation/ConditionalTemplate.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.hango.cloud.util.validator.annotation; - -import org.hango.cloud.util.validator.ConditionalTemplateValidator; - -import javax.validation.Constraint; -import javax.validation.Payload; -import java.lang.annotation.*; - -@Repeatable(ConditionalTemplates.class) -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Constraint(validatedBy = {ConditionalTemplateValidator.class}) -public @interface ConditionalTemplate { - - String message() default "This field is required."; - Class[] groups() default {}; - Class[] payload() default {}; - - String templateName(); - String[] required(); -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/annotation/ConditionalTemplates.java b/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/annotation/ConditionalTemplates.java deleted file mode 100644 index 23c71d23..00000000 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/util/validator/annotation/ConditionalTemplates.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.hango.cloud.util.validator.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) -public @interface ConditionalTemplates { - ConditionalTemplate[] value(); -} diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/GatewayCommonController.java b/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/GatewayCommonController.java index ae655c9e..916ca141 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/GatewayCommonController.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/GatewayCommonController.java @@ -88,4 +88,12 @@ public String getRegistryList() { result.put(RESULT, gatewayService.getRegistryList()); return apiReturn(result); } + + @RequestMapping(params = "Action=GetEnvoyService", method = RequestMethod.GET) + public String getEnvoyService(@RequestParam(name = "GwCluster") String gwCluster) { + Map result = new HashMap<>(); + result.put(RESULT, gatewayService.getEnvoyAddress(gwCluster)); + return apiReturn(result); + } + } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/GatewayPluginController.java b/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/GatewayPluginController.java index 6dd1e877..46a1d9a0 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/GatewayPluginController.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/GatewayPluginController.java @@ -5,6 +5,7 @@ import org.hango.cloud.core.editor.ResourceGenerator; import org.hango.cloud.meta.Plugin; import org.hango.cloud.meta.PluginSupportDetail; +import org.hango.cloud.meta.dto.CustomPluginDTO; import org.hango.cloud.meta.dto.PluginOrderDTO; import org.hango.cloud.service.GatewayService; import org.hango.cloud.service.PluginService; @@ -23,6 +24,7 @@ * @auther wupenghuai@corp.netease.com * @date 2019/8/2 **/ +@SuppressWarnings("java:S1192") @RestController @RequestMapping(value = "/api/plugin", params = "Version=2019-07-25") public class GatewayPluginController extends BaseController { @@ -61,6 +63,10 @@ public String getPlugins(@RequestParam(name = "GatewayKind", required = false, d return apiReturn(ImmutableMap.of("Plugins", Collections.emptyList())); } Map pluginMap = pluginService.getPlugins(); + if ( CollectionUtils.isEmpty(pluginMap)) { + return apiReturn(ImmutableMap.of("Plugins", Collections.emptyList())); + } + List plugins = pluginSupportDetails.stream() .map(PluginSupportDetail::getSchema) .filter(pluginMap::containsKey) @@ -89,20 +95,47 @@ public String getPluginOrder(@RequestBody PluginOrderDTO pluginOrderDTO) { */ @RequestMapping(params = "Action=PublishPluginOrder", method = RequestMethod.POST) public String publishPluginOrder(@RequestBody @Valid PluginOrderDTO pluginOrderDTO) { + boolean result = gatewayService.pluginOrderPortCheck(pluginOrderDTO); + if (!result){ + return apiReturn(ApiPlaneErrorCode.PluginOrderPortError); + } gatewayService.publishPluginOrder(pluginOrderDTO); return apiReturn(ApiPlaneErrorCode.Success); } /** - * 更新plm + * 更新plm item */ - @RequestMapping(params = "Action=UpdatePluginOrder", method = RequestMethod.POST) - public String updatePluginOrder(@RequestBody @Valid PluginOrderDTO pluginOrderDTO) { + @RequestMapping(params = "Action=UpdatePluginOrderItem", method = RequestMethod.POST) + public String updatePluginOrder(@RequestBody PluginOrderDTO pluginOrderDTO) { gatewayService.updatePluginOrder(pluginOrderDTO); return apiReturn(ApiPlaneErrorCode.Success); } + + /** + * 发布自定义插件 + */ + @RequestMapping(params = "Action=PublishCustomPlugin", method = RequestMethod.POST) + public String publishCustomPlugin(@RequestBody @Valid CustomPluginDTO customPluginDTO) { + Map result = new HashMap<>(); + result.put(RESULT, gatewayService.publishCustomPlugin(customPluginDTO)); + ErrorCode code = ApiPlaneErrorCode.Success; + return apiReturn(code.getStatusCode(), code.getCode(), null, result); + } + + /** + * 删除自定义插件 + */ + @RequestMapping(params = "Action=DeleteCustomPlugin",method = RequestMethod.POST) + public String deleteCustomPlugin(@RequestBody CustomPluginDTO customPluginDTO) { + Map result = new HashMap<>(); + result.put(RESULT, gatewayService.deleteCustomPlugin(customPluginDTO.getPluginName(), customPluginDTO.getLanguage())); + ErrorCode code = ApiPlaneErrorCode.Success; + return apiReturn(code.getStatusCode(), code.getCode(), null, result); + } + /** * 删除plm * @param pluginOrderDTO @@ -112,5 +145,4 @@ public String deletePluginOrder(@RequestBody PluginOrderDTO pluginOrderDTO) { gatewayService.deletePluginOrder(pluginOrderDTO); return apiReturn(ApiPlaneErrorCode.Success); } - } diff --git a/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/KubernetesGatewayController.java b/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/KubernetesGatewayController.java index f88d6363..f5341806 100644 --- a/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/KubernetesGatewayController.java +++ b/hango-api-plane-server/src/main/java/org/hango/cloud/web/controller/KubernetesGatewayController.java @@ -1,6 +1,7 @@ package org.hango.cloud.web.controller; import io.fabric8.kubernetes.api.model.gatewayapi.v1beta1.HTTPRoute; +import org.hango.cloud.meta.dto.IngressDTO; import org.hango.cloud.meta.dto.KubernetesGatewayDTO; import org.hango.cloud.service.KubernetesGatewayService; import org.hango.cloud.util.errorcode.ApiPlaneErrorCode; @@ -20,7 +21,7 @@ * @Date 2022/12/2 */ @RestController -@RequestMapping(value = "/api", params = "Version=2022-12-31") +@RequestMapping(value = "/api") public class KubernetesGatewayController extends BaseController{ @Autowired @@ -45,4 +46,14 @@ public String getKubernetesGateway(@RequestParam(name = "GatewayName", required ErrorCode code = ApiPlaneErrorCode.Success; return apiReturn(code.getStatusCode(), code.getCode(), null, result); } + + @RequestMapping(value = "/ingress", params = "Action=GetIngress", method = RequestMethod.GET) + public String getIngress(@RequestParam(name = "GatewayName", required = false) String gateway, + @RequestParam(name = "Namespace", required = false) String namespace) { + Map result = new HashMap<>(); + List kubernetesGateway = kubernetesGatewayService.getIngress(namespace, gateway); + result.put(RESULT_LIST, kubernetesGateway); + ErrorCode code = ApiPlaneErrorCode.Success; + return apiReturn(code.getStatusCode(), code.getCode(), null, result); + } } diff --git a/hango-api-plane-server/src/main/resources/mesh-config.properties b/hango-api-plane-server/src/main/resources/mesh-config.properties deleted file mode 100644 index 9549d1a3..00000000 --- a/hango-api-plane-server/src/main/resources/mesh-config.properties +++ /dev/null @@ -1,3 +0,0 @@ -meshProjectKey=hango.org/project -meshVersionKey=hango.org/version -meshAppKey=hango.org/app \ No newline at end of file diff --git a/hango-api-plane-server/src/main/resources/template/cloudShuttle.ftl b/hango-api-plane-server/src/main/resources/template/cloudShuttle.ftl deleted file mode 100644 index 10e27b22..00000000 --- a/hango-api-plane-server/src/main/resources/template/cloudShuttle.ftl +++ /dev/null @@ -1,55 +0,0 @@ -# 严选云内外分流 ---- -apiVersion: networking.istio.io/v1alpha3 -kind: VirtualService -<#include "common/metadata.ftl"/> -spec: - hosts: - - yx-provider - http: - - route: - - destination: - host: ${metadata.name} - subset: internal - weight: ${100 - nsfExtra.outWeight!0} - - destination: - host: qz-egress.qz.svc.cluster.local - weight: ${nsfExtra.outWeight!0} ---- -apiVersion: networking.istio.io/v1alpha3 -kind: DestinationRule -<#include "common/metadata.ftl"/> -spec: - host: ${metadata.name} - subsets: - - name: internal - labels: - app: ${metadata.name} ---- -apiVersion: rbac.istio.io/v1alpha1 -kind: ServiceRole -metadata: - name: ${metadata.name}-${metadata.namespace} - namespace: qz -spec: - rules: - - services: - - qz-egress.qz.svc.cluster.local - methods: - - GET - - HEAD - constraints: - - key: "request.headers[:authority]" - values: ["${metadata.name}"] ---- -apiVersion: rbac.istio.io/v1alpha1 -kind: ServiceRoleBinding -metadata: - name: ${metadata.name}-${metadata.namespace}-whitelist - namespace: qz -spec: - subjects: - - user: "*" - roleRef: - kind: ServiceRole - name: ${metadata.name}-${metadata.namespace} diff --git a/hango-api-plane-server/src/main/resources/template/faultInject.ftl b/hango-api-plane-server/src/main/resources/template/faultInject.ftl deleted file mode 100644 index ba106093..00000000 --- a/hango-api-plane-server/src/main/resources/template/faultInject.ftl +++ /dev/null @@ -1,24 +0,0 @@ -<#--deprecated--> -apiVersion: networking.istio.io/v1alpha3 -kind: VirtualService -<#include "common/metadata.ftl"/> -spec: - hosts: - - ${nsfExtra.host!} - http: - - route: -<#list nsfExtra.destinations! as des> - - destination: - host: ${nsfExtra.host!} - subset: ${des.subset!} - - fault: -<#if nsfExtra.fixedDelay??> - delay: - percent: ${nsfExtra.percent!} - fixedDelay: ${nsfExtra.fixedDelay!} -<#else> - abort: - percent: ${nsfExtra.percent!} - httpStatus: ${nsfExtra.httpStatus!} - diff --git a/hango-api-plane-server/src/main/resources/template/gateway/api/virtualService.ftl b/hango-api-plane-server/src/main/resources/template/gateway/api/virtualService.ftl index a3318c2d..9e95a455 100644 --- a/hango-api-plane-server/src/main/resources/template/gateway/api/virtualService.ftl +++ b/hango-api-plane-server/src/main/resources/template/gateway/api/virtualService.ftl @@ -14,41 +14,8 @@ spec: <#list t_virtual_service_hosts as host> - "${host}" -<#if t_virtual_service_virtual_cluster_name?has_content> - virtualCluster: - - headers: -<#if t_virtual_service_virtual_cluster_headers?has_content> -<#list t_virtual_service_virtual_cluster_headers as h> - ${h.key}: - ${h.type}: "${h.value?j_string}" - - - name: ${t_virtual_service_virtual_cluster_name} - -<#if t_api_host_plugins?has_content> -<#list t_api_host_plugins as p> -<@indent count=2>${p} - - http: -<#list t_api_match_plugins as p> -<@indent count=2><@supply>${p} - <@indent count=2><@supply> -<#if t_api_api_plugins?has_content> - plugins: - ${t_api_name}: - userPlugin: -<#list t_api_api_plugins?keys as userId> - - -<#if userId?has_content> - user: ${userId} - -<#list t_api_api_plugins[userId] as p> -<@indent count=8>${p} - - - <#if t_api_priority??> priority: ${t_api_priority} diff --git a/hango-api-plane-server/src/main/resources/template/gateway/gatewayPlugin.ftl b/hango-api-plane-server/src/main/resources/template/gateway/gatewayPlugin.ftl index 78a27eec..ffc8d3b7 100644 --- a/hango-api-plane-server/src/main/resources/template/gateway/gatewayPlugin.ftl +++ b/hango-api-plane-server/src/main/resources/template/gateway/gatewayPlugin.ftl @@ -26,21 +26,10 @@ spec: route: - "${t_gateway_plugin_route}" -<#if t_gateway_plugin_users?has_content> - user: -<#list t_gateway_plugin_users as u> - - ${u} - - <#if t_gateway_plugin_plugins?has_content> plugins: - <#list t_gateway_plugin_plugins as k,plugins> - <#list plugins as p> + <#list t_gateway_plugin_plugins as p> - -<#if k?has_content> - user: ${k} - <@indent count=6>${p} - \ No newline at end of file diff --git a/hango-api-plane-server/src/main/resources/template/gateway/service/destinationRule.ftl b/hango-api-plane-server/src/main/resources/template/gateway/service/destinationRule.ftl index 662497c5..a928b935 100644 --- a/hango-api-plane-server/src/main/resources/template/gateway/service/destinationRule.ftl +++ b/hango-api-plane-server/src/main/resources/template/gateway/service/destinationRule.ftl @@ -9,6 +9,13 @@ metadata: spec: host: ${t_destination_rule_host} altStatName: ${t_destination_rule_alt_stat_name} +<#if t_destination_rule_metadata_hub?has_content> + metadata: + LABELS: + <#list t_destination_rule_metadata_hub?keys as tagKey> + "${tagKey}": "${t_destination_rule_metadata_hub[tagKey]}" + + <#--- 默认所有subset都继承同一份trafficPolicy ---> subsets: <#--- 默认生成的subset ---> @@ -16,6 +23,13 @@ spec: altStatName: ${t_destination_rule_alt_stat_name} gwLabels: gw_cluster: ${t_api_gateway} +<#if t_destination_rule_metadata_hub?has_content> + metadata: + LABELS: + <#list t_destination_rule_metadata_hub?keys as tagKey> + "${tagKey}": "${t_destination_rule_metadata_hub[tagKey]}" + + <@indent count=4><@autoremove><#include "destinationRule_trafficPolicy.ftl"/> <#--- 自定义的subset ---> <#if t_destination_rule_extra_subsets?has_content> @@ -28,6 +42,13 @@ spec: labels: <#list ss.labels?keys as k> ${k}: ${ss.labels[k]} + <#if ss.metaLabelMap?has_content> + metadata: + LABELS: + <#list ss.metaLabelMap?keys as tagKey> + "${tagKey}": "${ss.metaLabelMap[tagKey]}" + + <#if ss.trafficPolicy?has_content> diff --git a/hango-api-plane-server/src/main/resources/template/loadBalancing.ftl b/hango-api-plane-server/src/main/resources/template/loadBalancing.ftl deleted file mode 100644 index d2b7c992..00000000 --- a/hango-api-plane-server/src/main/resources/template/loadBalancing.ftl +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: networking.istio.io/v1alpha3 -kind: DestinationRule -<#include "common/metadata.ftl"/> -spec: - host: ${nsfExtra.host!} - trafficPolicy: - loadBalancer: - simple: ${nsfExtra.simple!} diff --git a/hango-api-plane-server/src/main/resources/template/matchRouting.ftl b/hango-api-plane-server/src/main/resources/template/matchRouting.ftl deleted file mode 100644 index a27c5ada..00000000 --- a/hango-api-plane-server/src/main/resources/template/matchRouting.ftl +++ /dev/null @@ -1,24 +0,0 @@ -apiVersion: networking.istio.io/v1alpha3 -kind: VirtualService -<#include "common/metadata.ftl"/> -spec: - hosts: - - ${nsfExtra.host!} - http: - - match: - - headers: - <#list nsfExtra.headers! as header> - ${header.header}: - ${header.headerMatch?keys[0]}: ${header.headerMatch?values[0]} - - <#if nsfExtra.uri ??> - uri: - ${nsfExtra.uri.urlMatch?keys[0]}: ${nsfExtra.uri.urlMatch?values[0]} - - - route: - <#list nsfExtra.destinations! as des> - - destination: - host: ${nsfExtra.host!} - subset: ${des.subset!} - weight: ${des.weight!} - \ No newline at end of file diff --git a/hango-api-plane-server/src/main/resources/template/outlierDetection.ftl b/hango-api-plane-server/src/main/resources/template/outlierDetection.ftl deleted file mode 100644 index 225b8e64..00000000 --- a/hango-api-plane-server/src/main/resources/template/outlierDetection.ftl +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: networking.istio.io/v1alpha3 -kind: DestinationRule -<#include "common/metadata.ftl"/> -spec: - host: ${nsfExtra.host!} - trafficPolicy: - outlierDetection: - consecutiveErrors: ${nsfExtra.consecutiveErrors!} - interval: ${nsfExtra.interval!} - baseEjectionTime: ${nsfExtra.baseEjectionTime!} - maxEjectionPercent: ${nsfExtra.maxEjectionPercent!} \ No newline at end of file diff --git a/hango-api-plane-server/src/main/resources/template/plugin/cm/plugin-config.json b/hango-api-plane-server/src/main/resources/template/plugin/cm/plugin-config.json deleted file mode 100644 index 54a33913..00000000 --- a/hango-api-plane-server/src/main/resources/template/plugin/cm/plugin-config.json +++ /dev/null @@ -1,324 +0,0 @@ -{ - "item": [ - { - "name": "ianus-percent-limit", - "displayName": "百分比限流", - "schema": "plugin/portal/ianus-percent-limit.json", - "description": "百分比限流插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule", - "pluginPriority": "500", - "instructionForUse": "按百分比限流,例如允许50%流量通过", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "" - }, - { - "name": "rate-limiting", - "displayName": "集群限流", - "schema": "plugin/portal/rate-limiting.json", - "description": "频控插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule", - "pluginPriority": "1500", - "instructionForUse": "请求频率限流,限制服务请求流量", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "" - }, - { - "name": "ianus-router", - "displayName": "路由", - "schema": "plugin/portal/ianus-router.json", - "description": "路由插件", - "processor": "RouteProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule", - "pluginPriority": "3500", - "instructionForUse": "请求中断返回,支持定义响应码和响应Body", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "" - }, - { - "name": "ip-restriction", - "displayName": "IP黑白名单", - "schema": "plugin/portal/ip-restriction.json", - "description": "黑白名单插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule,global", - "pluginPriority": "1000", - "instructionForUse": "根据ip过滤请求", - "categoryKey": "security", - "categoryName": "安全", - "pluginGuidance": "" - }, - { - "name": "cors", - "displayName": "CORS", - "schema": "plugin/portal/cors.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule,global", - "pluginPriority": "2000", - "instructionForUse": "跨域访问CORS", - "categoryKey": "security", - "categoryName": "安全", - "pluginGuidance": "" - }, - { - "name": "jsonp", - "displayName": "JSONP", - "schema": "plugin/portal/jsonp.json", - "description": "jsonp插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule", - "pluginPriority": "2700", - "instructionForUse": "跨域访问JSONP", - "categoryKey": "security", - "categoryName": "安全", - "pluginGuidance": "" - }, - { - "name": "rewrite", - "displayName": "请求Path重写", - "schema": "plugin/portal/rewrite.json", - "description": "重写插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule", - "pluginPriority": "2500", - "instructionForUse": "修改请求Path前缀", - "categoryKey": "dataFormat", - "categoryName": "数据转换", - "pluginGuidance": "" - }, - { - "name": "cache", - "displayName": "缓存", - "schema": "plugin/portal/cache.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule,global", - "pluginPriority": "3000", - "instructionForUse": "缓存后端应答,以缓存响应请求", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "根据请求和响应匹配信息确定待缓存的请求响应结果,当后续请求符合请求匹配信息时,网关将已缓存的响应返回给客户端。设置自定义缓存Key计算,可以根据请求参数提供多个缓存值。" - }, - { - "name": "transformer", - "displayName": "请求重写", - "schema": "plugin/portal/request-transformer-new.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule", - "pluginPriority": "2300", - "instructionForUse": "修改请求Header、Query String以及Path", - "categoryKey": "dataFormat", - "categoryName": "数据转换", - "pluginGuidance": "" - }, - { - "name": "static-downgrade", - "displayName": "静态降级", - "schema": "plugin/portal/static-downgrade.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule,global", - "pluginPriority": "2200", - "instructionForUse": "当后端响应异常时,返回固定响应给客户端", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "当请求和响应匹配降级条件时,网关返回自定义的响应返回给客户端" - }, - { - "name": "dynamic-downgrade", - "displayName": "动态降级", - "schema": "plugin/portal/dynamic-downgrade.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule,global", - "pluginPriority": "2100", - "instructionForUse": "当后端响应异常时,返回缓存响应给客户端,或者转发请求给降级接口进行处理", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "当请求和响应匹配降级条件时,网关返回之前请求的响应缓存,或者转发请求到降级服务的接口进行处理,并返回降级处理响应结果" - }, - { - "name": "trace", - "displayName": "trace", - "schema": "plugin/portal/neTraceFileLog.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "global", - "pluginPriority": "4000", - "instructionForUse": "trace插件", - "categoryKey": "tracing", - "categoryName": "链路追踪", - "pluginGuidance": "" - }, - { - "name": "super-auth", - "displayName": "认证", - "schema": "plugin/portal/superAuth.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "global,routeRule", - "pluginPriority": "1700", - "instructionForUse": "提供AK/SK、JWT和Oauth2认证", - "categoryKey": "auth", - "categoryName": "认证鉴权", - "pluginGuidance": "" - }, - { - "name": "circuit-breaker", - "displayName": "熔断", - "schema": "plugin/portal/circuit-breaker.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule,global", - "pluginPriority": "3400", - "instructionForUse": "当后端服务错误率和时延超过阈值时,拦截客户端请求,保护后端服务", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "当错误率或连续慢响应次数超多阈值时触发熔断,在熔断状态下,网关将直接返回指定响应给客户端,等经过惩罚时间后,网关会尝试将请求转发到后端服务,如果响应无异常则恢复为正常状态否则继续处于熔断状态。" - }, - { - "name": "local-limiting", - "displayName": "本地限流", - "schema": "plugin/portal/local-limit.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "routeRule,global", - "pluginPriority": "3400", - "instructionForUse": "请求频率限流,按后端服务节点限流", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "" - }, - { - "name": "neBotDetection", - "displayName": "neBotDetection", - "schema": "plugin/portal/bot-detection.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "global,routeRule", - "pluginPriority": "1000", - "instructionForUse": "UA 黑白名单插件", - "categoryKey": "security", - "categoryName": "安全", - "pluginGuidance": "" - }, - { - "name": "neSecurity", - "displayName": "neSecurity", - "schema": "plugin/portal/security.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "global,routeRule", - "pluginPriority": "1000", - "instructionForUse": "安全拦截插件", - "categoryKey": "security", - "categoryName": "安全", - "pluginGuidance": "" - }, - { - "name": "neUrsAuth", - "displayName": "neUrsAuth", - "schema": "plugin/portal/urs-auth.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "global,routeRule", - "pluginPriority": "1000", - "instructionForUse": "URS认证插件", - "categoryKey": "security", - "categoryName": "安全", - "pluginGuidance": "" - }, - { - "name": "neRequestTermination", - "displayName": "neRequestTermination", - "schema": "plugin/portal/request-termination.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "global,routeRule", - "pluginPriority": "1000", - "instructionForUse": "请求阻断插件", - "categoryKey": "trafficPolicy", - "categoryName": "流量管理", - "pluginGuidance": "" - }, - { - "name": "neTraceSample", - "displayName": "neTraceSample", - "schema": "plugin/portal/trace-sample.json", - "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", - "pluginScope": "global,routeRule", - "pluginPriority": "1000", - "instructionForUse": "采样标识插件", - "categoryKey": "tracing", - "categoryName": "链路追踪", - "pluginGuidance": "" - } - ] -} diff --git a/hango-api-plane-server/src/main/resources/template/plugin/manager/plugin-manager-template.json b/hango-api-plane-server/src/main/resources/template/plugin/manager/plugin-manager-template.json index 4d57eb01..498dd755 100644 --- a/hango-api-plane-server/src/main/resources/template/plugin/manager/plugin-manager-template.json +++ b/hango-api-plane-server/src/main/resources/template/plugin/manager/plugin-manager-template.json @@ -1,7 +1,4 @@ { - "GatewayLabels": { - "gw_cluster": "{{ .Values.gateway_cluster }}" - }, "Plugins": [ { "enable": "true", diff --git a/hango-api-plane-server/src/main/resources/template/plugin/manager/plugin-support-config.json b/hango-api-plane-server/src/main/resources/template/plugin/manager/plugin-support-config.json index 82e32802..9b5f5ffe 100644 --- a/hango-api-plane-server/src/main/resources/template/plugin/manager/plugin-support-config.json +++ b/hango-api-plane-server/src/main/resources/template/plugin/manager/plugin-support-config.json @@ -96,11 +96,26 @@ ] }, { - "gatewayKind": "KubernetesIngress", + "gatewayKind": "Ingress", "name": "Kubernetes Ingress", "plugins": [ - {"schema": "resty","plugin": "proxy.filters.http.rider","display": true}, + {"schema": "local-limiting","plugin": "envoy.filters.http.local_ratelimit","display": true}, + {"schema": "rate-limiting","plugin": "envoy.filters.http.ratelimit","display": true}, + {"schema": "cluster-group-limiting","plugin": "envoy.filters.http.ratelimit","display": true}, + {"schema": "ianus-router","plugin": "envoy.filters.http.fault","display": false}, + {"schema": "static-downgrade","plugin": "proxy.filters.http.staticdowngrade","display": true}, + {"schema": "dynamic-downgrade","plugin": "proxy.filters.http.dynamicdowngrade","display": true}, + {"schema": "circuit-breaker","plugin": "proxy.filters.http.circuitbreaker","display": true}, + {"schema": "ip-restriction","plugin": "proxy.filters.http.iprestriction","display": true}, {"schema": "ua-restriction","plugin": "proxy.filters.http.ua_restriction","display": true}, + {"schema": "cors","plugin": "envoy.filters.http.cors","display": true}, + {"schema": "waf","plugin": "proxy.filters.http.waf","display": true}, + {"schema": "rewrite","plugin": "proxy.filters.http.path_rewrite","display": true}, + {"schema": "transformer","plugin": "proxy.filters.http.transformation","display": true}, + {"schema": "jwt-auth","plugin": "envoy.filters.http.jwt_authn","display": true}, + {"schema": "oauth2-auth","plugin": "proxy.filters.http.super_authz","display": true}, + {"schema": "resty","plugin": "proxy.filters.http.rider","display": true}, + {"schema": "","plugin": "proxy.filters.http.metadatahub","display": false}, {"schema": "","plugin": "proxy.filters.http.detailed_stats","display": false}, {"schema": "","plugin": "proxy.filters.http.soapjsontranscoder","display": false}, diff --git a/hango-api-plane-server/src/main/resources/template/plugin/route/circuit-breaker.json b/hango-api-plane-server/src/main/resources/template/plugin/route/circuit-breaker.json index 993a7b54..1071a504 100644 --- a/hango-api-plane-server/src/main/resources/template/plugin/route/circuit-breaker.json +++ b/hango-api-plane-server/src/main/resources/template/plugin/route/circuit-breaker.json @@ -1,20 +1,6 @@ { - "formatter": { - "kind": "circuit-breaker", - "config": { - "error_percent_threshold": "&errorPercent.errorPercentThreshold", - "min_request_amount": "&errorPercent.minRequestAmount", - "average_response_time": "&rt.rtThreshold", - "consecutive_slow_requests": "&rt.consecutiveSlowRequests", - "breakType": "&breakType", - "lookback_duration": "&lookbackDuration", - "break_duration": "&breakDuration" - }, - "response": { - "code": "&response.code", - "headers": "&response.headers", - "body": "&response.body" - } + "inject": { + "kind": "circuit-breaker" }, "layouts": [ { @@ -87,7 +73,7 @@ "layouts": [ { "key": "rtThreshold", - "alias": "慢响应时间(秒)", + "alias": "慢响应时间(s)", "help": "当后端响应时间超过该阈值时,则认为当前请求响应为慢响应", "placeholder": "单位s,范围 (0, 1000.0]", "type": "input", @@ -158,23 +144,26 @@ }, { "key": "headers", - "type": "array", + "type": "extend-form-table", "alias": "响应头", - "layouts": [ + "default":[], + "ruleList": [ { + "label": "响应头名称", "key": "key", + "placeholder": "请输入", "type": "input", - "alias": "响应头名称", - "rules": [ + "ruleNames": [ "Required", "MaxLength(200)" ] }, { + "label": "响应头值", "key": "value", + "placeholder": "请输入", "type": "input", - "alias": "响应头值", - "rules": [ + "ruleNames": [ "Required", "MaxLength(200)" ] diff --git a/hango-api-plane-server/src/main/resources/template/plugin/route/plugin-config.json b/hango-api-plane-server/src/main/resources/template/plugin/route/plugin-config.json index 92ac6a11..2ce7e2bb 100644 --- a/hango-api-plane-server/src/main/resources/template/plugin/route/plugin-config.json +++ b/hango-api-plane-server/src/main/resources/template/plugin/route/plugin-config.json @@ -5,12 +5,7 @@ "displayName": "百分比限流", "schema": "plugin/route/ianus-percent-limit.json", "description": "百分比限流插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule", - "pluginPriority": "500", "instructionForUse": "按百分比限流,例如允许50%流量通过", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -21,12 +16,7 @@ "displayName": "本地限流", "schema": "plugin/route/local-limit.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "3400", "instructionForUse": "请求频率限流,限制网关实例节点的服务请求流量", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -37,12 +27,7 @@ "displayName": "集群限流", "schema": "plugin/route/rate-limiting.json", "description": "频控插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule", - "pluginPriority": "1500", "instructionForUse": "基于分布式限流组件对服务请求流量进行限制", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -53,12 +38,7 @@ "displayName": "集群分组限流", "schema": "plugin/route/cluster-group-limiting.json", "description": "集群分组插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1678689539000", - "updateTime": "1678689539000", "pluginScope": "routeRule", - "pluginPriority": "1500", "instructionForUse": "基于分布式限流组件对服务请求流量进行分组限制", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -69,12 +49,7 @@ "displayName": "熔断", "schema": "plugin/route/circuit-breaker.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "3400", "instructionForUse": "当后端服务错误率和时延超过阈值时,拦截客户端请求,保护后端服务", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -85,12 +60,7 @@ "displayName": "静态降级", "schema": "plugin/route/static-downgrade.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "2200", "instructionForUse": "当后端响应异常时,返回固定响应给客户端", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -101,12 +71,7 @@ "displayName": "动态降级", "schema": "plugin/route/dynamic-downgrade.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "2100", "instructionForUse": "当后端响应异常时,返回缓存响应给客户端,或者转发请求给降级接口处理", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -117,12 +82,8 @@ "displayName": "本地缓存", "schema": "plugin/route/local-cache.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "3000", + "pluginPriority": "80", "instructionForUse": "本地缓存后端应答,以缓存响应请求", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -133,12 +94,7 @@ "displayName": "Redis缓存", "schema": "plugin/route/redis-cache.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "3000", "instructionForUse": "redis缓存后端应答,以缓存响应请求", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -149,12 +105,7 @@ "displayName": "请求中断", "schema": "plugin/route/ianus-router.json", "description": "请求中断插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule", - "pluginPriority": "3500", "instructionForUse": "请求中断返回,支持定义响应码和响应Body", "categoryKey": "trafficPolicy", "categoryName": "流量管理", @@ -165,12 +116,7 @@ "displayName": "JWT认证", "schema": "plugin/route/jwt-auth.json", "description": "JWT认证插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1660793528000", - "updateTime": "1660793528000", "pluginScope": "routeRule,global", - "pluginPriority": "500", "instructionForUse": "提供JWT认证", "categoryKey": "auth", "categoryName": "认证鉴权", @@ -181,12 +127,8 @@ "displayName": "OAuth2认证", "schema": "plugin/route/oauth2Auth.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "global,routeRule", - "pluginPriority": "1700", + "pluginPriority": "120", "instructionForUse": "提供OAuth2认证", "categoryKey": "auth", "categoryName": "认证鉴权", @@ -197,12 +139,7 @@ "displayName": "简单认证", "schema": "plugin/route/simpleAuth.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "global,routeRule", - "pluginPriority": "1700", "instructionForUse": "提供简单认证", "categoryKey": "auth", "categoryName": "认证鉴权", @@ -213,12 +150,7 @@ "displayName": "基础鉴权", "schema": "plugin/route/basic-rbac.json", "description": "提供基于外部认证鉴权", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1660793528000", - "updateTime": "1660793528000", "pluginScope": "routeRule,global", - "pluginPriority": "500", "instructionForUse": "提供基于外部认证鉴权", "categoryKey": "auth", "categoryName": "认证鉴权", @@ -229,12 +161,7 @@ "displayName": "IP黑白名单", "schema": "plugin/route/ip-restriction.json", "description": "黑白名单插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "1000", "instructionForUse": "根据IP过滤请求", "categoryKey": "security", "categoryName": "安全", @@ -245,12 +172,7 @@ "displayName": "UA黑白名单", "schema": "plugin/route/ua-restriction.json", "description": "UA黑白名单插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global,host", - "pluginPriority": "1100", "instructionForUse": "根据user-agent过滤请求", "categoryKey": "security", "categoryName": "安全", @@ -261,12 +183,7 @@ "displayName": "Referer黑白名单", "schema": "plugin/route/referer-restriction.json", "description": "Referer黑白名单插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "1100", "instructionForUse": "根据referer过滤请求", "categoryKey": "security", "categoryName": "安全", @@ -277,12 +194,7 @@ "displayName": "Header黑白名单", "schema": "plugin/route/header-restriction.json", "description": "根据header过滤请求", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "1100", "instructionForUse": "根据header过滤请求", "categoryKey": "security", "categoryName": "安全", @@ -293,12 +205,7 @@ "displayName": "CORS", "schema": "plugin/route/cors.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "2000", "instructionForUse": "跨域访问CORS", "categoryKey": "security", "categoryName": "安全", @@ -309,12 +216,7 @@ "displayName": "JSONP", "schema": "plugin/route/jsonp.json", "description": "jsonp插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule", - "pluginPriority": "2700", "instructionForUse": "跨域访问JSONP", "categoryKey": "security", "categoryName": "安全", @@ -325,12 +227,7 @@ "displayName": "WAF", "schema": "plugin/route/waf.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule,global", - "pluginPriority": "1000", "instructionForUse": "提供应用级安全防护", "categoryKey": "security", "categoryName": "安全", @@ -341,12 +238,7 @@ "displayName": "参数校验", "schema": "plugin/route/parameter-validate-rule.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1669886179", - "updateTime": "1669886179", "pluginScope": "routeRule,global", - "pluginPriority": "1100", "instructionForUse": "检查请求的header和query参数值", "categoryKey": "security", "categoryName": "安全", @@ -357,12 +249,7 @@ "displayName": "请求Path重写", "schema": "plugin/route/rewrite.json", "description": "重写插件", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule", - "pluginPriority": "2500", "instructionForUse": "修改请求Path前缀", "categoryKey": "dataFormat", "categoryName": "数据转换", @@ -373,12 +260,7 @@ "displayName": "请求重写", "schema": "plugin/route/request-transformer-new.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule", - "pluginPriority": "2300", "instructionForUse": "修改请求Header、Query String以及Path", "categoryKey": "dataFormat", "categoryName": "数据转换", @@ -389,12 +271,7 @@ "displayName": "响应头重写", "schema": "plugin/route/response-header-rewrite.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1636943353954", - "updateTime": "1636943353954", "pluginScope": "routeRule,global", - "pluginPriority": "2500", "instructionForUse": "修改请求的响应头信息", "categoryKey": "dataFormat", "categoryName": "数据转换", @@ -405,12 +282,7 @@ "displayName": "流量染色", "schema": "plugin/route/traffic-mark.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1636943353954", - "updateTime": "1636943353954", "pluginScope": "routeRule,global", - "pluginPriority": "2500", "instructionForUse": "为符合条件的请求带上染色标志", "categoryKey": "dataFormat", "categoryName": "数据转换", @@ -421,12 +293,7 @@ "displayName": "请求body转换", "schema": "plugin/route/request-body-rewrite.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule", - "pluginPriority": "1000", "instructionForUse": "修改json格式的请求body内容", "categoryKey": "dataFormat", "categoryName": "数据转换", @@ -437,12 +304,7 @@ "displayName": "响应body转换", "schema": "plugin/route/response-body-rewrite.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "routeRule", - "pluginPriority": "1000", "instructionForUse": "修改json格式的响应body内容", "categoryKey": "dataFormat", "categoryName": "数据转换", @@ -453,12 +315,7 @@ "displayName": "soap转换", "schema": "plugin/route/soap-json-transcoder.json", "description": "", - "processor": "AggregateGatewayPluginProcessor", - "author": "system", - "createTime": "1572537600000", - "updateTime": "1572537600000", "pluginScope": "", - "pluginPriority": "3400", "instructionForUse": "soap转rest", "categoryKey": "trafficPolicy", "categoryName": "流量管理", diff --git a/hango-api-plane-server/src/main/resources/template/plugin/route/static-downgrade.json b/hango-api-plane-server/src/main/resources/template/plugin/route/static-downgrade.json index a392531c..8bfc34cd 100644 --- a/hango-api-plane-server/src/main/resources/template/plugin/route/static-downgrade.json +++ b/hango-api-plane-server/src/main/resources/template/plugin/route/static-downgrade.json @@ -1,316 +1,382 @@ { - "formatter": { - "kind": "static-downgrade", - "condition?": "&condition", - "response": { - "code": "&response.code", - "headers": "@response.headers", - "body": "&response.body" - } + "inject": { + "kind": "static-downgrade" }, "layouts": [ { - "key": "condition", - "alias": "静态降级条件", - "type": "layouts", - "layouts": [ + "alias": "静态降级条件" + }, + { + "alias": "请求匹配", + "type": "switch", + "key": "requestSwitch", + "style": { + "marginLeft": "60px" + } + }, + { + "visible": { + "true": [ + "requestSwitch" + ] + }, + "alias": "请求方法", + "key": "requestMethod", + "type": "extend-inline-form", + "indent": 3, + "style": { + "marginLeft": "120px" + }, + "default":{}, + "moudle": [ { - "key": "request", - "alias": "请求", - "type": "layouts", - "layouts": [ + "key": "requestMethodList", + "type": "multi_select", + "options": [ + { + "text": "GET", + "value": "GET" + }, + { + "text": "POST", + "value": "POST" + }, + { + "text": "PUT", + "value": "PUT" + }, { - "key": "requestSwitch", - "alias": "请求匹配", - "type": "switch", - "default": false + "text": "DELETE", + "value": "DELETE" }, { - "key": "method", - "alias": "请求方法", - "type": "multi_select", - "visible": { - "this.requestSwitch": true - }, - "options": [ - { - "text": "GET", - "value": "GET" - }, - { - "text": "POST", - "value": "POST" - }, - { - "text": "PUT", - "value": "PUT" - }, - { - "text": "DELETE", - "value": "DELETE" - }, - { - "text": "OPTIONS", - "value": "OPTIONS" - }, - { - "text": "HEAD", - "value": "HEAD" - }, - { - "text": "TRACE", - "value": "TRACE" - }, - { - "text": "CONNECT", - "value": "CONNECT" - }, - { - "text": "PATCH", - "value": "PATCH" - } - ] + "text": "OPTIONS", + "value": "OPTIONS" }, { - "key": "path", - "alias": "请求路径", - "type": "layouts", - "visible": { - "this.requestSwitch": true - }, - "layouts": [ - { - "key": "match_type", - "alias": "匹配方式", - "type": "select", - "default": "exact_match", - "options": [ - { - "text": "精确匹配", - "value": "exact_match" - }, - { - "text": "正则表达式", - "value": "safe_regex_match" - } - ] - }, - { - "key": "value", - "type": "input", - "alias": "取值" - } - ] + "text": "HEAD", + "value": "HEAD" }, { - "key": "host", - "alias": "域名", - "type": "layouts", - "visible": { - "this.requestSwitch": true - }, - "layouts": [ - { - "key": "match_type", - "alias": "匹配方式", - "type": "select", - "default": "exact_match", - "options": [ - { - "text": "精确匹配", - "value": "exact_match" - }, - { - "text": "正则表达式", - "value": "safe_regex_match" - } - ] - }, - { - "key": "value", - "type": "input", - "alias": "取值" - } - ] + "text": "TRACE", + "value": "TRACE" }, { - "key": "headers", - "alias": "请求头", - "type": "array", - "visible": { - "this.requestSwitch": true - }, - "layouts": [ - { - "key": "headerKey", - "alias": "请求头名称", - "type": "input", - "rules": [ - "MaxLength(200)" - ] - }, - { - "key": "match_type", - "alias": "匹配方式", - "type": "select", - "default": "exact_match", - "options": [ - { - "text": "精确匹配", - "value": "exact_match" - }, - { - "text": "正则表达式", - "value": "safe_regex_match" - } - ] - }, - { - "key": "value", - "type": "input", - "alias": "取值" - } - ] + "text": "CONNECT", + "value": "CONNECT" + }, + { + "text": "PATCH", + "value": "PATCH" } ] + } + ] + }, + { + "visible": { + "true": [ + "requestSwitch" + ] + }, + "style": { + "marginLeft": "120px" + }, + "alias": "请求路径", + "key": "requestPath", + "type": "extend-inline-form", + "indent": true, + "default":{}, + "moudle": [ + { + "default": "exact_match", + "options": [ + { + "text": "精确匹配", + "value": "exact_match" + }, + { + "text": "正则表达式", + "value": "safe_regex_match" + } + ], + "type": "select", + "key": "match_type" }, { - "key": "response", - "alias": "响应", - "type": "layouts", - "layouts": [ + "type": "input", + "key": "value" + } + ] + }, + { + "visible": { + "true": [ + "requestSwitch" + ] + }, + "style": { + "marginLeft": "120px" + }, + "alias": "域名", + "key": "requestHost", + "type": "extend-inline-form", + "indent": true, + "default":{}, + "moudle": [ + { + "default": "exact_match", + "options": [ { - "key": "code", - "alias": "状态码", - "type": "layouts", - "layouts": [ - { - "key": "match_type", - "alias": "匹配方式", - "type": "select", - "default": "exact_match", - "options": [ - { - "text": "精确匹配", - "value": "exact_match" - }, - { - "text": "正则表达式", - "value": "safe_regex_match" - } - ] - }, - { - "key": "value", - "alias": "取值", - "type": "input", - "rules": [ - "Required", - "Number", - "MinNumber(200)", - "MaxNumber(599)" - ], - "customRules": { - "keys": [ - "this", - "match_type" - ], - "safe_regex_match": [ - "Required", - "RegExp" - ] - } - } - ] + "text": "精确匹配", + "value": "exact_match" }, { - "key": "headers", - "alias": "响应头", - "type": "array", - "layouts": [ - { - "key": "headerKey", - "alias": "响应头名称", - "type": "input", - "rules": [ - "MaxLength(200)" - ] - }, - { - "key": "match_type", - "alias": "匹配方式", - "type": "select", - "default": "exact_match", - "options": [ - { - "text": "精确匹配", - "value": "exact_match" - }, - { - "text": "正则表达式", - "value": "safe_regex_match" - } - ] - }, - { - "key": "value", - "type": "input", - "alias": "取值" - } - ] + "text": "正则表达式", + "value": "safe_regex_match" } + ], + "type": "select", + "key": "match_type" + }, + { + "type": "input", + "key": "value" + } + ] + }, + { + "visible": { + "true": [ + "requestSwitch" + ] + }, + "style": { + "marginLeft": "120px" + }, + "alias": "请求头", + "default": [], + "description": "", + "key": "requestHeaders", + "layouts": null, + "indent": true, + "type": "extend-form-table", + "formTableConfig": { + "emptyText": "暂无数据" + }, + "ruleList": [ + { + "label": "请求头名称", + "key": "requestHeaderKey", + "placeholder": "请输入", + "type": "input", + "ruleNames": [ + "MaxLength(99)" ] + }, + { + "options": [ + { + "text": "精确匹配", + "value": "exact_match" + }, + { + "text": "正则表达式", + "value": "safe_regex_match" + } + ], + "label": "匹配方式", + "type": "select", + "key": "match_type", + "ruleNames": [ + "Required" + ] + }, + { + "label": "取值", + "type": "input", + "placeholder": "请输入", + "key": "requestHeaderValue" } ] }, { - "key": "response", - "alias": "降级后响应内容", - "type": "layouts", - "layouts": [ + "alias": "响应匹配", + "style": { + "marginLeft": "60px" + } + }, + { + "style": { + "marginLeft": "120px" + }, + "alias": "响应码", + "key": "responseCode", + "type": "extend-inline-form", + "indent": true, + "default":{}, + "rules": [ + "Required" + ], + "moudle": [ + { + "default": "exact_match", + "options": [ + { + "text": "精确匹配", + "value": "exact_match" + }, + { + "text": "正则表达式", + "value": "safe_regex_match" + } + ], + "rules": [ + "Required" + ], + "type": "select", + "key": "match_type" + }, { - "key": "code", "type": "input", - "default": "200", - "alias": "状态码", + "key": "value", "rules": [ "Required", "Number", "MinNumber(200)", "MaxNumber(599)" + ], + "customRules": { + "keys": [ + "this", + "match_type" + ], + "safe_regex_match": [ + "Required", + "RegExp" + ] + } + } + ] + }, + { + "style": { + "marginLeft": "120px" + }, + "alias": "响应头", + "default": [], + "description": "", + "key": "responseHeaders", + "layouts": null, + "indent": true, + "type": "extend-form-table", + "formTableConfig": { + "emptyText": "暂无数据" + }, + "ruleList": [ + { + "label": "响应头名称", + "key": "responseHeaderKey", + "placeholder": "请输入", + "type": "input", + "ruleNames": [ + "MaxLength(200)" ] }, { - "key": "headers", - "type": "array", - "alias": "响应头", - "layouts": [ + "options": [ { - "key": "key", - "type": "input", - "alias": "响应头名称", - "rules": [ - "Required", - "MaxLength(200)" - ] + "text": "精确匹配", + "value": "exact_match" }, { - "key": "value", - "type": "input", - "alias": "响应头值", - "rules": [ - "Required", - "MaxLength(200)" - ] + "text": "正则表达式", + "value": "safe_regex_match" } + ], + "label": "匹配方式", + "type": "select", + "key": "match_type", + "ruleNames": [ + "Required" + ] + }, + { + "label": "取值", + "type": "input", + "placeholder": "请输入", + "key": "responseHeaderValue" + } + ] + }, + { + "alias": "降级后响应内容", + "style": { + "marginLeft": "60px" + } + }, + { + "style": { + "marginLeft": "120px" + }, + "key": "downgradeResponseCode", + "type": "input", + "default": "200", + "alias": "状态码", + "rules": [ + "Required", + "Number", + "MinNumber(200)", + "MaxNumber(599)" + ] + }, + { + "style": { + "marginLeft": "120px" + }, + "alias": "响应头", + "default": [], + "description": "", + "key": "downgradeResponseHeaders", + "layouts": null, + "indent": true, + "type": "extend-form-table", + "formTableConfig": { + "emptyText": "暂无数据" + }, + "ruleList": [ + { + "label": "响应头名称", + "key": "downgradeResponseHeaderKey", + "placeholder": "请输入", + "type": "input", + "ruleNames": [ + "Required", + "MaxLength(200)" ] }, { - "key": "body", + "label": "响应头值", + "key": "downgradeResponseHeaderValue", + "placeholder": "请输入", "type": "input", - "alias": "响应体" + "ruleNames": [ + "Required", + "MaxLength(200)" + ] } ] + }, + { + "style": { + "marginLeft": "120px" + }, + "key": "downgradeResponseBody", + "type": "input", + "alias": "响应体", + "rules": [ + "Required" + ] } ] } \ No newline at end of file diff --git a/hango-api-plane-server/src/main/resources/template/plugin/route/waf.json b/hango-api-plane-server/src/main/resources/template/plugin/route/waf.json index a1ed7ec4..fa0aac89 100644 --- a/hango-api-plane-server/src/main/resources/template/plugin/route/waf.json +++ b/hango-api-plane-server/src/main/resources/template/plugin/route/waf.json @@ -13,6 +13,7 @@ "help": "防止拒绝服务攻击,针对 IP 级别的请求数统计实现防御。在指定时间窗口内,某一个IP的请求数超过阈值时,在惩罚时间内拦截来自该IP的请求", "alias": "DOS", "type": "switch", + "visible": false, "default": false }, { diff --git a/hango-api-plane-server/src/main/resources/template/rbac_ingress.ftl b/hango-api-plane-server/src/main/resources/template/rbac_ingress.ftl deleted file mode 100644 index 61639d12..00000000 --- a/hango-api-plane-server/src/main/resources/template/rbac_ingress.ftl +++ /dev/null @@ -1,47 +0,0 @@ -#黑白名单,使用com.netease.cloud.nsf.meta.WhiteList进行填充 ---- -apiVersion: rbac.istio.io/v1alpha1 -kind: ServiceRole -metadata: - name: qz-ingress-whitelist - namespace: ${namespace} -spec: - rules: -# 仅作占位符 - - services: ["${service}.${namespace}.svc.cluster.local"] ---- -apiVersion: rbac.istio.io/v1alpha1 -kind: ServiceRoleBinding -metadata: - name: qz-ingress-whitelist - namespace: ${namespace} -spec: - subjects: - - user: "cluster.local/ns/istio-system/sa/qz-ingress" - roleRef: - kind: ServiceRole - name: qz-ingress-whitelist ---- -apiVersion: rbac.istio.io/v1alpha1 -kind: ServiceRole -metadata: - name: qz-ingress-passed - namespace: ${namespace} -spec: -# 仅作占位符 - rules: - - services: ["${service}.${namespace}.svc.cluster.local"] ---- -apiVersion: rbac.istio.io/v1alpha1 -kind: ServiceRoleBinding -metadata: - name: qz-ingress-passed - namespace: ${namespace} -spec: - subjects: - - user: "cluster.local/ns/istio-system/sa/qz-ingress" - roleRef: - kind: ServiceRole - name: qz-ingress-passed - - diff --git a/hango-api-plane-server/src/main/resources/template/sidecarVersionManagement.ftl b/hango-api-plane-server/src/main/resources/template/sidecarVersionManagement.ftl deleted file mode 100644 index cb119240..00000000 --- a/hango-api-plane-server/src/main/resources/template/sidecarVersionManagement.ftl +++ /dev/null @@ -1,43 +0,0 @@ -apiVersion: networking.istio.io/v1alpha3 -kind: VersionManager -metadata: - name: version-manager - namespace: ${t_namespace} -spec: - defaultVersion: envoy - retryPolicy: - neverRetry: false - retryTime: 5 - retryInterval: 3s - sidecarVersionSpec: -<#list t_version_manager_workloads! as w> - - podsHash: none - <#if w.expectedVersion?? > - expectedVersion: ${w.expectedVersion} - - <#if w.iptablesParams?? > - iptablesParams: ${w.iptablesParams} - - <#if w.iptablesDetail?? > - iptablesDetail: '${w.iptablesDetail}' - - <#if w.workLoadType == "Deployment"> - viaDeployment: - name: ${w.workLoadName} - - <#if w.workLoadType == "StatefulSet"> - viaStatefulSet: - name: ${w.workLoadName} - - <#if w.workLoadType == "Service"> - viaService: - name: ${w.workLoadName} - - <#if w.workLoadType == "LabelSelector"> - viaLabelSelector: - labels: - <#list w.labels?keys as key> - ${key}: ${w.labels[key]!} - - - \ No newline at end of file diff --git a/hango-api-plane-server/src/main/resources/template/weightRouting.ftl b/hango-api-plane-server/src/main/resources/template/weightRouting.ftl deleted file mode 100644 index 96388201..00000000 --- a/hango-api-plane-server/src/main/resources/template/weightRouting.ftl +++ /dev/null @@ -1,14 +0,0 @@ -apiVersion: networking.istio.io/v1alpha3 -kind: VirtualService -<#include "common/metadata.ftl"/> -spec: - hosts: - - ${nsfExtra.host!} - http: - - route: -<#list nsfExtra.destinations! as des> - - destination: - host: ${nsfExtra.host!} - subset: ${des.subset!} - weight: ${des.weight!} - \ No newline at end of file diff --git a/hango-api-plane-server/src/main/resources/template/whiteList.ftl b/hango-api-plane-server/src/main/resources/template/whiteList.ftl deleted file mode 100644 index 52ec2166..00000000 --- a/hango-api-plane-server/src/main/resources/template/whiteList.ftl +++ /dev/null @@ -1,42 +0,0 @@ -# 严选TLS+分流, 使用com.netease.cloud.nsf.meta.WhiteList进行填充 ---- -apiVersion: networking.istio.io/v1alpha3 -kind: VirtualService -metadata: - name: ${service} - namespace: ${namespace} -spec: - hosts: - - ${service} - http: - - route: - - destination: - host: ${service} - weight: 100 - - destination: - host: qz-egress.istio-system.svc.cluster.local - weight: 0 ---- -apiVersion: networking.istio.io/v1alpha3 -kind: DestinationRule -metadata: - name: ${service} - namespace: ${namespace} -spec: - host: ${service} - trafficPolicy: - tls: - mode: ISTIO_MUTUAL - host: ${service} ---- -apiVersion: "authentication.istio.io/v1alpha1" -kind: "Policy" -metadata: - name: ${service} - namespace: ${namespace} -spec: - targets: - - name: ${service} - peers: - - mtls: - mode: STRICT diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/envoy/EnvoyHttpClientTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/envoy/EnvoyHttpClientTest.java index c0ef9aff..631929e1 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/envoy/EnvoyHttpClientTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/envoy/EnvoyHttpClientTest.java @@ -5,9 +5,10 @@ import io.fabric8.kubernetes.api.model.PodSpec; import io.fabric8.kubernetes.api.model.PodStatus; import org.hango.cloud.core.BaseTest; -import org.hango.cloud.core.k8s.KubernetesClient; +import org.hango.cloud.core.k8s.K8sClient; import org.hango.cloud.meta.ServiceHealth; import org.hango.cloud.service.GatewayService; +import org.hango.cloud.service.KubernetesGatewayService; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -30,11 +31,15 @@ public class EnvoyHttpClientTest extends BaseTest { RestTemplate restTemplate; @MockBean - KubernetesClient k8sClient; + K8sClient k8sClient; + @Autowired private GatewayService gatewayService; + @Autowired + private KubernetesGatewayService kubernetesGatewayService; + @Autowired ObjectMapper objectMapper; @@ -43,7 +48,7 @@ public void getServiceHealth() { String resp = "{\"cluster_statuses\":[{\"name\":\"xds_cluster\",\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.108.238.209\",\"port_value\":15010}},\"stats\":[{\"value\":\"2\",\"name\":\"cx_connect_fail\"},{\"value\":\"427\",\"name\":\"cx_total\"},{\"value\":\"427\",\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"value\":\"426\",\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"value\":\"1\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"value\":\"1\",\"name\":\"rq_active\"}],\"health_status\":{\"failed_active_hc\":false,\"eds_health_status\":\"HEALTHY\"},\"weight\":1,\"hostname\":\"istio-pilot.istio-system.svc.cluster.local\"}]},{\"name\":\"outbound|9901||istio-galley.istio-system.svc.cluster.local\",\"added_via_api\":true,\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.244.2.65\",\"port_value\":9901}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"failed_eds_health\":true,\"eds_health_status\":\"HEALTHY\"},\"weight\":1}]},{\"name\":\"outbound|9379||gateway-prometheus.gateway-system.svc.cluster.local\",\"added_via_api\":true,\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.244.2.6\",\"port_value\":9090}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"eds_health_status\":\"HEALTHY\"},\"weight\":1},{\"address\":{\"socket_address\":{\"address\":\"10.244.2.6\",\"port_value\":9090}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"eds_health_status\":\"HEALTHY\"},\"weight\":1}]},{\"name\":\"outbound|9379|sb1|gateway-prometheus.gateway-system.svc.cluster.local\",\"added_via_api\":true,\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.244.2.6\",\"port_value\":9090}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"eds_health_status\":\"HEALTHY\"},\"weight\":1}]}]}"; when(restTemplate.getForObject(anyString(), any())).thenReturn(resp); - when(k8sClient.getObjectList(any(), any(), any())).thenReturn(Arrays.asList(getPod(null, getPodStatus("1.1.1.1", "Running")))); + when(k8sClient.getPods(any(), any())).thenReturn(Arrays.asList(getPod(null, getPodStatus("1.1.1.1", "Running")))); List serviceHealth = envoyHttpClient.getServiceHealth(null, null, "gw1"); diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/IstioModelEngineTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/IstioModelEngineTest.java index 801ceea1..50f0a47a 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/IstioModelEngineTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/IstioModelEngineTest.java @@ -7,7 +7,6 @@ import istio.networking.v1alpha3.VirtualServiceOuterClass; import me.snowdrop.istio.api.networking.v1alpha3.ServiceEntry; import me.snowdrop.istio.api.networking.v1alpha3.VirtualService; -import org.assertj.core.util.Lists; import org.hango.cloud.core.BaseTest; import org.hango.cloud.core.editor.EditorContext; import org.hango.cloud.core.editor.ResourceType; @@ -26,7 +25,6 @@ import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.util.CollectionUtils; import slime.microservice.plugin.v1alpha1.EnvoyPluginOuterClass; import java.util.*; @@ -84,11 +82,6 @@ private API getAPI(String name, String service, List gateways, List virtualClusterHeaders){ - api.setVirtualClusterName(virtualClusterName); - api.setVirtualClusterHeaders(virtualClusterHeaders); - return api; - } private Service getService(String type, String backend, int weight, String code, String gateway, String protocol, String serviceTag) { Service s = new Service(); @@ -218,33 +211,13 @@ public void testTranslateAPI() { Assert.assertTrue(httpList.get(0).getRouteCount() == 3); } }); - - - //virtualCluster test - //base api test - API api2 = setAPIVirtualCluster(api, "test-vc", Lists.newArrayList()); - - List resources2 = gatewayIstioModelEngine.translate(api2); - - Assert.assertTrue(resources2.size() == 2); - - resources2.stream() - .map(r -> r.getResource()) - .forEach(r -> { - if (r.getKind().equals(VirtualService.class.getSimpleName())) { - K8sTypes.VirtualService vs = (K8sTypes.VirtualService) r; - VirtualServiceOuterClass.VirtualCluster virtualCluster = vs.getSpec().getVirtualCluster(0); - - Assert.assertTrue(virtualCluster.getName().equals("test-vc")); - } - }); } @Test public void testTranslatePluginManager() { PluginOrderDTO po = new PluginOrderDTO(); - po.setGatewayLabels(ImmutableMap.of("k1","v1", "k2", "v2")); + po.setGwCluster("prod-gateway"); po.setPlugins(ImmutableList.of( getPlugin("p1", true, null), getPlugin("p2", false, null), @@ -256,10 +229,11 @@ public void testTranslatePluginManager() { K8sTypes.PluginManager pm = (K8sTypes.PluginManager) res.get(0).getResource(); - Assert.assertTrue(pm.getSpec().getWorkloadLabels().size() == 2); + Assert.assertTrue(pm.getSpec().getWorkloadLabels().size() == 1); Assert.assertTrue(pm.getSpec().getPluginCount() == 3); PluginOrderDTO po1 = new PluginOrderDTO(); + po1.setGwCluster("prod-gateway"); po1.setPlugins(ImmutableList.of( getPlugin("p1", false, null), getPlugin("p2", true, ImmutableMap.of("key","good")))); @@ -269,8 +243,7 @@ public void testTranslatePluginManager() { Assert.assertTrue(res1.size() == 1); K8sTypes.PluginManager pm1 = (K8sTypes.PluginManager) res1.get(0).getResource(); - Assert.assertTrue(CollectionUtils.isEmpty(pm1.getSpec().getWorkloadLabels())); - Assert.assertTrue(pm1.getMetadata().getName().equals("qz-global")); + Assert.assertTrue(pm1.getMetadata().getName().equals("gw-cluster-prod-gateway")); assertEquals(2, pm1.getSpec().getPluginCount()); assertEquals("p1", pm1.getSpec().getPlugin(0).getName()); // assertEquals(false, pm1.getSpec().getPlugin().get(0).getEnable()); diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/PilotHttpClientTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/PilotHttpClientTest.java index c53145af..2899ef0c 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/PilotHttpClientTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/PilotHttpClientTest.java @@ -16,6 +16,7 @@ import java.util.Arrays; import java.util.List; +import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; @@ -46,7 +47,7 @@ public void getEndpointList() { Service service = buildService("10.10.10.18", 8080); - when(k8sClient.getObjectList(any(),any(),any())).thenReturn(Arrays.asList(service)); + when(k8sClient.getObjectList(any(),any(),anyMap())).thenReturn(Arrays.asList(service)); when(restTemplate.getForEntity(anyString(), any())).thenReturn(entity); List endpoints = istioHttpClient.getEndpointList(); diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/service/impl/DefaultResourceManagerTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/service/impl/DefaultResourceManagerTest.java index 5bee11b7..33b78ff8 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/service/impl/DefaultResourceManagerTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/gateway/service/impl/DefaultResourceManagerTest.java @@ -1,13 +1,13 @@ package org.hango.cloud.core.gateway.service.impl; +import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.api.model.PodSpec; +import io.fabric8.kubernetes.api.model.PodStatus; import org.hango.cloud.core.BaseTest; import org.hango.cloud.core.envoy.EnvoyHttpClient; import org.hango.cloud.core.gateway.service.ResourceManager; -import org.hango.cloud.core.k8s.KubernetesClient; +import org.hango.cloud.core.k8s.K8sClient; import org.hango.cloud.meta.ServiceHealth; -import io.fabric8.kubernetes.api.model.Pod; -import io.fabric8.kubernetes.api.model.PodSpec; -import io.fabric8.kubernetes.api.model.PodStatus; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -34,14 +34,14 @@ public class DefaultResourceManagerTest extends BaseTest { RestTemplate restTemplate; @MockBean - KubernetesClient k8sClient; + K8sClient k8sClient; @Test public void getServiceHealthList() { String resp = "{\"cluster_statuses\":[{\"name\":\"xds_cluster\",\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.108.238.209\",\"port_value\":15010}},\"stats\":[{\"value\":\"2\",\"name\":\"cx_connect_fail\"},{\"value\":\"427\",\"name\":\"cx_total\"},{\"value\":\"427\",\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"value\":\"426\",\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"value\":\"1\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"value\":\"1\",\"name\":\"rq_active\"}],\"health_status\":{\"failed_active_hc\":false,\"eds_health_status\":\"HEALTHY\"},\"weight\":1,\"hostname\":\"istio-pilot.istio-system.svc.cluster.local\"}]},{\"name\":\"outbound|9901|subset1|istio-galley.istio-system.svc.cluster.local\",\"added_via_api\":true,\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.244.2.65\",\"port_value\":9901}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"eds_health_status\":\"HEALTHY\"},\"weight\":1}]},{\"name\":\"outbound|9901|subset2|istio-galley.istio-system.svc.cluster.local\",\"added_via_api\":true,\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.244.2.65\",\"port_value\":9901}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"eds_health_status\":\"HEALTHY\"},\"weight\":1}]},{\"name\":\"outbound|9379|sb1|gateway-prometheus.gateway-system.svc.cluster.local\",\"added_via_api\":true,\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.244.2.6\",\"port_value\":9090}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"eds_health_status\":\"HEALTHY\"},\"weight\":1},{\"address\":{\"socket_address\":{\"address\":\"10.244.2.7\",\"port_value\":9090}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"failed_active_health_check\":true,\"eds_health_status\":\"HEALTHY\"},\"weight\":1}]},{\"name\":\"outbound|9379|sb3|gateway-prometheus.gateway-system.svc.cluster.local\",\"added_via_api\":true,\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.244.2.6\",\"port_value\":9090}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"eds_health_status\":\"HEALTHY\"},\"weight\":1},{\"address\":{\"socket_address\":{\"address\":\"10.244.2.7\",\"port_value\":9090}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"eds_health_status\":\"HEALTHY\"},\"weight\":1}]},{\"name\":\"outbound|9379|sb2|gateway-prometheus.gateway-system.svc.cluster.local\",\"added_via_api\":true,\"host_statuses\":[{\"address\":{\"socket_address\":{\"address\":\"10.244.2.6\",\"port_value\":9090}},\"stats\":[{\"name\":\"cx_connect_fail\"},{\"name\":\"cx_total\"},{\"name\":\"rq_error\"},{\"name\":\"rq_success\"},{\"name\":\"rq_timeout\"},{\"name\":\"rq_total\"},{\"type\":\"GAUGE\",\"name\":\"cx_active\"},{\"type\":\"GAUGE\",\"name\":\"rq_active\"}],\"health_status\":{\"failed_active_health_check\":true,\"eds_health_status\":\"HEALTHY\"},\"weight\":1}]}]}"; when(restTemplate.getForObject(anyString(), any())).thenReturn(resp); - when(k8sClient.getObjectList(any(), any(), any())).thenReturn(Arrays.asList(getPod(null, getPodStatus("1.1.1.1", "Running")))); + when(k8sClient.getPods(any(), any())).thenReturn(Arrays.asList(getPod(null, getPodStatus("1.1.1.1", "Running")))); String gateway = "gw1"; String host1 = "istio-galley.istio-system.svc.cluster.local"; diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/merger/GatewayRateLimitConfigMapMergerTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/merger/GatewayRateLimitConfigMapMergerTest.java deleted file mode 100644 index 8de282eb..00000000 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/merger/GatewayRateLimitConfigMapMergerTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package org.hango.cloud.core.k8s.merger; - -import org.hango.cloud.meta.ConfigMapRateLimit; -import org.hango.cloud.util.CommonUtil; -import io.fabric8.kubernetes.api.model.ConfigMap; -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - -@SuppressWarnings("java:S1192") -public class GatewayRateLimitConfigMapMergerTest { - - @Test - public void testMerge() { - - RateLimitConfigMapMerger merger = new GatewayRateLimitConfigMapMerger(); - - Map data1 = buildData("config.yaml", - " descriptors:\n" + - " - key: auto-test1-gw1\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: HOUR\n" + - " value: Service[httpbin]-User[none]-Gateway[gw1]-Api[test1]-Id[08638e47-48db\n" + - " - key: auto-test2\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: HOUR\n" + - " value: Service[httpbin]-User[none]-Gateway[gw2]-Api[test2]-Id[08638e47-48db\n" + - " - key: auto-test3\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: HOUR\n" + - " value: Service[httpbin]-User[none]-Gateway[gw3]-Api[test3]-Id[08638e47-48db\n" + - " domain: qingzhou"); - - Map data2 = buildData("config.yaml", - " descriptors:\n" + - " - key: auto-test1-gw2\n" + - " rate_limit:\n" + - " requests_per_unit: 2\n" + - " unit: MINUTE\n" + - " value: Service[httpbin]-User[none]-Gateway[gw2]-Api[test1]-Id[08638e47-b\n" + - " - key: auto-test4\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: HOUR\n" + - " value: Service[httpbin]-User[none]-Gateway[gw4]-Api[test4]-Id[08638e47-48db\n" + - " - key: auto-test3\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: HOUR\n" + - " value: Service[httpbin]-User[none]-Gateway[gw3]-Api[test3]-Id[08638e47-48db\n" + - " domain: qingzhou\n"); - - - ConfigMap configMap1 = buildConfigMap(data1); - ConfigMap configMap2 = buildConfigMap(null); - ConfigMap configMap3 = buildConfigMap(data2); - - String s = CommonUtil.obj2yaml(configMap1); - - Assert.assertEquals(configMap1, merger.merge(configMap1, null)); - Assert.assertEquals(configMap1, merger.merge(null, configMap1)); - Assert.assertEquals(configMap1, merger.merge(configMap1, configMap2)); - Assert.assertEquals(configMap1, merger.merge(configMap2, configMap1)); - - ConfigMap merge1 = merger.merge(configMap1, configMap3); - String raw = merge1.getData().get("config.yaml"); - ConfigMapRateLimit cmrl = CommonUtil.yaml2Obj(raw, ConfigMapRateLimit.class); - - Assert.assertEquals("qingzhou", cmrl.getDomain()); - Assert.assertEquals(5, cmrl.getDescriptors().size()); - - for (ConfigMapRateLimit.ConfigMapRateLimitDescriptor desc : cmrl.getDescriptors()) { - if (desc.getKey().equals("auto-test1-gw1")) { - Assert.assertEquals("HOUR", desc.getRateLimit().getUnit()); - Assert.assertEquals(new Integer(1), desc.getRateLimit().getRequestsPerUnit()); - } else if (desc.getKey().equals("auto-test1-gw2")) { - Assert.assertEquals("MINUTE", desc.getRateLimit().getUnit()); - Assert.assertEquals(new Integer(2), desc.getRateLimit().getRequestsPerUnit()); - } else if (desc.getKey().equals("auto-test2")) { - Assert.assertEquals("HOUR", desc.getRateLimit().getUnit()); - } else if (desc.getKey().equals("auto-test4")) { - Assert.assertEquals("HOUR", desc.getRateLimit().getUnit()); - Assert.assertEquals(new Integer(1), desc.getRateLimit().getRequestsPerUnit()); - } - } - } - - private ConfigMap buildConfigMap(Map data) { - ConfigMap cm = new ConfigMap(); - cm.setData(data); - return cm; - } - - private Map buildData(String key, String val) { - Map data = new HashMap<>(); - data.put(key, val); - return data; - } -} \ No newline at end of file diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/merger/SmartLimiterMergerTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/merger/SmartLimiterMergerTest.java deleted file mode 100644 index b5480d1e..00000000 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/merger/SmartLimiterMergerTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.hango.cloud.core.k8s.merger; - -import org.hango.cloud.util.CommonUtil; -import com.netease.slime.api.microservice.v1alpha1.SmartLimiter; -import com.netease.slime.api.microservice.v1alpha1.SmartLimiterBuilder; -import org.junit.Assert; -import org.junit.Test; - -@SuppressWarnings("java:S1192") -public class SmartLimiterMergerTest { - - @Test - public void testMerge() { - - String yaml1 = "apiVersion: microservice.netease.com/v1alpha1\n" + - "kind: SmartLimiter\n" + - "metadata:\n" + - " name: a\n" + - " namespace: powerful-v13\n" + - "spec:\n" + - " ratelimitConfig:\n" + - " rate_limit_conf:\n" + - " descriptors:\n" + - " - key: header_match\n" + - " unit: 1\n" + - " value: Service[a.default]-User[none]-Gateway[null]-Api[3]-Id[cc41fdbd-5994-46ef-9f8d-213136ce4d88]\n" + - " - key: header_match\n" + - " unit: 3\n" + - " value: Service[a.default]-User[none]-Gateway[null]-Api[3]-Id[bfbd4dd4-a373-427d-949c-ae7c97aa74a0]\n" + - " - key: header_match\n" + - " rate_limit:\n" + - " unit: 1\n" + - " value: Service[a.default]-User[none]-Gateway[null]-Api[3]-Id[849252b3-d6c2-4306-9611-e95071f24fd5]\n" + - " - key: header_match\n" + - " unit: 3\n" + - " value: Service[a.default]-User[none]-Gateway[null]-Api[3]-Id[5db406cf-943b-477a-81ab-a7fc63b567fe]"; - - String yaml2 = "apiVersion: microservice.netease.com/v1alpha1\n" + - "kind: SmartLimiter\n" + - "metadata:\n" + - " name: a\n" + - " namespace: powerful-v13\n" + - "spec:\n" + - " ratelimitConfig:\n" + - " rate_limit_conf:\n" + - " descriptors:\n" + - " - key: header_match\n" + - " unit: 1\n" + - " value: Service[b.default]-User[none]-Gateway[null]-Api[3]-Id[cc41fdbd-5994-46ef-9f8d-213136ce4d88]\n" + - " - key: header_match\n" + - " unit: 3\n" + - " value: Service[b.default]-User[none]-Gateway[null]-Api[3]-Id[bfbd4dd4-a373-427d-949c-ae7c97aa74a0]\n"; - - SmartLimiter sl1 = CommonUtil.yaml2Obj(yaml1, SmartLimiter.class); - SmartLimiter sl2 = CommonUtil.yaml2Obj(yaml2, SmartLimiter.class); - SmartLimiter sl1_1 = CommonUtil.yaml2Obj(yaml1, SmartLimiter.class); - SmartLimiter sl2_1 = CommonUtil.yaml2Obj(yaml2, SmartLimiter.class); - SmartLimiter sl1_2 = CommonUtil.yaml2Obj(yaml1, SmartLimiter.class); - - - SmartLimiter emptySl = new SmartLimiterBuilder().build(); - - SmartLimiterMerger merger = new SmartLimiterMerger(); - SmartLimiter merged1 = merger.merge(sl1, sl2); - Assert.assertEquals(2, merged1.getSpec().getRatelimitConfig().getRateLimitConf().getDescriptors().size()); - - SmartLimiter merged2 = merger.merge(sl2_1, sl1_1); - Assert.assertEquals(4, merged2.getSpec().getRatelimitConfig().getRateLimitConf().getDescriptors().size()); - - SmartLimiter merged3 = merger.merge(sl1_2, emptySl); - Assert.assertNull(merged3.getSpec()); - - SmartLimiter merged4 = merger.merge(emptySl, sl1); - Assert.assertSame(sl1, merged4); - - } -} diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/operator/SidecarOperatorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/operator/SidecarOperatorTest.java deleted file mode 100644 index a9b6ecd1..00000000 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/operator/SidecarOperatorTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package org.hango.cloud.core.k8s.operator; - -import org.hango.cloud.util.CommonUtil; -import me.snowdrop.istio.api.networking.v1alpha3.Sidecar; -import org.junit.Assert; -import org.junit.Test; - -import java.util.List; - -@SuppressWarnings("java:S1192") -public class SidecarOperatorTest { - - @Test - public void testMerge() { - - String yaml1 = "apiVersion: networking.istio.io/v1alpha3\n" + - "kind: Sidecar\n" + - "metadata:\n" + - " name: a\n" + - " namespace: powerful-debug-2\n" + - "spec:\n" + - " workloadSelector:\n" + - " labels:\n" + - " app: a\n" + - " egress:\n" + - " - bind: 0.0.0.0\n" + - " hosts:\n" + - " - \"*/b.powerful-debug-2.svc.cluster.local\"\n" + - " - \"*/c.powerful-debug-2.svc.cluster.local\"\n"; - - String yaml2 = "apiVersion: networking.istio.io/v1alpha3\n" + - "kind: Sidecar\n" + - "metadata:\n" + - " name: a\n" + - " namespace: powerful-debug-2\n" + - "spec:\n" + - " workloadSelector:\n" + - " labels:\n" + - " app: a\n" + - " egress:\n" + - " - bind: 0.0.0.0\n" + - " hosts:\n" + - " - \"*/b.powerful-debug-2.svc.cluster.local\"\n" + - " - \"*/c.powerful-debug-2.svc.cluster.local\"\n" + - " - \"*/d.powerful-debug-2.svc.cluster.local\"\n"; - - String yaml3 = "apiVersion: networking.istio.io/v1alpha3\n" + - "kind: Sidecar\n" + - "metadata:\n" + - " name: a\n" + - " namespace: powerful-debug-2\n" + - "spec:\n" + - " workloadSelector:\n" + - " labels:\n" + - " app: a\n" + - " egress:\n" + - " - bind: 0.0.0.0\n" + - " hosts:\n" + - " - \"*/b.powerful-debug-2.svc.cluster.local\"\n"; - - - Sidecar sidecar1 = CommonUtil.yaml2Obj(yaml1, Sidecar.class); - Sidecar sidecar2 = CommonUtil.yaml2Obj(yaml2, Sidecar.class); - - Sidecar sidecar1_1 = CommonUtil.yaml2Obj(yaml1, Sidecar.class); - Sidecar sidecar3 = CommonUtil.yaml2Obj(yaml3, Sidecar.class); - - SidecarOperator operator = new SidecarOperator(); - - Sidecar merged1 = operator.merge(sidecar1, null); - Assert.assertEquals(sidecar1, merged1); - Sidecar merged2 = operator.merge(null, sidecar2); - Assert.assertEquals(sidecar2, merged2); - Sidecar merged3 = operator.merge(sidecar1, sidecar2); - Assert.assertEquals(3, getHosts(merged3).size()); - Sidecar merged4 = operator.merge(sidecar1_1, sidecar3); - Assert.assertEquals(2, getHosts(merged4).size()); - - } - - private List getHosts(Sidecar sidecar) { - return sidecar.getSpec().getEgress().get(0).getHosts(); - } - -} diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/subtracter/GatewayRateLimitConfigMapSubtracterTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/subtracter/GatewayRateLimitConfigMapSubtracterTest.java deleted file mode 100644 index c502a620..00000000 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/k8s/subtracter/GatewayRateLimitConfigMapSubtracterTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.hango.cloud.core.k8s.subtracter; - -import org.hango.cloud.meta.ConfigMapRateLimit; -import org.hango.cloud.util.CommonUtil; -import io.fabric8.kubernetes.api.model.ConfigMap; -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - -public class GatewayRateLimitConfigMapSubtracterTest { - - @Test - public void subtract() { - - Map data1 = buildData("config.yaml", - " descriptors:\n" + - " - key: auto-test1\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: HOUR\n" + - " value: Service[httpbin]-User[none]-Gateway[gw1]-Api[test1]-Id[08638e47-48db\n" + - " - key: auto-test1\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: MINUTE\n" + - " value: Service[httpbin]-User[none]-Gateway[gw1]-Api[test1]-Id[08638e47-\n" + - " - key: auto-test3\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: HOUR\n" + - " value: Service[httpbin]-User[none]-Gateway[gw1]-Api[test3]-Id[08638e47-48db\n" + - " - key: auto-test1\n" + - " rate_limit:\n" + - " requests_per_unit: 1\n" + - " unit: MINUTE\n" + - " value: Service[httpbin]-User[none]-Gateway[gw2]-Api[test1]-Id[08638e47-\n" + - " domain: qingzhou"); - - - RateLimitConfigMapSubtracter subtracter = new GatewayRateLimitConfigMapSubtracter("gw1", "test1"); - ConfigMap configMap1 = buildConfigMap(data1); - ConfigMap subtracted = subtracter.subtract(configMap1); - - String rawData = subtracted.getData().get("config.yaml"); - ConfigMapRateLimit rateLimitConfig = CommonUtil.yaml2Obj(rawData, ConfigMapRateLimit.class); - - Assert.assertEquals(2, rateLimitConfig.getDescriptors().size()); - ConfigMapRateLimit.ConfigMapRateLimitDescriptor oneRateLimit = rateLimitConfig.getDescriptors().get(0); - Assert.assertEquals("auto-test3", oneRateLimit.getKey()); - Assert.assertEquals("HOUR", oneRateLimit.getRateLimit().getUnit()); - Assert.assertEquals(new Integer(1), oneRateLimit.getRateLimit().getRequestsPerUnit()); - } - - private ConfigMap buildConfigMap(Map data) { - ConfigMap cm = new ConfigMap(); - cm.setData(data); - return cm; - } - - private Map buildData(String key, String val) { - Map data = new HashMap<>(); - data.put(key, val); - return data; - } -} \ No newline at end of file diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/AuthProcessorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/AuthProcessorTest.java index 891211fe..e1b07e15 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/AuthProcessorTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/AuthProcessorTest.java @@ -22,14 +22,14 @@ private void testSimpleAuth() { "\"appNameSetting\":{\"parameterType\":\"header\",\"parameterName\":\"x-auth\"}}"; FragmentHolder f = simpleAuthProcessor.process(plugin, serviceInfo); - System.out.println(f.getVirtualServiceFragment().getContent().trim()); + System.out.println(f.getGatewayPluginsFragment().getContent().trim()); Assert.assertEquals("need_authorization: false\n" + "missing_auth_allow: false\n" + - "authn_policy_name: proxy.super_authz.authn_policy.token_authn_policy\n" + + "authn_policy_name: \"proxy.super_authz.authn_policy.token_authn_policy\"\n" + "authn_policy_config:\n" + - " token_format: ANY\n" + - " token_source: x-auth\n" + - " token_rename: authorization\n" + - " send_context: true", f.getVirtualServiceFragment().getContent().trim()); + " token_format: \"ANY\"\n" + + " token_source: \"x-auth\"\n" + + " token_rename: \"authorization\"\n" + + " send_context: true", f.getGatewayPluginsFragment().getContent().trim()); } } \ No newline at end of file diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/DynamicDowngradeProcessorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/DynamicDowngradeProcessorTest.java index 17338a9e..c07a72ae 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/DynamicDowngradeProcessorTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/DynamicDowngradeProcessorTest.java @@ -182,6 +182,6 @@ public void process() { + " \"200\": 50\n" + "key_maker:\n" + " query_params: []\n" - + " headers_keys: []", f3.getVirtualServiceFragment().getContent().trim()); + + " headers_keys: []", f3.getGatewayPluginsFragment().getContent().trim()); } } \ No newline at end of file diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/LocalCacheProcessorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/LocalCacheProcessorTest.java index 307a9f69..6b75445c 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/LocalCacheProcessorTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/LocalCacheProcessorTest.java @@ -59,6 +59,6 @@ public void process() { + " regex: \"200|\"\n" + "cache_ttls:\n" + " LocalHttpCache:\n" - + " default: 2000000", f.getVirtualServiceFragment().getContent().trim()); + + " default: 2000000", f.getGatewayPluginsFragment().getContent().trim()); } } \ No newline at end of file diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RateLimitProcessorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RateLimitProcessorTest.java deleted file mode 100644 index 980bf171..00000000 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RateLimitProcessorTest.java +++ /dev/null @@ -1,172 +0,0 @@ -package org.hango.cloud.core.plugin.processor; - -import org.hango.cloud.core.plugin.FragmentHolder; -import org.junit.Assert; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; - -public class RateLimitProcessorTest extends BasePluginTest { - - @Autowired - RateLimitProcessor processor; - - @Test - public void process() { - - String plugin1 = "{\n" + - " \"kind\": \"rate-limiting\",\n" + - " \"limit_by_list\": [\n" + - " {\n" + - " \"identifier_extractor\": \"Header[plugin]\",\n" + - " \"pre_condition\": [\n" + - " {\n" + - " \"operator\": \"=\",\n" + - " \"right_value\": \"ratelimit\"\n" + - " }\n" + - " ],\n" + - " \"second\": 5,\n" + - " \"hour\": 10\n" + - " }\n" + - " ]\n" + - "}"; - - String plugin2 = "{\n" + - " \"kind\": \"rate-limiting\",\n" + - " \"limit_by_list\": [\n" + - " {\n" + - " \"identifier_extractor\": \"Header[plugin]\",\n" + - " \"pre_condition\": [\n" + - " {\n" + - " \"operator\": \"present\",\n" + - " \"invert\": true\n" + - " }\n" + - " ],\n" + - " \"hour\": 1\n" + - " }\n" + - " ]\n" + - "}"; - - String plugin3 = "{\n" + - " \"kind\": \"rate-limiting\",\n" + - " \"limit_by_list\": [\n" + - " {\n" + - " \"pre_condition\": [\n" + - " {\n" + - " \"custom_extractor\": \"Header[plugin1]\",\n" + - " \"operator\": \"present\",\n" + - " \"invert\": true\n" + - " },\n" + - " {\n" + - " \"custom_extractor\": \"Header[plugin2]\",\n" + - " \"operator\": \"=\",\n" + - " \"right_value\": \"ratelimit\"\n" + - " }\n" + - " ],\n" + - " \"hour\": 1\n" + - " }\n" + - " ]\n" + - "}"; - String plugin4 = "{\n" + - " \"kind\": \"rate-limiting\",\n" + - " \"limit_by_list\": [\n" + - " {\n" + - " \"pre_condition\": [\n" + - " {\n" + - " \"custom_extractor\": \"Header[plugin1]\",\n" + - " \"operator\": \"present\",\n" + - " \"invert\": true\n" + - " },\n" + - " {\n" + - " \"custom_extractor\": \"Header[plugin2]\",\n" + - " \"operator\": \"=\",\n" + - " \"right_value\": \"ratelimit\"\n" + - " }\n" + - " ],\n" + - " \"hour\": 1,\n" + - " \"type\": \"Local\"\n" + - " }\n" + - " ]\n" + - "}"; - - String plugin5 = "{\n" + - " \"kind\": \"rate-limiting\",\n" + - " \"limit_by_list\": [\n" + - " {\n" + - " \"hour\": 1\n" + - " }\n" + - " ]\n" + - "}"; - - String plugin6 = "{\n" + - " \"kind\": \"rate-limiting\",\n" + - " \"limit_by_list\": [\n" + - " {\n" + - " \"identifier_extractor\": \"Header[plugin]\",\n" + - " \"second\": 5,\n" + - " \"hour\": 10\n" + - " }\n" + - " ]\n" + - "}"; - - String plugin7 = "{\n" + - " \"kind\": \"rate-limiting\",\n" + - " \"limit_by_list\": [\n" + - " {\n" + - " \"identifier_extractor\": \"Header[plugin]\",\n" + - " \"second\": 5,\n" + - " \"hour\": \"\"\n" + - " }\n" + - " ]\n" + - "}"; - - FragmentHolder fragment1 = processor.process(plugin1, serviceInfo); - FragmentHolder fragment2 = processor.process(plugin2, serviceInfo); - FragmentHolder fragment3 = processor.process(plugin3, serviceInfo); - FragmentHolder fragment4 = processor.process(plugin4, serviceInfo); - FragmentHolder fragment5 = processor.process(plugin4, nullInfo); - FragmentHolder fragment6 = processor.process(plugin5, serviceInfo); - FragmentHolder fragment7 = processor.process(plugin6, serviceInfo); - FragmentHolder fragment8 = processor.process(plugin7, serviceInfo); - //TODO assert - - Assert.assertEquals("domain: \"qingzhou\"\n" + - "descriptors:\n" + - "- key: \"generic_key\"\n" + - " value: \"Service[svvc]-User[none]-Gateway[proxy]-Api[api]-Id[hash:-1360152201]\"\n" + - " descriptors:\n" + - " - key: \"WithoutValueHeader[plugin]\"\n" + - " rate_limit:\n" + - " unit: \"SECOND\"\n" + - " requests_per_unit: 5", fragment8.getSharedConfigFragment().getContent().trim()); - } - - @Test - public void hash() { - String plugin1 = "{\n" + - " \"kind\": \"rate-limiting\",\n" + - " \"limit_by_list\": [\n" + - " {\n" + - " \"pre_condition\": [\n" + - " {\n" + - " \"custom_extractor\": \"Header[plugin1]\",\n" + - " \"operator\": \"present\",\n" + - " \"invert\": true\n" + - " },\n" + - " {\n" + - " \"custom_extractor\": \"Header[plugin2]\",\n" + - " \"operator\": \"=\",\n" + - " \"right_value\": \"ratelimit\"\n" + - " }\n" + - " ],\n" + - " \"hour\": 1,\n" + - " \"second\": 1\n" + - " }\n" + - " ]\n" + - "}"; - - - FragmentHolder fragment1 = processor.process(plugin1, serviceInfo); - FragmentHolder fragment2 = processor.process(plugin1, serviceInfo); - Assert.assertEquals(fragment1.getSharedConfigFragment().getContent(),fragment2.getSharedConfigFragment().getContent()); - } -} \ No newline at end of file diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RedisCacheProcessorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RedisCacheProcessorTest.java index c77d99f5..e811d1db 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RedisCacheProcessorTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RedisCacheProcessorTest.java @@ -58,6 +58,6 @@ public void process() { + " regex: \"200|\"\n" + "cache_ttls:\n" + " RedisHttpCache:\n" - + " default: 2000", f.getVirtualServiceFragment().getContent().trim()); + + " default: 2000", f.getGatewayPluginsFragment().getContent().trim()); } } \ No newline at end of file diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RequestBodyReWriteProcessorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RequestBodyReWriteProcessorTest.java index 90c5aa3e..80543784 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RequestBodyReWriteProcessorTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/RequestBodyReWriteProcessorTest.java @@ -65,6 +65,6 @@ public void process() { " json_body_transformation:\n" + " json_transformations:\n" + " - json_pointer: \"/a\"\n" + - " json_value: \"2\"", f.getVirtualServiceFragment().getContent().trim()); + " json_value: \"2\"", f.getGatewayPluginsFragment().getContent().trim()); } } diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/ResponseBodyReWriteProcessorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/ResponseBodyReWriteProcessorTest.java index e24d0b29..be8523dd 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/ResponseBodyReWriteProcessorTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/ResponseBodyReWriteProcessorTest.java @@ -118,6 +118,6 @@ public void process() { " - json_pointer: \"/code\"\n" + " json_value: \"200\"\n" + " - json_pointer: \"/messgae\"\n" + - " json_value: \"success\"", f.getVirtualServiceFragment().getContent().trim()); + " json_value: \"success\"", f.getGatewayPluginsFragment().getContent().trim()); } } diff --git a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/WafProcessorTest.java b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/WafProcessorTest.java index 78decfcd..f25ec3ce 100644 --- a/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/WafProcessorTest.java +++ b/hango-api-plane-server/src/test/java/org/hango/cloud/core/plugin/processor/WafProcessorTest.java @@ -50,7 +50,7 @@ public void process() { " tx.dos_burst_time_slice: 2\n" + " tx.dos_counter_threshold: 200\n" + "- waf_rule_path: \"/etc/envoy/waf/REQUEST-913-SCANNER-DETECTION.conf\""; - String actual = f.getVirtualServiceFragment().getContent().trim(); + String actual = f.getGatewayPluginsFragment().getContent().trim(); System.out.println("================================================================================================"); System.out.println("expected value is :" + expected); System.out.println("================================================================================================"); diff --git a/hango-api-plane-server/src/test/resources/mesh-config.properties b/hango-api-plane-server/src/test/resources/mesh-config.properties deleted file mode 100644 index 5f976640..00000000 --- a/hango-api-plane-server/src/test/resources/mesh-config.properties +++ /dev/null @@ -1,3 +0,0 @@ -meshProjectKey=nsf.skiff.netease.com/project -meshVersionKey=nsf.skiff.netease.com/version -meshAppKey=nsf.skiff.netease.com/app \ No newline at end of file diff --git a/hango-proto/src/main/proto/networking/v1alpha3/destination_rule.proto b/hango-proto/src/main/proto/networking/v1alpha3/destination_rule.proto index 7eac4140..6b3cd1a3 100644 --- a/hango-proto/src/main/proto/networking/v1alpha3/destination_rule.proto +++ b/hango-proto/src/main/proto/networking/v1alpha3/destination_rule.proto @@ -17,6 +17,8 @@ import "google/protobuf/duration.proto"; import "networking/v1alpha3/virtual_service.proto"; import "gogoproto/gogo.proto"; import "google/protobuf/wrappers.proto"; +import "google/protobuf/struct.proto"; + // $title: Destination Rule // $description: Configuration affecting load balancing, outlier detection, etc. @@ -147,6 +149,11 @@ message DestinationRule { // mark default cluster for specific proxy(envoy) map gwLabels = 21; + + google.protobuf.Struct meta = 22; + + // metadata info for qz,'stats_meta' fields here + map metadata = 23; } // Traffic policies to apply for a specific destination, across all @@ -261,6 +268,11 @@ message Subset { // mark subset for specific proxy(envoy) map gwLabels = 22; + + google.protobuf.Struct meta = 23; + + // metadata info for qz, 'stats_meta' fields here + map metadata = 24; } // Load balancing policies to apply for a specific destination. See Envoy's diff --git a/hango-proto/src/main/proto/networking/v1alpha3/plugin_manager.proto b/hango-proto/src/main/proto/networking/v1alpha3/plugin_manager.proto index 1d00f2c7..8aeed25b 100644 --- a/hango-proto/src/main/proto/networking/v1alpha3/plugin_manager.proto +++ b/hango-proto/src/main/proto/networking/v1alpha3/plugin_manager.proto @@ -80,6 +80,7 @@ message Plugin { Wasm wasm = 6; // plugin compiled inside envoy Inline inline = 7; + Rider rider = 9; } uint32 port = 8; } @@ -90,6 +91,17 @@ message Wasm { google.protobuf.Struct settings = 3; } +message Rider { + google.protobuf.Struct settings = 1; + string plugin_name = 2; + string url = 3; + string sha256 = 4; + oneof image_pull_secret { + string image_pull_secret_name = 5; + string image_pull_secret_content = 6; + } +} + message Inline { google.protobuf.Struct settings = 1;