-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.tf
106 lines (86 loc) · 2.8 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
terraform {
required_version = ">= 0.12.2"
required_providers {
kubernetes = "~> 1.10"
local = ">= 1.4"
tls = ">= 2.1"
random = ">= 2.2"
}
}
resource "tls_private_key" "fluxcd" {
algorithm = "RSA"
rsa_bits = 4096
count = var.generate_ssh_key && var.ssh_private_key == "" ? 1 : 0
}
resource "kubernetes_namespace" "fluxcd" {
metadata {
name = "fluxcd"
}
count = var.namespace == "" ? 1 : 0
}
resource "kubernetes_secret" "flux_ssh" {
metadata {
name = "flux-ssh"
namespace = local.flux_namespace
}
data = {
identity = var.ssh_private_key != "" ? var.ssh_private_key : concat(tls_private_key.fluxcd.*.private_key_pem, [""])[0]
}
lifecycle {
ignore_changes = [ metadata[0].annotations ]
}
count = var.generate_ssh_key || var.ssh_private_key != "" ? 1 : 0
}
locals {
helm_install_script = "${path.module}/scripts/helm-install.sh"
flux_namespace = coalesce(var.namespace, concat(kubernetes_namespace.fluxcd.*.metadata.0.name, [""])[0])
flux_environment = {
KUBECONFIG = var.kubeconfig_filename
NAMESPACE = local.flux_namespace
CHART_NAME = "fluxcd/flux"
CHART_VERSION = var.flux_chart_version
RELEASE_NAME = "flux"
YAML_VALUES = yamlencode(local.flux_values)
YAML_CUSTOM_VALUES = yamlencode(var.flux_values)
}
helm_operator_environment = {
KUBECONFIG = var.kubeconfig_filename
NAMESPACE = local.flux_namespace
CHART_NAME = "fluxcd/helm-operator"
CHART_VERSION = var.helm_operator_chart_version
RELEASE_NAME = "helm-operator"
YAML_VALUES = yamlencode(local.helm_operator_values)
YAML_CUSTOM_VALUES = yamlencode(var.helm_operator_values)
}
}
resource "null_resource" "flux" {
provisioner "local-exec" {
on_failure = fail
command = local.helm_install_script
environment = local.flux_environment
}
provisioner "local-exec" {
command = "helm delete flux --namespace ${local.flux_namespace}"
environment = local.flux_environment
when = destroy
}
triggers = local.flux_environment
}
resource "null_resource" "helm_operator" {
provisioner "local-exec" {
on_failure = fail
command = local.helm_install_script
environment = local.helm_operator_environment
}
provisioner "local-exec" {
command = "helm delete helm-operator --namespace ${local.flux_namespace}"
environment = local.helm_operator_environment
when = destroy
}
provisioner "local-exec" {
command = "kubectl delete -f https://raw.githubusercontent.com/fluxcd/helm-operator/master/deploy/flux-helm-release-crd.yaml"
environment = local.helm_operator_environment
when = destroy
}
triggers = local.helm_operator_environment
}