kubernetes - Terraform:模块输出未被识别为变量

标签 kubernetes terraform devops terraform-provider-gcp

我想只是一个快速的理智检查,也许我的眼睛变得困惑了。我正在将一个单一的 terraform 文件分解为模块。

我的 main.tf只调用两个模块,gke对于 google kubernetes 引擎和 storage这会在之前创建的集群上创建一个持久卷。

模块 gke有一个 outputs.tf输出以下内容:

output "client_certificate" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.client_certificate}"
 sensitive = true
}
output "client_key" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.client_key}"
 sensitive = true
}
output "cluster_ca_certificate" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.cluster_ca_certificate}"
 sensitive = true
}
output "host" {
  value     = "${google_container_cluster.kube-cluster.endpoint}"
 sensitive = true
}

然后在main.tf对于存储模块,我有:
client_certificate     = "${base64decode(var.client_certificate)}"
client_key             = "${base64decode(var.client_key)}"
cluster_ca_certificate = "${base64decode(var.cluster_ca_certificate)}"
host     = "${var.host}"

然后在根目录 main.tf我有以下内容:
client_certificate = "${module.gke.client_certificate}"
client_key = "${module.gke.client_key}"
cluster_ca_certificate = "${module.gke.cluster_ca_certificate}"
host = "${module.gke.host}"

从我所见,它看起来是正确的。证书、 key 和主机变量的值应该从 gke 输出。模块 outputs.tf , 由 main.tf 提取的根,然后交付给 storage作为常规变量。

我是不是弄错了?或者我只是发疯了,似乎有些不对劲。

当我运行一个计划时,我被问到变量没有被填充。

编辑:

添加一些附加信息,包括我的代码。

如果我为它要求的变量手动添加虚拟条目,则会收到以下错误:
Macbook: $ terraform plan
var.client_certificate
  Enter a value: 1

var.client_key
  Enter a value: 2

var.cluster_ca_certificate
  Enter a value: 3

var.host
  Enter a value: 4
...
(filtered out usual text)
...
 * module.storage.data.google_container_cluster.kube-cluster: 1 error(s) occurred:

* module.storage.data.google_container_cluster.kube-cluster: data.google_container_cluster.kube-cluster: project: required field is not set

看起来它在提示 data.google_container_cluster 资源需要项目属性。但它不是一个有效的资源。它是给提供者的,但它是为提供者填写的。

下面的代码:

文件夹结构:
root-folder/
├── gke/
│   ├── main.tf
│   ├── outputs.tf
│   ├── variables.tf
├── storage/
│   ├── main.tf
│   └── variables.tf
├── main.tf
├── staging.json
├── terraform.tfvars
└── variables.tf

根文件夹/gke/main.tf:
provider "google" {
  credentials = "${file("staging.json")}"
  project     = "${var.project}"
  region      = "${var.region}"
  zone        = "${var.zone}"
}

resource "google_container_cluster" "kube-cluster" {
  name               = "kube-cluster"
  description        = "kube-cluster"
  zone               = "europe-west2-a"
  initial_node_count = "2"
  enable_kubernetes_alpha = "false"
  enable_legacy_abac = "true"

  master_auth {
    username = "${var.username}"
    password = "${var.password}"
  }

  node_config {
    machine_type = "n1-standard-2"
    disk_size_gb = "20"
    oauth_scopes = [
      "https://www.googleapis.com/auth/compute",
      "https://www.googleapis.com/auth/devstorage.read_only",
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring"
    ]
  }
}

根文件夹/gke/outputs.tf:
output "client_certificate" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.client_certificate}"
 sensitive = true
}
output "client_key" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.client_key}"
 sensitive = true
}
output "cluster_ca_certificate" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.cluster_ca_certificate}"
 sensitive = true
}
output "host" {
  value     = "${google_container_cluster.kube-cluster.endpoint}"
 sensitive = true
}

根文件夹/gke/variables.tf:
variable "region" {
  description = "GCP region, e.g. europe-west2"
  default = "europe-west2"
}
variable "zone" {
  description = "GCP zone, e.g. europe-west2-a (which must be in gcp_region)"
  default = "europe-west2-a"
}
variable "project" {
  description = "GCP project name"
}
variable "username" {
  description = "Default admin username"
}
variable "password" {
  description = "Default admin password"
}

/根文件夹/storage/main.cf:
provider "kubernetes" {
  host     = "${var.host}"
  username = "${var.username}"
  password = "${var.password}"
  client_certificate     = "${base64decode(var.client_certificate)}"
  client_key             = "${base64decode(var.client_key)}"
  cluster_ca_certificate = "${base64decode(var.cluster_ca_certificate)}"
}
data "google_container_cluster" "kube-cluster" {
  name   = "${var.cluster_name}"
  zone   = "${var.zone}"
}
resource "kubernetes_storage_class" "kube-storage-class" {
  metadata {
    name = "kube-storage-class"
  }
  storage_provisioner = "kubernetes.io/gce-pd"
  parameters {
    type = "pd-standard"
  }
}
resource "kubernetes_persistent_volume_claim" "kube-claim" {
  metadata {
    name      = "kube-claim"
  }
  spec {
    access_modes       = ["ReadWriteOnce"]
    storage_class_name = "kube-storage-class"
    resources {
      requests {
        storage = "10Gi"
      }
    }
  }
}

/root/storage/variables.tf:
variable "username" {
  description = "Default admin username."
}
variable "password" {
  description = "Default admin password."
}
variable "client_certificate" {
  description = "Client certificate, output from the GKE/Provider module."
}
variable "client_key" {
  description = "Client key, output from the GKE/Provider module."
}
variable "cluster_ca_certificate" {
  description = "Cluster CA Certificate, output from the GKE/Provider module."
}
variable "cluster_name" {
  description = "Cluster name."
}
variable "zone" {
  description = "GCP Zone"
}
variable "host" {
  description = "Host endpoint, output from the GKE/Provider module."
}

/根文件夹/main.tf:
module "gke" {
  source = "./gke"
  project = "${var.project}"
  region = "${var.region}"
  username = "${var.username}"
  password = "${var.password}"
}
module "storage" {
  source = "./storage"
  host = "${module.gke.host}"
  username = "${var.username}"
  password = "${var.password}"
  client_certificate = "${module.gke.client_certificate}"
  client_key = "${module.gke.client_key}"
  cluster_ca_certificate = "${module.gke.cluster_ca_certificate}"
  cluster_name = "${var.cluster_name}"
  zone = "${var.zone}"
}

/根文件夹/variables.tf:
variable "project" {}
variable "region" {}
variable "username" {}
variable "password" {}
variable "gc_disk_size" {}
variable "kpv_vol_size" {}
variable "host" {}
variable "client_certificate" {}
variable "client_key" {}
variable "cluster_ca_certificate" {}
variable "cluster_name" {}
variable "zone" {}

我不会粘贴我的 staging.json 的内容和 terraform.tfvars出于显而易见的原因:)

最佳答案

在您的 /root-folder/variables.tf ,删除以下条目:

variable "host" {}
variable "client_certificate" {}
variable "client_key" {}
variable "cluster_ca_certificate" {}

这些本身并不是根级别的 Terraform 代码需要的变量。相反,它们被作为 1 个模块的输出 --> 输入传递给第 2 个模块。

关于kubernetes - Terraform:模块输出未被识别为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53502041/

相关文章:

kubernetes - 如何为 serviceaccount 创建 kubectl 配置文件

kubernetes - 从在 Kubernetes 的同一命名空间中运行的进程中删除 ClusterRoleBinding 和命名空间

azure - 由于区分大小写,导入资源后在 Terraform Plan 阶段观察到资源 ID 强制替换(Azurerm 模块、Azure 云)

ruby-on-rails - 无法使用NodePort服务连接到Kubernetes中的Redis Pod

jenkins - groovy.lang.MissingPropertyException : No such property: any for class: WorkflowScript

docker - 在kubernetes部署中将configMap作为参数传递

kubernetes - Kubernetes 命名空间和 Linux 命名空间之间的区别?

ssl - setupletsencrypt ClusterIssuer with terraform

amazon-web-services - 了解lifecycle_rules和ignore_changes

azure - 如何从 Azure DevOps 中的另一个管道触发一个管道阶段?