|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +data "google_compute_image" "fluxfw_compute_x86_64_image" { |
| 16 | + project = var.project_id |
| 17 | + family = var.family |
| 18 | +} |
| 19 | + |
| 20 | +data "google_compute_zones" "available" { |
| 21 | + project = var.project_id |
| 22 | + region = var.region |
| 23 | +} |
| 24 | + |
| 25 | +locals { |
| 26 | + automatic_restart = var.compact_placement ? false : var.automatic_restart |
| 27 | + compute_images = { |
| 28 | + "x86-64" = { |
| 29 | + image = data.google_compute_image.fluxfw_compute_x86_64_image.self_link |
| 30 | + project = data.google_compute_image.fluxfw_compute_x86_64_image.project |
| 31 | + } |
| 32 | + } |
| 33 | + on_host_maintenance = var.compact_placement ? "TERMINATE" : var.on_host_maintenance |
| 34 | +} |
| 35 | + |
| 36 | +resource "google_compute_resource_policy" "collocated" { |
| 37 | + count = var.compact_placement ? 1 : 0 |
| 38 | + name = "${var.name_prefix}-collocated-policy" |
| 39 | + project = var.project_id |
| 40 | + region = var.region |
| 41 | + group_placement_policy { |
| 42 | + vm_count = var.num_instances |
| 43 | + collocation = "COLLOCATED" |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +module "flux_compute_instance_template" { |
| 48 | + source = "github.com/terraform-google-modules/terraform-google-vm/modules/instance_template" |
| 49 | + region = var.region |
| 50 | + project_id = var.project_id |
| 51 | + name_prefix = var.name_prefix |
| 52 | + subnetwork = var.subnetwork |
| 53 | + gpu = var.gpu |
| 54 | + service_account = var.service_account |
| 55 | + tags = ["ssh", "flux", "compute"] |
| 56 | + machine_type = var.machine_type |
| 57 | + disk_size_gb = 256 |
| 58 | + source_image = local.compute_images["${var.machine_arch}"].image |
| 59 | + source_image_project = local.compute_images["${var.machine_arch}"].project |
| 60 | + automatic_restart = local.automatic_restart |
| 61 | + on_host_maintenance = local.on_host_maintenance |
| 62 | + startup_script = var.boot_script |
| 63 | + |
| 64 | + metadata = { |
| 65 | + "enable-oslogin" : "TRUE", |
| 66 | + "VmDnsSetting" : "GlobalDefault", |
| 67 | + "nfs-mounts" : jsonencode(var.nfs_mounts), |
| 68 | + "gpus-attached" : var.gpu != null ? "TRUE" : "FALSE" |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +module "flux_compute_instances" { |
| 73 | + source = "github.com/terraform-google-modules/terraform-google-vm/modules/compute_instance" |
| 74 | + region = var.region |
| 75 | + zone = data.google_compute_zones.available.names[0] |
| 76 | + hostname = var.name_prefix |
| 77 | + add_hostname_suffix = true |
| 78 | + num_instances = var.num_instances |
| 79 | + resource_policies = var.compact_placement ? [ google_compute_resource_policy.collocated[0].self_link ] : [] |
| 80 | + instance_template = module.flux_compute_instance_template.self_link |
| 81 | + subnetwork = var.subnetwork |
| 82 | +} |
0 commit comments