-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
102 lines (93 loc) · 3.82 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
locals {
namespaces_manifest = templatefile(
"${path.module}/templates/namespaces.yml",
{
namespaces = var.namespaces
metadata_identifier = var.kubernetes_metadata_identifier
}
)
services_manifest = templatefile(
"${path.module}/templates/services.yml",
{
services = var.services
default_namespace = var.kubernetes_namespace
metadata_identifier = var.kubernetes_metadata_identifier
}
)
secrets_manifest = templatefile(
"${path.module}/templates/secrets.yml",
{
secrets = var.secrets
default_namespace = var.kubernetes_namespace
metadata_identifier = var.kubernetes_metadata_identifier
}
)
configmaps_manifest = templatefile(
"${path.module}/templates/configmaps.yml",
{
configmaps = var.configmaps
default_namespace = var.kubernetes_namespace
metadata_identifier = var.kubernetes_metadata_identifier
}
)
flux_manifest = templatefile(
"${path.module}/templates/flux.yml",
{
flux_instances = var.flux_instances
metadata_identifier = var.kubernetes_metadata_identifier
}
)
}
resource "null_resource" "kubernetes_infra_conf" {
triggers = {
kubernetes_installation_id = var.kubernetes_installation_id
namespaces_manifest = local.namespaces_manifest
services_manifest = local.services_manifest
secrets_manifest = local.secrets_manifest
configmaps_manifest = local.configmaps_manifest
flux_manifest = local.flux_manifest
}
connection {
host = var.bastion_external_ip
type = "ssh"
user = var.bastion_user
port = var.bastion_port
private_key = var.bastion_key_pair.private_key
}
provisioner "remote-exec" {
inline = [
"mkdir -p ${var.manifests_path}"
]
}
provisioner "file" {
content = local.namespaces_manifest
destination = "${var.manifests_path}/namespaces.yml"
}
provisioner "file" {
content = local.services_manifest
destination = "${var.manifests_path}/services.yml"
}
provisioner "file" {
content = local.secrets_manifest
destination = "${var.manifests_path}/secrets.yml"
}
provisioner "file" {
content = local.configmaps_manifest
destination = "${var.manifests_path}/configmaps.yml"
}
provisioner "file" {
content = local.flux_manifest
destination = "${var.manifests_path}/flux.yml"
}
#Apply the services and secrets on the kubernetes cluster
provisioner "remote-exec" {
inline = [
length(var.secrets) > 0 ? "${var.artifacts_path}/kubectl --kubeconfig=${var.artifacts_path}/admin.conf apply --prune=true --selector=\"${var.kubernetes_metadata_identifier}=infrastructure_namespaces\" -f ${var.manifests_path}/namespaces.yml" : ":",
length(var.services) > 0 ? "${var.artifacts_path}/kubectl --kubeconfig=${var.artifacts_path}/admin.conf apply --prune=true --selector=\"${var.kubernetes_metadata_identifier}=infrastructure_services\" -f ${var.manifests_path}/services.yml" : ":",
length(var.secrets) > 0 ? "${var.artifacts_path}/kubectl --kubeconfig=${var.artifacts_path}/admin.conf apply --prune=true --selector=\"${var.kubernetes_metadata_identifier}=infrastructure_secrets\" -f ${var.manifests_path}/secrets.yml" : ":",
length(var.configmaps) > 0 ? "${var.artifacts_path}/kubectl --kubeconfig=${var.artifacts_path}/admin.conf apply --prune=true --selector=\"${var.kubernetes_metadata_identifier}=infrastructure_configmaps\" -f ${var.manifests_path}/configmaps.yml" : ":",
length(var.flux_instances) > 0 ? "${var.artifacts_path}/kubectl --kubeconfig=${var.artifacts_path}/admin.conf apply --prune=true --selector=\"${var.kubernetes_metadata_identifier}=infrastructure_flux_instances\" -f ${var.manifests_path}/flux.yml" : ":",
"rm -r ${var.manifests_path}"
]
}
}