Skip to content

Commit 0f1a6a8

Browse files
Add autopilot support (#12)
* Add autopilot support * Fix nodes location logic * terraform-docs: automated action * Add output * Fix formatting * Add variable to enable subnet private access * terraform-docs: automated action * terraform-docs: automated action * Add subnet name to outputs * terraform-docs: automated action * Change node-pools var structure, update doc, fix autopilot support * Remove commented code, remove unused variable, add description * Fix examples, formatting * terraform-docs: automated action --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 9f66878 commit 0f1a6a8

File tree

9 files changed

+126
-91
lines changed

9 files changed

+126
-91
lines changed

README.md

Lines changed: 42 additions & 15 deletions
Large diffs are not rendered by default.

examples/terraform/private-cluster-existing-project/main.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ module "gke" {
88
k8s_network_base = "10.100.0.0/16"
99
regional = false
1010
zones = ["europe-central2-a"]
11-
node_pools = [
12-
{
13-
name = "default-pool"
11+
node_pools = {
12+
default-pool = {
1413
disk_size_gb = 50
1514
max_count = 3
1615
preemptible = true
1716
}
18-
]
17+
}
1918
}

examples/terraform/private-cluster-new-project/main.tf

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@ module "gke" {
99
subnet_network = "10.1.0.0/20"
1010
regional = false
1111
zones = ["europe-central2-a"]
12-
node_pools = [
13-
{
14-
name = "default-pool"
12+
node_pools = {
13+
default-pool = {
1514
disk_size_gb = 50
1615
max_count = 3
17-
preemptible = true
16+
labels = {
17+
"node.pool/name" = "default"
18+
}
19+
oauth_scopes = ["https://www.googleapis.com/auth/compute"]
20+
spot = true
21+
taint = [
22+
{
23+
key = "test"
24+
value = "test"
25+
effect = "NO_SCHEDULE"
26+
}
27+
]
1828
}
19-
]
29+
}
2030
}

examples/terraform/public-cluster-existing-project/main.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ module "gke" {
99
regional = false
1010
zones = ["europe-central2-a"]
1111
enable_private_nodes = false
12-
node_pools = [
13-
{
14-
name = "default-pool"
12+
node_pools = {
13+
default-pool = {
1514
disk_size_gb = 50
1615
max_count = 3
1716
preemptible = true
1817
}
19-
]
18+
}
2019
}

examples/terraform/public-cluster-new-project/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ module "gke" {
1010
regional = false
1111
zones = ["europe-central2-a"]
1212
enable_private_nodes = false
13-
node_pools = [
14-
{
13+
node_pools = {
14+
default-pool = {
1515
name = "default-pool"
1616
disk_size_gb = 50
1717
max_count = 3
1818
preemptible = true
1919
}
20-
]
20+
}
2121
}

locals.tf

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
locals {
2-
project_id = var.create_project ? module.project.0.project_id : var.project_id
3-
project_name = var.project_name != "" ? var.project_name : var.platform_name
4-
subnet_name = "${var.platform_name}-subnet"
5-
router = "${var.platform_name}-router"
6-
cloud_nat_name = "${var.platform_name}-cloud-nat"
7-
pods_network_name = "${local.subnet_name}-pods"
8-
services_network_name = "${local.subnet_name}-services"
9-
pods_ip_range = cidrsubnet(var.k8s_network_base, 4, 1)
10-
services_ip_range = cidrsubnet(var.k8s_network_base, 4, 2)
11-
location = var.regional ? var.region : var.zones.0
12-
node_pool_names = [for np in toset(var.node_pools) : np.name]
13-
node_pools = zipmap(local.node_pool_names, tolist(toset(var.node_pools)))
14-
node_locations = var.regional ? var.zones : slice(var.zones, 1, length(var.zones))
15-
node_pool_oauth_scopes = { for key, value in var.additional_node_pool_oauth_scopes : key => distinct(concat(value, var.default_node_pools_oauth_scopes)) }
2+
project_id = var.create_project ? module.project.0.project_id : var.project_id
3+
project_name = var.project_name != "" ? var.project_name : var.platform_name
4+
subnet_name = "${var.platform_name}-subnet"
5+
router = "${var.platform_name}-router"
6+
cloud_nat_name = "${var.platform_name}-cloud-nat"
7+
pods_network_name = "${local.subnet_name}-pods"
8+
services_network_name = "${local.subnet_name}-services"
9+
pods_ip_range = cidrsubnet(var.k8s_network_base, 4, 1)
10+
services_ip_range = cidrsubnet(var.k8s_network_base, 4, 2)
11+
location = var.regional ? var.region : var.zones.0
12+
node_locations = var.regional ? (length(var.zones) != 0 ? var.zones : null) : slice(var.zones, 1, length(var.zones))
1613
}

main.tf

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module "project" {
22
source = "registry.terraform.io/terraform-google-modules/project-factory/google"
3-
version = "13.0.0"
3+
version = "14.2.1"
44
billing_account = var.billing_account
55
name = var.platform_name
66
org_id = var.org_id
@@ -10,8 +10,8 @@ module "project" {
1010
}
1111

1212
module "project_services" {
13-
source = "registry.terraform.io/terraform-google-modules/project-factory/google//modules/project_services"
14-
version = "13.0.0"
13+
source = "terraform-google-modules/project-factory/google//modules/project_services"
14+
version = "14.2.1"
1515
project_id = var.project_id
1616
activate_apis = var.activate_apis
1717
disable_services_on_destroy = var.disable_services_on_destroy
@@ -20,15 +20,16 @@ module "project_services" {
2020

2121
module "network" {
2222
source = "registry.terraform.io/terraform-google-modules/network/google"
23-
version = "5.0.0"
23+
version = "7.1.0"
2424
network_name = var.platform_name
2525
project_id = local.project_id
2626
auto_create_subnetworks = false
2727
subnets = [
2828
{
29-
subnet_name = local.subnet_name
30-
subnet_ip = var.subnet_network
31-
subnet_region = var.region
29+
subnet_name = local.subnet_name
30+
subnet_ip = var.subnet_network
31+
subnet_region = var.region
32+
subnet_private_access = var.subnet_private_access
3233
}
3334
]
3435
secondary_ranges = {
@@ -60,7 +61,7 @@ resource "google_compute_address" "cloud_nat_address" {
6061

6162
module "cloud_nat" {
6263
source = "registry.terraform.io/terraform-google-modules/cloud-nat/google"
63-
version = "2.2.0"
64+
version = "4.0.0"
6465
project_id = local.project_id
6566
region = var.region
6667
network = module.network.network_name
@@ -81,7 +82,8 @@ resource "google_container_cluster" "gke" {
8182
node_locations = local.node_locations
8283
network = module.network.network_self_link
8384
subnetwork = local.subnet_name
84-
remove_default_node_pool = true
85+
remove_default_node_pool = var.enable_autopilot == null ? true : null
86+
enable_autopilot = var.enable_autopilot
8587
initial_node_count = 1
8688
node_config {
8789
machine_type = var.default_pool_machine_type
@@ -95,11 +97,15 @@ resource "google_container_cluster" "gke" {
9597
depends_on = [
9698
module.network.subnets
9799
]
100+
ip_allocation_policy {
101+
cluster_secondary_range_name = local.pods_network_name
102+
services_secondary_range_name = local.services_network_name
103+
}
98104
}
99105

100106
resource "google_container_node_pool" "pools" {
101107
provider = google-beta
102-
for_each = local.node_pools
108+
for_each = var.node_pools
103109
location = local.location
104110
project = local.project_id
105111
cluster = google_container_cluster.gke.name
@@ -129,8 +135,10 @@ resource "google_container_node_pool" "pools" {
129135
disk_type = lookup(each.value, "disk_type", "pd-standard")
130136
preemptible = lookup(each.value, "preemptible", false)
131137
spot = lookup(each.value, "spot", false)
132-
labels = lookup(var.node_pools_labels, each.value["name"], {})
133-
oauth_scopes = lookup(local.node_pool_oauth_scopes, each.value["name"], [])
138+
labels = lookup(each.value, "labels", {})
139+
oauth_scopes = lookup(each.value, "oauth_scopes", var.default_node_pools_oauth_scopes)
140+
service_account = lookup(each.value, "service_account", null)
141+
taint = lookup(each.value, "taint", [])
134142

135143
dynamic "guest_accelerator" {
136144
for_each = lookup(each.value, "guest_accelerator", null) != null ? [1] : []
@@ -159,4 +167,3 @@ resource "google_container_registry" "registry" {
159167
project = local.project_id
160168
location = var.gcr_location
161169
}
162-

outputs.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,19 @@ output "vpc_id" {
3434
value = module.network.network_id
3535
description = "VPC (network) ID"
3636
}
37+
output "vpc_self_link" {
38+
value = module.network.network_self_link
39+
description = "VPC (network) self link"
40+
}
3741
output "gke_zones" {
3842
value = google_container_cluster.gke.node_locations
3943
description = "List of zones where the cluster lives"
4044
}
4145
output "nat_ip" {
42-
value = google_compute_address.cloud_nat_address.*.address
46+
value = google_compute_address.cloud_nat_address.*.address
47+
description = "The IP address allocated for NAT"
48+
}
49+
output "subnetwork_name" {
50+
value = module.network.subnets_names.0
51+
description = "Name of the subnetwork"
4352
}

variables.tf

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ variable "project_id" {
2222
default = ""
2323
description = "Existing project id. Required if `create_project` set to `false`"
2424
validation {
25-
condition = can(regex("^[a-z]{1}[0-9a-z-]{5,29}$", var.project_id))
25+
condition = (var.project_id == "" || can(regex("^[a-z]{1}[0-9a-z-]{5,29}$", var.project_id)))
2626
error_message = "The project id must be 6 to 30 characters in length, can only contain lowercase letters, numbers, and hyphens"
2727
}
2828
}
@@ -31,7 +31,7 @@ variable "project_name" {
3131
default = ""
3232
description = "The name of the created project. Defaults to `platform_name` if not set."
3333
validation {
34-
condition = length(var.project_name) < 25 && length(var.project_name) > 4
34+
condition = (var.project_name == "" || length(var.project_name) < 25 && length(var.project_name) > 4)
3535
error_message = "The project name should contain only 25 characters. Last 5 characters up to 30 total are generated"
3636
}
3737
}
@@ -70,24 +70,11 @@ variable "zones" {
7070
default = []
7171
description = "List of zones for `zonal` cluster. Required if `regional` set to `false`."
7272
}
73-
variable "node_pools" {
74-
type = list(any)
75-
default = [
76-
{
77-
name = "default-node-pool"
78-
},
79-
]
80-
description = "List of node pools. For parameter details refer to node_pool variable table below"
81-
}
8273

83-
variable "node_pools_labels" {
84-
type = map(map(string))
85-
default = {
86-
"default-node-pool" = {
87-
"node.pool/name" = "default-node-pool"
88-
},
89-
}
90-
description = "List of node pools labels. https://registry.terraform.io/modules/terraform-google-modules/kubernetes-engine/google/21.1.0/submodules/private-cluster-update-variant?tab=inputs#:~:text=default%2Dnode%2Dpool%22%20%7D%20%5D-,node_pools_labels,-map(map(string"
74+
variable "node_pools" {
75+
type = map(map(any))
76+
default = {}
77+
description = "The object which describes the node pools. The structure is described in the README file."
9178
}
9279

9380
variable "master_ipv4_cidr_block" {
@@ -144,22 +131,22 @@ variable "default_pool_machine_type" {
144131
description = "In some cases the GKE won't be created unless the default pool uses specific machine type (for example confidential nodes) so we have to set the type even if the default pool is removed."
145132
}
146133

147-
variable "additional_node_pool_oauth_scopes" {
148-
type = map(list(string))
149-
default = {
150-
default-node-pool = []
151-
}
152-
description = "Node pool oauth scopes added to specified node pool in addition to default_node_pool_oauth_scopes. It's referenced by node_pool `name`"
153-
}
154-
155134
variable "default_node_pools_oauth_scopes" {
156135
type = list(string)
157136
default = [
158-
"https://www.googleapis.com/auth/devstorage.read_only",
159-
"https://www.googleapis.com/auth/cloud-platform",
160-
"https://www.googleapis.com/auth/logging.write",
161-
"https://www.googleapis.com/auth/monitoring",
162-
"https://www.googleapis.com/auth/compute"
137+
"https://www.googleapis.com/auth/cloud-platform"
163138
]
164139
description = "Default node pool oauth scopes added to all node pools"
165140
}
141+
142+
variable "enable_autopilot" {
143+
type = bool
144+
default = null
145+
description = "Whether to enable Autopilot feature"
146+
}
147+
148+
variable "subnet_private_access" {
149+
type = bool
150+
default = true
151+
description = "Whether to enable google private IP access for the subnet"
152+
}

0 commit comments

Comments
 (0)