You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by an...@apache.org on 2015/12/21 17:25:19 UTC

jclouds-labs-google git commit: JCLOUDS-1001: Adding preemptible support to the Google Compute provider

Repository: jclouds-labs-google
Updated Branches:
  refs/heads/1.9.x 01b788d00 -> 1f6f7f06d


JCLOUDS-1001: Adding preemptible support to the Google Compute provider

1.9.x backport of https://github.com/jclouds/jclouds/commit/962980cd9d22fc2e7b54cab45a4d17550d1fb137


Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/commit/1f6f7f06
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/tree/1f6f7f06
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/diff/1f6f7f06

Branch: refs/heads/1.9.x
Commit: 1f6f7f06dfd47797bc97e0cdff56352ef87bc413
Parents: 01b788d
Author: Michael Wilson <mi...@cloudera.com>
Authored: Wed Dec 16 17:16:39 2015 -0500
Committer: Andrew Phillips <an...@apache.org>
Committed: Mon Dec 21 17:24:25 2015 +0100

----------------------------------------------------------------------
 .../GoogleComputeEngineServiceAdapter.java      | 22 ++++++++++++++++---
 .../GoogleComputeEngineTemplateOptions.java     | 17 +++++++++++++++
 .../googlecomputeengine/domain/Instance.java    |  8 ++++---
 .../googlecomputeengine/domain/NewInstance.java |  6 ++++-
 .../features/InstanceApi.java                   | 23 ++++++++++++++++++++
 .../GoogleComputeEngineServiceLiveTest.java     |  5 +++--
 .../features/InstanceApiLiveTest.java           |  4 +++-
 .../features/InstanceApiMockTest.java           | 10 +++++++++
 .../parse/ParseInstanceTest.java                |  2 +-
 .../src/test/resources/instance_get.json        |  3 ++-
 .../src/test/resources/instance_insert_2.json   |  5 +++++
 .../src/test/resources/instance_insert_ssd.json |  5 +++++
 .../src/test/resources/instance_list.json       |  3 ++-
 13 files changed, 100 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java
index 653388e..ee50d92 100644
--- a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java
+++ b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java
@@ -119,12 +119,15 @@ public final class GoogleComputeEngineServiceAdapter
       List<AttachDisk> disks = Lists.newArrayList();
       disks.add(AttachDisk.newBootDisk(template.getImage().getUri(), getDiskTypeArgument(options, zone)));
 
+      Scheduling scheduling = getScheduling(options);
+
       NewInstance newInstance = NewInstance.create(
             name, // name
             template.getHardware().getUri(), // machineType
             options.network(), // network
             disks, // disks
-            group // description
+            group, // description
+            scheduling
       );
 
       // Add tags from template and for security groups
@@ -163,8 +166,8 @@ public final class GoogleComputeEngineServiceAdapter
             null, // disks
             newInstance.metadata(), // metadata
             null, // serviceAccounts
-            Scheduling.create(OnHostMaintenance.MIGRATE, true) // scheduling
-      ));
+            scheduling) // scheduling
+      );
       checkState(instanceVisible.apply(instance), "instance %s is not api visible!", instance.get());
 
       // Add lookup for InstanceToNodeMetadata
@@ -300,4 +303,17 @@ public final class GoogleComputeEngineServiceAdapter
 
       return null;
    }
+
+   public Scheduling getScheduling(GoogleComputeEngineTemplateOptions options) {
+      OnHostMaintenance onHostMaintenance = OnHostMaintenance.MIGRATE;
+      boolean automaticRestart = true;
+
+      // Preemptible instances cannot use a MIGRATE maintenance strategy or automatic restarts
+      if (options.preemptible()) {
+         onHostMaintenance = OnHostMaintenance.TERMINATE;
+         automaticRestart = false;
+      }
+
+      return Scheduling.create(onHostMaintenance, automaticRestart, options.preemptible());
+   }
 }

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/options/GoogleComputeEngineTemplateOptions.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/options/GoogleComputeEngineTemplateOptions.java b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/options/GoogleComputeEngineTemplateOptions.java
index 666454a..7b4350e 100644
--- a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/options/GoogleComputeEngineTemplateOptions.java
+++ b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/options/GoogleComputeEngineTemplateOptions.java
@@ -30,6 +30,7 @@ public final class GoogleComputeEngineTemplateOptions extends TemplateOptions {
    private URI network = null;
    private boolean autoCreateKeyPair = true;
    private String bootDiskType;
+   private boolean preemptible = false;
 
    @Override
    public GoogleComputeEngineTemplateOptions clone() {
@@ -46,6 +47,7 @@ public final class GoogleComputeEngineTemplateOptions extends TemplateOptions {
          eTo.network(network());
          eTo.autoCreateKeyPair(autoCreateKeyPair());
          eTo.bootDiskType(bootDiskType());
+         eTo.preemptible(preemptible());
       }
    }
 
@@ -76,6 +78,21 @@ public final class GoogleComputeEngineTemplateOptions extends TemplateOptions {
    }
 
    /**
+    * Sets whether the resulting instance should be preemptible.
+    */
+   public GoogleComputeEngineTemplateOptions preemptible(boolean preemptible) {
+      this.preemptible = preemptible;
+      return this;
+   }
+
+   /**
+    * Gets whether the resulting instance should be preemptible.
+    */
+   public boolean preemptible() {
+      return preemptible;
+   }
+
+   /**
     * Sets whether an SSH key pair should be created automatically.
     */
    public GoogleComputeEngineTemplateOptions autoCreateKeyPair(boolean autoCreateKeyPair) {

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/Instance.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/Instance.java b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/Instance.java
index dcbc344..71176d8 100644
--- a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/Instance.java
+++ b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/Instance.java
@@ -186,16 +186,18 @@ public abstract class Instance {
           * If you would like your instance to be restarted, set the automaticRestart flag to true.
           * Your instance may be restarted more than once, and it may be restarted outside the window
           * of maintenance events.
+          * If you would like your instance to be preemptible, set the preemptible flag to true.
           */
          TERMINATE
       }
 
       public abstract OnHostMaintenance onHostMaintenance();
       public abstract boolean automaticRestart();
+      public abstract boolean preemptible();
 
-      @SerializedNames({ "onHostMaintenance", "automaticRestart" })
-      public static Scheduling create(OnHostMaintenance onHostMaintenance, boolean automaticRestart) {
-         return new AutoValue_Instance_Scheduling(onHostMaintenance, automaticRestart);
+      @SerializedNames({ "onHostMaintenance", "automaticRestart", "preemptible" })
+      public static Scheduling create(OnHostMaintenance onHostMaintenance, boolean automaticRestart, boolean preemptible) {
+         return new AutoValue_Instance_Scheduling(onHostMaintenance, automaticRestart, preemptible);
       }
 
       Scheduling() {

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/NewInstance.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/NewInstance.java b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/NewInstance.java
index 57b22a8..3d3aa0f 100644
--- a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/NewInstance.java
+++ b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/NewInstance.java
@@ -81,6 +81,10 @@ public abstract class NewInstance {
    }
 
    public static NewInstance create(String name, URI machineType, URI network, List<AttachDisk> disks, String description) {
+      return create(name, machineType, network, disks, description, null);
+   }
+
+   public static NewInstance create(String name, URI machineType, URI network, List<AttachDisk> disks, String description, Scheduling scheduling) {
       checkArgument(disks.get(0).boot(), "disk 0 must be a boot disk! %s", disks);
       boolean foundBoot = false;
       for (AttachDisk disk : disks) {
@@ -90,7 +94,7 @@ public abstract class NewInstance {
          }
       }
       return create(name, machineType, null, ImmutableList.of(NetworkInterface.create(network)), ImmutableList.copyOf(disks),
-            description, Tags.create(), Metadata.create(), null, null);
+            description, Tags.create(), Metadata.create(), null, scheduling);
    }
 
    @SerializedNames({ "name", "machineType", "canIpForward", "networkInterfaces", "disks", "description", "tags", "metadata",

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/InstanceApi.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/InstanceApi.java b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/InstanceApi.java
index 5405de2..43d708a 100644
--- a/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/InstanceApi.java
+++ b/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/InstanceApi.java
@@ -237,6 +237,7 @@ public interface InstanceApi {
     * Sets an instance's scheduling options.
     * @see <a href = "https://cloud.google.com/compute/docs/instances#onhostmaintenance"/>
     *
+    * @deprecated See {@link #setScheduling(String, org.jclouds.googlecomputeengine.domain.Instance.Scheduling.OnHostMaintenance, boolean, boolean)}. Will be removed in 2.0
     * @param instanceName The name of the instance
     * @param onHostMaintenance either MIGRATE or TERMINATE the default is MIGRATE (Live Migration).
     * @param automaticRestart Defines whether the Instance should be automatically
@@ -244,6 +245,7 @@ public interface InstanceApi {
     *  Used when onHostMaintenance is set to TERMINATE.
     * @return
     */
+   @Deprecated
    @Named("Instances:setScheduling")
    @POST
    @Path("/{instance}/setScheduling")
@@ -253,6 +255,27 @@ public interface InstanceApi {
                            @PayloadParam("automaticRestart") boolean automaticRestart);
 
    /**
+    * Sets an instance's scheduling options.
+    * @see <a href = "https://cloud.google.com/compute/docs/instances#onhostmaintenance"/>
+    *
+    * @param instanceName The name of the instance
+    * @param onHostMaintenance either MIGRATE or TERMINATE the default is MIGRATE (Live Migration).
+    * @param automaticRestart Defines whether the Instance should be automatically
+    *  restarted when it is terminated by Compute Engine (not terminated by user).
+    *  Used when onHostMaintenance is set to TERMINATE.
+    * @param preemptible Defines whether the Instance should be launched as spot instance
+    * @return
+    */
+   @Named("Instances:setScheduling")
+   @POST
+   @Path("/{instance}/setScheduling")
+   @MapBinder(BindToJsonPayload.class)
+   Operation setScheduling(@PathParam("instance") String instanceName,
+                           @PayloadParam("onHostMaintenance") Scheduling.OnHostMaintenance onHostMaintenance,
+                           @PayloadParam("automaticRestart") boolean automaticRestart,
+                           @PayloadParam("preemptible") boolean preemptible);
+
+   /**
     * This method starts an instance that was stopped using the using the {@link #stop(String)} method.
     * @param instance - name of the instance to be started
     */

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java
index b4ee392..550cb5c 100644
--- a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java
+++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java
@@ -74,12 +74,12 @@ public class GoogleComputeEngineServiceLiveTest extends BaseComputeServiceLiveTe
       }
    }
 
-   public void testCreateNodeWithSsd() throws Exception {
+   public void testCreatePreemptibleNodeWithSsd() throws Exception {
       String group = this.group + "ssd";
       try {
          TemplateOptions options = client.templateOptions();
 
-         options.as(GoogleComputeEngineTemplateOptions.class).bootDiskType("pd-ssd");
+         options.as(GoogleComputeEngineTemplateOptions.class).bootDiskType("pd-ssd").preemptible(true);
 
          // create a node
          Set<? extends NodeMetadata> nodes =
@@ -92,6 +92,7 @@ public class GoogleComputeEngineServiceLiveTest extends BaseComputeServiceLiveTe
          Instance instance = api.instancesInZone(node.getLocation().getId()).get(node.getName());
          Disk disk = api.disksInZone(node.getLocation().getId()).get(toName(instance.disks().get(0).source()));
          assertTrue(disk.type().toString().endsWith("pd-ssd"));
+         assertTrue(instance.scheduling().preemptible());
 
       } finally {
          client.destroyNodesMatching(NodePredicates.inGroup(group));

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiLiveTest.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiLiveTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiLiveTest.java
index 4c74a3d..a61d196 100644
--- a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiLiveTest.java
+++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiLiveTest.java
@@ -185,12 +185,14 @@ public class InstanceApiLiveTest extends BaseGoogleComputeEngineApiLiveTest {
    public void testSetScheduling() {
       Instance instance = api().get(INSTANCE_NAME);
       assertEquals(instance.scheduling().automaticRestart(), true);
+      assertEquals(instance.scheduling().preemptible(), false);
       assertEquals(instance.scheduling().onHostMaintenance(), Scheduling.OnHostMaintenance.MIGRATE);
 
-      assertOperationDoneSuccessfully(api().setScheduling(INSTANCE_NAME, Scheduling.OnHostMaintenance.TERMINATE, false));
+      assertOperationDoneSuccessfully(api().setScheduling(INSTANCE_NAME, Scheduling.OnHostMaintenance.TERMINATE, false, false));
 
       Instance instanceAltered = api().get(INSTANCE_NAME);
       assertEquals(instanceAltered.scheduling().automaticRestart(), false);
+      assertEquals(instanceAltered.scheduling().preemptible(), false);
       assertEquals(instanceAltered.scheduling().onHostMaintenance(), Scheduling.OnHostMaintenance.TERMINATE);
    }
 

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java
index 067dada..1bc3821 100644
--- a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java
+++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/InstanceApiMockTest.java
@@ -231,6 +231,16 @@ public class InstanceApiMockTest extends BaseGoogleComputeEngineApiMockTest {
             "{\"onHostMaintenance\": \"TERMINATE\",\"automaticRestart\": true}");
    }
 
+   public void setScheduling_preemtible() throws Exception {
+      server.enqueue(jsonResponse("/zone_operation.json"));
+
+      assertEquals(instanceApi().setScheduling("test-1", OnHostMaintenance.TERMINATE, true, false),
+            new ParseZoneOperationTest().expected(url("/projects")));
+
+      assertSent(server, "POST", "/projects/party/zones/us-central1-a/instances/test-1/setScheduling",
+            "{\"onHostMaintenance\": \"TERMINATE\",\"automaticRestart\": true,\"preemptible\": false}");
+   }
+
    public void start_test() throws Exception {
       server.enqueue(jsonResponse("/zone_operation.json"));
 

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java
index bf699cf..8671b52 100644
--- a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java
+++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseInstanceTest.java
@@ -89,7 +89,7 @@ public class ParseInstanceTest extends BaseGoogleComputeEngineParseTest<Instance
                     .put("jclouds-image", baseUrl + "/debian-cloud/global/images/debian-7-wheezy-v20140718")
                     .put("jclouds-delete-boot-disk", "true"), // metadata
             ImmutableList.of(ServiceAccount.create("default", ImmutableList.of("myscope"))), // serviceAccounts
-            Instance.Scheduling.create(OnHostMaintenance.MIGRATE, false) // scheduling
+            Instance.Scheduling.create(OnHostMaintenance.MIGRATE, false, false) // scheduling
       );
    }
 }

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/test/resources/instance_get.json
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/test/resources/instance_get.json b/google-compute-engine/src/test/resources/instance_get.json
index b2795b6..788450b 100644
--- a/google-compute-engine/src/test/resources/instance_get.json
+++ b/google-compute-engine/src/test/resources/instance_get.json
@@ -73,6 +73,7 @@
    },
     "scheduling": {
         "onHostMaintenance": "MIGRATE",
-        "automaticRestart": false
+        "automaticRestart": false,
+        "preemptible": false
    }
 }

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/test/resources/instance_insert_2.json
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/test/resources/instance_insert_2.json b/google-compute-engine/src/test/resources/instance_insert_2.json
index 78002a6..0310aba 100644
--- a/google-compute-engine/src/test/resources/instance_insert_2.json
+++ b/google-compute-engine/src/test/resources/instance_insert_2.json
@@ -35,5 +35,10 @@
         "value": "test"
       }
     ]
+  },
+  "scheduling": {
+    "onHostMaintenance": "MIGRATE",
+    "automaticRestart": true,
+    "preemptible": false
   }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/test/resources/instance_insert_ssd.json
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/test/resources/instance_insert_ssd.json b/google-compute-engine/src/test/resources/instance_insert_ssd.json
index 797aa4a..b6d6b7b 100644
--- a/google-compute-engine/src/test/resources/instance_insert_ssd.json
+++ b/google-compute-engine/src/test/resources/instance_insert_ssd.json
@@ -36,5 +36,10 @@
         "value": "test"
       }
     ]
+  },
+  "scheduling": {
+    "onHostMaintenance": "MIGRATE",
+    "automaticRestart": true,
+    "preemptible": false
   }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/jclouds-labs-google/blob/1f6f7f06/google-compute-engine/src/test/resources/instance_list.json
----------------------------------------------------------------------
diff --git a/google-compute-engine/src/test/resources/instance_list.json b/google-compute-engine/src/test/resources/instance_list.json
index ee31a1a..ef60ead 100644
--- a/google-compute-engine/src/test/resources/instance_list.json
+++ b/google-compute-engine/src/test/resources/instance_list.json
@@ -78,7 +78,8 @@
       },
       "scheduling": {
         "onHostMaintenance": "MIGRATE",
-        "automaticRestart": false
+        "automaticRestart": false,
+        "preemptible": false
       }
     }
   ]