2018-08-20 23:15:47 +00:00
|
|
|
locals {
|
|
|
|
service_account_path = "${path.module}/service-account.yaml"
|
|
|
|
}
|
|
|
|
|
2018-08-20 22:26:37 +00:00
|
|
|
provider "google" {
|
2018-11-29 21:48:02 +00:00
|
|
|
project = "${var.project}"
|
|
|
|
region = "us-central1"
|
|
|
|
|
2018-10-02 21:14:57 +00:00
|
|
|
credentials = "${file("vault-helm-dev-creds.json")}"
|
2018-08-20 22:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_id" "suffix" {
|
|
|
|
byte_length = 4
|
|
|
|
}
|
|
|
|
|
2018-09-11 19:35:16 +00:00
|
|
|
data "google_container_engine_versions" "main" {
|
2019-07-31 18:26:12 +00:00
|
|
|
location = "${var.zone}"
|
|
|
|
version_prefix = "1.12."
|
2018-09-11 19:35:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 21:48:02 +00:00
|
|
|
data "google_service_account" "gcpapi" {
|
|
|
|
account_id = "${var.gcp_service_account}"
|
|
|
|
}
|
2018-10-29 15:36:23 +00:00
|
|
|
|
2019-07-31 18:26:12 +00:00
|
|
|
resource "google_kms_key_ring" "keyring" {
|
|
|
|
name = "vault-helm-unseal-kr"
|
|
|
|
location = "global"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_kms_crypto_key" "vault-helm-unseal-key" {
|
|
|
|
name = "vault-helm-unseal-key"
|
|
|
|
key_ring = "${google_kms_key_ring.keyring.self_link}"
|
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
prevent_destroy = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 22:26:37 +00:00
|
|
|
resource "google_container_cluster" "cluster" {
|
2018-10-02 21:14:57 +00:00
|
|
|
name = "vault-helm-dev-${random_id.suffix.dec}"
|
2018-08-20 22:26:37 +00:00
|
|
|
project = "${var.project}"
|
|
|
|
enable_legacy_abac = true
|
2018-10-04 20:07:41 +00:00
|
|
|
initial_node_count = 3
|
2018-08-20 22:26:37 +00:00
|
|
|
zone = "${var.zone}"
|
2018-09-11 19:35:16 +00:00
|
|
|
min_master_version = "${data.google_container_engine_versions.main.latest_master_version}"
|
|
|
|
node_version = "${data.google_container_engine_versions.main.latest_node_version}"
|
2018-11-29 21:48:02 +00:00
|
|
|
|
|
|
|
node_config {
|
|
|
|
#service account for nodes to use
|
|
|
|
oauth_scopes = [
|
|
|
|
"https://www.googleapis.com/auth/cloud-platform",
|
|
|
|
"https://www.googleapis.com/auth/compute",
|
|
|
|
"https://www.googleapis.com/auth/devstorage.read_write",
|
|
|
|
"https://www.googleapis.com/auth/logging.write",
|
|
|
|
"https://www.googleapis.com/auth/monitoring",
|
|
|
|
"https://www.googleapis.com/auth/service.management.readonly",
|
|
|
|
"https://www.googleapis.com/auth/servicecontrol",
|
|
|
|
"https://www.googleapis.com/auth/trace.append",
|
|
|
|
]
|
|
|
|
|
|
|
|
service_account = "${data.google_service_account.gcpapi.email}"
|
|
|
|
}
|
2018-08-20 22:26:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 23:15:47 +00:00
|
|
|
resource "null_resource" "kubectl" {
|
|
|
|
count = "${var.init_cli ? 1 : 0 }"
|
|
|
|
|
2019-07-31 18:26:12 +00:00
|
|
|
triggers = {
|
2018-08-20 23:15:47 +00:00
|
|
|
cluster = "${google_container_cluster.cluster.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# On creation, we want to setup the kubectl credentials. The easiest way
|
|
|
|
# to do this is to shell out to gcloud.
|
|
|
|
provisioner "local-exec" {
|
|
|
|
command = "gcloud container clusters get-credentials --zone=${var.zone} ${google_container_cluster.cluster.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# On destroy we want to try to clean up the kubectl credentials. This
|
|
|
|
# might fail if the credentials are already cleaned up or something so we
|
|
|
|
# want this to continue on failure. Generally, this works just fine since
|
|
|
|
# it only operates on local data.
|
|
|
|
provisioner "local-exec" {
|
|
|
|
when = "destroy"
|
|
|
|
on_failure = "continue"
|
|
|
|
command = "kubectl config get-clusters | grep ${google_container_cluster.cluster.name} | xargs -n1 kubectl config delete-cluster"
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioner "local-exec" {
|
|
|
|
when = "destroy"
|
|
|
|
on_failure = "continue"
|
|
|
|
command = "kubectl config get-contexts | grep ${google_container_cluster.cluster.name} | xargs -n1 kubectl config delete-context"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "null_resource" "helm" {
|
2018-10-02 21:14:57 +00:00
|
|
|
count = "${var.init_cli ? 1 : 0 }"
|
2018-08-20 23:17:46 +00:00
|
|
|
depends_on = ["null_resource.kubectl"]
|
2018-08-20 23:15:47 +00:00
|
|
|
|
2019-07-31 18:26:12 +00:00
|
|
|
triggers = {
|
2018-08-20 23:15:47 +00:00
|
|
|
cluster = "${google_container_cluster.cluster.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioner "local-exec" {
|
|
|
|
command = <<EOF
|
|
|
|
kubectl apply -f '${local.service_account_path}'
|
|
|
|
helm init --service-account helm
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
}
|