1

I'm new to Terraform, I need suggestion about handling one case where I need to use data from counted data-source to un-counted provider.

I need to fetch the gke-cluster context and pass it to provider helm/kubernetes to provision resources on it . There can be multiple gke -clusters, so I'm using count on the data-source google_container_cluster and then I was trying to get the endpoint from it . Like below :

data "google_container_cluster" "my_cluster" {
    count      = var.enabled ? length(var.gke_clusters) : 0
    location   = lookup(var.gke_clusters[count.index], "cluster_location")
    name   = lookup(var.gke_clusters[count.index], "cluster_name")
}
provider "helm" {
  kubernetes {    
    host  = "https://${data.google_container_cluster.my_cluster.endpoint}"
    token = data.google_client_config.provider.access_token
    cluster_ca_certificate = base64decode(
    data.google_container_cluster.my_cluster.master_auth[0].cluster_ca_certificate,
    )    
  }
}

But this does not work because when I'm referring to my_cluster I have to specify the index , and if I specify the index my_cluster[count.index], I get an error that Error: Reference to "count" in non-counted context.

Thanks in advance.

Learner
  • 111
  • 2

1 Answers1

1

If I have read this correctly you are trying to target different clusters with your provider so you can then use a helm_release.

You need to have distinct providers for each cluster and differentiate them with an alias so.

provider "helm" {
  kubernetes {    
    host  = "https://my_endpoint1"
    token = "provider.access_token1"
    cluster_ca_certificate = "base64encodedcerthere1"
  }
}

provider "helm" { alias = "cluster2" kubernetes {
host = "https://my_endpoint2" token = "provider.access_token2" cluster_ca_certificate = "base64encodedcerthere2" } }

the first provider will be used where no alias is provided and the second used only where an alias is used for the provider so for example:

resource "helm_release" "example" {
  name       = "my-bob-release"
  ...
}

the above uses the default un-named provider

resource "helm_release" "example" {
  provider   = helm.cluster2
  name       = "my-bob-release"
  ...
}