Search code examples
terraformterraform-provider-kubernetes

How do I apply a CRD from github to a cluster with terraform?


I want to install a CRD with terraform, I was hoping it would be easy as doing this:

data "http" "crd" {
  url = "https://raw.githubusercontent.com/kubernetes-sigs/application/master/deploy/kube-app-manager-aio.yaml"
  request_headers = {
    Accept = "text/plain"
  }
}

resource "kubernetes_manifest" "install-crd" {
  manifest = data.http.crd.body
}

But I get this error:

can't unmarshal tftypes.String into *map[string]tftypes.Value, expected
map[string]tftypes.Value

Trying to convert it to yaml with yamldecode also doesn't work because yamldecode doesn't support multi-doc yaml files.

I could use exec, but I was already doing that while waiting for the kubernetes_manifest resource to be released. Does kubernetes_manifest only support a single resource or can it be used to create several from a raw text manifest file?


Solution

  • kubernetes_manifest (emphasis mine)

    Represents one Kubernetes resource by supplying a manifest attribute

    That sounds to me like it does not support multiple resources / a multi doc yaml file.

    However you can manually split the incoming document and yamldecode the parts of it:

    locals {
      yamls = [for data in split("---", data.http.crd.body): yamldecode(data)]
    }
    
    resource "kubernetes_manifest" "install-crd" {
      count = length(local.yamls)
      manifest = local.yamls[count.index]
    }
    

    Unfortunately on my machine this then complains about

    'status' attribute key is not allowed in manifest configuration

    for exactly one of the 11 manifests.

    And since I have no clue of kubernetes I have no idea what that means or wether or not it needs fixing.

    Alternatively you can always use a null_resource with a script that fetches the yaml document and uses bash tools or python or whatever is installed to convert and split and filter the incoming yaml.