我正在尝试使用 Terraform 将集群部署到 GKE,并在执行此操作时使用地图的值。 当引用集群模块的键时,不使用这些值,并且要求我输入。
这是我的 gke_cluster 模块的配置:
terraform/modules/gke_cluster/main.tf:
variable "node_pool_overwrites" {
description = "Overwritten node pool configurations for specific clusters"
type = map(any)
default = {}
}
resource "google_container_cluster" "gke_cluster" {
name = var.name
location = var.location
initial_node_count = 1
remove_default_node_pool = true
dynamic "node_pool" {
for_each = merge(
lookup(var.default_node_pool_configs),
var.node_pool_overwrites
)
content {
name = node_pool.key
node_count = node_pool.value.count
node_config {
machine_type = node_pool.value.hw
image_type = "COS"
labels = {
type = node_pool.key
name = var.name
location = var.location
}
// Additional config for specialized node pools
dynamic "taint" {
for_each = node_pool.key == "proxy" || node_pool.key == "proxyspot" ? [1] : []
content {
key = "dedicated"
value = "proxy"
effect = "NO_EXECUTE"
}
}
}
autoscaling {
min_node_count = node_pool.value.min
max_node_count = node_pool.value.max
}
management {
auto_upgrade = true
auto_repair = true
}
}
}
}
terraform/modules/gke_cluster/outputs.tf:
output "endpoint" {
description = "The endpoint of the GKE cluster."
value = google_container_cluster.gke_cluster.endpoint
}
output "name" {
description = "The name of the GKE cluster."
value = google_container_cluster.gke_cluster.name
}
output "location" {
description = "The location of the GKE cluster."
value = google_container_cluster.gke_cluster.location
}
terraform/modules/gke_cluster/variables.tf:
variable "name" {
description = "The name of the GKE cluster."
type = string
}
variable "location" {
description = "The location to host the GKE cluster."
type = string
}
variable "node_pools" {
description = "A map of node pool configurations."
type = map(any)
}
variable "default_node_pool_configs" {
description = "Default node pool configurations for GKE clusters"
type = map(any)
default = {
"pop2" = {
default = {
hw = "custom-2-4096"
disk = "pd-standard"
disk_size_gb = 15
count = 2
min = 0
max = 0
},
proxy = {
hw = "highcpu-4"
disk = "pd-standard"
disk_size_gb = 15
count = 0
min = 0
max = 20
},
proxyspot = {
hw = "highcpu-4"
disk = "pd-standard"
disk_size_gb = 15
count = 1
min = 0
max = 20
}
},
"pop4" = {
default = {
hw = "highcpu-4"
disk = "pd-standard"
disk_size_gb = 15
count = 2
min = 0
max = 0
},
proxy = {
hw = "highcpu-4"
disk = "pd-standard"
disk_size_gb = 50
count = 0
min = 0
max = 20
},
proxyspot = {
hw = "highcpu-4"
disk = "pd-standard"
disk_size_gb = 15
count = 1
min = 0
max = 20
}
},
"pop4xl" = {
default = {
hw = "highcpu-4"
disk = "pd-standard"
disk_size_gb = 100
count = 2
min = 0
max = 0
},
proxy = {
hw = "highcpu-8"
disk = "pd-standard"
disk_size_gb = 50
count = 0
min = 0
max = 20
},
proxyspot = {
hw = "highcpu-8"
disk = "pd-standard"
disk_size_gb = 15
count = 1
min = 0
max = 20
}
},
"pop6xl" = {
default = {
hw = "custom-6-8192"
disk = "pd-standard"
disk_size_gb = 15
count = 2
min = 0
max = 0
},
proxy = {
hw = "highcpu-8"
disk = "pd-standard"
disk_size_gb = 15
count = 0
min = 0
max = 20
},
proxyspot = {
hw = "highcpu-8"
disk = "pd-standard"
disk_size_gb = 15
count = 1
min = 0
max = 20
}
},
"pop8" = {
default = {
hw = "highcpu-8"
disk = "pd-standard"
disk_size_gb = 15
count = 2
min = 0
max = 0
},
proxy = {
hw = "highcpu-8"
disk = "pd-standard"
disk_size_gb = 15
count = 0
min = 0
max = 20
},
proxyspot = {
hw = "highcpu-8"
disk = "pd-standard"
disk_size_gb = 15
count = 1
min = 0
max = 20
}
}
}
}
以下是我的测试集群的配置:
terraform/clusters/test_madrid_cluster/main.tf:
module "test-madrid-cluster" {
source = "../../modules/gke_cluster"
name = "test-madrid-cluster"
location = "europe-southwest1-a"
node_pools = var.default_node_pool_configs["pop2"]
// Pass the default node pool configs to the module
default_node_pool_configs = var.default_node_pool_configs
}
terraform/clusters/test_madrid_cluster/variables.tf:
variable "default_node_pool_configs" {
description = "Default node pool configurations for GKE clusters"
type = map(any)
}
在
terraform/clusters/test_madrid_cluster/内运行
terraform plan
时,我的期望是使用 pop2
值(来自 terraform/modules/gke_cluster/variables.tf 内的 default_node_pool_configs 映射)。
相反,系统会提示我输入值:
❯ terraform plan
var.default_node_pool_configs
Default node pool configurations for GKE clusters
Enter a value:
如何修复我的参考,以便正确分配地图的值?
如果您的目标是声明的模块为其变量
default_node_pool_configs
使用其默认值,那么您不希望使用模块声明中的输入值覆盖默认值。从声明中删除参数将利用模块变量声明中的默认值:
module "test-madrid-cluster" {
source = "../../modules/gke_cluster"
name = "test-madrid-cluster"
location = "europe-southwest1-a"
node_pools = var.default_node_pool_configs["pop2"]
}