Use and create kubernetes providers dynamically for another module

Hi,

I want to create several k8s clusters in different regions in GCP, which goes essentially like this (https://github.com/terraform-google-modules/terraform-google-kubernetes-engine/blob/master/examples/simple_regional_with_kubeconfig/main.tf)

locals {
  cluster_type = "simple-regional"
}

data "google_client_config" "default" {}

provider "kubernetes" {
  host                   = "https://${module.gke.endpoint}"
  token                  = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(module.gke.ca_certificate)
}

module "gke" {
  source                 = "../../"
  project_id             = var.project_id
  name                   = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
  regional               = true
  region                 = var.region
  network                = var.network
  subnetwork             = var.subnetwork
  ip_range_pods          = var.ip_range_pods
  ip_range_services      = var.ip_range_services
  create_service_account = false
  service_account        = var.compute_engine_service_account
  skip_provisioners      = var.skip_provisioners
}

module "gke_auth" {
  source = "../../modules/auth"

  project_id   = var.project_id
  location     = module.gke.location
  cluster_name = module.gke.name
}

Now this works fine, also when I use a for_each loop to create several clusters. Now my next aim is to deploy via helm charts certain things into the clusters. For this, I wrote a module whose main.tf looks as follows:

resource "kubernetes_namespace" "example" {
  metadata {
    name = "my-first-namespace"
  }

  provider = kubernetes
}

and I call that module in the primary main.tf as follows:

module "helm_general" {
  source = "../../modules/helm_general"

  depends_on = [time_sleep.wait]
  for_each = module.gke

  providers {
    kubernetes = provider "kubernetes" {
      cluster_ca_certificate = each.value.ca_certificate
      host                   = each.value.host
      token                 = each.value.client_token
    }
  }
}

Yet, Terraform keeps telling me

│ Error: Missing newline after argument
│
│   on main.tf line 109, in module "helm_general":
│  109:     kubernetes = provider "kubernetes" {
│
│ An argument definition must end with a newline.

I already tried to bring in the credentials for each cluster as variables into the module and to setup the provider in the module, but Terraform kept telling me that providers cannot be instantiated in modules that are called with for_each.

Hence, my question is whether there is a way to do this dynamically as can deal with an unlimited number of k8s clusters and I do not want to write it down for each cluster explicitly in the main.tf file.

Many thanks for your input.