-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
167 lines (132 loc) · 4.08 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
terraform {
required_version = ">= 0.12.7"
backend "gcs" {
bucket = "dish-co-devops"
prefix = "terraform/state"
}
}
locals {
cluster_region = var.region
cluster_location = "${var.cluster_location == null ? "${var.region}-a" : var.cluster_location}"
cluster_service_account_name = "gke-cluster"
cluster_service_account_description = "GKE cluster service account"
}
provider "google" {
version = "~> 2.9.0"
project = var.project
region = var.region
scopes = [
# Default scopes
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
"https://www.googleapis.com/auth/devstorage.full_control",
# Required for google_client_openid_userinfo
"https://www.googleapis.com/auth/userinfo.email",
]
}
provider "google-beta" {
version = "~> 2.9.0"
project = var.project
region = var.region
scopes = [
# Default scopes
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
"https://www.googleapis.com/auth/devstorage.full_control",
# Required for google_client_openid_userinfo
"https://www.googleapis.com/auth/userinfo.email",
]
}
provider "random" {
version = "~> 2.2"
}
provider "null" {
version = "~> 2.1"
}
provider "template" {
version = "~> 2.1"
}
module "gke" {
source = "./modules/gke-public-cluster"
cluster_name = var.cluster_name
project = var.project
location = local.cluster_location
region = var.region
cluster_service_account_name = "${var.cluster_name}-cluster-sa"
cluster_service_account_roles = ["roles/cloudsql.client"]
machine_type = var.cluster_machine_type
max_node_count = var.cluster_max_node_count
}
data "google_client_config" "client" {}
data "google_client_openid_userinfo" "terraform_user" {}
data "template_file" "dev-gke_host_endpoint" {
template = module.gke.cluster_endpoint
}
data "template_file" "access_token" {
template = data.google_client_config.client.access_token
}
data "template_file" "gke_cluster_ca_certificate" {
template = module.gke.cluster_ca_certificate
}
provider "kubernetes" {
version = "1.10.0"
load_config_file = "false"
host = data.template_file.dev-gke_host_endpoint.rendered
token = data.template_file.access_token.rendered
cluster_ca_certificate = data.template_file.gke_cluster_ca_certificate.rendered
}
resource "kubernetes_namespace" "envs" {
for_each = toset(var.environments)
metadata {
annotations = {
name = each.key
}
name = each.key
}
depends_on = [module.gke]
}
resource "random_password" "mysql" {
length = 16
special = true
}
module "mysql" {
source = "./modules/mysql-private-ip"
project = var.project
region = var.region
name_prefix = var.cluster_name
master_user_name = var.cluster_name
master_user_password = random_password.mysql.result
private_network = module.gke.cluster_network
}
resource "google_sql_database" "envs" {
for_each = toset(var.environments)
instance = module.mysql.master_instance_name
name = each.key
}
resource "random_password" "mysql_users" {
for_each = toset(var.environments)
length = 16
special = true
}
resource "google_sql_user" "users" {
for_each = toset(var.environments)
name = "notejam-${each.key}"
instance = module.mysql.master_instance_name
password = random_password.mysql_users[each.key].result
host = "%"
}
resource "kubernetes_secret" "mysql" {
for_each = toset(var.environments)
metadata {
name = "db-secret"
namespace = each.key
}
data = {
username = google_sql_user.users[each.key].name
password = google_sql_user.users[each.key].password
host = module.mysql.master_private_ip
}
depends_on = [kubernetes_namespace.envs, google_sql_user.users]
}