forked from axeal/tf-do-rke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
169 lines (147 loc) · 5.35 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
168
169
# Configure the DigitalOcean Provider
provider "digitalocean" {
token = var.do_token
}
data "template_file" "userdata" {
template = file("files/userdata")
vars = {
docker_version = var.docker_version
}
}
data "digitalocean_account" "do-account" {
}
resource "digitalocean_droplet" "rke-all" {
count = var.count_all_nodes
image = var.image
name = "${var.prefix}-rke-all-${count.index}"
private_networking = true
region = var.region
size = var.all_size
user_data = data.template_file.userdata.rendered
ssh_keys = var.ssh_keys
tags = [join("", ["user:", replace(split("@", data.digitalocean_account.do-account.email)[0], ".", "-")])]
}
resource "digitalocean_droplet" "rke-etcd" {
count = var.count_etcd_nodes
image = var.image
name = "${var.prefix}-rke-etcd-${count.index}"
private_networking = true
region = var.region
size = var.etcd_size
user_data = data.template_file.userdata.rendered
ssh_keys = var.ssh_keys
tags = [join("", ["user:", replace(split("@", data.digitalocean_account.do-account.email)[0], ".", "-")])]
}
resource "digitalocean_droplet" "rke-controlplane" {
count = var.count_controlplane_nodes
image = var.image
name = "${var.prefix}-rke-controlplane-${count.index}"
private_networking = true
region = var.region
size = var.controlplane_size
user_data = data.template_file.userdata.rendered
ssh_keys = var.ssh_keys
tags = [join("", ["user:", replace(split("@", data.digitalocean_account.do-account.email)[0], ".", "-")])]
}
resource "digitalocean_droplet" "rke-worker" {
count = var.count_worker_nodes
image = var.image
name = "${var.prefix}-rke-worker-${count.index}"
private_networking = true
region = var.region
size = var.worker_size
user_data = data.template_file.userdata.rendered
ssh_keys = var.ssh_keys
tags = [join("", ["user:", replace(split("@", data.digitalocean_account.do-account.email)[0], ".", "-")])]
}
resource "digitalocean_record" "dns" {
domain = var.digitalocean_domain
type = "A"
ttl = 30
name = "${var.prefix}-tf-do-rke"
value = digitalocean_droplet.rke-all[0].ipv4_address
}
data "template_file" "all_nodes" {
template = file("files/node.yml.tmpl")
count = var.count_all_nodes
vars = {
public_ip = digitalocean_droplet.rke-all[count.index].ipv4_address
roles = "[controlplane,worker,etcd]"
}
}
data "template_file" "etcd_nodes" {
template = file("files/node.yml.tmpl")
count = var.count_etcd_nodes
vars = {
public_ip = digitalocean_droplet.rke-etcd[count.index].ipv4_address
roles = "[etcd]"
}
}
data "template_file" "controlplane_nodes" {
template = file("files/node.yml.tmpl")
count = var.count_controlplane_nodes
vars = {
public_ip = digitalocean_droplet.rke-controlplane[count.index].ipv4_address
roles = "[controlplane]"
}
}
data "template_file" "worker_nodes" {
template = file("files/node.yml.tmpl")
count = var.count_worker_nodes
vars = {
public_ip = digitalocean_droplet.rke-worker[count.index].ipv4_address
roles = "[worker]"
}
}
data "template_file" "nodes" {
template = file("files/nodes.yml.tmpl")
vars = {
nodes = chomp(
join(
"",
[
join("", data.template_file.all_nodes.*.rendered),
join("", data.template_file.etcd_nodes.*.rendered),
join("", data.template_file.controlplane_nodes.*.rendered),
join("", data.template_file.worker_nodes.*.rendered),
],
),
)
}
}
resource "local_file" "rke-config" {
content = data.template_file.nodes.rendered
filename = "${path.module}/cluster.yml"
}
resource "local_file" "ssh_config" {
content = templatefile("${path.module}/files/ssh_config.tmpl", {
prefix = var.prefix
rke-all = [for node in digitalocean_droplet.rke-all : node.ipv4_address],
rke-etcd = [for node in digitalocean_droplet.rke-etcd : node.ipv4_address],
rke-controlplane = [for node in digitalocean_droplet.rke-controlplane : node.ipv4_address],
rke-worker = [for node in digitalocean_droplet.rke-worker : node.ipv4_address],
})
filename = "${path.module}/ssh_config"
}
resource "null_resource" "rke-state" {
provisioner "local-exec" {
when = destroy
# Remove cluster.rkestate and kube_config_cluster.yml auto-generated by RKE when nodes destroyed
command = "rm -f cluster.rkestate kube_config_cluster.yml"
}
}
output "rke-all-nodes" {
value = [for node in digitalocean_droplet.rke-all : { name = node.name, ip = node.ipv4_address }]
}
output "rke-etcd-nodes" {
value = [for node in digitalocean_droplet.rke-etcd : { name = node.name, ip = node.ipv4_address }]
}
output "rke-controlplane-nodes" {
value = [for node in digitalocean_droplet.rke-controlplane : { name = node.name, ip = node.ipv4_address }]
}
output "rke-worker-nodes" {
value = [for node in digitalocean_droplet.rke-worker : { name = node.name, ip = node.ipv4_address }]
}
output "rancher-hostname" {
value = "${var.prefix}-tf-do-rke.${var.digitalocean_domain}"
}