Skip to content

Commit

Permalink
Validate instance types for GCP (nebari-dev#2730)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcmcand authored Dec 9, 2024
2 parents ed6736e + 8dad7d0 commit 46b5686
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/_nebari/provider/cloud/google_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ def regions() -> Set[str]:
return {region.name for region in response}


@functools.lru_cache()
def instances(region: str) -> Set[str]:
"""Return a set of available compute instances in a region."""
credentials, project_id = load_credentials()
zones_client = compute_v1.services.region_zones.RegionZonesClient(
credentials=credentials
)
instances_client = compute_v1.InstancesClient(credentials=credentials)

return {
instance.machine_type.split("/")[-1]
for zone in zones_client.list(project=project_id, region=region)
for instance in instances_client.list(project=project_id, zone=zone.name)
}


@functools.lru_cache()
def kubernetes_versions(region: str) -> List[str]:
"""Return list of available kubernetes supported by cloud provider. Sorted from oldest to latest."""
Expand Down
15 changes: 15 additions & 0 deletions src/_nebari/stages/infrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,21 @@ def _check_input(cls, data: Any) -> Any:
raise ValueError(
f"\nInvalid `kubernetes-version` provided: {data['kubernetes_version']}.\nPlease select from one of the following supported Kubernetes versions: {available_kubernetes_versions} or omit flag to use latest Kubernetes version available."
)

# check if instances are valid
available_instances = google_cloud.instances(data["region"])
if "node_groups" in data:
for _, node_group in data["node_groups"].items():
instance = (
node_group["instance"]
if hasattr(node_group, "__getitem__")
else node_group.instance
)
if instance not in available_instances:
raise ValueError(
f"Google Cloud Platform instance {instance} not one of available instance types={available_instances}"
)

return data


Expand Down
5 changes: 5 additions & 0 deletions tests/tests_unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def _mock_return_value(return_value):
"us-central1",
"us-east1",
],
"_nebari.provider.cloud.google_cloud.instances": [
"e2-standard-4",
"e2-standard-8",
"e2-highmem-4",
],
}

for attribute_path, return_value in MOCK_VALUES.items():
Expand Down

0 comments on commit 46b5686

Please sign in to comment.