|
1 | 1 | data "google_compute_default_service_account" "default" {
|
2 | 2 | project = var.project_id
|
3 |
| - depends_on = [ google_project_service.api["compute.googleapis.com"] ] |
4 | 3 | }
|
5 | 4 |
|
6 | 5 | locals {
|
7 |
| - node_pools = flatten([ for i, deployment in var.deployments : [ |
8 |
| - { |
9 |
| - name = "${var.name}-${deployment.name}-coder" |
10 |
| - zone = deployment.zone |
11 |
| - size = deployment.coder_node_pool_size |
12 |
| - cluster_i = i |
13 |
| - }, |
14 |
| - { |
15 |
| - name = "${var.name}-${deployment.name}-workspaces" |
16 |
| - zone = deployment.zone |
17 |
| - size = deployment.workspaces_node_pool_size |
18 |
| - cluster_i = i |
19 |
| - }, |
20 |
| - { |
21 |
| - name = "${var.name}-${deployment.name}-misc" |
22 |
| - zone = deployment.zone |
23 |
| - size = deployment.misc_node_pool_size |
24 |
| - cluster_i = i |
25 |
| - } |
26 |
| - ] ]) |
| 6 | + abs_module_path = abspath(path.module) |
| 7 | + rel_kubeconfig_path = "../../.coderv2/${var.name}-cluster.kubeconfig" |
| 8 | + cluster_kubeconfig_path = abspath("${local.abs_module_path}/${local.rel_kubeconfig_path}") |
27 | 9 | }
|
28 | 10 |
|
29 |
| -resource "google_container_cluster" "cluster" { |
30 |
| - count = length(var.deployments) |
31 |
| - name = "${var.name}-${var.deployments[count.index].name}" |
32 |
| - location = var.deployments[count.index].zone |
| 11 | +resource "google_container_cluster" "primary" { |
| 12 | + name = var.name |
| 13 | + location = var.zone |
33 | 14 | project = var.project_id
|
34 | 15 | network = google_compute_network.vpc.name
|
35 |
| - subnetwork = google_compute_subnetwork.subnet[count.index].name |
| 16 | + subnetwork = google_compute_subnetwork.subnet.name |
36 | 17 | networking_mode = "VPC_NATIVE"
|
37 | 18 | default_max_pods_per_node = 256
|
38 | 19 | ip_allocation_policy { # Required with networking_mode=VPC_NATIVE
|
@@ -71,15 +52,14 @@ resource "google_container_cluster" "cluster" {
|
71 | 52 | }
|
72 | 53 | }
|
73 | 54 |
|
74 |
| -resource "google_container_node_pool" "node_pool" { |
75 |
| - count = length(local.node_pools) |
76 |
| - name = local.node_pools[count.index].name |
77 |
| - location = local.node_pools[count.index].zone |
| 55 | +resource "google_container_node_pool" "coder" { |
| 56 | + name = "${var.name}-coder" |
| 57 | + location = var.zone |
78 | 58 | project = var.project_id
|
79 |
| - cluster = google_container_cluster.cluster[local.node_pools[count.index].cluster_i].name |
| 59 | + cluster = google_container_cluster.primary.name |
80 | 60 | autoscaling {
|
81 | 61 | min_node_count = 1
|
82 |
| - max_node_count = local.node_pools[count.index].size |
| 62 | + max_node_count = var.nodepool_size_coder |
83 | 63 | }
|
84 | 64 | node_config {
|
85 | 65 | oauth_scopes = [
|
@@ -107,3 +87,100 @@ resource "google_container_node_pool" "node_pool" {
|
107 | 87 | ignore_changes = [management[0].auto_repair, management[0].auto_upgrade, timeouts]
|
108 | 88 | }
|
109 | 89 | }
|
| 90 | + |
| 91 | +resource "google_container_node_pool" "workspaces" { |
| 92 | + name = "${var.name}-workspaces" |
| 93 | + location = var.zone |
| 94 | + project = var.project_id |
| 95 | + cluster = google_container_cluster.primary.name |
| 96 | + autoscaling { |
| 97 | + min_node_count = 0 |
| 98 | + total_max_node_count = var.nodepool_size_workspaces |
| 99 | + } |
| 100 | + management { |
| 101 | + auto_upgrade = false |
| 102 | + } |
| 103 | + node_config { |
| 104 | + oauth_scopes = [ |
| 105 | + "https://www.googleapis.com/auth/logging.write", |
| 106 | + "https://www.googleapis.com/auth/monitoring", |
| 107 | + "https://www.googleapis.com/auth/trace.append", |
| 108 | + "https://www.googleapis.com/auth/devstorage.read_only", |
| 109 | + "https://www.googleapis.com/auth/service.management.readonly", |
| 110 | + "https://www.googleapis.com/auth/servicecontrol", |
| 111 | + ] |
| 112 | + disk_size_gb = var.node_disk_size_gb |
| 113 | + machine_type = var.nodepool_machine_type_workspaces |
| 114 | + image_type = var.node_image_type |
| 115 | + preemptible = var.node_preemptible |
| 116 | + service_account = data.google_compute_default_service_account.default.email |
| 117 | + tags = ["gke-node", "${var.project_id}-gke"] |
| 118 | + labels = { |
| 119 | + env = var.project_id |
| 120 | + } |
| 121 | + metadata = { |
| 122 | + disable-legacy-endpoints = "true" |
| 123 | + } |
| 124 | + } |
| 125 | + lifecycle { |
| 126 | + ignore_changes = [management[0].auto_repair, management[0].auto_upgrade, timeouts] |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +resource "google_container_node_pool" "misc" { |
| 131 | + name = "${var.name}-misc" |
| 132 | + location = var.zone |
| 133 | + project = var.project_id |
| 134 | + cluster = google_container_cluster.primary.name |
| 135 | + node_count = var.state == "stopped" ? 0 : var.nodepool_size_misc |
| 136 | + management { |
| 137 | + auto_upgrade = false |
| 138 | + } |
| 139 | + node_config { |
| 140 | + oauth_scopes = [ |
| 141 | + "https://www.googleapis.com/auth/logging.write", |
| 142 | + "https://www.googleapis.com/auth/monitoring", |
| 143 | + "https://www.googleapis.com/auth/trace.append", |
| 144 | + "https://www.googleapis.com/auth/devstorage.read_only", |
| 145 | + "https://www.googleapis.com/auth/service.management.readonly", |
| 146 | + "https://www.googleapis.com/auth/servicecontrol", |
| 147 | + ] |
| 148 | + disk_size_gb = var.node_disk_size_gb |
| 149 | + machine_type = var.nodepool_machine_type_misc |
| 150 | + image_type = var.node_image_type |
| 151 | + preemptible = var.node_preemptible |
| 152 | + service_account = data.google_compute_default_service_account.default.email |
| 153 | + tags = ["gke-node", "${var.project_id}-gke"] |
| 154 | + labels = { |
| 155 | + env = var.project_id |
| 156 | + } |
| 157 | + metadata = { |
| 158 | + disable-legacy-endpoints = "true" |
| 159 | + } |
| 160 | + } |
| 161 | + lifecycle { |
| 162 | + ignore_changes = [management[0].auto_repair, management[0].auto_upgrade, timeouts] |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +resource "null_resource" "cluster_kubeconfig" { |
| 167 | + depends_on = [google_container_cluster.primary] |
| 168 | + triggers = { |
| 169 | + path = local.cluster_kubeconfig_path |
| 170 | + name = google_container_cluster.primary.name |
| 171 | + project_id = var.project_id |
| 172 | + zone = var.zone |
| 173 | + } |
| 174 | + provisioner "local-exec" { |
| 175 | + command = <<EOF |
| 176 | + KUBECONFIG=${self.triggers.path} gcloud container clusters get-credentials ${self.triggers.name} --project=${self.triggers.project_id} --zone=${self.triggers.zone} |
| 177 | + EOF |
| 178 | + } |
| 179 | + |
| 180 | + provisioner "local-exec" { |
| 181 | + when = destroy |
| 182 | + command = <<EOF |
| 183 | + rm -f ${self.triggers.path} |
| 184 | + EOF |
| 185 | + } |
| 186 | +} |
0 commit comments