Skip to content

Commit

Permalink
PTFE-1041 setup capability to spawn GCP spot instances
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarmet committed Oct 4, 2023
1 parent fcaa58e commit cb9e505
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions runner_manager/models/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Items,
Metadata,
NetworkInterface,
Scheduling,
)
from mypy_boto3_ec2.literals import InstanceTypeType, VolumeTypeType
from mypy_boto3_ec2.type_defs import (
Expand Down Expand Up @@ -110,6 +111,7 @@ class GCPInstanceConfig(InstanceConfig):
image_family: str = "ubuntu-2004-lts"
image_project: str = "ubuntu-os-cloud"
machine_type: str = "e2-small"
spot: bool = False
network: str = "global/networks/default"
enable_nested_virtualization: bool = True
labels: Optional[Dict[str, str]] = {}
Expand All @@ -124,6 +126,18 @@ class GCPInstanceConfig(InstanceConfig):
class Config:
arbitrary_types_allowed = True

@property
def scheduling(self) -> Scheduling:
"""Configure scheduling."""
if self.spot:
return Scheduling(
provisioning_model="SPOT", instance_termination_action="DELETE"
)
else:
return Scheduling(
provisioning_model="STANDARD", instance_termination_action="DEFAULT"
)

def configure_metadata(self, runner: Runner) -> Metadata:
items: List[Items] = []
env: RunnerEnv = self.runner_env(runner)
Expand All @@ -148,6 +162,7 @@ def configure_instance(self, runner: Runner) -> Instance:
advanced_machine_features=AdvancedMachineFeatures(
enable_nested_virtualization=self.enable_nested_virtualization
),
scheduling=self.scheduling,
)


Expand Down
11 changes: 11 additions & 0 deletions tests/unit/backend/test_gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ def test_gcp_instance(runner: Runner):
assert startup is True


def test_gcp_spot_config(runner: Runner):
gcp_instance: GCPInstanceConfig = GCPInstanceConfig(spot=True)
instance = gcp_instance.configure_instance(runner)
assert instance.scheduling.provisioning_model == "SPOT"
assert instance.scheduling.instance_termination_action == "DELETE"
gcp_instance: GCPInstanceConfig = GCPInstanceConfig(spot=False)
instance = gcp_instance.configure_instance(runner)
assert instance.scheduling.provisioning_model == "STANDARD"
assert instance.scheduling.instance_termination_action == "DEFAULT"


@mark.skipif(
not os.getenv("GOOGLE_APPLICATION_CREDENTIALS"), reason="GCP credentials not found"
)
Expand Down

0 comments on commit cb9e505

Please sign in to comment.