-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmicrok8s-worker-node.tf
88 lines (74 loc) · 2.82 KB
/
microk8s-worker-node.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
resource "digitalocean_volume" "microk8s-worker-node" {
region = var.region
count = var.node_count
name = "microk8s-worker-fs-${count.index}"
size = var.worker_node_disksize
description = "A volume to attach to the worker"
}
resource "digitalocean_droplet" "microk8s-worker-node" {
image = var.os_image
name = "microk8s-worker-${var.cluster_name}-${count.index}"
region = var.region
size = var.worker_node_size
count = var.worker_node_count
private_networking = true
tags = [
digitalocean_tag.microk8s-worker.id
]
ssh_keys = [
var.digitalocean_ssh_fingerprint,
]
user_data = element(data.template_file.node_config.*.rendered, count.index)
volume_ids = [element(digitalocean_volume.microk8s-worker-node.*.id, count.index)]
}
# Tag to label nodes
resource "digitalocean_tag" "microk8s-worker" {
name = "microk8s-worker-${var.cluster_name}"
}
resource "null_resource" "join_workers" {
count = var.worker_node_count
depends_on = [null_resource.setup_tokens, null_resource.join_nodes]
triggers = {
rerun = random_id.cluster_token.hex
}
connection {
host = element(digitalocean_droplet.microk8s-worker-node.*.ipv4_address, count.index)
user = "root"
type = "ssh"
private_key = file(var.digitalocean_private_key)
timeout = "20m"
}
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = "while [[ $(cat /tmp/current_joining_worker_node.txt) != \"${count.index}\" ]]; do echo \"${count.index} is waiting...\";sleep 5;done"
}
provisioner "file" {
content = templatefile("${path.module}/templates/join-worker.sh",
{
dns_zone = var.dns_zone
cluster_token = random_id.cluster_token.hex
main_node_ip = digitalocean_droplet.microk8s-node[0].ipv4_address_private
})
destination = "/usr/local/bin/join-worker.sh"
}
provisioner "remote-exec" {
inline = [
"sh /usr/local/bin/join-worker.sh"
]
}
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = "echo \"${count.index+1}\" > /tmp/current_joining_worker_node.txt"
}
}
# Discrete DNS records for each controller's private IPv4 for ingress usage
resource "digitalocean_record" "microk8s-worker-node" {
count = var.worker_node_count
# DNS zone where record should be created
domain = var.dns_zone
# DNS record (will be prepended to domain)
name = "microk8s-worker-worker-node-${count.index}"
type = "A"
ttl = 300
value = element(digitalocean_droplet.microk8s-worker-node.*.ipv4_address, count.index)
}