@@ -20,12 +20,14 @@ resource "kubernetes_namespace" "fluxcd" {
20
20
metadata {
21
21
name = " fluxcd"
22
22
}
23
+
24
+ count = var. namespace == " " ? 1 : 0
23
25
}
24
26
25
27
resource "kubernetes_secret" "flux_ssh" {
26
28
metadata {
27
29
name = " flux-ssh"
28
- namespace = kubernetes_namespace . fluxcd . metadata . 0 . name
30
+ namespace = local . flux_namespace
29
31
}
30
32
31
33
data = {
@@ -40,25 +42,65 @@ resource "kubernetes_secret" "flux_ssh" {
40
42
}
41
43
42
44
locals {
43
- flux_install_script = " ${ path . module } /scripts/flux-install.sh"
44
- flux_install_environment = {
45
- KUBECONFIG = var.kubeconfig_filename
46
- FLUX_CHART_VERSION = var.flux_chart_version
47
- FLUX_YAML_VALUES = yamlencode (local. flux_values )
48
- FLUX_YAML_CUSTOM_VALUES = yamlencode (var. flux_values )
49
- HELM_OPERATOR_CHART_VERSION = var.helm_operator_chart_version
50
- HELM_OPERATOR_YAML_VALUES = yamlencode (local. helm_operator_values )
51
- HELM_OPERATOR_YAML_CUSTOM_VALUES = yamlencode (var. helm_operator_values )
45
+ helm_install_script = " ${ path . module } /scripts/helm-install.sh"
46
+ flux_namespace = coalesce (var. namespace , concat (kubernetes_namespace. fluxcd . * . metadata . 0 . name , [" " ])[0 ])
47
+
48
+ flux_environment = {
49
+ KUBECONFIG = var.kubeconfig_filename
50
+ NAMESPACE = local.flux_namespace
51
+ CHART_NAME = " fluxcd/flux"
52
+ CHART_VERSION = var.flux_chart_version
53
+ RELEASE_NAME = " flux"
54
+ YAML_VALUES = yamlencode (local. flux_values )
55
+ YAML_CUSTOM_VALUES = yamlencode (var. flux_values )
56
+ }
57
+ helm_operator_environment = {
58
+ KUBECONFIG = var.kubeconfig_filename
59
+ NAMESPACE = local.flux_namespace
60
+ CHART_NAME = " fluxcd/helm-operator"
61
+ CHART_VERSION = var.helm_operator_chart_version
62
+ RELEASE_NAME = " helm-operator"
63
+ YAML_VALUES = yamlencode (local. helm_operator_values )
64
+ YAML_CUSTOM_VALUES = yamlencode (var. helm_operator_values )
52
65
}
53
66
}
54
67
55
- resource "null_resource" "flux_install " {
68
+ resource "null_resource" "flux " {
56
69
57
70
provisioner "local-exec" {
58
71
on_failure = fail
59
- command = local. flux_install_script
60
- environment = local. flux_install_environment
72
+ command = local. helm_install_script
73
+ environment = local. flux_environment
74
+ }
75
+
76
+ provisioner "local-exec" {
77
+ command = " helm delete flux --namespace ${ local . flux_namespace } "
78
+ environment = local. flux_environment
79
+ when = destroy
80
+ }
81
+
82
+ triggers = local. flux_environment
83
+ }
84
+
85
+ resource "null_resource" "helm_operator" {
86
+
87
+ provisioner "local-exec" {
88
+ on_failure = fail
89
+ command = local. helm_install_script
90
+ environment = local. helm_operator_environment
91
+ }
92
+
93
+ provisioner "local-exec" {
94
+ command = " helm delete helm-operator --namespace ${ local . flux_namespace } "
95
+ environment = local. helm_operator_environment
96
+ when = destroy
97
+ }
98
+
99
+ provisioner "local-exec" {
100
+ command = " kubectl delete -f https://raw.githubusercontent.com/fluxcd/helm-operator/master/deploy/flux-helm-release-crd.yaml"
101
+ environment = local. helm_operator_environment
102
+ when = destroy
61
103
}
62
104
63
- triggers = local. flux_install_environment
105
+ triggers = local. helm_operator_environment
64
106
}
0 commit comments