-
Notifications
You must be signed in to change notification settings - Fork 2
/
gke.tf
253 lines (202 loc) · 7.98 KB
/
gke.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
variable "gke_username" {
default = ""
description = "gke username"
}
variable "gke_password" {
default = ""
description = "gke password"
}
variable "gke_num_nodes" {
default = 1
description = "number of gke nodes"
}
variable "auth0_token" {}
variable "aws_root_access_key" {}
variable "aws_root_secret_key" {}
######################################################## aws part ########################################################
provider "aws" {
version = "~> 2.0"
region = "us-east-1"
access_key = var.aws_root_access_key
secret_key = var.aws_root_secret_key
}
module "bucket_access"{
bucket_name = "smartshare_user_dev"
source="./s3"
}
##########################################################################################################################
# GKE cluster
resource "google_container_cluster" "primary" {
name = "${var.project_id}-gke"
location = var.region
remove_default_node_pool = true
initial_node_count = 1
network = google_compute_network.vpc.name
subnetwork = google_compute_subnetwork.subnet.name
master_auth {
username = var.gke_username
password = var.gke_password
client_certificate_config {
issue_client_certificate = false
}
}
}
# Separately Managed Node Pool
resource "google_container_node_pool" "primary_nodes" {
name = "${google_container_cluster.primary.name}-node-pool"
location = var.region
cluster = google_container_cluster.primary.name
node_count = var.gke_num_nodes
node_config {
oauth_scopes = [
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
]
labels = {
env = var.project_id
}
# preemptible = true
machine_type = "n1-standard-1"
tags = ["gke-node", "${var.project_id}-gke"]
metadata = {
disable-legacy-endpoints = "true"
}
}
provisioner "local-exec" {
command = "gcloud container clusters get-credentials ${google_container_cluster.primary.name} --region ${var.region};echo $?"
}
}
resource "google_compute_address" "frontend" {
name = "frontend-ip"
depends_on = [google_container_node_pool.primary_nodes]
}
output "frontend-ip" {
value = google_compute_address.frontend.address
}
resource "google_compute_address" "gateway" {
name = "gateway-ip"
depends_on = [google_container_node_pool.primary_nodes]
}
output "gateway-ip" {
value = google_compute_address.gateway.address
}
output "kubernetes_cluster_name" {
value = google_container_cluster.primary.name
description = "GKE Cluster Name"
}
resource "null_resource" "passing_aws_secret" {
provisioner "local-exec" {
command = "kubectl create secret generic aws-credentials --from-literal=aws_access_key=${module.bucket_access.access_key} --from-literal=aws_secret_key=${module.bucket_access.secret} --from-literal=aws_region='us-east-1'"
}
depends_on = [google_container_node_pool.primary_nodes]
}
resource "null_resource" "modifying_kubectl_services_yamls" {
provisioner "local-exec" {
working_dir = "${path.module}/k8s"
command = "yq w -i frontend-service.yaml 'spec.loadBalancerIP' ${google_compute_address.frontend.address} --style=double;yq w -i gateway-service.yaml 'spec.loadBalancerIP' ${google_compute_address.gateway.address} --style=double"
}
depends_on = [null_resource.passing_aws_secret]
}
resource "null_resource" "modifying_kubectl_deployment_yamls" {
provisioner "local-exec" {
working_dir = "${path.module}/k8s"
command = <<EOT
yq w -i frontend-deployment.yaml 'spec.template.spec.containers[0].env[0].value' http://${google_compute_address.gateway.address}:8081 --style=double
yq w -i frontend-deployment.yaml 'spec.template.spec.containers[0].env[1].value' http://${google_compute_address.frontend.address}:4200 --style=double
yq w -i gateway-deployment.yaml 'spec.template.spec.containers[0].env[0].value' http://${google_compute_address.frontend.address}:4200 --style=double
EOT
}
depends_on = [null_resource.modifying_kubectl_services_yamls]
}
resource "null_resource" "execute_kubectl_commands" {
provisioner "local-exec" {
working_dir = "${path.module}/k8s"
command= <<EOT
kubectl apply -f kafka.yaml;kubectl apply -f postgres.yaml;kubectl apply -f redis.yaml;kubectl apply -f lock-deployment.yaml;kubectl apply -f lock-service.yaml;kubectl apply -f admin-deployment.yaml
kubectl apply -f admin-service.yaml;kubectl apply -f core-deployment.yaml;kubectl apply -f core-service.yaml
kubectl apply -f gateway-service.yaml;kubectl apply -f frontend-service.yaml
kubectl apply -f gateway-deployment.yaml;kubectl apply -f frontend-deployment.yaml
kubectl apply -f prometheus-deployment.yml
kubectl apply -f prometheus-service.yml
kubectl apply -f grafana-deployment.yml
kubectl apply -f grafana-service.yml
EOT
}
depends_on = [null_resource.modifying_kubectl_deployment_yamls]
}
resource "null_resource" "configure_auth0" {
provisioner "local-exec" {
working_dir = "."
command = <<EOT
callb1="http://${google_compute_address.frontend.address}:4200/signin-callback"
callb2="http://${google_compute_address.frontend.address}:4200/assets/silent-callback.html"
origins="http://${google_compute_address.frontend.address}:4200/assets/silent-callback.html"
allow="http://${google_compute_address.frontend.address}:4200/signout-callback"
jq --arg c1 $callb1 --arg c2 $callb2 --arg o $origins --arg a $allow '.callbacks[2]=$c1|.callbacks[3]=$c2|.allowed_origins[1]=$o| .web_origins[1]=$o| .allowed_logout_urls[1]=$a' auth.json >auth1.json
curl -H 'Authorization: Bearer ${var.auth0_token}' -X PATCH -H 'Content-Type: application/json' -d @auth1.json https://smartshare.eu.auth0.com/api/v2/clients/KLGG9PHgbxBwiHJinbFByrLdbYg1Gll5
EOT
}
depends_on = [null_resource.execute_kubectl_commands]
}
########################################### prometheus & grafana #############################
# provider "helm" {
# kubernetes {
# host = google_container_cluster.primary.endpoint
# client_certificate = base64decode(google_container_cluster.primary.master_auth.0.client_certificate)
# client_key = base64decode(google_container_cluster.primary.master_auth.0.client_key)
# cluster_ca_certificate = base64decode(google_container_cluster.primary.master_auth.0.cluster_ca_certificate)
# }
# }
# resource "helm_release" "prometheus_smarshare" {
# name = "prometheus"
# repository = "https://kubernetes-charts.storage.googleapis.com"
# chart = "stable/prometheus"
# values = [file("prometheus.yml")]
# set {
# name = "rbac.create"
# value = "false"
# }
# set {
# name = "server.service.type"
# value = "LoadBalancer"
# }
# depends_on = [null_resource.execute_kubectl_commands]
# }
# resource "helm_release" "grafana" {
# name = "grafana"
# repository = "https://charts.cloudposse.com/incubator/"
# chart = "stable/grafana"
# set {
# name = "rbac.create"
# value = "false"
# }
# set {
# name = "server.service.type"
# value = "LoadBalancer"
# }
# depends_on = [helm_release.prometheus]
# }
# resource "null_resource" "prometheus" {
# provisioner "local-exec" {
# working_dir = "."
# command = <<EOT
# helm repo add stable https://kubernetes-charts.storage.googleapis.com/
# helm install prometheus stable/prometheus --set rbac.create=false --set server.service.type=LoadBalancer -f prometheus.yml
# EOT
# }
# depends_on = [null_resource.execute_kubectl_commands]
# }
# resource "null_resource" "grafana" {
# provisioner "local-exec" {
# working_dir = "."
# command = "helm install grafana stable/grafana --set rbac.create=false --set server.service.type=LoadBalancer"
# }
# depends_on = [null_resource.prometheus]
# }
# module "release-prometheus-operator" {
# source = "OpenQAI/release-prometheus-operator/helm"
# version = "0.0.3"
# grafana_service_type = "LoadBalancer"
# prometheus_service_type="LoadBalancer"
# grafana_adminPassword="admin"
# }