使用 Alias 时 Terraform 无法加载凭据

问题描述 投票:0回答:1

这是我的项目结构

modules
|-gke_cluster
  |-main.tf
  |-output.tf
  |-variables.tf
main.tf
production.tf
variable.tf

main.tf(根)

terraform {

cloud {
    organization = "myorder"

    workspaces {
      name = "PT-cli-2"  
    }
  }
}
     

生产.tf

provider "google" {
  credentials = var.gcp_credentials
  project     = "xxxxx-develop"
  region      = "asia-southeast1"
  alias = "gke3"
}

# google_container_cluster.gke_cluster.endpoint
data "google_client_config" "default" {}

provider "kubernetes" {
  host                   = "https://${module.gke_cluster.cluster_endpoint}"
  token                  = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(module.gke_cluster.cluster_ca_certificate)
    # load_config_file = false
    ignore_annotations = [
    "^autopilot\\.gke\\.io\\/.*",
    "^cloud\\.google\\.com\\/.*"
  ]
}

module "gke_cluster" {
  source = "./modules/gke_cluster"
  region       = var.region
  clusterName  = var.clusterName
  diskSize     = var.diskSize
  minNode      = var.minNode
  maxNode      = var.maxNode
  machineType  = var.machineType
  providers = {
    google = google.gke3
  }
}
# data "google_client_config" "default" {}
module "busybox" {
  source       = "./modules/busybox"
  clusterName  = var.clusterName
  cluster_name = module.gke_cluster.clusterName # Assuming your GKE module outputs this
}

模块/gke_cluster/main.tf

terraform {
  required_providers {
  google = {
    source  = "hashicorp/google"
    version = ">=5.26.0"
  }
}

}

resource "google_container_cluster" "gke1_gke_cluster" {
  name     = var.clusterName
  location = var.region # Replace this with your desired region
  enable_shielded_nodes    = "true"
  remove_default_node_pool = true
  deletion_protection = false
  initial_node_count  = 1

  release_channel {
    channel = "STABLE"
  }

  addons_config {
    http_load_balancing {
      disabled = false
    }
  }

  networking_mode = "VPC_NATIVE"
  ip_allocation_policy {
    cluster_ipv4_cidr_block  = "/16"
    services_ipv4_cidr_block = "/22"
  }

  timeouts {
    create = "20m"
    update = "20m"
  }

  lifecycle {
    ignore_changes = [node_pool]
  }
}

resource "google_container_node_pool" "gke1_primary_nodes" {
  name       = "${var.clusterName}-pool"
  location   = var.region # Replace this with your desired region
  cluster    = google_container_cluster.gke1_gke_cluster.name
  node_count = 1

  management {
    auto_repair  = true
    auto_upgrade = true
  }

  autoscaling {
    min_node_count = var.minNode
    max_node_count = var.maxNode
  }

  timeouts {
    create = "20m"
    update = "20m"
  }

  node_config {
    preemptible  = true
    machine_type = var.machineType

    oauth_scopes = [
      "https://www.googleapis.com/auth/compute",
      "https://www.googleapis.com/auth/cloud-platform",
      "https://www.googleapis.com/auth/devstorage.read_only",
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
    ]
  }
}

我的错误

╷
│ Error: Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block.  No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'
│ 
│   with provider["registry.terraform.io/hashicorp/google"],
│   on <empty> line 0:
│   (source code not available)
│ 
│ google: could not find default credentials. See
│ https://cloud.google.com/docs/authentication/external/set-up-adc for more
│ information
╵
╷
│ Error: Invalid provider configuration
│ 
│ Provider "registry.terraform.io/hashicorp/google" requires explicit
│ configuration. Add a provider block to the root module and configure the
│ provider's required arguments as described in the provider documentation.

当我尝试在模块中使用别名时,我的 terraform 代码不起作用

当我删除时,我的 terraform 代码可以工作

  • 来自提供商和模块的别名

这里有什么我错过的吗?

仅供参考。我的凭证文件存储在 Terraform 工作区变量中

google-cloud-platform terraform google-kubernetes-engine terraform-provider-gcp
1个回答
0
投票

终于找到解决办法了

# data "google_client_config" "default" {}

上面的这一行只是覆盖了我的提供商部分

© www.soinside.com 2019 - 2024. All rights reserved.