-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.tf
104 lines (90 loc) · 2.92 KB
/
cluster.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
provider "google" {
}
resource "random_string" "random_prefix" {
length = 4
special = false
lower = true
upper = false
numeric = false
}
resource "google_project" "my_project" {
name = "${var.project_name}-${random_string.random_prefix.result}"
project_id = "${var.project_id}-${random_string.random_prefix.result}"
labels = var.labels
org_id = var.org_id
billing_account = var.billing_account
}
resource "google_project_service" "container" {
project = google_project.my_project.project_id
service = "container.googleapis.com"
}
resource "google_container_cluster" "artem-tf-cluster" {
count = var.num_clusters
project = google_project.my_project.project_id
name = "${var.k8s_cluster_name}-${count.index}-${random_string.random_prefix.result}"
location = var.location
remove_default_node_pool = true
initial_node_count = 1
master_auth {
client_certificate_config {
issue_client_certificate = false
}
}
depends_on = [ google_project_service.container ]
}
resource "google_container_node_pool" "primary_notes_pool" {
count = var.num_clusters
name = var.node_pool_name
location = var.location
cluster = google_container_cluster.artem-tf-cluster[count.index].name
project = google_project.my_project.project_id
autoscaling {
# min_node_count = 1
# max_node_count = 1
total_max_node_count = 9
total_min_node_count = 3
}
node_config {
preemptible = true
machine_type = var.machine_type
}
depends_on = [google_project_service.container]
}
data "google_project" "dns-tsb-sandbox" {
project_id = var.dns_project_id
}
data "google_dns_managed_zone" "gcp_sandbox" {
project = data.google_project.dns-tsb-sandbox.project_id
name = "gcp-sandbox-tetrate-io"
}
resource "google_dns_record_set" "dns" {
project = data.google_project.dns-tsb-sandbox.project_id
name = "artem.${data.google_dns_managed_zone.gcp_sandbox.dns_name}"
type = "A"
ttl = 300
managed_zone = data.google_dns_managed_zone.gcp_sandbox.name
rrdatas = [google_container_cluster.artem-tf-cluster[0].endpoint]
depends_on = [ google_container_cluster.artem-tf-cluster[0] ]
}
resource "local_file" "creds" {
filename = "creds.sh"
content = <<-EOT
gcloud config set project ${google_project.my_project.project_id}
%{ for idx, cluster in google_container_cluster.artem-tf-cluster.*.name ~}
gcloud container clusters get-credentials --region ${var.location} ${cluster}
kubectx ${cluster}=gke_${google_project.my_project.project_id}_${var.location}_${cluster}
%{ endfor ~}
EOT
}
resource "null_resource" "add_clusters_to_kubectl" {
depends_on = [ local_file.creds ]
provisioner "local-exec" {
command = "chmod +x creds.sh && bash creds.sh"
}
}
output "Project_ID" {
value = google_project.my_project.project_id
}
output "Clusters" {
value = google_container_cluster.artem-tf-cluster[*].name
}