Few improvements; checking Pod creation & wait till pods getting created, 
terminate Pods forcefully to make sure Pods are not running.


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/30e08c83
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/30e08c83
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/30e08c83

Branch: refs/heads/container-autoscaling
Commit: 30e08c8319183bafbc5365d3a026e159a541dc46
Parents: c84ffe6
Author: Nirmal Fernando <[email protected]>
Authored: Wed Oct 8 22:21:05 2014 +0530
Committer: Nirmal Fernando <[email protected]>
Committed: Wed Oct 8 22:21:30 2014 +0530

----------------------------------------------------------------------
 .../impl/CloudControllerServiceImpl.java        | 50 ++++++++++++++++----
 1 file changed, 42 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/30e08c83/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
 
b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
index 4042140..da96ab0 100644
--- 
a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
+++ 
b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
@@ -1372,7 +1372,7 @@ public class CloudControllerServiceImpl implements 
CloudControllerService {
         }
 
         try {
-            validateProperty(StratosConstants.KUBERNETES_MIN_REPLICAS, ctxt);
+            String minReplicas = 
validateProperty(StratosConstants.KUBERNETES_MIN_REPLICAS, ctxt);
             String kubernetesClusterId = 
validateProperty(StratosConstants.KUBERNETES_CLUSTER_ID, ctxt);
             String kubernetesMasterIp = 
validateProperty(StratosConstants.KUBERNETES_MASTER_IP, 
containerClusterContext);
             String kubernetesPortRange = 
validateProperty(StratosConstants.KUBERNETES_PORT_RANGE, 
containerClusterContext);
@@ -1408,19 +1408,30 @@ public class CloudControllerServiceImpl implements 
CloudControllerService {
                        
                        kubApi.createService(service);
                        
+                       // set host port
+                       
ctxt.addProperty(StratosConstants.ALLOCATED_SERVICE_HOST_PORT, 
service.getPort());
+                       
                        if (log.isDebugEnabled()) {
                                log.debug("Cloud Controller successfully 
started the service "
                                                + controller + " via Kubernetes 
layer.");
                        }
                        
-                       // needs to wait few seconds before running label 
queries
-                       Thread.sleep(3000);
-                       
                        // create a label query
                        Label l = new Label();
                        l.setName(clusterId);
                        // execute the label query
-                       Pod[] newlyCreatedPods = kubApi.getSelectedPods(new 
Label[]{l});
+                       Pod[] newlyCreatedPods = new Pod[0];
+                       int expectedCount = Integer.parseInt(minReplicas);
+                       
+                       for (int i = 0; i < expectedCount ; i++) {
+                           newlyCreatedPods = kubApi.getSelectedPods(new 
Label[]{l});
+                           
+                           log.info("Pods "+newlyCreatedPods.length);
+                           if(newlyCreatedPods.length == expectedCount) {
+                               break;
+                           }
+                           Thread.sleep(5000);
+            }
                        
                        if (log.isDebugEnabled()) {
                            
@@ -1436,10 +1447,10 @@ public class CloudControllerServiceImpl implements 
CloudControllerService {
                 context.setCartridgeType(cartridgeType);
                 context.setClusterId(clusterId);
                 
-                
context.setProperties(CloudControllerUtil.addProperty(containerClusterContext
+                context.setProperties(CloudControllerUtil.addProperty(context
                         .getProperties(), 
StratosConstants.ALLOCATED_SERVICE_HOST_PORT,
-                        CloudControllerUtil.getProperty(ctxt.getProperties(),
-                                
StratosConstants.ALLOCATED_SERVICE_HOST_PORT)));
+                        String.valueOf(service.getPort())));
+                
                 dataHolder.addMemberContext(context);
                 
                 // trigger topology
@@ -1565,6 +1576,29 @@ public class CloudControllerServiceImpl implements 
CloudControllerService {
                        // we're not going to throw this error, but proceed 
with other deletions
                        log.error("Failed to update Kubernetes Controller with 
id: "+clusterId, e);
                }
+               
+               // delete pods forcefully
+        try {
+            // create a label query
+            Label l = new Label();
+            l.setName(clusterId);
+            // execute the label query
+            Pod[] pods = kubApi.getSelectedPods(new Label[]{l});
+            
+            for (Pod pod : pods) {
+                try {
+                    // delete pods forcefully
+                    kubApi.deletePod(pod.getId());
+                } catch (KubernetesClientException ignore) {
+                    // we can't do nothing here
+                    log.warn(String.format("Failed to delete Pod [%s] 
forcefully!", pod.getId()));
+                }
+            }
+        } catch (KubernetesClientException e) {
+            // we're not going to throw this error, but proceed with other 
deletions
+            log.error("Failed to delete pods forcefully for cluster: 
"+clusterId, e);
+        }
+               
                // delete the replication controller.
                try {
                        kubApi.deleteReplicationController(clusterId);

Reply via email to