From ad47aa938d53f4277e27f4ad70a15765c84418e6 Mon Sep 17 00:00:00 2001 From: Blake Rosenthal Date: Thu, 19 Sep 2024 12:54:06 -0700 Subject: [PATCH 1/3] add instances func to google_cloud --- src/_nebari/provider/cloud/google_cloud.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/_nebari/provider/cloud/google_cloud.py b/src/_nebari/provider/cloud/google_cloud.py index 6b54e40e9..9644445f9 100644 --- a/src/_nebari/provider/cloud/google_cloud.py +++ b/src/_nebari/provider/cloud/google_cloud.py @@ -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.""" From bcca5163f93f6a4ed1ba3d00b4f60d57d216bc66 Mon Sep 17 00:00:00 2001 From: Blake Rosenthal Date: Thu, 19 Sep 2024 13:06:31 -0700 Subject: [PATCH 2/3] validate instance types --- src/_nebari/stages/infrastructure/__init__.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/_nebari/stages/infrastructure/__init__.py b/src/_nebari/stages/infrastructure/__init__.py index 682e9d50b..69f953d65 100644 --- a/src/_nebari/stages/infrastructure/__init__.py +++ b/src/_nebari/stages/infrastructure/__init__.py @@ -401,6 +401,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 {node_group.instance} not one of available instance types={available_instances}" + ) + return data From 6266a23750ee78e0e73dec60c975548d2f2011c1 Mon Sep 17 00:00:00 2001 From: Blake Rosenthal Date: Thu, 19 Sep 2024 14:31:55 -0700 Subject: [PATCH 3/3] fix error string formatting --- src/_nebari/stages/infrastructure/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_nebari/stages/infrastructure/__init__.py b/src/_nebari/stages/infrastructure/__init__.py index 69f953d65..605a93d51 100644 --- a/src/_nebari/stages/infrastructure/__init__.py +++ b/src/_nebari/stages/infrastructure/__init__.py @@ -413,7 +413,7 @@ def _check_input(cls, data: Any) -> Any: ) if instance not in available_instances: raise ValueError( - f"Google Cloud Platform instance {node_group.instance} not one of available instance types={available_instances}" + f"Google Cloud Platform instance {instance} not one of available instance types={available_instances}" ) return data