terraform-provider-flux
terraform-provider-flux copied to clipboard
Expose `install` manifests as a list of YAML manifests
I've just been playing with this provider & the Kubernetes Alpha provider and came to the conclusion that things would be simpler if the flux_install
data source exposed the content
attribute also as a list of YAML manifests rather than just as a multi-doc manifest.
The problem I faced is trying to turn the multi-doc manifest into a list of YAML documents. While Terraform does have the split
function, the value being split on isn't a regex and ---
appears in places other than between documents 18 times.
Another solution to this, as mentioned in https://github.com/fluxcd/terraform-provider-flux/issues/143#issuecomment-845994715, would be to have a resource to correctly manage the lifecycle of the Flux installation.
For anyone else attempting to achieve the same thing, one solution is to use replace
on the flux_install
content
to replace /---\n/
with a sentinel value and then split
on that sentinel value. This list can then be safely yamldecode
and passed into kubernetes_manifest
.
locals {
splited_yaml_install = split("-*-", trim(replace(data.flux_install.main.content, "/---\n/", "-*-"), "-*-"))
splited_yaml_sync = split("-*-", trim(replace(data.flux_sync.main.content, "/---\n/", "-*-"), "-*-"))
}
resource "kubectl_manifest" "install" {
count = length(local.splited_yaml_install)
yaml_body = local.splited_yaml_install[count.index]
depends_on = [kubernetes_namespace.flux_system]
}
resource "kubectl_manifest" "sync" {
count = length(local.splited_yaml_sync)
yaml_body = local.splited_yaml_sync[count.index]
depends_on = [kubernetes_namespace.flux_system, kubectl_manifest.install]
}
resource "kubectl_manifest" "sync" { count = length(local.splited_yaml_sync) yaml_body = local.splited_yaml_sync[count.index] depends_on = [kubernetes_namespace.flux_system, kubectl_manifest.install] }
As the generate content for the sync
contains a comment first and and ---
second, terraform want to create an empty yaml
, which it can't apply of course, to skip the first I changed the sync
resource to the following
resource "kubectl_manifest" "sync" {
count = length(local.splited_yaml_sync)-1
yaml_body = local.splited_yaml_sync[count.index+1]
depends_on = [kubernetes_namespace.flux_system, kubectl_manifest.install]
}