Skip to content

Commit

Permalink
For code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion committed Nov 5, 2024
1 parent 6ffeaac commit b4a284a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@

package com.alibaba.nacos.config.server.model.event;

/**
* XDS config change event.
*
* @author PoisonGravity
*/
public class IstioConfigChangeEvent extends ConfigDataChangeEvent {

private static final long serialVersionUID = -2618455009648617192L;

public final String content;

public final String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

import java.util.Arrays;

/**
* Config Tag util.
*
* @author PoisonGravity
*/
public class ConfigTagUtil {

public static final String VIRTUAL_SERVICE = "virtual-service";
Expand Down
75 changes: 40 additions & 35 deletions istio/src/main/java/com/alibaba/nacos/istio/xds/LdsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
import static com.alibaba.nacos.istio.api.ApiConstants.LISTENER_TYPE;
import static io.envoyproxy.envoy.config.core.v3.ApiVersion.V2_VALUE;

/**
* LDS of XDS protocol generator.
*
* @author PoisonGravity
*/
public class LdsGenerator implements ApiGenerator<Any> {

public static final String INIT_LISTENER = "bootstrap_listener";
Expand All @@ -72,7 +77,7 @@ public class LdsGenerator implements ApiGenerator<Any> {

public static final String DEFAULT_FILTER_TYPE = "envoy.filters.network.http_connection_manager";

public static final String DEFAULT_HTTPMANAGER_PREFIX = "ingress_http";
public static final String DEFAULT_HTTPMANAGER_PREFIX = "ingress_http";

public static final String DEFAULT_HTTP_ROUTER_TYPE = "envoy.filters.http.router";

Expand All @@ -98,13 +103,14 @@ public List<Any> generate(PushRequest pushRequest) {
.getIstioServiceMap();
List<Any> result = new ArrayList<>();
result.add(buildBootstrapListener());

if (pushRequest.isFull()) {
for (Map.Entry<String, IstioService> entry : istioServiceMap.entrySet()) {
IstioService istioService = entry.getValue();
if (istioService != null) {
String rdsName = entry.getKey() + ROUTE_CONFIGURATION_SUFFIX;
result.add(buildDynamicListener(entry.getKey(), INIT_LISTENER_ADDRESS, istioService.getPort(), rdsName));
result.add(buildDynamicListener(entry.getKey(), INIT_LISTENER_ADDRESS, istioService.getPort(),
rdsName));
} else {
Loggers.MAIN.error("Attempt to create listener for non-existent service");
}
Expand All @@ -122,8 +128,9 @@ public List<Resource> deltaGenerate(PushRequest pushRequest) {
* Constructs a default Envoy listener configuration with specified parameters.
* This method prepares the necessary configurations for both bootstrap and non-bootstrap scenarios.
*/
private static Any buildDefaultListener(String listenerName, String listenerAddress, int listenerPort, String rdsName, boolean isBootstrap) {
if (!isBootstrap && ((listenerName == null) || (listenerPort == 0) || (rdsName == null))) {
private static Any buildDefaultListener(String listenerName, String listenerAddress, int listenerPort,
String rdsName, boolean isBootstrap) {
if (isValid(listenerName, listenerPort, rdsName, isBootstrap)) {
Loggers.MAIN.error("Listener name, Listener port and RDS name cannot be null.");
return null;
}
Expand All @@ -135,29 +142,20 @@ private static Any buildDefaultListener(String listenerName, String listenerAddr
listenerBuilder.setAddress(Address.newBuilder()
.setSocketAddress(SocketAddress.newBuilder().setAddress(listenerAddress).setPortValue(portValue)));

String routeConfigName = isBootstrap ? INIT_LISTENER + ROUTE_CONFIGURATION_SUFFIX : listenerName + ROUTE_CONFIGURATION_SUFFIX;
String routeConfigName =
isBootstrap ? INIT_LISTENER + ROUTE_CONFIGURATION_SUFFIX : listenerName + ROUTE_CONFIGURATION_SUFFIX;
String virtualHostName = isBootstrap ? INIT_LISTENER : listenerName;

RouteConfiguration routeConfiguration = RouteConfiguration.newBuilder()
.setName(routeConfigName)
.addVirtualHosts(
VirtualHost.newBuilder()
.setName(virtualHostName)
.addDomains("*")
.addRoutes(
Route.newBuilder()
.setMatch(RouteMatch.newBuilder().setPrefix("/").build())
.setRoute(RouteAction.newBuilder().setCluster(
BOOTSTRAP_UPSTREAM_CLUSTER).build())
.build()
)
.build()
)
.build();

HttpConnectionManager httpConnectionManager = HttpConnectionManager.newBuilder().setStatPrefix(DEFAULT_HTTPMANAGER_PREFIX)
.addAccessLog(buildAccessLog()).setCodecType(HttpConnectionManager.CodecType.AUTO)
.setRouteConfig(routeConfiguration).addHttpFilters(createHttpFilter()).build();
RouteConfiguration routeConfiguration = RouteConfiguration.newBuilder().setName(routeConfigName)
.addVirtualHosts(VirtualHost.newBuilder().setName(virtualHostName).addDomains("*").addRoutes(
Route.newBuilder().setMatch(RouteMatch.newBuilder().setPrefix("/").build())
.setRoute(RouteAction.newBuilder().setCluster(BOOTSTRAP_UPSTREAM_CLUSTER).build())
.build()).build()).build();

HttpConnectionManager httpConnectionManager = HttpConnectionManager.newBuilder()
.setStatPrefix(DEFAULT_HTTPMANAGER_PREFIX).addAccessLog(buildAccessLog())
.setCodecType(HttpConnectionManager.CodecType.AUTO).setRouteConfig(routeConfiguration)
.addHttpFilters(createHttpFilter()).build();

listenerBuilder.addFilterChains(createFilterChain(httpConnectionManager));

Expand All @@ -178,7 +176,8 @@ private static Any buildBootstrapListener() {
* Constructs a default listener configuration for static environments.
* This method is designed to provide configurations for scenarios where dynamic discovery services might not be used.
*/
private static Any buildDefaultStaticListener(String listenerName, String listenerAddress, int listenerPort, String rdsName) {
private static Any buildDefaultStaticListener(String listenerName, String listenerAddress, int listenerPort,
String rdsName) {
if (INIT_LISTENER.equals(listenerName)) {
return buildBootstrapListener();
}
Expand All @@ -202,16 +201,18 @@ private static Any buildDynamicListener(String listenerName, String listenerAddr

listenerAddress = (listenerAddress == null) ? INIT_LISTENER_ADDRESS : listenerAddress;
Listener.Builder listenerBuilder = Listener.newBuilder().setName(listenerName);

listenerBuilder.setAddress(Address.newBuilder()
.setSocketAddress(SocketAddress.newBuilder().setAddress(listenerAddress).setPortValue(listenerPort + DEFAULT_PORT_INCREMENT)));

listenerBuilder.setAddress(Address.newBuilder().setSocketAddress(
SocketAddress.newBuilder().setAddress(listenerAddress)
.setPortValue(listenerPort + DEFAULT_PORT_INCREMENT)));
ConfigSource configSource = createConfigSource();

Rds rds = Rds.newBuilder().setConfigSource(configSource).setRouteConfigName(rdsName).build();

HttpConnectionManager httpConnectionManager = HttpConnectionManager.newBuilder().setStatPrefix(DEFAULT_HTTPMANAGER_PREFIX)
.addAccessLog(buildAccessLog()).setCodecType(HttpConnectionManager.CodecType.AUTO).setRds(rds)
.addHttpFilters(createHttpFilter()).build();
HttpConnectionManager httpConnectionManager = HttpConnectionManager.newBuilder()
.setStatPrefix(DEFAULT_HTTPMANAGER_PREFIX).addAccessLog(buildAccessLog())
.setCodecType(HttpConnectionManager.CodecType.AUTO).setRds(rds).addHttpFilters(createHttpFilter())
.build();

listenerBuilder.addFilterChains(createFilterChain(httpConnectionManager));

Expand Down Expand Up @@ -239,7 +240,11 @@ private static HttpFilter createHttpFilter() {

private static FilterChain createFilterChain(HttpConnectionManager httpConnectionManager) {
return FilterChain.newBuilder().setName(DEFAULT_FILTER_CHAIN_NAME).addFilters(
Filter.newBuilder().setName(DEFAULT_FILTER_TYPE)
.setTypedConfig(Any.pack(httpConnectionManager))).build();
Filter.newBuilder().setName(DEFAULT_FILTER_TYPE).setTypedConfig(Any.pack(httpConnectionManager)))
.build();
}

private static boolean isValid(String listenerName, int listenerPort, String rdsName, boolean isBootstrap) {
return !isBootstrap && ((listenerName == null) || (listenerPort == 0) || (rdsName == null));
}
}
91 changes: 43 additions & 48 deletions istio/src/main/java/com/alibaba/nacos/istio/xds/RdsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
import static com.alibaba.nacos.istio.api.ApiConstants.ROUTE_TYPE;
import static com.alibaba.nacos.istio.util.IstioCrdUtil.buildClusterName;

/**
* Rds of Xds Generator.
*
* @author PoisonGravity
*/
public class RdsGenerator implements ApiGenerator<Any> {

public static final String DEFAULT_ROUTE_CONFIGURATION = "default_route_configuration";
Expand Down Expand Up @@ -70,28 +75,24 @@ public static RdsGenerator getInstance() {

@Override
public List<Any> generate(PushRequest pushRequest) {

List<Any> result = new ArrayList<>();
Set<String> reasons = pushRequest.getReason();
if (reasons.contains(DEFAULT_ROUTE_CONFIGURATION)) {
reasons.stream()
.filter(reason -> !DEFAULT_ROUTE_CONFIGURATION.equals(reason))
.forEach(reason -> {
result.add(buildDefaultRouteConfiguration(reason));
});
reasons.stream().filter(reason -> !DEFAULT_ROUTE_CONFIGURATION.equals(reason)).forEach(reason -> {
result.add(buildDefaultRouteConfiguration(reason));
});
} else if (reasons.contains(CONFIG_REASON)) {
reasons.stream()
.filter(reason -> !CONFIG_REASON.equals(reason))
.forEach(reason -> {
VirtualService vs = parseContent(reason, VirtualService.class);
result.add(generateRdsFromVirtualService(vs, pushRequest));
});
reasons.stream().filter(reason -> !CONFIG_REASON.equals(reason)).forEach(reason -> {
VirtualService vs = parseContent(reason, VirtualService.class);
result.add(generateRdsFromVirtualService(vs, pushRequest));
});
} else {
reasons.forEach(reason -> {
result.add(buildDefaultRouteConfiguration(reason));
});
}

return result;
}

Expand All @@ -106,23 +107,14 @@ private static Any buildDefaultRouteConfiguration(String routeConfigurationName)
}
String virtualHostName = routeConfigurationName;
if (routeConfigurationName.endsWith(ROUTE_CONFIGURATION_SUFFIX)) {
virtualHostName = routeConfigurationName.substring(0, routeConfigurationName.length() - ROUTE_CONFIGURATION_SUFFIX.length());
virtualHostName = routeConfigurationName.substring(0,
routeConfigurationName.length() - ROUTE_CONFIGURATION_SUFFIX.length());
}
RouteConfiguration routeConfiguration = RouteConfiguration.newBuilder()
.setName(routeConfigurationName)
.addVirtualHosts(
VirtualHost.newBuilder()
.setName(virtualHostName)
.addDomains("*")
.addRoutes(
Route.newBuilder()
.setMatch(RouteMatch.newBuilder().setPrefix("/").build())
.setRoute(RouteAction.newBuilder().setCluster(BOOTSTRAP_UPSTREAM_CLUSTER).build())
.build()
)
.build()
)
.build();
RouteConfiguration routeConfiguration = RouteConfiguration.newBuilder().setName(routeConfigurationName)
.addVirtualHosts(VirtualHost.newBuilder().setName(virtualHostName).addDomains("*").addRoutes(
Route.newBuilder().setMatch(RouteMatch.newBuilder().setPrefix("/").build())
.setRoute(RouteAction.newBuilder().setCluster(BOOTSTRAP_UPSTREAM_CLUSTER).build())
.build()).build()).build();

return Any.newBuilder().setValue(routeConfiguration.toByteString()).setTypeUrl(ROUTE_TYPE).build();
}
Expand All @@ -136,7 +128,8 @@ private static Any buildDefaultRouteConfiguration(String routeConfigurationName)
public static Any generateRdsFromVirtualService(VirtualService virtualService, PushRequest pushRequest) {
List<VirtualService.Spec.Http> httpRoutes = virtualService.getSpec().getHttp();
List<String> hosts = virtualService.getSpec().getHosts();
Map<String, IstioService> istioServiceMap = pushRequest.getResourceSnapshot().getIstioResources().getIstioServiceMap();
Map<String, IstioService> istioServiceMap = pushRequest.getResourceSnapshot().getIstioResources()
.getIstioServiceMap();
List<String> hostnames = getMatchingHostnames(hosts, pushRequest);
String virtualHostName = virtualService.getMetadata().getName();
for (Map.Entry<String, IstioService> entry : istioServiceMap.entrySet()) {
Expand All @@ -146,19 +139,18 @@ public static Any generateRdsFromVirtualService(VirtualService virtualService, P
Loggers.MAIN.info("Setting virtualHostName: {}", virtualHostName);
}
}
VirtualHost.Builder virtualHostBuilder = VirtualHost.newBuilder()
.setName(virtualHostName)
VirtualHost.Builder virtualHostBuilder = VirtualHost.newBuilder().setName(virtualHostName)
.addAllDomains(hostnames);

for (VirtualService.Spec.Http httpRoute : httpRoutes) {
processHttpRoute(httpRoute, virtualHostBuilder, pushRequest);
}

RouteConfiguration routeConfiguration = RouteConfiguration.newBuilder()
.setName(virtualService.getMetadata().getName() + ROUTE_CONFIGURATION_SUFFIX).addVirtualHosts(virtualHostBuilder)
.build();
.setName(virtualService.getMetadata().getName() + ROUTE_CONFIGURATION_SUFFIX)
.addVirtualHosts(virtualHostBuilder).build();
return Any.newBuilder().setValue(routeConfiguration.toByteString()).setTypeUrl(ROUTE_TYPE).build();

}

private <T> T parseContent(String content, Class<T> valueType) {
Expand All @@ -167,16 +159,17 @@ private <T> T parseContent(String content, Class<T> valueType) {

private static List<String> getMatchingHostnames(List<String> hosts, PushRequest pushRequest) {
List<String> hostnames = new ArrayList<>();
Map<String, IstioService> istioServiceMap = pushRequest.getResourceSnapshot().getIstioResources().getIstioServiceMap();
for (String host: hosts) {
Map<String, IstioService> istioServiceMap = pushRequest.getResourceSnapshot().getIstioResources()
.getIstioServiceMap();
for (String host : hosts) {
if ("*".equals(host)) {
hostnames.add("*");
break;
}
for (Map.Entry<String, IstioService> entry : istioServiceMap.entrySet()) {
if (entry.getKey().contains(host)) {
String hostname = buildClusterName(TrafficDirection.OUTBOUND, "",
host + DOMAIN_SUFFIX, entry.getValue().getPort());
String hostname = buildClusterName(TrafficDirection.OUTBOUND, "", host + DOMAIN_SUFFIX,
entry.getValue().getPort());
Loggers.MAIN.info("Matching hostname: {}", hostname);
hostnames.add(hostname);
}
Expand All @@ -185,7 +178,8 @@ private static List<String> getMatchingHostnames(List<String> hosts, PushRequest
return hostnames;
}

private static void processHttpRoute(VirtualService.Spec.Http httpRoute, VirtualHost.Builder virtualHostBuilder, PushRequest pushRequest) {
private static void processHttpRoute(VirtualService.Spec.Http httpRoute, VirtualHost.Builder virtualHostBuilder,
PushRequest pushRequest) {
Route.Builder routeBuilder = Route.newBuilder();

if (httpRoute.getName() != null) {
Expand All @@ -198,15 +192,14 @@ private static void processHttpRoute(VirtualService.Spec.Http httpRoute, Virtual
routeMatchBuilder.setPrefix(match.getUri().getPrefix());
} else if (match.getUri().getExact() != null) {
routeMatchBuilder.setPath(match.getUri().getExact());
} else if (match.getUri().getRegex() != null) { // 检查是否定义了正则表达式
RegexMatcher regexMatcher = RegexMatcher.newBuilder()
.setRegex(match.getUri().getRegex())
.build();
} else if (match.getUri().getRegex() != null) {
// 检查是否定义了正则表达式
RegexMatcher regexMatcher = RegexMatcher.newBuilder().setRegex(match.getUri().getRegex()).build();
routeMatchBuilder.setSafeRegex(regexMatcher);
}
routeBuilder.setMatch(routeMatchBuilder);
}

if (httpRoute.getRedirect() != null) {
setRedirectAction(httpRoute.getRedirect(), routeBuilder);
} else {
Expand All @@ -215,8 +208,10 @@ private static void processHttpRoute(VirtualService.Spec.Http httpRoute, Virtual
virtualHostBuilder.addRoutes(routeBuilder);
}

private static void setRouteAction(VirtualService.Spec.Http httpRoute, Route.Builder routeBuilder, PushRequest pushRequest) {
Map<String, IstioService> istioServiceMap = pushRequest.getResourceSnapshot().getIstioResources().getIstioServiceMap();
private static void setRouteAction(VirtualService.Spec.Http httpRoute, Route.Builder routeBuilder,
PushRequest pushRequest) {
Map<String, IstioService> istioServiceMap = pushRequest.getResourceSnapshot().getIstioResources()
.getIstioServiceMap();
RouteAction.Builder routeActionBuilder = RouteAction.newBuilder();
String hostName = httpRoute.getRoute().get(0).getDestination().getHost();
if (httpRoute.getRewrite() != null) {
Expand Down

0 comments on commit b4a284a

Please sign in to comment.