Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only the first manifest is applied #271

Open
simonebenati opened this issue Jun 27, 2023 · 4 comments
Open

only the first manifest is applied #271

simonebenati opened this issue Jun 27, 2023 · 4 comments

Comments

@simonebenati
Copy link

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
Copy link

@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
Copy link

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

@simonebenati
Copy link
Author

@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
Copy link

eric-price commented Dec 27, 2023

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants