-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: daizhenyu <1449308021@qq.com>
- Loading branch information
Showing
10 changed files
with
1,053 additions
and
94 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
...mplement/src/main/java/io/sermant/implement/service/xds/utils/CdsProtocolTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sermant.implement.service.xds.utils; | ||
|
||
import io.envoyproxy.envoy.config.cluster.v3.Cluster; | ||
import io.envoyproxy.envoy.config.cluster.v3.Cluster.LbPolicy; | ||
import io.sermant.core.service.xds.entity.XdsCluster; | ||
import io.sermant.core.service.xds.entity.XdsLbPolicy; | ||
import io.sermant.core.service.xds.entity.XdsServiceCluster; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Convert cds protocol data to Sermant data model | ||
* | ||
* @author daizhenyu | ||
* @since 2024-05-10 | ||
**/ | ||
public class CdsProtocolTransformer { | ||
private static final int CLUSTER_SUBSET_INDEX = 2; | ||
|
||
private static final String VERTICAL_LINE_SEPARATOR = "\\|"; | ||
|
||
private CdsProtocolTransformer() { | ||
} | ||
|
||
/** | ||
* get the mapping between service name of k8s and cluster of istio | ||
* | ||
* @param clusters clusters | ||
* @return XdsServiceCluster map | ||
*/ | ||
public static Map<String, XdsServiceCluster> getServiceClusters(List<Cluster> clusters) { | ||
Map<String, Set<XdsCluster>> xdsClusters = clusters.stream() | ||
.filter(Objects::nonNull) | ||
.map(cluster -> parseCluster(cluster)) | ||
.filter(xdsCluster -> xdsCluster.getServiceName() != null) | ||
.collect(Collectors.groupingBy( | ||
XdsCluster::getServiceName, | ||
Collectors.toSet() | ||
)); | ||
Map<String, XdsServiceCluster> xdsServiceClusterMap = new HashMap<>(); | ||
for (Entry<String, Set<XdsCluster>> clusterEntry : xdsClusters.entrySet()) { | ||
XdsServiceCluster serviceCluster = new XdsServiceCluster(); | ||
serviceCluster.setBaseClusterName(getServiceBaseClusterName(clusterEntry.getValue())); | ||
Map<String, XdsCluster> clusterMap = clusterEntry.getValue().stream() | ||
.collect(Collectors.toMap( | ||
XdsCluster::getClusterName, | ||
xdsCluster -> xdsCluster | ||
)); | ||
serviceCluster.setClusters(clusterMap); | ||
xdsServiceClusterMap.put(clusterEntry.getKey(), serviceCluster); | ||
} | ||
return xdsServiceClusterMap; | ||
} | ||
|
||
private static XdsCluster parseCluster(Cluster cluster) { | ||
XdsCluster xdsCluster = new XdsCluster(); | ||
Optional<String> serviceNameFromCluster = XdsCommonUtils.getServiceNameFromCluster(cluster.getName()); | ||
if (!serviceNameFromCluster.isPresent()) { | ||
return xdsCluster; | ||
} | ||
xdsCluster.setClusterName(cluster.getName()); | ||
xdsCluster.setServiceName(serviceNameFromCluster.get()); | ||
xdsCluster.setLocalityLb(cluster.getCommonLbConfig().hasLocalityWeightedLbConfig()); | ||
xdsCluster.setLbPolicy(parseClusterLbPolicy(cluster.getLbPolicy())); | ||
return xdsCluster; | ||
} | ||
|
||
private static String getServiceBaseClusterName(Set<XdsCluster> xdsClusters) { | ||
for (XdsCluster cluster : xdsClusters) { | ||
String clusterName = cluster.getClusterName(); | ||
String[] splitCluster = clusterName.split(VERTICAL_LINE_SEPARATOR); | ||
if (splitCluster[CLUSTER_SUBSET_INDEX].equals("")) { | ||
return clusterName; | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
private static XdsLbPolicy parseClusterLbPolicy(LbPolicy lbPolicy) { | ||
if (lbPolicy == LbPolicy.RANDOM) { | ||
return XdsLbPolicy.RANDOM; | ||
} | ||
if (lbPolicy == LbPolicy.ROUND_ROBIN) { | ||
return XdsLbPolicy.ROUND_ROBIN; | ||
} | ||
if (lbPolicy == LbPolicy.LEAST_REQUEST) { | ||
return XdsLbPolicy.LEAST_REQUEST; | ||
} | ||
if (lbPolicy == LbPolicy.RING_HASH) { | ||
return XdsLbPolicy.RING_HASH; | ||
} | ||
if (lbPolicy == LbPolicy.MAGLEV) { | ||
return XdsLbPolicy.MAGLEV; | ||
} | ||
return XdsLbPolicy.UNRECOGNIZED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.