Created
January 24, 2022 10:05
-
-
Save bsamartins/cbada934a161147bf59de9ef4318b50a to your computer and use it in GitHub Desktop.
Deploying Flux With Terraform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform { | |
required_providers { | |
kubectl = { | |
source = "gavinbunney/kubectl" | |
} | |
flux = { | |
source = "fluxcd/flux" | |
} | |
} | |
} | |
provider "flux" {} | |
provider "kubernetes" { | |
config_path = "~/.kube/config" | |
} | |
variable "target_path" { | |
default = "/test" | |
} | |
data "flux_install" "main" { | |
target_path = var.target_path | |
} | |
resource "kubernetes_namespace" "flux_system" { | |
metadata { | |
name = "flux-system" | |
} | |
lifecycle { | |
ignore_changes = [ | |
metadata[0].labels, | |
] | |
} | |
} | |
# Split multi-doc YAML with | |
# https://registry.terraform.io/providers/gavinbunney/kubectl/latest | |
data "kubectl_file_documents" "install" { | |
content = data.flux_install.main.content | |
} | |
# Convert documents list to include parsed yaml data | |
locals { | |
apply = [ for v in data.kubectl_file_documents.install.documents : { | |
data: yamldecode(v) | |
content: v | |
} | |
] | |
} | |
# Apply manifests on the cluster | |
resource "kubectl_manifest" "apply" { | |
for_each = { for v in local.apply : lower(join("/", compact([v.data.apiVersion, v.data.kind, lookup(v.data.metadata, "namespace", ""), v.data.metadata.name]))) => v.content } | |
depends_on = [kubernetes_namespace.flux_system] | |
yaml_body = each.value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment