Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

given key is not an element when using latest private-cluter #419

Closed
endorama opened this issue Feb 5, 2020 · 4 comments · Fixed by #422
Closed

given key is not an element when using latest private-cluter #419

endorama opened this issue Feb 5, 2020 · 4 comments · Fixed by #422

Comments

@endorama
Copy link
Contributor

endorama commented Feb 5, 2020

I'm tring to setup a private cluster, using the private-cluster module.

Unfortunately an error comes up and I've not been able to find it's root cause.

I'm using this module at version 7.0.0

The error is:

Error: Invalid index

  on .terraform/modules/cluster/terraform-google-modules-terraform-google-kubernetes-engine-7be707a/modules/private-cluster/main.tf line 100, in locals:
 100:   cluster_endpoint = var.enable_private_nodes ? (var.deploy_using_private_endpoint ? google_container_cluster.primary.private_cluster_config.0.private_endpoint : google_container_cluster.primary.private_cluster_config.0.public_endpoint) : google_container_cluster.primary.endpoint
    |----------------
    | google_container_cluster.primary.private_cluster_config is empty list of object

The given key does not identify an element in this collection value.

My usage of the module is:

module "cluster" {
  # source  = "terraform-google-modules/kubernetes-engine/google"
  source  = "terraform-google-modules/kubernetes-engine/google//modules/beta-private-cluster"
  version = "~> 7.0"

  project_id         = local.google_project
  name               = local.cluster_name
  kubernetes_version = "1.15.8-gke.2"
  regional           = true
  region             = var.cluster_location
  zones              = var.cluster_zones

  remove_default_node_pool = true
  maintenance_start_time   = "04:00"
  http_load_balancing      = false
  grant_registry_access    = true

  enable_private_nodes          = true
  enable_private_endpoint       = false
  deploy_using_private_endpoint = false

  network    = module.network.network_name
  subnetwork = "${local.cluster_name}-subnet-${var.cluster_location}"

  ip_range_pods     = "${local.cluster_name}-subnet-${var.cluster_location}-pods"
  ip_range_services = "${local.cluster_name}-subnet-${var.cluster_location}-services"

  master_authorized_networks = [
    { cidr_block = "[REDACTED]", display_name = "[REDACTED]" }
  ]
}

Any hint on how to fix it or where to look for further debug? Thanks!

@morgante
Copy link
Contributor

morgante commented Feb 5, 2020

Thanks for the report. It looks like the error is on this line.

Could you try this fix (and regenerate the modules)?

-  cluster_endpoint = var.enable_private_nodes ? (var.deploy_using_private_endpoint ? google_container_cluster.primary.private_cluster_config.0.private_endpoint : google_container_cluster.primary.private_cluster_config.0.public_endpoint) : google_container_cluster.primary.endpoint
+  cluster_endpoint = (var.enable_private_nodes && length(google_container_cluster.primary.private_cluster_config) > 0) ? (var.deploy_using_private_endpoint ? google_container_cluster.primary.private_cluster_config.0.private_endpoint : google_container_cluster.primary.private_cluster_config.0.public_endpoint) : google_container_cluster.primary.endpoint

@endorama
Copy link
Contributor Author

endorama commented Feb 6, 2020

@morgante it works! Both plan and apply worked as expected.

endorama added a commit to endorama/terraform-google-kubernetes-engine that referenced this issue Feb 6, 2020
As google_container_cluster.primary.private_cluster_config may be empty,
we check its length before trying to access it.

When creating a new private cluster, a "Invalid index" error was showing
up during the planning.

By checking for variable length before trying to access it the plan (and
apply) works as expected.

Issue: [terraform-google-modules#419]

Co-authored-by: Morgante Pell <morgantep@google.com>
@endorama
Copy link
Contributor Author

endorama commented Feb 6, 2020

I'd like to understand in what case google_container_cluster.primary.private_cluster_config may be empty. 🤔

I was expecting that block to be set when enable_private_nodes is true, but that does not seem the case? (I'm still new to the dynamic Terraform syntax)

@morgante
Copy link
Contributor

morgante commented Feb 6, 2020

Truth be told, I'm not quite sure how/when it ends up being empty either. I'm guessing it has something to do with changing the value of enable_private_nodes to true.

In either case, I think it's good to have this failsafe.

morgante added a commit that referenced this issue Feb 6, 2020
* fix Invalid index error when creating a private cluster

As google_container_cluster.primary.private_cluster_config may be empty,
we check its length before trying to access it.

When creating a new private cluster, a "Invalid index" error was showing
up during the planning.

By checking for variable length before trying to access it the plan (and
apply) works as expected.

Issue: [#419]

Co-authored-by: Morgante Pell <morgantep@google.com>

* perform generation

Co-authored-by: Morgante Pell <morgante.pell@morgante.net>
CPL-markus pushed a commit to WALTER-GROUP/terraform-google-kubernetes-engine that referenced this issue Jul 15, 2024
…m-google-modules#422)

* fix Invalid index error when creating a private cluster

As google_container_cluster.primary.private_cluster_config may be empty,
we check its length before trying to access it.

When creating a new private cluster, a "Invalid index" error was showing
up during the planning.

By checking for variable length before trying to access it the plan (and
apply) works as expected.

Issue: [terraform-google-modules#419]

Co-authored-by: Morgante Pell <morgantep@google.com>

* perform generation

Co-authored-by: Morgante Pell <morgante.pell@morgante.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants