/** * Find cluster port mapping that corresponds to cartridge port mapping. * * @param clusterPortMappings * @param portMapping * @return */ private ClusterPortMapping findClusterPortMapping( Collection<ClusterPortMapping> clusterPortMappings, PortMapping portMapping) { for (ClusterPortMapping clusterPortMapping : clusterPortMappings) { if (clusterPortMapping.getName().equals(portMapping.getName())) { return clusterPortMapping; } } return null; }
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); }
/** * Generate kubernetes service ports for cluster. * * @param kubernetesClusterContext * @param clusterId * @param cartridge */ private void generateKubernetesServicePorts( String applicationId, String clusterId, KubernetesClusterContext kubernetesClusterContext, Cartridge cartridge) { synchronized (KubernetesIaas.class) { if (cartridge != null) { StringBuilder portMappingStrBuilder = new StringBuilder(); for (PortMapping portMapping : Arrays.asList(cartridge.getPortMappings())) { Collection<ClusterPortMapping> clusterPortMappings = CloudControllerContext.getInstance().getClusterPortMappings(applicationId, clusterId); if (clusterPortMappings == null) { throw new CloudControllerException( String.format( "Cluster port mappings not found: " + "[application-id] %s [cluster-id] %s", applicationId, clusterId)); } ClusterPortMapping clusterPortMapping = findClusterPortMapping(clusterPortMappings, portMapping); if (clusterPortMappings == null) { throw new CloudControllerException( String.format( "Cluster port mapping not found: " + "[application-id] %s [cluster-id] %s [transport] %s", applicationId, clusterId, portMapping.getName())); } if (clusterPortMapping.getKubernetesServiceType() == null) { throw new CloudControllerException( String.format( "Kubernetes service type not " + "found [application-id] %s [cluster-id] %s [cartridge] %s", applicationId, clusterId, cartridge)); } String serviceType = portMapping.getKubernetesPortType(); clusterPortMapping.setKubernetesServiceType(serviceType); // If kubernetes service port is already set, skip setting a new one if (clusterPortMapping.getKubernetesServicePort() == 0) { if (serviceType.equals(KubernetesConstants.NODE_PORT)) { int nextServicePort = kubernetesClusterContext.getNextServicePort(); if (nextServicePort == -1) { throw new RuntimeException( String.format( "Could not generate service port: [cluster-id] %s " + "[port] %d", clusterId, portMapping.getPort())); } clusterPortMapping.setKubernetesServicePort(nextServicePort); } else { clusterPortMapping.setKubernetesServicePort(portMapping.getPort()); } } else { if (log.isDebugEnabled()) { log.debug( String.format( "Kubernetes service port is already set: [application-id] %s " + "[cluster-id] %s [port] %d [service-port] %d", applicationId, clusterId, clusterPortMapping.getPort(), clusterPortMapping.getKubernetesServicePort())); } } // Add port mappings to payload if (portMappingStrBuilder.toString().length() > 0) { portMappingStrBuilder.append(";"); } portMappingStrBuilder.append( String.format( "NAME:%s|PROTOCOL:%s|PORT:%d|PROXY_PORT:%d", clusterPortMapping.getName(), clusterPortMapping.getProtocol(), clusterPortMapping.getKubernetesServicePort(), clusterPortMapping.getProxyPort())); if (log.isInfoEnabled()) { log.info( String.format( "Kubernetes service port generated: [application-id] %s " + "[cluster-id] %s [port] %d [service-port] %d", applicationId, clusterId, clusterPortMapping.getPort(), clusterPortMapping.getKubernetesServicePort())); } } NameValuePair nameValuePair = new NameValuePair(PORT_MAPPINGS, portMappingStrBuilder.toString()); payload.add(nameValuePair); // Persist service ports added to cluster port mappings CloudControllerContext.getInstance().persist(); } } }