-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
144 lines (116 loc) · 3.26 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
provider "google" {
project = var.project
region = var.region
version = "~> 2.20.0"
}
resource "google_compute_instance" "default" {
count = var.cluster_size
name = "${var.server_prefix}-${count.index}"
machine_type = var.machine_type
zone = "${var.region}-${var.zones[count.index]}"
can_ip_forward = true
tags = var.tags
boot_disk {
device_name = "boot"
initialize_params {
image = var.image
size = var.boot_disk_size
type = var.boot_disk_type
}
}
attached_disk {
device_name = "gluster"
source = element(
formatlist("%v", google_compute_disk.default.*.name),
count.index,
)
}
network_interface {
subnetwork = var.subnetwork
network_ip = cidrhost(var.subnet_mask, -count.index - var.ip_offset) # This takes last 3 IPs of the subnet (they are usually free - if not ip_offset can be uset to shift ips)
access_config {
// Ephemeral IP
}
}
metadata = {
VmDnsSetting = var.vm_dns_setting
}
metadata_startup_script = element(
formatlist("%v", data.template_file.provision_script.*.rendered),
count.index,
)
service_account {
scopes = ["compute-rw", "logging-write", "monitoring-write", "storage-full"]
}
depends_on = [
google_compute_disk.default,
google_compute_subnetwork.default,
]
}
resource "google_compute_disk" "default" {
count = var.cluster_size
name = "${var.data_disk_prefix}-${count.index}"
type = var.data_disk_type
zone = "${var.region}-${var.zones[count.index]}"
size = var.data_disk_size
snapshot = var.data_disk_snapshot
labels = {
environment = "glusterfs"
}
}
data "template_file" "provision_script" {
count = var.cluster_size
template = file("${path.module}/files/glusterfs_provision_server.sh")
vars = {
cluster_size = var.cluster_size
server_prefix = var.server_prefix
volume_names = join(" ", var.volume_names)
group = var.group
user = var.user
replicas_number = var.replicas_number
}
}
data "template_file" "endpoint" {
count = var.cluster_size
template = file("${path.module}/files/endpoint.json.tpl")
vars = {
ip = element(
google_compute_instance.default.*.network_interface.0.address,
count.index,
)
}
}
data "template_file" "kubernetes_endpoints" {
template = file("${path.module}/files/kubernetes_endpoints.json.tpl")
vars = {
endpoints = join(",\n ", data.template_file.endpoint.*.rendered)
kubernetes_endpoint_name = var.kubernetes_endpoint_name
}
}
resource "google_compute_subnetwork" "default" {
name = var.subnetwork
ip_cidr_range = var.subnet_mask
region = var.region
network = var.network
}
resource "google_compute_firewall" "default" {
name = "gluster-firewall"
network = var.network
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = ["1-65535"]
}
source_tags = var.allowed_source_tags
target_tags = var.tags
}
resource "null_resource" "export_rendered_template" {
provisioner "local-exec" {
command = "cat > ${var.kubernetes_endpoint_file_path} <<EOL\n${data.template_file.kubernetes_endpoints.rendered}\nEOL"
}
triggers = {
update_config = timestamp()
}
}