From b1ff629ffd07f96751568e7bd8241c9de5061acc Mon Sep 17 00:00:00 2001 From: Jvst Me Date: Mon, 7 Jul 2025 23:08:56 +0200 Subject: [PATCH] Private GCP gateways Allow configuring private GCP gateways without TLS certificates. ```yaml type: gateway name: example domain: gateway.example.com backend: gcp region: europe-west9 public_ip: false certificate: null ``` --- .../_internal/core/backends/gcp/compute.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/dstack/_internal/core/backends/gcp/compute.py b/src/dstack/_internal/core/backends/gcp/compute.py index 7e60afff43..8a03030c0d 100644 --- a/src/dstack/_internal/core/backends/gcp/compute.py +++ b/src/dstack/_internal/core/backends/gcp/compute.py @@ -8,6 +8,7 @@ import google.cloud.compute_v1 as compute_v1 from cachetools import TTLCache, cachedmethod from google.cloud import tpu_v2 +from google.cloud.compute_v1.types.compute import Instance from gpuhunt import KNOWN_TPUS import dstack._internal.core.backends.gcp.auth as auth @@ -19,6 +20,7 @@ ComputeWithGatewaySupport, ComputeWithMultinodeSupport, ComputeWithPlacementGroupSupport, + ComputeWithPrivateGatewaySupport, ComputeWithVolumeSupport, generate_unique_gateway_instance_name, generate_unique_instance_name, @@ -83,6 +85,7 @@ class GCPCompute( ComputeWithMultinodeSupport, ComputeWithPlacementGroupSupport, ComputeWithGatewaySupport, + ComputeWithPrivateGatewaySupport, ComputeWithVolumeSupport, Compute, ): @@ -395,11 +398,7 @@ def update_provisioning_data( if instance.status in ["PROVISIONING", "STAGING"]: return if instance.status == "RUNNING": - if allocate_public_ip: - hostname = instance.network_interfaces[0].access_configs[0].nat_i_p - else: - hostname = instance.network_interfaces[0].network_i_p - provisioning_data.hostname = hostname + provisioning_data.hostname = _get_instance_ip(instance, allocate_public_ip) provisioning_data.internal_ip = instance.network_interfaces[0].network_i_p return raise ProvisioningError( @@ -512,6 +511,7 @@ def create_gateway( service_account=self.config.vm_service_account, network=self.config.vpc_resource_name, subnetwork=subnetwork, + allocate_public_ip=configuration.public_ip, ) operation = self.instances_client.insert(request=request) gcp_resources.wait_for_extended_operation(operation, "instance creation") @@ -522,7 +522,7 @@ def create_gateway( instance_id=instance_name, region=configuration.region, # used for instance termination availability_zone=zone, - ip_address=instance.network_interfaces[0].access_configs[0].nat_i_p, + ip_address=_get_instance_ip(instance, configuration.public_ip), backend_data=json.dumps({"zone": zone}), ) @@ -1024,3 +1024,9 @@ def _is_tpu_provisioning_data(provisioning_data: JobProvisioningData) -> bool: backend_data_dict = json.loads(provisioning_data.backend_data) is_tpu = backend_data_dict.get("is_tpu", False) return is_tpu + + +def _get_instance_ip(instance: Instance, public_ip: bool) -> str: + if public_ip: + return instance.network_interfaces[0].access_configs[0].nat_i_p + return instance.network_interfaces[0].network_i_p