JCLOUDS-1001. Add preemptible support to the Google Compute provider.

The Google Compute provider should support preemptible instances.
This commit is contained in:
Michael Wilson 2015-09-15 17:00:01 -07:00 committed by Ignasi Barrera
parent 042222b647
commit 962980cd9d
14 changed files with 75 additions and 17 deletions

View File

@ -0,0 +1 @@
/.apt_generated/

View File

@ -139,6 +139,8 @@ public final class GoogleComputeEngineServiceAdapter
tags.add(naming.name(ports));
}
Scheduling scheduling = getScheduling(options);
NewInstance newInstance = new NewInstance.Builder( name,
template.getHardware().getUri(), // machineType
network,
@ -146,6 +148,7 @@ public final class GoogleComputeEngineServiceAdapter
.description(group)
.tags(Tags.create(null, ImmutableList.copyOf(tags)))
.serviceAccounts(options.serviceAccounts())
.scheduling(scheduling)
.build();
// Add metadata from template and for ssh key and image id
@ -177,8 +180,8 @@ public final class GoogleComputeEngineServiceAdapter
null, // disks
newInstance.metadata(), // metadata
newInstance.serviceAccounts(), // 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
@ -315,4 +318,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());
}
}

View File

@ -30,6 +30,7 @@ public final class GoogleComputeEngineTemplateOptions extends TemplateOptions {
private boolean autoCreateKeyPair = true;
private List<ServiceAccount> serviceAccounts;
private String bootDiskType;
private boolean preemptible = false;
@Override
public GoogleComputeEngineTemplateOptions clone() {
@ -46,6 +47,7 @@ public final class GoogleComputeEngineTemplateOptions extends TemplateOptions {
eTo.autoCreateKeyPair(autoCreateKeyPair());
eTo.serviceAccounts(serviceAccounts());
eTo.bootDiskType(bootDiskType());
eTo.preemptible(preemptible());
}
}
@ -90,6 +92,21 @@ public final class GoogleComputeEngineTemplateOptions extends TemplateOptions {
return this;
}
/**
* 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;
}
/**
* Gets the list of service accounts, with their specified scopes, that will be authorize on created instances.
*/

View File

@ -186,16 +186,19 @@ 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() {

View File

@ -242,6 +242,7 @@ public interface InstanceApi {
* @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")
@ -250,7 +251,8 @@ public interface InstanceApi {
@MapBinder(BindToJsonPayload.class)
Operation setScheduling(@PathParam("instance") String instanceName,
@PayloadParam("onHostMaintenance") Scheduling.OnHostMaintenance onHostMaintenance,
@PayloadParam("automaticRestart") boolean automaticRestart);
@PayloadParam("automaticRestart") boolean automaticRestart,
@PayloadParam("preemptible") boolean preemptible);
/**
* This method starts an instance that was stopped using the using the {@link #stop(String)} method.

View File

@ -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));

View File

@ -18,6 +18,7 @@ package org.jclouds.googlecomputeengine.features;
import static org.jclouds.googlecomputeengine.options.ListOptions.Builder.filter;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
@ -109,7 +110,7 @@ public class InstanceApiLiveTest extends BaseGoogleComputeEngineApiLiveTest {
.description("description")
.tags(Tags.create(null, ImmutableList.of("tag1")))
.serviceAccounts(ImmutableList.of(ServiceAccount.create("default", ImmutableList.of("https://www.googleapis.com/auth/compute"))))
.scheduling(Scheduling.create(OnHostMaintenance.MIGRATE, true))
.scheduling(Scheduling.create(OnHostMaintenance.MIGRATE, true, false))
.build();
return api;
@ -132,6 +133,7 @@ public class InstanceApiLiveTest extends BaseGoogleComputeEngineApiLiveTest {
assertEquals(instance.description(), "description");
assertEquals(instance.serviceAccounts().get(0).scopes(), ImmutableList.of("https://www.googleapis.com/auth/compute"));
assertTrue(instance.scheduling().automaticRestart());
assertFalse(instance.scheduling().preemptible());
assertEquals(instance.scheduling().onHostMaintenance(), OnHostMaintenance.MIGRATE);
}
@ -205,12 +207,15 @@ 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);
}

View File

@ -227,11 +227,11 @@ public class InstanceApiMockTest extends BaseGoogleComputeEngineApiMockTest {
public void setScheduling() throws Exception {
server.enqueue(jsonResponse("/zone_operation.json"));
assertEquals(instanceApi().setScheduling("test-1", OnHostMaintenance.TERMINATE, true),
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}");
"{\"onHostMaintenance\": \"TERMINATE\",\"automaticRestart\": true,\"preemptible\": false}");
}
public void start_test() throws Exception {
@ -279,7 +279,7 @@ public class InstanceApiMockTest extends BaseGoogleComputeEngineApiMockTest {
.metadata(Metadata.create().put("aKey", "aValue"))
.serviceAccounts(ImmutableList.of(ServiceAccount.create("default",
ImmutableList.of("https://www.googleapis.com/auth/compute"))))
.scheduling(Scheduling.create(OnHostMaintenance.MIGRATE, true))
.scheduling(Scheduling.create(OnHostMaintenance.MIGRATE, true, false))
.build();
assertEquals(instanceApi().create(newInstance), new ParseZoneOperationTest().expected(url("/projects")));

View File

@ -88,7 +88,7 @@ public class ParseInstanceTest extends BaseGoogleComputeEngineParseTest<Instance
.put("aKey", "aValue")
.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
);
}
}

View File

@ -69,6 +69,7 @@
},
"scheduling": {
"onHostMaintenance": "MIGRATE",
"automaticRestart": false
"automaticRestart": false,
"preemptible": false
}
}

View File

@ -35,5 +35,10 @@
"value": "test"
}
]
},
"scheduling": {
"onHostMaintenance": "MIGRATE",
"automaticRestart": true,
"preemptible": false
}
}

View File

@ -42,6 +42,7 @@
],
"scheduling": {
"onHostMaintenance": "MIGRATE",
"automaticRestart": true
"automaticRestart": true,
"preemptible": false
}
}

View File

@ -36,5 +36,10 @@
"value": "test"
}
]
},
"scheduling": {
"onHostMaintenance": "MIGRATE",
"automaticRestart": true,
"preemptible": false
}
}

View File

@ -74,7 +74,8 @@
},
"scheduling": {
"onHostMaintenance": "MIGRATE",
"automaticRestart": false
"automaticRestart": false,
"preemptible": false
}
}
]