Skip to content

Commit 52964e7

Browse files
committedDec 27, 2019
Split fluxcd and helm operator to separate resources
1 parent 5f3d490 commit 52964e7

File tree

5 files changed

+94
-51
lines changed

5 files changed

+94
-51
lines changed
 

‎main.tf

+56-14
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ resource "kubernetes_namespace" "fluxcd" {
2020
metadata {
2121
name = "fluxcd"
2222
}
23+
24+
count = var.namespace == "" ? 1 : 0
2325
}
2426

2527
resource "kubernetes_secret" "flux_ssh" {
2628
metadata {
2729
name = "flux-ssh"
28-
namespace = kubernetes_namespace.fluxcd.metadata.0.name
30+
namespace = local.flux_namespace
2931
}
3032

3133
data = {
@@ -40,25 +42,65 @@ resource "kubernetes_secret" "flux_ssh" {
4042
}
4143

4244
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)
5265
}
5366
}
5467

55-
resource "null_resource" "flux_install" {
68+
resource "null_resource" "flux" {
5669

5770
provisioner "local-exec" {
5871
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
61103
}
62104

63-
triggers = local.flux_install_environment
105+
triggers = local.helm_operator_environment
64106
}

‎scripts/flux-destroy.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
helm delete flux --namespace fluxcd
6+
helm delete helm-operator --namespace fluxcd
7+
8+
kubectl delete -f https://raw.githubusercontent.com/fluxcd/helm-operator/master/deploy/flux-helm-release-crd.yaml

‎scripts/flux-install.sh

-37
This file was deleted.

‎scripts/helm-install.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
set -exo pipefail
4+
5+
VALUES_DIRECTORY=/tmp
6+
NAMESPACE=${NAMESPACE:-fluxcd}
7+
8+
YAML_VALUES_FILE="${VALUES_DIRECTORY}/${RELEASE_NAME}-values.yaml"
9+
YAML_CUSTOM_VALUES_FILE="${VALUES_DIRECTORY}/${RELEASE_NAME}-custom-values.yaml"
10+
echo "${YAML_VALUES}" > "${YAML_VALUES_FILE}"
11+
echo "${YAML_CUSTOM_VALUES}" > "${YAML_CUSTOM_VALUES_FILE}"
12+
13+
helm repo add fluxcd https://charts.fluxcd.io
14+
15+
helm upgrade -i "${RELEASE_NAME}" "${CHART_NAME}" \
16+
--wait \
17+
--namespace "${NAMESPACE}" \
18+
--version "${CHART_VERSION}" \
19+
--values "${YAML_VALUES_FILE}" \
20+
--values "${YAML_CUSTOM_VALUES_FILE}"
21+
22+
rm -f \
23+
"${YAML_VALUES_FILE}" \
24+
"${YAML_CUSTOM_VALUES_FILE}"

‎variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ variable "kubeconfig_filename" {
33
description = "Path to kubeconfig"
44
}
55

6+
variable "namespace" {
7+
type = string
8+
description = "Name of the namespace where releases will be deployed. If emptu, the module will attempt to create the namespace"
9+
default = ""
10+
}
11+
612
variable "flux_chart_version" {
713
type = string
814
description = "Flux chart version"

0 commit comments

Comments
 (0)