forked from GoogleCloudPlatform/microservices-demo
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
93 lines (77 loc) · 2.75 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
# 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 {
}
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 = "kubectl wait --for=condition=ready pods --all -n ${var.namespace} --timeout=-1s 2> /dev/null"
}
depends_on = [
resource.null_resource.apply_deployment
]
}