@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; }
@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; }
// TODO: Fix repeated DB call even if DB's cache no longer hits the disk private Set<String> getServiceNamesInStack(long stackId) { Set<String> servicesInEnv = new HashSet<String>(); List<? extends Service> services = objectManager.find(Service.class, SERVICE.STACK_ID, stackId, SERVICE.REMOVED, null); for (Service service : services) { servicesInEnv.add(service.getName().toLowerCase()); } return servicesInEnv; }
// TODO: Fix repeated DB call even if DB's cache no longer hits the disk private Set<String> getServiceNamesInStack(long environmentId) { Set<String> servicesInEnv = new HashSet<String>(); List<? extends Service> services = objectManager.find( Service.class, SERVICE.ENVIRONMENT_ID, environmentId, SERVICE.REMOVED, null); for (Service service : services) { servicesInEnv.add(service.getName()); } return servicesInEnv; }
protected void validateLinks(final Map<String, ServiceLink> newServiceLinks) { for (ServiceLink link : newServiceLinks.values()) { Service targetService = objMgr.loadResource(Service.class, link.getServiceId()); if (targetService == null || targetService.getRemoved() != null || targetService.getState().equalsIgnoreCase(CommonStatesConstants.REMOVING)) { Object obfuscatedId = idFormatter.formatId("service", link.getServiceId()); String obfuscatedIdStr = obfuscatedId != null ? obfuscatedId.toString() : null; String svcName = targetService != null ? targetService.getName() : obfuscatedIdStr; ValidationErrorCodes.throwValidationError( ValidationErrorCodes.INVALID_REFERENCE, "Service " + svcName + " is removed"); } } }
private void populateLinksForService( Service service, Collection<Long> servicesToExportIds, Map<String, Object> composeServiceData) { List<String> serviceLinksWithNames = new ArrayList<>(); List<String> externalLinksServices = new ArrayList<>(); List<? extends ServiceConsumeMap> consumedServiceMaps = consumeMapDao.findConsumedServices(service.getId()); for (ServiceConsumeMap consumedServiceMap : consumedServiceMaps) { Service consumedService = objectManager.findOne( Service.class, SERVICE.ID, consumedServiceMap.getConsumedServiceId()); String linkName = consumedService.getName() + ":" + (consumedServiceMap.getName() != null ? consumedServiceMap.getName() : consumedService.getName()); if (servicesToExportIds.contains(consumedServiceMap.getConsumedServiceId())) { serviceLinksWithNames.add(linkName); } else if (!consumedService.getEnvironmentId().equals(service.getEnvironmentId())) { Environment externalEnvironment = objectManager.loadResource(Environment.class, consumedService.getEnvironmentId()); externalLinksServices.add(externalEnvironment.getName() + "/" + linkName); } } if (!serviceLinksWithNames.isEmpty()) { composeServiceData.put( ServiceDiscoveryConfigItem.LINKS.getDockerName(), serviceLinksWithNames); } if (!externalLinksServices.isEmpty()) { composeServiceData.put( ServiceDiscoveryConfigItem.EXTERNALLINKS.getDockerName(), externalLinksServices); } }
@SuppressWarnings("unchecked") protected void populateLoadBalancerServiceLabels( Service service, String launchConfigName, Map<String, Object> composeServiceData) { if (!service .getKind() .equalsIgnoreCase(ServiceDiscoveryConstants.KIND.LOADBALANCERSERVICE.name())) { return; } Map<String, String> labels = new HashMap<>(); if (composeServiceData.get(InstanceConstants.FIELD_LABELS) != null) { labels.putAll( (HashMap<String, String>) composeServiceData.get(InstanceConstants.FIELD_LABELS)); } // get all consumed services maps List<? extends ServiceConsumeMap> consumedServiceMaps = consumeMapDao.findConsumedServices(service.getId()); // for each port, populate the label for (ServiceConsumeMap map : consumedServiceMaps) { Service consumedService = objectManager.loadResource(Service.class, map.getConsumedServiceId()); List<String> ports = DataAccessor.fieldStringList(map, LoadBalancerConstants.FIELD_LB_TARGET_PORTS); String consumedServiceName = consumedService.getName(); if (!service.getEnvironmentId().equals(consumedService.getEnvironmentId())) { Environment env = objectManager.loadResource(Environment.class, consumedService.getEnvironmentId()); consumedServiceName = env.getName() + "/" + consumedServiceName; } String labelName = ServiceDiscoveryConstants.LABEL_LB_TARGET + consumedServiceName; StringBuilder bldr = new StringBuilder(); for (String port : ports) { bldr.append(port).append(","); } if (bldr.length() > 0) { labels.put(labelName, bldr.toString().substring(0, bldr.length() - 1)); } } if (!labels.isEmpty()) { composeServiceData.put(InstanceConstants.FIELD_LABELS, labels); } }
protected String getLoadBalancerName(Service service) { Environment env = objectManager.findOne(Environment.class, ENVIRONMENT.ID, service.getEnvironmentId()); return String.format("%s_%s", env.getName(), service.getName()); }