-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.tf
58 lines (58 loc) · 2.42 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
data "template_file" "master" {
template = "${file("cluster/master_node.yml.tpl")}"
count = "${yandex_compute_instance.master.count}"
vars {
name = "${element(yandex_compute_instance.master.*.name, count.index)}"
user = "${var.username}"
int_ip = "${element(yandex_compute_instance.master.*.network_interface.0.ip_address, count.index)}"
nat_ip = "${element(yandex_compute_instance.master.*.network_interface.0.nat_ip_address, count.index)}"
}
}
data "template_file" "worker" {
template = "${file("cluster/worker_node.yml.tpl")}"
count = "${yandex_compute_instance.worker.count}"
vars {
name = "${element(yandex_compute_instance.worker.*.name, count.index)}"
user = "${var.username}"
int_ip = "${element(yandex_compute_instance.worker.*.network_interface.0.ip_address, count.index)}"
nat_ip = "${element(yandex_compute_instance.worker.*.network_interface.0.nat_ip_address, count.index)}"
}
}
data "template_file" "cluster-config" {
template = "${file("cluster/cluster.yml.tpl")}"
vars {
cluster = "${var.cluster_name}"
private_key = "${var.private_key_file}"
master_nodes = "${join("\n", data.template_file.master.*.rendered)}"
worker_nodes = "${join("\n", data.template_file.worker.*.rendered)}"
}
}
output "cluster" {
value = "${data.template_file.cluster-config.rendered}"
}
data "template_file" "config_file" {
template = "${var.output_path}${var.cluster_name}.yml"
}
data "template_file" "kubeconfig_file" {
template = "${var.output_path}kube_config_${var.cluster_name}.yml"
}
resource "null_resource" "cluster" {
triggers {
config = "${data.template_file.cluster-config.rendered}"
}
provisioner "local-exec" {
command = "test -e ${var.output_path} || mkdir -p ${var.output_path}"
}
provisioner "local-exec" {
command = "echo \"${data.template_file.cluster-config.rendered}\" > ${data.template_file.config_file.rendered}"
}
provisioner "local-exec" {
command = "test -e ${var.private_key_file} || echo \"${tls_private_key.ssh-key.private_key_pem}\" > ${var.private_key_file} && chmod 400 ${var.private_key_file}"
}
provisioner "local-exec" {
command = "test -e ${data.template_file.kubeconfig_file.rendered} && scripts/rke.sh ${data.template_file.config_file.rendered} up --update-only || scripts/rke.sh ${data.template_file.config_file.rendered} up"
}
}
output "kubeconfig" {
value = "${data.template_file.kubeconfig_file.rendered}"
}