private void populateNetworkForService(
     Service service, String launchConfigName, Map<String, Object> composeServiceData) {
   Object networkMode =
       composeServiceData.get(ServiceDiscoveryConfigItem.NETWORKMODE.getDockerName());
   if (networkMode != null) {
     if (networkMode.equals(DockerNetworkConstants.NETWORK_MODE_CONTAINER)) {
       Map<String, Object> serviceData =
           ServiceDiscoveryUtil.getLaunchConfigDataAsMap(service, launchConfigName);
       // network mode can be passed by container, or by service name, so check both
       // networkFromContainerId wins
       Integer targetContainerId =
           DataAccessor.fieldInteger(service, DockerInstanceConstants.DOCKER_CONTAINER);
       if (targetContainerId != null) {
         Instance instance =
             objectManager.loadResource(Instance.class, targetContainerId.longValue());
         String instanceName = ServiceDiscoveryUtil.getInstanceName(instance);
         composeServiceData.put(
             ServiceDiscoveryConfigItem.NETWORKMODE.getDockerName(),
             DockerNetworkConstants.NETWORK_MODE_CONTAINER + ":" + instanceName);
       } else {
         Object networkLaunchConfig =
             serviceData.get(ServiceDiscoveryConstants.FIELD_NETWORK_LAUNCH_CONFIG);
         if (networkLaunchConfig != null) {
           composeServiceData.put(
               ServiceDiscoveryConfigItem.NETWORKMODE.getDockerName(),
               DockerNetworkConstants.NETWORK_MODE_CONTAINER + ":" + networkLaunchConfig);
         }
       }
     } else if (networkMode.equals(DockerNetworkConstants.NETWORK_MODE_MANAGED)) {
       composeServiceData.remove(ServiceDiscoveryConfigItem.NETWORKMODE.getDockerName());
     }
   }
 }
  @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;
  }
Пример #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;
  }
  @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);
    }
  }
  @Override
  public List<Integer> getServiceInstanceUsedOrderIds(Service service, String launchConfigName) {
    Environment env =
        objectManager.findOne(Environment.class, ENVIRONMENT.ID, service.getEnvironmentId());
    // get all existing instances to check if the name is in use by the instance of the same service
    List<Integer> usedIds = new ArrayList<>();
    // list all the instances
    List<? extends ServiceExposeMap> instanceServiceMaps =
        exposeMapDao.getNonRemovedServiceInstanceMap(service.getId());

    for (ServiceExposeMap instanceServiceMap : instanceServiceMaps) {
      Instance instance =
          objectManager.loadResource(Instance.class, instanceServiceMap.getInstanceId());
      if (ServiceDiscoveryUtil.isServiceGeneratedName(env, service, instance)) {

        String configName =
            launchConfigName == null
                    || launchConfigName.equals(ServiceDiscoveryConstants.PRIMARY_LAUNCH_CONFIG_NAME)
                ? ""
                : launchConfigName + "_";

        String id =
            instance
                .getName()
                .replace(
                    String.format("%s_%s_%s", env.getName(), service.getName(), configName), "");
        if (id.matches("\\d+")) {
          usedIds.add(Integer.valueOf(id));
        }
      }
    }
    return usedIds;
  }
  @Override
  @SuppressWarnings("unchecked")
  public void createLoadBalancerService(
      Service service, List<? extends Long> certIds, Long defaultCertId) {
    String lbName = getLoadBalancerName(service);
    // 1. create load balancer config
    Map<String, Object> lbConfigData =
        (Map<String, Object>)
            DataAccessor.field(
                service,
                ServiceDiscoveryConstants.FIELD_LOAD_BALANCER_CONFIG,
                jsonMapper,
                Map.class);

    if (lbConfigData == null) {
      lbConfigData = new HashMap<String, Object>();
    }

    LoadBalancerConfig lbConfig = createDefaultLoadBalancerConfig(lbName, lbConfigData, service);

    // 2. add listeners to the config based on the ports info
    Map<String, Object> launchConfigData =
        ServiceDiscoveryUtil.getLaunchConfigDataAsMap(service, null);
    createListeners(service, lbConfig, launchConfigData);

    // 3. create a load balancer
    createLoadBalancer(service, lbName, lbConfig, launchConfigData, certIds, defaultCertId);
  }
  @Override
  public Object resourceAction(String type, ApiRequest request, ResourceManager next) {
    if (request.getAction().equalsIgnoreCase(ServiceDiscoveryConstants.ACTION_SERVICE_UPGRADE)) {
      Service service = objManager.loadResource(Service.class, request.getId());
      if (ServiceDiscoveryUtil.isNoopService(service, allocatorService)) {
        throw new ValidationErrorException(
            ValidationErrorCodes.INVALID_ACTION, "Can't upgrade selector only service");
      }
    }

    return super.resourceAction(type, request, next);
  }
Пример #8
0
 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;
 }
  @SuppressWarnings("unchecked")
  private void populateVolumesForService(
      Service service, String launchConfigName, Map<String, Object> composeServiceData) {
    List<String> namesCombined = new ArrayList<>();
    List<String> launchConfigNames = new ArrayList<>();
    Map<String, Object> launchConfigData =
        ServiceDiscoveryUtil.getLaunchConfigDataAsMap(service, launchConfigName);
    Object dataVolumesLaunchConfigs =
        launchConfigData.get(ServiceDiscoveryConstants.FIELD_DATA_VOLUMES_LAUNCH_CONFIG);

    if (dataVolumesLaunchConfigs != null) {
      launchConfigNames.addAll((List<String>) dataVolumesLaunchConfigs);
    }

    // 1. add launch config names
    namesCombined.addAll(launchConfigNames);

    // 2. add instance names if specified
    List<? extends Integer> instanceIds =
        (List<? extends Integer>) launchConfigData.get(DockerInstanceConstants.FIELD_VOLUMES_FROM);

    if (instanceIds != null) {
      for (Integer instanceId : instanceIds) {
        Instance instance =
            objectManager.findOne(Instance.class, INSTANCE.ID, instanceId, INSTANCE.REMOVED, null);
        String instanceName = ServiceDiscoveryUtil.getInstanceName(instance);
        if (instanceName != null) {
          namesCombined.add(instanceName);
        }
      }
    }

    if (!namesCombined.isEmpty()) {
      composeServiceData.put(ServiceDiscoveryConfigItem.VOLUMESFROM.getDockerName(), namesCombined);
    }
  }