terraform-provider-flux
terraform-provider-flux copied to clipboard
Setup `PostBuild` and `substitute` for flux kustomization
I would like to use substitution https://fluxcd.io/docs/components/kustomize/kustomization/#variable-substitution , but I need to set it up during the installation by terraform. I can't find a way how to do it. I will appreciate every advice, thank you ...
I think this might be what you're looking for:
https://github.com/fluxcd/terraform-provider-flux/blob/f5e906abbd58c10c65baca9c3f90cda950e80ab5/examples/customize-flux/main.tf#L35
It's not kustomize/postbuild/substituteFrom, but it is a way to get for example the service account ID from a GKE service account that Terraform creates, and use it in a patch that updates Flux during installation.
First of all, thank you @kingdonb . I find a solution, It's not nice but is working
data "flux_sync" "app" {
target_path = var.flux_app_target_path
url = var.flux_app_target_url
branch = var.flux_app_branch
name = var.flux_app_name
secret = var.flux_app_secret_name
}
data "kubectl_file_documents" "app" {
content = data.flux_sync.app.content
}
locals {
postBuildParameters = {
substitute = {
...
}
}
decoded_documents = [ for v in data.kubectl_file_documents.app.documents : yamldecode(v) ]
documents = [ for v in local.decoded_documents :
merge( v, v.kind == "Kustomization" ? { postBuild = local.postBuildParameters } : {})
]
}
I actually like the look of that! It's expressive in an hcl sort of way and close to the metal in terms of Flux.
The part I don't like is, how do you keep this:
substitute = {
...
}
updated and in sync with whatever Flux is doing. I guess if you make the content at ... a block list of hard-coded variables then this is not a great solution, (but if it works it works...)
Should be like this:
postBuildParameters = {
substituteFrom = [
{
kind = "ConfigMap"
name = "cluster-vars"
}
]
}
decoded_documents = [ for v in data.kubectl_file_documents.sync.documents : yamldecode(v) ]
documents = [ for v in local.decoded_documents :
merge( v, v.kind == "Kustomization" ? { spec = merge(v.spec, {postBuild = local.postBuildParameters}) } : {})
]