Esempio n. 1
0
 protected List<ServiceInstanceData> populateTargetInstancesData(
     Map<Long, List<ServiceInstanceData>> servicesTargetInstances,
     Map<Long, List<ServiceDnsEntryData>> clientServiceIdToServiceData,
     Service targetService) {
   List<ServiceInstanceData> targetInstancesData = new ArrayList<>();
   if (targetService
       .getKind()
       .equalsIgnoreCase(ServiceDiscoveryConstants.KIND.DNSSERVICE.name())) {
     List<ServiceDnsEntryData> behindAlias =
         clientServiceIdToServiceData.get(targetService.getId());
     if (behindAlias != null) {
       for (ServiceDnsEntryData behindAliasEntry : behindAlias) {
         List<ServiceInstanceData> toAdd =
             servicesTargetInstances.get(behindAliasEntry.getTargetService().getId());
         if (toAdd != null) {
           targetInstancesData.addAll(toAdd);
         }
       }
     }
   } else {
     List<ServiceInstanceData> toAdd = servicesTargetInstances.get(targetService.getId());
     if (toAdd != null) {
       targetInstancesData.addAll(toAdd);
     }
   }
   return targetInstancesData;
 }
Esempio n. 2
0
  protected List<DnsEntryData> convertToDnsEntryData(
      final boolean isVIPProvider,
      List<ServiceDnsEntryData> serviceDnsData,
      long accountId,
      long vnetId) {
    final Map<Long, IpAddress> instanceIdToHostIpMap =
        getInstanceWithHostNetworkingToIpMap(accountId);
    Map<Long, List<ServiceInstanceData>> servicesClientInstances =
        getServiceInstancesData(accountId, true, vnetId);
    Map<Long, List<ServiceInstanceData>> servicesTargetInstances =
        getServiceInstancesData(accountId, false, vnetId);
    Map<Long, List<ServiceDnsEntryData>> clientServiceIdToServiceData = new HashMap<>();
    for (ServiceDnsEntryData data : serviceDnsData) {
      Long clientServiceId = data.getClientService().getId();
      List<ServiceDnsEntryData> existingData = clientServiceIdToServiceData.get(clientServiceId);
      if (existingData == null) {
        existingData = new ArrayList<>();
      }
      existingData.add(data);
      clientServiceIdToServiceData.put(clientServiceId, existingData);
    }

    List<DnsEntryData> returnData = new ArrayList<>();

    for (ServiceDnsEntryData serviceData : serviceDnsData) {
      Service clientService = serviceData.getClientService();
      Map<String, Map<String, String>> resolve = new HashMap<>();
      Map<String, String> resolveCname = new HashMap<>();
      Service targetService = serviceData.getTargetService();
      List<ServiceInstanceData> targetInstancesData =
          populateTargetInstancesData(
              servicesTargetInstances, clientServiceIdToServiceData, targetService);

      for (ServiceInstanceData targetInstance : targetInstancesData) {
        populateResolveInfo(
            isVIPProvider,
            instanceIdToHostIpMap,
            serviceData,
            clientService,
            resolve,
            resolveCname,
            targetService,
            targetInstance);
      }

      if (servicesClientInstances.containsKey(clientService.getId())) {
        for (ServiceInstanceData clientInstance :
            servicesClientInstances.get(clientService.getId())) {
          DnsEntryData data = new DnsEntryData();
          String clientIp = getIpAddress(clientInstance, true, instanceIdToHostIpMap);
          data.setSourceIpAddress(clientIp);
          data.setResolveServicesAndContainers(resolve);
          data.setInstance(clientInstance.getInstance());
          data.setResolveCname(resolveCname);
          returnData.add(data);
        }
      }
    }
    return returnData;
  }
Esempio n. 3
0
  protected void populateResolveInfo(
      final boolean isVIPProvider,
      final Map<Long, IpAddress> instanceIdToHostIpMap,
      ServiceDnsEntryData serviceData,
      Service clientService,
      Map<String, Map<String, String>> resolve,
      Map<String, String> resolveCname,
      Service targetService,
      ServiceInstanceData targetInstance) {
    String targetInstanceName =
        targetInstance.getInstance() == null ? null : targetInstance.getInstance().getName();

    boolean self = clientService.getId().equals(targetService.getId());
    String dnsName =
        getDnsName(targetService, serviceData.getConsumeMap(), targetInstance.getExposeMap(), self);

    String targetIp =
        isVIPProvider
            ? clientService.getVip()
            : getIpAddress(targetInstance, false, instanceIdToHostIpMap);
    if (targetIp != null) {
      Map<String, String> ips = resolve.get(dnsName);
      if (ips == null) {
        ips = new HashMap<>();
      }
      ips.put(targetIp, targetInstanceName);

      resolve.put(dnsName, ips);
    } else {
      String cname = targetInstance.getExposeMap().getHostName();
      if (cname != null) {
        resolveCname.put(dnsName, cname);
      }
    }
  }