gavinbunney / terraform-provider-kubectl

Terraform provider to handle raw kubernetes manifest yaml files
https://registry.terraform.io/providers/gavinbunney/kubectl
Mozilla Public License 2.0
609 stars 102 forks source link

only the first manifest is applied #271

Open simonebenati opened 1 year ago

simonebenati commented 1 year ago

I am aware that this has been answered already here: kubectl/issues/52

But I tried to set up the solution it was provided but it doesn't work for me. At the moment I have the argocd template file downloaded with helm, I then am using

resource "kubectl_manifest" "apply_argocd" {
    yaml_body = data.helm_template.argocd_template.manifest
}

Like in the issue #52 it only applies the first manifest. How can I apply everything skipping the ignoring the "---" ?

DrkCloudStrife commented 1 year ago

@simonebenati You need to load the file with kubectl_file_documents and use the for_each in the kubectl_manifest resource as per the documentation here https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/kubectl_file_documents#example-usage-with-for_each

I haven't tested this, however, I'm assuming this will work for you:

# pass the content to kubectl_file_documents. https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/kubectl_file_documents
data "kubectl_file_documents" "helm-template-manifest" {
  content = data.helm_template.argocd_template.manifest
}

# Use kubectl_file_documents to split multi-document into the kubectl_manifest resource
resource "kubectl_manifest" "apply_argocd" {
  for_each = data.kubectl_file_documents.helm-template-manifest.manifests
  yaml_body = each.value
}
janibashamd commented 11 months ago

FYI work-around i did is to divide yaml to multiple manifests with each one having only 1 after

simonebenati commented 10 months ago

@DrkCloudStrife Hi, I haven't tested your solution yet because I've been working on different things but I'll ask a colleague to try it out.

@janibashamd I don't think this can be a solution for very long manifests of operators and such to divide everything into smaller manifests it's a super tedious manual job..

eric-price commented 7 months ago

I haven't had any luck with the method of using a for_each or count on a kubectl data resource. Terraform complains that it doesn't know the length beforehand and it would need to be applied by targeting it.

I got it working with this method:

locals {
  crd_manifests          = split("---", file("../../modules/aws/eks-addons/cert_manager/files/crds.yaml"))
}

resource "kubectl_manifest" "crds" {
  count          = length(local.crd_manifests)
  yaml_body = element(local.crd_manifests, count.index)
}