Skip to content

Commit

Permalink
[code quality] [nacos-istio] [all] simple the judge logic, constant e…
Browse files Browse the repository at this point in the history
…xport (alibaba#6012)
  • Loading branch information
brotherlu-xcq authored Jun 9, 2021
1 parent 79be070 commit 5e7f2d5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void sendResources(Map<String, ResourceOuterClass.Resource> resourceMap)
* generate response by resource.
*
* @param resourceMap all mcp resource
* @return
* @return discovery response.
*/
private DiscoveryResponse generateResponse(Map<String, ResourceOuterClass.Resource> resourceMap) {
List<Any> anies = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void sendResources(Map<String, ResourceOuterClass.Resource> resourceMap)
* generate response by resource.
*
* @param resourceMap all mcp resource
* @return
* @return mcp resources.
*/
private Mcp.Resources generateResponse(Map<String, ResourceOuterClass.Resource> resourceMap) {
return Mcp.Resources.newBuilder().addAllResources(resourceMap.values())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ public class NacosToMcpResources {

private static final long MCP_PUSH_PERIOD_MILLISECONDS = 10000L;

private static final String SEPARATOR = ".";

private static final String DEFAULT_SUFFIX = ".DEFAULT-GROUP";

private static final String PORT_PARAM = "http";

private static final String CLUSTER_PARAM = "cluster";

private static final String VIRTUAL_ANNOTATION = "virtual";

private static final String DEFAULT_VIRTUAL = "1";

private static final String HTTP = "HTTP";

@Autowired
private NacosMcpOverXdsService nacosMcpOverXdsService;

Expand Down Expand Up @@ -144,18 +158,18 @@ public void run() {

private String convertName(Service service) {
if (!Constants.DEFAULT_GROUP.equals(service.getGroup())) {
return service.getName() + "." + service.getGroup() + "." + service.getNamespace();
return service.getName() + SEPARATOR + service.getGroup() + SEPARATOR + service.getNamespace();
}
//DEFAULT_GROUP is invalid for istio,because the istio host only supports: [0-9],[A-Z],[a-z],-,*
return service.getName() + ".DEFAULT-GROUP" + "." + service.getNamespace();
return service.getName() + DEFAULT_SUFFIX + SEPARATOR + service.getNamespace();
}

private ResourceOuterClass.Resource convertService(Service service) {
String serviceName = convertName(service);
ServiceEntryOuterClass.ServiceEntry.Builder serviceEntryBuilder = ServiceEntryOuterClass.ServiceEntry
.newBuilder().setResolution(ServiceEntryOuterClass.ServiceEntry.Resolution.STATIC)
.setLocation(ServiceEntryOuterClass.ServiceEntry.Location.MESH_INTERNAL)
.addHosts(serviceName + "." + SERVICE_NAME_SPLITTER);
.addHosts(serviceName + SEPARATOR + SERVICE_NAME_SPLITTER);

ServiceInfo serviceInfo = serviceStorage.getData(service);

Expand All @@ -171,17 +185,17 @@ private ResourceOuterClass.Resource convertService(Service service) {
}
Map<String, String> metadata = instance.getMetadata();
if (StringUtils.isNotEmpty(instance.getClusterName())) {
metadata.put("cluster", instance.getClusterName());
metadata.put(CLUSTER_PARAM, instance.getClusterName());
}
WorkloadEntryOuterClass.WorkloadEntry workloadEntry = WorkloadEntryOuterClass.WorkloadEntry.newBuilder()
.setAddress(instance.getIp()).setWeight((int) instance.getWeight()).putAllLabels(metadata)
.putPorts("http", instance.getPort()).build();
.putPorts(PORT_PARAM, instance.getPort()).build();

serviceEntryBuilder.addEndpoints(workloadEntry);
}

serviceEntryBuilder.addPorts(
GatewayOuterClass.Port.newBuilder().setNumber(port).setName("http").setProtocol("HTTP").build());
GatewayOuterClass.Port.newBuilder().setNumber(port).setName(PORT_PARAM).setProtocol(HTTP).build());

ServiceEntryOuterClass.ServiceEntry serviceEntry = serviceEntryBuilder.build();

Expand All @@ -191,7 +205,7 @@ private ResourceOuterClass.Resource convertService(Service service) {
Any any = Any.newBuilder().setValue(serviceEntry.toByteString()).setTypeUrl(MESSAGE_TYPE_URL).build();
MetadataOuterClass.Metadata metadata = MetadataOuterClass.Metadata.newBuilder()
.setName(SERVICE_NAME_SPLITTER + "/" + serviceName)
.putAllAnnotations(serviceMetadataGetter.getExtendData()).putAnnotations("virtual", "1")
.putAllAnnotations(serviceMetadataGetter.getExtendData()).putAnnotations(VIRTUAL_ANNOTATION, DEFAULT_VIRTUAL)
.setCreateTime(Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000).build())
.setVersion(serviceInfo.getChecksum()).build();

Expand Down

0 comments on commit 5e7f2d5

Please sign in to comment.