@SuppressWarnings("unchecked") protected Map<String, Object> populateLaunchConfigData(Map<String, Object> deployParams) { Map<String, Object> launchConfigData = ServiceDiscoveryUtil.buildServiceInstanceLaunchData(service, deployParams, launchConfigName, context.allocationHelper); launchConfigData.put("name", this.instanceName); launchConfigData.remove(ServiceDiscoveryConfigItem.RESTART.getCattleName()); Object labels = launchConfigData.get(InstanceConstants.FIELD_LABELS); if (labels != null) { String overrideHostName = ((Map<String, String>) labels) .get(ServiceConstants.LABEL_OVERRIDE_HOSTNAME); if (StringUtils.equalsIgnoreCase(overrideHostName, "container_name")) { String domainName = (String) launchConfigData.get(DockerInstanceConstants.FIELD_DOMAIN_NAME); String overrideName = getOverrideHostName(domainName, this.instanceName); launchConfigData.put(InstanceConstants.FIELD_HOSTNAME, overrideName); } } launchConfigData.put(InstanceConstants.FIELD_SERVICE_INSTANCE_SERVICE_INDEX_ID, this.serviceIndex.getId()); launchConfigData.put(InstanceConstants.FIELD_SERVICE_INSTANCE_SERVICE_INDEX, this.serviceIndex.getServiceIndex()); launchConfigData.put(InstanceConstants.FIELD_ALLOCATED_IP_ADDRESS, serviceIndex.getAddress()); return launchConfigData; }
protected Map<String, List<Long>> getUsedServiceIndexesIds(boolean cleanupDuplicates) { // revamp healthy/bad units by excluding units with duplicated indexes Map<String, List<Long>> launchConfigToServiceIndexes = new HashMap<>(); Iterator<DeploymentUnit> it = healthyUnits.iterator(); while (it.hasNext()) { DeploymentUnit healthyUnit = it.next(); for (DeploymentUnitInstance instance : healthyUnit.getDeploymentUnitInstances()) { if (instance.getServiceIndex() == null) { continue; } Long serviceIndexId = instance.getServiceIndex().getId(); String launchConfigName = instance.getLaunchConfigName(); List<Long> usedServiceIndexes = launchConfigToServiceIndexes.get(launchConfigName); if (usedServiceIndexes == null) { usedServiceIndexes = new ArrayList<>(); } if (cleanupDuplicates) { if (usedServiceIndexes.contains(serviceIndexId)) { badUnits.add(healthyUnit); it.remove(); break; } } usedServiceIndexes.add(serviceIndexId); launchConfigToServiceIndexes.put(launchConfigName, usedServiceIndexes); } } return launchConfigToServiceIndexes; }
@Override protected void populatedData(Account system, List<Object> toCreate) { List<Instance> instances = objectManager .find(Instance.class, INSTANCE.REMOVED, new Condition(ConditionType.NULL)); List<ServiceIndex> serviceIndexes = objectManager .find(ServiceIndex.class, SERVICE_INDEX.REMOVED, new Condition(ConditionType.NULL)); List<Long> serviceIndexIds = new ArrayList<>(); for (ServiceIndex index : serviceIndexes) { serviceIndexIds.add(index.getId()); } for (Instance instance : instances) { Long toUpdate = DataAccessor.fieldLong(instance, InstanceConstants.FIELD_SERVICE_INSTANCE_SERVICE_INDEX_ID); if (toUpdate != null && serviceIndexIds.contains(toUpdate)) { Map<String, Object> data = new HashMap<>(); data.put(InstanceConstants.FIELD_SERVICE_INSTANCE_SERVICE_INDEX_ID, toUpdate); objectManager.setFields(instance, data); } } } }
public void cleanupUnusedAndDuplicatedServiceIndexes() { Map<String, List<Long>> launchConfigToServiceIndexes = getUsedServiceIndexesIds(true); for (ServiceIndex serviceIndex : context.objectManager.find(ServiceIndex.class, SERVICE_INDEX.SERVICE_ID, service.getId(), SERVICE_INDEX.REMOVED, null)) { boolean remove = false; List<Long> usedServiceIndexes = launchConfigToServiceIndexes.get(serviceIndex.getLaunchConfigName()); if (usedServiceIndexes == null) { remove = true; } else { if (!usedServiceIndexes.contains(serviceIndex.getId())) { remove = true; } } if (remove) { context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, serviceIndex, null); } } } }
@Override protected void populatedData(Account system, List<Object> toCreate) { List<Instance> instances = objectManager .find(Instance.class, INSTANCE.REMOVED, new Condition(ConditionType.NULL)); List<ServiceIndex> serviceIndexes = objectManager .find(ServiceIndex.class, SERVICE_INDEX.REMOVED, new Condition(ConditionType.NULL)); Map<Long, ServiceIndex> serviceIndexIdsToIndexes = new HashMap<>(); for (ServiceIndex index : serviceIndexes) { serviceIndexIdsToIndexes.put(index.getId(), index); } for (Instance instance : instances) { Long indexId = instance.getServiceIndexId(); if (indexId == null) { continue; } ServiceIndex index = serviceIndexIdsToIndexes.get(indexId); if (index == null) { continue; } Map<String, Object> data = new HashMap<>(); data.put(InstanceConstants.FIELD_SERVICE_INSTANCE_SERVICE_INDEX, index.getServiceIndex()); objectManager.setFields(instance, data); } } }
/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.ServiceIndex from) { setId(from.getId()); setName(from.getName()); setAccountId(from.getAccountId()); setKind(from.getKind()); setUuid(from.getUuid()); setDescription(from.getDescription()); setState(from.getState()); setCreated(from.getCreated()); setRemoved(from.getRemoved()); setRemoveTime(from.getRemoveTime()); setData(from.getData()); setServiceIndex(from.getServiceIndex()); setLaunchConfigName(from.getLaunchConfigName()); setServiceId(from.getServiceId()); setAddress(from.getAddress()); }