Skip to content

Unable to create multiple node pools #1397

@krzkowalczyk

Description

@krzkowalczyk

TL;DR

It is not possible to create GKE cluster with multiple node pools at once.
It only works if we create GKE cluster with one node pool, and later on add additional ones.

Expected behavior

All defined node pools are being created successfully

Observed behavior

Where I want to create a new cluster containing multiple node pools, during terraform plan or terraform apply there is an error:


│ Error: Invalid for_each argument
│ 
│   on .terraform/modules/gke/modules/private-cluster/cluster.tf line 311, in resource "google_container_node_pool" "pools":
│  311:   for_each = local.node_pools
│     ├────────────────
│     │ local.node_pools is a map of map of string, known only after apply
│ 
│ The "for_each" map includes keys derived from resource attributes that cannot be determined until apply, and so Terraform cannot determine the full
│ set of keys that will identify the instances of this resource.
│ 
│ When working with unknown values in for_each, it's better to define the map keys statically in your configuration and place apply-time results only in
│ the map values.
│ 
│ Alternatively, you could use the -target planning option to first apply only the resources that the for_each value depends on, and then apply a second
│ time to fully converge.

Therefore, If I define just one node pool and create the cluster, and later add second pool terraform properly creates the missing pool.
The problem exists only if I want to create at once GKE cluster + multiple node pools.

Terraform Configuration

module "gke" {
  source                     = "terraform-google-modules/kubernetes-engine/google//modules/private-cluster"
  version                    = "23.0.0"
  project_id                 = var.project_id
  name                       = "${var.project_id}-gke-euwest1-main"
  region                     = var.region
  zones                      = ["${var.region}-d", "${var.region}-b", "${var.region}-c"]
  network                    = data.terraform_remote_state.net.outputs.vpc_name
  subnetwork                 = "${var.project_id}-euwest1-net-1"
  ip_range_pods              = "${var.project_id}-euwest1-net-1-sbt-pod-range"
  ip_range_services          = "${var.project_id}-euwest1-net-1-sbt-service-range"
  http_load_balancing        = true
  network_policy             = true
  horizontal_pod_autoscaling = false
  filestore_csi_driver       = false
  enable_private_endpoint    = false
  enable_private_nodes       = true
  master_ipv4_cidr_block     = "172.18.0.0/28"
  release_channel            = "STABLE"
  remove_default_node_pool   = true

  node_pools = [
    {
      name               = "node-pool-${var.region}-b"
      machine_type       = "e2-medium"
      node_locations     = "${var.region}-b"
      min_count          = 1
      max_count          = 3
      local_ssd_count    = 0
      spot               = false
      disk_size_gb       = 60
      disk_type          = "pd-standard"
      image_type         = "COS_CONTAINERD"
      enable_gcfs        = false
      enable_gvnic       = false
      auto_repair        = true
      auto_upgrade       = true
      service_account    = google_service_account.node_pool_service_account.email
      preemptible        = false
      initial_node_count = 1
    },
    {
      name               = "node-pool-${var.region}-c"
      machine_type       = "n2-standard-2"
      node_locations     = "${var.region}-c"
      min_count          = 1
      max_count          = 3
      local_ssd_count    = 0
      spot               = false
      disk_size_gb       = 60
      disk_type          = "pd-standard"
      image_type         = "COS_CONTAINERD"
      enable_gcfs        = false
      enable_gvnic       = false
      auto_repair        = true
      auto_upgrade       = true
      service_account    = google_service_account.node_pool_service_account.email
      preemptible        = false
      initial_node_count = 1
    },
  ]

  node_pools_oauth_scopes = {
    all = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
    ]
  }

  node_pools_labels = {
    all = {}
    "node-pool-${var.region}-b" = {
      zone = "${var.region}-b"
    },
    "node-pool-${var.region}-c" = {
      zone = "${var.region}-c"
    }
  }

  node_pools_metadata = {
    all = {}

    "node-pool-${var.region}-b" = {
      zone = "${var.region}-b"
    },
    "node-pool-${var.region}-c" = {
      zone = "${var.region}-c"
    }
  }

  node_pools_taints = {
    all = []
  }

  node_pools_tags = {
    all = []
  }
}

Terraform Version

Terraform v1.2.9
on darwin_amd64
+ provider registry.terraform.io/hashicorp/google v4.33.0
+ provider registry.terraform.io/hashicorp/kubernetes v2.13.1
+ provider registry.terraform.io/hashicorp/random v3.4.2

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requestedtriagedScoped and ready for work

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions