public static void handleApplicationClustersCreated(String appId, List<Cluster> appClusters) { TopologyManager.acquireWriteLock(); try { Topology topology = TopologyManager.getTopology(); for (Cluster cluster : appClusters) { Service service = topology.getService(cluster.getServiceName()); if (service == null) { log.error( "Service " + cluster.getServiceName() + " not found in Topology, unable to create Application cluster"); } else { service.addCluster(cluster); log.info("Application Cluster " + cluster.getClusterId() + " created in CC topology"); } } TopologyManager.updateTopology(topology); } finally { TopologyManager.releaseWriteLock(); } log.debug("Creating cluster port mappings: [appication-id] " + appId); for (Cluster cluster : appClusters) { String cartridgeType = cluster.getServiceName(); Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType); if (cartridge == null) { throw new CloudControllerException( "Cartridge not found: [cartridge-type] " + cartridgeType); } for (PortMapping portMapping : cartridge.getPortMappings()) { ClusterPortMapping clusterPortMapping = new ClusterPortMapping( appId, cluster.getClusterId(), portMapping.getName(), portMapping.getProtocol(), portMapping.getPort(), portMapping.getProxyPort()); if (portMapping.getKubernetesPortType() != null) { clusterPortMapping.setKubernetesServiceType(portMapping.getKubernetesPortType()); } CloudControllerContext.getInstance().addClusterPortMapping(clusterPortMapping); log.debug("Cluster port mapping created: " + clusterPortMapping.toString()); } } // Persist cluster port mappings CloudControllerContext.getInstance().persist(); // Send application clusters created event TopologyEventPublisher.sendApplicationClustersCreated(appId, appClusters); }
private boolean doProcess(ClusterCreatedEvent event, Topology topology) { Cluster cluster = event.getCluster(); String applicationId = cluster.getAppId(); String serviceName = cluster.getServiceName(); String clusterId = cluster.getClusterId(); // Apply application filter if (TopologyApplicationFilter.apply(applicationId)) { return false; } // Apply service filter if (TopologyServiceFilter.apply(serviceName)) { return false; } // Apply cluster filter if (TopologyClusterFilter.apply(clusterId)) { return false; } // Validate event against the existing topology Service service = topology.getService(serviceName); if (service == null) { if (log.isWarnEnabled()) { log.warn(String.format("Service does not exist: [service] %s", serviceName)); } return false; } if (service.clusterExists(clusterId)) { if (log.isDebugEnabled()) { log.debug( String.format( "Cluster already exists in service: [service] %s " + "[cluster] %s", serviceName, clusterId)); } } else { // Apply changes to the topology service.addCluster(cluster); if (log.isInfoEnabled()) { log.info(String.format("Cluster created: %s", cluster.toString())); } } // Notify event listeners notifyEventListeners(event); return true; }