-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
100 lines (83 loc) · 2.99 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Definition of local variables
locals {
base_apis = [
"container.googleapis.com",
"monitoring.googleapis.com",
"cloudtrace.googleapis.com",
"cloudprofiler.googleapis.com"
]
memorystore_apis = ["redis.googleapis.com"]
cluster_name = google_container_cluster.my_cluster.name
}
# Enable Google Cloud APIs
module "enable_google_apis" {
source = "terraform-google-modules/project-factory/google//modules/project_services"
version = "~> 14.0"
project_id = var.gcp_project_id
disable_services_on_destroy = false
# activate_apis is the set of base_apis and the APIs required by user-configured deployment options
activate_apis = concat(local.base_apis, var.memorystore ? local.memorystore_apis : [])
}
# Create GKE cluster
resource "google_container_cluster" "my_cluster" {
name = var.name
location = var.region
# Enabling autopilot for this cluster
enable_autopilot = true
# Setting an empty ip_allocation_policy to allow autopilot cluster to spin up correctly
ip_allocation_policy {
}
# Avoid setting deletion_protection to false
# until you're ready (and certain you want) to destroy the cluster.
# deletion_protection = false
depends_on = [
module.enable_google_apis
]
}
# Get credentials for cluster
module "gcloud" {
source = "terraform-google-modules/gcloud/google"
version = "~> 3.0"
platform = "linux"
additional_components = ["kubectl", "beta"]
create_cmd_entrypoint = "gcloud"
# Module does not support explicit dependency
# Enforce implicit dependency through use of local variable
create_cmd_body = "container clusters get-credentials ${local.cluster_name} --zone=${var.region} --project=${var.gcp_project_id}"
}
# Apply YAML kubernetes-manifest configurations
resource "null_resource" "apply_deployment" {
provisioner "local-exec" {
interpreter = ["bash", "-exc"]
command = "kubectl apply -k ${var.filepath_manifest} -n ${var.namespace}"
}
depends_on = [
module.gcloud
]
}
# Wait condition for all Pods to be ready before finishing
resource "null_resource" "wait_conditions" {
provisioner "local-exec" {
interpreter = ["bash", "-exc"]
command = <<-EOT
kubectl wait --for=condition=AVAILABLE apiservice/v1beta1.metrics.k8s.io --timeout=180s
kubectl wait --for=condition=ready pods --all -n ${var.namespace} --timeout=280s
EOT
}
depends_on = [
resource.null_resource.apply_deployment
]
}