@SuppressWarnings("unchecked")
  protected void populateSidekickLabels(
      Service service, Map<String, Object> composeServiceData, boolean isPrimary) {
    List<? extends String> configs = ServiceDiscoveryUtil.getServiceLaunchConfigNames(service);
    configs.remove(ServiceDiscoveryConstants.PRIMARY_LAUNCH_CONFIG_NAME);
    StringBuilder sidekicks = new StringBuilder();
    for (String config : configs) {
      sidekicks.append(config).append(",");
    }
    Map<String, String> labels = new HashMap<>();
    if (composeServiceData.get(InstanceConstants.FIELD_LABELS) != null) {
      labels.putAll(
          (HashMap<String, String>) composeServiceData.get(InstanceConstants.FIELD_LABELS));
      labels.remove(ServiceDiscoveryConstants.LABEL_SIDEKICK);
    }
    if (!sidekicks.toString().isEmpty() && isPrimary) {
      String sidekicksFinal = sidekicks.toString().substring(0, sidekicks.length() - 1);
      labels.put(ServiceDiscoveryConstants.LABEL_SIDEKICK, sidekicksFinal);
    }

    if (!labels.isEmpty()) {
      composeServiceData.put(InstanceConstants.FIELD_LABELS, labels);
    } else {
      composeServiceData.remove(InstanceConstants.FIELD_LABELS);
    }
  }
  @SuppressWarnings("unchecked")
  private Map<String, Object> createComposeData(
      List<? extends Service> servicesToExport, boolean forDockerCompose) {
    Map<String, Object> data = new HashMap<String, Object>();
    Collection<Long> servicesToExportIds =
        CollectionUtils.collect(servicesToExport, TransformerUtils.invokerTransformer("getId"));
    for (Service service : servicesToExport) {
      List<String> launchConfigNames = ServiceDiscoveryUtil.getServiceLaunchConfigNames(service);
      for (String launchConfigName : launchConfigNames) {
        boolean isPrimaryConfig =
            launchConfigName.equals(ServiceDiscoveryConstants.PRIMARY_LAUNCH_CONFIG_NAME);
        Map<String, Object> cattleServiceData =
            ServiceDiscoveryUtil.getServiceDataAsMap(service, launchConfigName, allocatorService);
        Map<String, Object> composeServiceData = new HashMap<>();
        for (String cattleService : cattleServiceData.keySet()) {
          translateRancherToCompose(
              forDockerCompose, cattleServiceData, composeServiceData, cattleService, service);
        }

        if (forDockerCompose) {
          populateLinksForService(service, servicesToExportIds, composeServiceData);
          populateNetworkForService(service, launchConfigName, composeServiceData);
          populateVolumesForService(service, launchConfigName, composeServiceData);
          addExtraComposeParameters(service, launchConfigName, composeServiceData);
          populateSidekickLabels(service, composeServiceData, isPrimaryConfig);
          populateLoadBalancerServiceLabels(service, launchConfigName, composeServiceData);
        }

        if (!composeServiceData.isEmpty()) {
          data.put(isPrimaryConfig ? service.getName() : launchConfigName, composeServiceData);
        }
      }
    }
    return data;
  }
Exemple #3
0
  protected Map<String, List<Instance>> formDeploymentUnits(Service service, Type type) {
    List<String> launchConfigNames = ServiceDiscoveryUtil.getServiceLaunchConfigNames(service);
    Map<String, List<Instance>> deploymentUnitInstances = new HashMap<>();
    for (String launchConfigName : launchConfigNames) {
      String toVersion =
          ServiceDiscoveryUtil.getLaunchConfigObject(
                  service, launchConfigName, ServiceDiscoveryConstants.FIELD_VERSION)
              .toString();
      List<Instance> instances = new ArrayList<>();
      if (type == Type.ToUpgrade) {
        instances.addAll(exposeMapDao.getInstancesToUpgrade(service, launchConfigName, toVersion));
      } else if (type == Type.UpgradedManaged) {
        instances.addAll(
            exposeMapDao.getUpgradedInstances(service, launchConfigName, toVersion, true));
      } else if (type == Type.ToCleanup) {
        instances.addAll(exposeMapDao.getInstancesToCleanup(service, launchConfigName, toVersion));
      } else if (type == Type.UpgradedUnmanaged) {
        instances.addAll(
            exposeMapDao.getUpgradedInstances(service, launchConfigName, toVersion, false));
      } else if (type == Type.ToRestart) {
        instances.addAll(getServiceInstancesToRestart(service));
      }
      for (Instance instance : instances) {
        addInstanceToDeploymentUnits(deploymentUnitInstances, instance);
      }
    }

    return deploymentUnitInstances;
  }
 private Map<Long, DeploymentUnitInstanceIdGenerator> populateUsedNames(List<Service> services) {
   Map<Long, DeploymentUnitInstanceIdGenerator> generator = new HashMap<>();
   for (Service service : services) {
     Map<String, List<Integer>> launchConfigUsedIds = new HashMap<>();
     for (String launchConfigName : ServiceDiscoveryUtil.getServiceLaunchConfigNames(service)) {
       List<Integer> usedIds = sdSvc.getServiceInstanceUsedSuffixes(service, launchConfigName);
       launchConfigUsedIds.put(launchConfigName, usedIds);
     }
     generator.put(
         service.getId(), new DeploymentUnitInstanceIdGeneratorImpl(launchConfigUsedIds));
   }
   return generator;
 }