From dd46dd11b3eda96e49b67dc6afec7b4ca46975aa Mon Sep 17 00:00:00 2001 From: Pubudu Gunatilaka Date: Tue, 4 Aug 2015 13:12:08 +0530 Subject: [PATCH 1/2] Changing nodeport configurable in kubernetes --- .../controller/domain/ClusterPortMapping.java | 11 +- .../cloud/controller/domain/PortMapping.java | 9 + .../iaases/kubernetes/KubernetesIaas.java | 66 +- .../messaging/topology/TopologyBuilder.java | 43 +- .../beans/cartridge/PortMappingBean.java | 16 +- .../client/KubernetesApiClient.java | 26 +- .../client/KubernetesConstants.java | 1 + .../KubernetesAPIClientInterface.java | 17 +- .../client/live/AbstractLiveTest.java | 4 +- .../live/KubernetesApiClientLiveTest.java | 3 +- .../domain/topology/KubernetesService.java | 9 + .../rest/endpoint/api/StratosApiV41Utils.java | 26 + .../util/converter/ObjectConverter.java | 4 +- samples/cartridges/kubernetes/php.json | 3 +- .../resources/CloudControllerService.wsdl | 1101 ++++++++--------- 15 files changed, 723 insertions(+), 616 deletions(-) diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/domain/ClusterPortMapping.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/domain/ClusterPortMapping.java index e816459ec8..ee0ba91e41 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/domain/ClusterPortMapping.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/domain/ClusterPortMapping.java @@ -32,6 +32,7 @@ public class ClusterPortMapping extends PortMapping implements Serializable { private String clusterId; private boolean kubernetes; private int kubernetesServicePort; + private String kubernetesServiceType; public ClusterPortMapping() { } @@ -61,6 +62,14 @@ public void setKubernetesServicePort(int kubernetesServicePort) { kubernetes = true; } + public void setKubernetesServiceType(String kubernetesServiceType) { + this.kubernetesServiceType = kubernetesServiceType; + } + + public String getKubernetesServiceType() { + return kubernetesServiceType; + } + public boolean isKubernetes() { return kubernetes; } @@ -73,6 +82,6 @@ public boolean equals(Object obj) { public String toString() { return "[application-id] " + getApplicationId() + " [cluster-id] " + getClusterId() + ", " + - super.toString() + " [kubernetes-service-port] "+ getKubernetesServicePort(); + super.toString() + " [kubernetes-service-port] " + getKubernetesServicePort(); } } diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/domain/PortMapping.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/domain/PortMapping.java index b33eb9742d..5e2e35e713 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/domain/PortMapping.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/domain/PortMapping.java @@ -28,6 +28,15 @@ public class PortMapping implements Serializable { private String protocol; private int port; private int proxyPort; + private String kubernetesPortType; + + public String getKubernetesPortType() { + return kubernetesPortType; + } + + public void setKubernetesPortType(String kubernetesPortType) { + this.kubernetesPortType = kubernetesPortType; + } public PortMapping() { } diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/kubernetes/KubernetesIaas.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/kubernetes/KubernetesIaas.java index 6fdaaf4475..5904346fb3 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/kubernetes/KubernetesIaas.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/kubernetes/KubernetesIaas.java @@ -520,12 +520,13 @@ private void createKubernetesServices(KubernetesApiClient kubernetesApi, Cluster } // Create kubernetes service for port mapping - int nodePort = clusterPortMapping.getKubernetesServicePort(); + int servicePort = clusterPortMapping.getKubernetesServicePort(); + String serviceType = clusterPortMapping.getKubernetesServiceType(); String containerPortName = KubernetesIaasUtil.preparePortNameFromPortMapping(clusterPortMapping); try { // Services need to use minions private IP addresses for creating iptable rules - kubernetesApi.createService(serviceId, serviceLabel, nodePort, containerPortName, + kubernetesApi.createService(serviceId, serviceLabel, servicePort, serviceType, containerPortName, containerPort, sessionAffinity); } finally { // Persist kubernetes service sequence no @@ -546,14 +547,23 @@ private void createKubernetesServices(KubernetesApiClient kubernetesApi, Cluster String[] minionPublicIPArray = minionPublicIPList.toArray(new String[minionPublicIPList.size()]); kubernetesService.setPublicIPs(minionPublicIPArray); kubernetesService.setProtocol(clusterPortMapping.getProtocol()); - kubernetesService.setPort(service.getSpec().getPorts().get(0).getNodePort()); + + String kubernetesServiceType = service.getSpec().getType(); + kubernetesService.setServiceType(kubernetesServiceType); + + if (kubernetesServiceType.equals(KubernetesConstants.NODE_PORT)) { + kubernetesService.setPort(service.getSpec().getPorts().get(0).getNodePort()); + } else { + kubernetesService.setPort(service.getSpec().getPorts().get(0).getPort()); + } + kubernetesService.setContainerPort(containerPort); kubernetesServices.add(kubernetesService); if (log.isInfoEnabled()) { log.info(String.format("Kubernetes service successfully created: [cluster] %s [service] %s " + "[protocol] %s [node-port] %d [container-port] %s", clusterId, - serviceId, clusterPortMapping.getProtocol(), nodePort, containerPort)); + serviceId, clusterPortMapping.getProtocol(), servicePort, containerPort)); } } } @@ -589,35 +599,48 @@ private void generateKubernetesServicePorts(String applicationId, String cluster StringBuilder portMappingStrBuilder = new StringBuilder(); for (PortMapping portMapping : Arrays.asList(cartridge.getPortMappings())) { - 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())); - } - Collection clusterPortMappings = CloudControllerContext.getInstance().getClusterPortMappings(applicationId, clusterId); - if(clusterPortMappings == null) { + 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) { + if (clusterPortMappings == null) { throw new CloudControllerException(String.format("Cluster port mapping not found: " + - "[application-id] %s [cluster-id] %s [transport] %s", applicationId, clusterId, + "[application-id] %s [cluster-id] %s [transport] %s", applicationId, clusterId, portMapping.getName())); } - clusterPortMapping.setKubernetesServicePort(nextServicePort); + 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 (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()); + } // 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", + portMappingStrBuilder.append(String.format("NAME:%s|PROTOCOL:%s|PORT:%d|PROXY_PORT:%d|TYPE:%s", clusterPortMapping.getName(), clusterPortMapping.getProtocol(), - clusterPortMapping.getKubernetesServicePort(), clusterPortMapping.getProxyPort())); + clusterPortMapping.getKubernetesServicePort(), clusterPortMapping.getProxyPort(), + clusterPortMapping.getKubernetesServiceType())); if (log.isInfoEnabled()) { log.info(String.format("Kubernetes service port generated: [application-id] %s " + @@ -644,8 +667,8 @@ private void generateKubernetesServicePorts(String applicationId, String cluster * @return */ private ClusterPortMapping findClusterPortMapping(Collection clusterPortMappings, PortMapping portMapping) { - for(ClusterPortMapping clusterPortMapping : clusterPortMappings) { - if(clusterPortMapping.getName().equals(portMapping.getName())) { + for (ClusterPortMapping clusterPortMapping : clusterPortMappings) { + if (clusterPortMapping.getName().equals(portMapping.getName())) { return clusterPortMapping; } } @@ -869,6 +892,7 @@ public void allocateIpAddresses(String clusterId, MemberContext memberContext, P /** * Remove kubernetes services if available for application cluster. + * * @param applicationId * @param clusterId */ @@ -876,12 +900,12 @@ public static void removeKubernetesServices(String applicationId, String cluster ClusterContext clusterContext = CloudControllerContext.getInstance().getClusterContext(clusterId); - if(clusterContext != null) { + if (clusterContext != null) { String kubernetesClusterId = clusterContext.getKubernetesClusterId(); - if(org.apache.commons.lang3.StringUtils.isNotBlank(kubernetesClusterId)) { + if (org.apache.commons.lang3.StringUtils.isNotBlank(kubernetesClusterId)) { KubernetesClusterContext kubernetesClusterContext = CloudControllerContext.getInstance().getKubernetesClusterContext(kubernetesClusterId); - if(kubernetesClusterContext != null) { + if (kubernetesClusterContext != null) { KubernetesApiClient kubernetesApiClient = kubernetesClusterContext.getKubApi(); for (KubernetesService kubernetesService : clusterContext.getKubernetesServices()) { log.info(String.format("Deleting kubernetes service: [application-id] %s " + diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java index acc7bd4c60..b907dff4f5 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/messaging/topology/TopologyBuilder.java @@ -31,6 +31,7 @@ import org.apache.stratos.cloud.controller.statistics.publisher.BAMUsageDataPublisher; import org.apache.stratos.cloud.controller.util.CloudControllerUtil; import org.apache.stratos.common.Property; +import org.apache.stratos.kubernetes.client.KubernetesConstants; import org.apache.stratos.messaging.domain.application.ClusterDataHolder; import org.apache.stratos.messaging.domain.instance.ClusterInstance; import org.apache.stratos.messaging.domain.topology.*; @@ -202,17 +203,20 @@ public static void handleApplicationClustersCreated(String appId, List } log.debug("Creating cluster port mappings: [appication-id] " + appId); - for(Cluster cluster : appClusters) { + for (Cluster cluster : appClusters) { String cartridgeType = cluster.getServiceName(); Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType); - if(cartridge == null) { + if (cartridge == null) { throw new CloudControllerException("Cartridge not found: [cartridge-type] " + cartridgeType); } - for(PortMapping portMapping : cartridge.getPortMappings()) { + 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()); } @@ -879,20 +883,23 @@ public static void handleClusterActivatedEvent(ClusterStatusClusterActivatedEven try { // Generate access URLs for kubernetes services for (KubernetesService kubernetesService : kubernetesServices) { - // Public IP = Kubernetes minion public IP - String[] publicIPs = kubernetesService.getPublicIPs(); - if ((publicIPs != null) && (publicIPs.length > 0)) { - for (String publicIP : publicIPs) { - // There can be a String array with null values - if (publicIP != null) { - // Using type URI since only http, https, ftp, file, jar protocols are supported in URL - URI accessURL = new URI(kubernetesService.getProtocol(), null, publicIP, - kubernetesService.getPort(), null, null, null); - cluster.addAccessUrl(accessURL.toString()); - clusterInstanceActivatedEvent.addAccessUrl(accessURL.toString()); - } else { - log.error(String.format("Could not create access URL for [Kubernetes-service] %s , " + - "since Public IP is not available", kubernetesService.getId())); + + if (kubernetesService.getServiceType().equals(KubernetesConstants.NODE_PORT)) { + // Public IP = Kubernetes minion public IP + String[] publicIPs = kubernetesService.getPublicIPs(); + if ((publicIPs != null) && (publicIPs.length > 0)) { + for (String publicIP : publicIPs) { + // There can be a String array with null values + if (publicIP != null) { + // Using type URI since only http, https, ftp, file, jar protocols are supported in URL + URI accessURL = new URI(kubernetesService.getProtocol(), null, publicIP, + kubernetesService.getPort(), null, null, null); + cluster.addAccessUrl(accessURL.toString()); + clusterInstanceActivatedEvent.addAccessUrl(accessURL.toString()); + } else { + log.error(String.format("Could not create access URL for [Kubernetes-service] %s , " + + "since Public IP is not available", kubernetesService.getId())); + } } } } @@ -1081,7 +1088,7 @@ public static void handleClusterTerminatingEvent(ClusterStatusClusterTerminating // Remove kubernetes services if available ClusterContext clusterContext = CloudControllerContext.getInstance().getClusterContext(event.getClusterId()); - if(StringUtils.isNotBlank(clusterContext.getKubernetesClusterId())) { + if (StringUtils.isNotBlank(clusterContext.getKubernetesClusterId())) { KubernetesIaas.removeKubernetesServices(event.getAppId(), event.getClusterId()); } } else { diff --git a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/beans/cartridge/PortMappingBean.java b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/beans/cartridge/PortMappingBean.java index b0db909ee6..0bc919eab1 100644 --- a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/beans/cartridge/PortMappingBean.java +++ b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/beans/cartridge/PortMappingBean.java @@ -28,6 +28,15 @@ public class PortMappingBean { private String protocol; private int port; private int proxyPort; + private String kubernetesPortType; + + public String getKubernetesPortType() { + return kubernetesPortType; + } + + public void setKubernetesPortType(String kubernetesPortType) { + this.kubernetesPortType = kubernetesPortType; + } public String getName() { return name; @@ -63,8 +72,9 @@ public void setProxyPort(int proxyPort) { public String toString() { return " [ Name: " + getName() + ", " + - "Protocol: " + getProtocol() + ", " + - "Port: " + getPort() + ", " + - "Proxy port: " + getProxyPort() + "] "; + "Protocol: " + getProtocol() + ", " + + "Port: " + getPort() + ", " + + "Proxy port: " + getProxyPort() + ", " + + "Type: " + getKubernetesPortType() + "] "; } } diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java index f176cd9799..cfdaf773b8 100644 --- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java +++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java @@ -155,22 +155,23 @@ public void deletePod(String podId) throws KubernetesClientException { * * @param serviceId Service id * @param serviceLabel Service name to be used by the label name - * @param nodePort Port to be exposed by the kubernetes node + * @param servicePort Port to be exposed by the kubernetes node * @param containerPortName Container port name defined in the port label * @param containerPort Container port * @param sessionAffinity Session affinity configuration + * @param serviceType Service type * @throws KubernetesClientException */ @Override - public void createService(String serviceId, String serviceLabel, int nodePort, + public void createService(String serviceId, String serviceLabel, int servicePort, String serviceType, String containerPortName, int containerPort, String sessionAffinity) throws KubernetesClientException { try { if (log.isDebugEnabled()) { - log.debug(String.format("Creating kubernetes service: [service-id] %s [service-name] %s [service-port] %d " + - "[container-port-name] %s", serviceId, serviceLabel, nodePort, - containerPortName)); + log.debug(String.format("Creating kubernetes service: [service-id] %s [service-name] %s [service-port] " + + "%d [container-port-name] %s [service-type] %s", serviceId, serviceLabel, servicePort, + containerPortName, serviceType)); } // Create service definition @@ -183,7 +184,12 @@ public void createService(String serviceId, String serviceLabel, int nodePort, service.getMetadata().setName(serviceId); service.getSpec().setSessionAffinity(sessionAffinity); - service.getSpec().setType(KubernetesConstants.NODE_PORT); + + if (serviceType.equals(KubernetesConstants.NODE_PORT)) { + service.getSpec().setType(KubernetesConstants.NODE_PORT); + } else { + service.getSpec().setType(KubernetesConstants.CLUSTER_IP); + } // Set port List ports = new ArrayList(); @@ -191,7 +197,9 @@ public void createService(String serviceId, String serviceLabel, int nodePort, port.setName(containerPortName); port.setPort(containerPort); port.setTargetPort(new IntOrString(containerPort)); - port.setNodePort(nodePort); + if (serviceType.equals(KubernetesConstants.NODE_PORT)) { + port.setNodePort(servicePort); + } ports.add(port); service.getSpec().setPorts(ports); @@ -211,12 +219,12 @@ public void createService(String serviceId, String serviceLabel, int nodePort, if (log.isDebugEnabled()) { log.debug(String.format("Kubernetes service created successfully: [service-id] %s [service-name] %s " + "[node-port] %d [container-port-name] %s [container-port] %d", serviceId, serviceLabel, - nodePort, containerPortName, containerPort)); + servicePort, containerPortName, containerPort)); } } catch (Exception e) { String message = String.format("Could not create kubernetes service: [service-id] %s [service-name] %s " + "[node-port] %d [container-port-name] %s [container-port] %d", serviceId, serviceLabel, - nodePort, containerPortName, containerPort); + servicePort, containerPortName, containerPort); log.error(message, e); throw new KubernetesClientException(message, e); } diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesConstants.java b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesConstants.java index 9c8fc7abde..d4dd02c427 100644 --- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesConstants.java +++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesConstants.java @@ -33,4 +33,5 @@ public class KubernetesConstants { public static final String RESOURCE_CPU = "cpu"; public static final String RESOURCE_MEMORY = "memory"; public static final String NODE_PORT = "NodePort"; + public static final String CLUSTER_IP = "ClusterIP"; } diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java index 6741d6bf06..ceeca1cb7b 100644 --- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java +++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java @@ -33,12 +33,12 @@ public interface KubernetesAPIClientInterface { /** * Create pod. * - * @param podId id of the pod - * @param podLabel pod label - * @param dockerImage docker image name - * @param cpu number of cpu cores - * @param memory memory allocation in mega bytes - * @param ports ports to be opened + * @param podId id of the pod + * @param podLabel pod label + * @param dockerImage docker image name + * @param cpu number of cpu cores + * @param memory memory allocation in mega bytes + * @param ports ports to be opened * @param environmentVariables environment variables * @throws KubernetesClientException */ @@ -76,13 +76,14 @@ public void createPod(String podId, String podLabel, String dockerImage, int cpu * * @param serviceId * @param serviceLabel - * @param nodePort + * @param servicePort + * @param serviceType * @param containerPortName * @param containerPort * @param sessionAffinity * @throws KubernetesClientException */ - public void createService(String serviceId, String serviceLabel, int nodePort, + public void createService(String serviceId, String serviceLabel, int servicePort, String serviceType, String containerPortName, int containerPort, String sessionAffinity) throws KubernetesClientException; diff --git a/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/AbstractLiveTest.java b/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/AbstractLiveTest.java index 293723cee1..0ee580de5c 100644 --- a/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/AbstractLiveTest.java +++ b/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/AbstractLiveTest.java @@ -196,10 +196,10 @@ public void deletePods() { } } - protected void createService(String serviceId, String serviceName, int nodePort, String containerPortName, + protected void createService(String serviceId, String serviceName, int nodePort, String serviceType, String containerPortName, int containerPort, List publicIPs) throws KubernetesClientException, InterruptedException, IOException { log.info("Creating service..."); - client.createService(serviceId, serviceName, nodePort, containerPortName, containerPort, + client.createService(serviceId, serviceName, nodePort, serviceType, containerPortName, containerPort, KubernetesConstants.SESSION_AFFINITY_CLIENT_IP); serviceIdList.add(serviceId); diff --git a/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java b/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java index 64c2f69579..79c32a76ff 100644 --- a/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java +++ b/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java @@ -71,8 +71,9 @@ public void testServiceCreation() throws Exception { String serviceId = "tomcat-domain-1"; String serviceName = "stratos-test-pod"; String containerPortName = "http-1"; + String serviceType = "NodePort"; - createService(serviceId, serviceName, SERVICE_PORT, containerPortName, containerPort, minionPublicIPs); + createService(serviceId, serviceName, SERVICE_PORT, serviceType, containerPortName, containerPort, minionPublicIPs); createPod("stratos-test-pod-3", serviceName, containerPortName, 1, 512); createPod("stratos-test-pod-4", serviceName, containerPortName, 2, 512); diff --git a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/topology/KubernetesService.java b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/topology/KubernetesService.java index fc02179184..9cee9d7c84 100644 --- a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/topology/KubernetesService.java +++ b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/topology/KubernetesService.java @@ -34,6 +34,15 @@ public class KubernetesService implements Serializable { private String protocol; private int port; private int containerPort; + private String serviceType; + + public String getServiceType() { + return serviceType; + } + + public void setServiceType(String serviceType) { + this.serviceType = serviceType; + } public String getId() { return id; diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java index fae0f5e917..fdf9aa620b 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java @@ -104,6 +104,9 @@ public class StratosApiV41Utils { public static final String APPLICATION_STATUS_DEPLOYED = "Deployed"; public static final String APPLICATION_STATUS_CREATED = "Created"; public static final String APPLICATION_STATUS_UNDEPLOYING = "Undeploying"; + public static final String KUBERNETES_SERVICE_TYPE_NODEPORT = "NodePort"; + public static final String KUBERNETES_SERVICE_TYPE_CLUSTERIP = "ClusterIP"; + public static final String KUBERNETES_IAAS_PROVIDER = "kubernetes"; private static final Log log = LogFactory.getLog(StratosApiV41Utils.class); @@ -127,6 +130,14 @@ public static void addCartridge(CartridgeBean cartridgeBean) throws RestAPIExcep cartridgeBean.getType())); } + boolean isKubernetesIaasProviderAvailable = false; + + for (IaasProviderBean providers : iaasProviders) { + if (providers.getType().equals(KUBERNETES_IAAS_PROVIDER)) { + isKubernetesIaasProviderAvailable = true; + } + } + for (PortMappingBean portMapping : cartridgeBean.getPortMapping()) { if (StringUtils.isBlank(portMapping.getName())) { portMapping.setName(portMapping.getProtocol() + "-" + portMapping.getPort()); @@ -136,6 +147,21 @@ public static void addCartridge(CartridgeBean cartridgeBean) throws RestAPIExcep cartridgeBean.getType(), portMapping.getName())); } } + + String type = portMapping.getKubernetesPortType(); + + if (isKubernetesIaasProviderAvailable) { + if (type == null) { + throw new RestAPIException((String.format("Type is not found in portmapping: %s - Possible " + + "values - %s and %s", portMapping.getName(), KUBERNETES_SERVICE_TYPE_NODEPORT, + KUBERNETES_SERVICE_TYPE_CLUSTERIP))); + } else if (!type.equals(KUBERNETES_SERVICE_TYPE_NODEPORT) && !type.equals(KUBERNETES_SERVICE_TYPE_CLUSTERIP)) { + throw new RestAPIException((String.format("Type is not found in portmapping: %s - Possible " + + "values - %s and %s", portMapping.getName(), KUBERNETES_SERVICE_TYPE_NODEPORT, + KUBERNETES_SERVICE_TYPE_CLUSTERIP))); + } + } + } Cartridge cartridgeConfig = createCartridgeConfig(cartridgeBean); diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/util/converter/ObjectConverter.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/util/converter/ObjectConverter.java index c452de8b58..798882ecb2 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/util/converter/ObjectConverter.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/util/converter/ObjectConverter.java @@ -131,6 +131,7 @@ private static PortMapping[] convertPortMappingBeansToStubPortMappings(List convertPortMappingsToStubPortMappingBeans( portMappingBean.setProtocol(portMapping.getProtocol()); portMappingBean.setPort(portMapping.getPort()); portMappingBean.setProxyPort(portMapping.getProxyPort()); + portMappingBean.setKubernetesPortType(portMapping.getKubernetesPortType()); portMappingBeans.add(portMappingBean); } return portMappingBeans; @@ -1387,7 +1389,7 @@ private static SubscribableInfoContext convertSubscribableInfo(SubscribableInfo infoContext.setDeploymentPolicy(subscribableInfo.getDeploymentPolicy()); infoContext.setMaxMembers(subscribableInfo.getMaxMembers()); infoContext.setMinMembers(subscribableInfo.getMinMembers()); - infoContext.setLvsVirtualIP(subscribableInfo.getLvsVirtualIP()); + infoContext.setLvsVirtualIP(subscribableInfo.getLvsVirtualIP()); if (subscribableInfo.getArtifactRepository() != null) { ArtifactRepositoryBean artifactRepository = subscribableInfo.getArtifactRepository(); diff --git a/samples/cartridges/kubernetes/php.json b/samples/cartridges/kubernetes/php.json index 85adc130e8..cdb47ab971 100755 --- a/samples/cartridges/kubernetes/php.json +++ b/samples/cartridges/kubernetes/php.json @@ -13,7 +13,8 @@ "name": "http-80", "protocol": "http", "port": "80", - "proxyPort": "8281" + "proxyPort": "8281", + "kubernetesPortType":"NodePort" } ], "deployment": { diff --git a/service-stubs/org.apache.stratos.cloud.controller.service.stub/src/main/resources/CloudControllerService.wsdl b/service-stubs/org.apache.stratos.cloud.controller.service.stub/src/main/resources/CloudControllerService.wsdl index 03abeb0ca9..c63166cf3c 100644 --- a/service-stubs/org.apache.stratos.cloud.controller.service.stub/src/main/resources/CloudControllerService.wsdl +++ b/service-stubs/org.apache.stratos.cloud.controller.service.stub/src/main/resources/CloudControllerService.wsdl @@ -1,295 +1,333 @@ - + - + - - + + - + + + - + - + - + - + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + - + - + - + - + - + - + - + - + + + + - + - + - + - + - + - + + + + - + - + - + - + - + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + @@ -303,7 +341,7 @@ - + @@ -331,7 +369,7 @@ - + @@ -345,7 +383,7 @@ - + @@ -375,384 +413,346 @@ - + - + - + - + - + - + - + - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - + - - - + - + - + - + - + - + - + - + - - - - + - + - + - + - + - + - + - + - + - + - + - + - - + - + - + - + - + - + - - - - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + - + - + - + - + - + - + - + - + - + - + - + - - - + - + - + - + - + - + - + - + @@ -776,120 +776,120 @@ - + - + - + - - - - + - + - - - - + - + - + - + - + - + - + - - - - - - + + - + - + - + - + + + + + + + + - + - + + + + - + - + - + @@ -910,34 +910,34 @@ - + - - - - - - - - - - - + - + + + + + + + + + + + @@ -945,9 +945,93 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1017,53 +1101,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + @@ -1080,22 +1124,6 @@ - - - - - - - - - - - - - - - - @@ -1119,14 +1147,14 @@ - + - + @@ -1151,35 +1179,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1257,18 +1256,18 @@ - - - - - - + + + + + + @@ -1302,20 +1301,20 @@ - - + + - - + + - - + + - - + + @@ -1422,6 +1421,12 @@ + + + + + + @@ -1431,11 +1436,11 @@ - - + + - - + + @@ -1443,12 +1448,6 @@ - - - - - - @@ -1542,16 +1541,16 @@ - - - - - + + + + + @@ -1574,16 +1573,16 @@ - - - - - + + + + + @@ -1659,23 +1658,23 @@ + + + + - - - + + + - - - - @@ -1816,28 +1815,28 @@ - - + + - - + + - - + + - - + + @@ -1894,8 +1893,8 @@ - - + + @@ -1906,8 +1905,8 @@ - - + + @@ -2101,15 +2100,6 @@ - - - - - - - - - @@ -2122,8 +2112,8 @@ - - + + @@ -2140,6 +2130,15 @@ + + + + + + + + + @@ -2335,28 +2334,28 @@ - - + + - - + + - - + + - - + + @@ -2413,8 +2412,8 @@ - - + + @@ -2425,8 +2424,8 @@ - - + + @@ -2620,15 +2619,6 @@ - - - - - - - - - @@ -2641,8 +2631,8 @@ - - + + @@ -2659,6 +2649,15 @@ + + + + + + + + + @@ -2758,370 +2757,370 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - - + + - + - + - - + + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + From 0e782a9bcbdffcd642fab5720f00a089026cb84f Mon Sep 17 00:00:00 2001 From: Pubudu Gunatilaka Date: Tue, 4 Aug 2015 13:18:10 +0530 Subject: [PATCH 2/2] Changing PortMappingBean class for kubernetesPortType --- .../apache/stratos/common/beans/cartridge/PortMappingBean.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/beans/cartridge/PortMappingBean.java b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/beans/cartridge/PortMappingBean.java index 0bc919eab1..cff84c6d70 100644 --- a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/beans/cartridge/PortMappingBean.java +++ b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/beans/cartridge/PortMappingBean.java @@ -74,7 +74,6 @@ public String toString() { return " [ Name: " + getName() + ", " + "Protocol: " + getProtocol() + ", " + "Port: " + getPort() + ", " + - "Proxy port: " + getProxyPort() + ", " + - "Type: " + getKubernetesPortType() + "] "; + "Proxy port: " + getProxyPort() + "] "; } }