4

I am using the Kubernetes Provider to describe services/pods in Terraform.

It can get confusing using the Hashicorp Configuration Language to define kubernetes_pod or kubernetes_service resources because the Kubernetes documentation describes everything in YAML which it means you need to translate it into HCL.

Is it possible to define pods as YAML and use them with kubernetes_pod and kubernetes_service resources as templates?

3 Answers 3

3

While Terraform normally uses HCL, this is a superset of JSON (much like YAML itself) so can also read JSON.

One possible option would be to take the YAML examples you already have and convert them into JSON and then use Terraform on those.

Unfortunately, that's unlikely to work because keywords are likely to be different for how Terraform is expecting things so you'd need to write something to do some basic translation of the input YAML to a Terraform resource JSON. At this point, it'd probably be worth just adding HCL output to the conversion so your outputted Terraform config is more readable if you ever intend to keep the Terraform config around instead of just one shot converting and applying the config.

The benefit of doing things this way would be that you have a reusable Kubernetes config that could be ran using kubectl or other tools but gives you the power of Terraform's lifecycle management, being able to plan changes and integration with non Kubernetes parts of your infrastructure (such as setting up instances to run the Kubernetes cluster on).

I've not used it much but I believe Kops will allow you to keep pod/service config in typical Kubernetes YAML files but can then use Terraform to manage the configuration and even allows you to output the Terraform configuration so you can run it outside of Kops itself.

2
  • Appreciate the response. I Suppose there is no way to do something like config = template(...) then...? Nov 11, 2017 at 17:41
  • Unfortunately not. There are some resources that support something similar including one of the AWS API Gateway resources that has recently added support for using a swagger spec file. So it might be worth raising a feature request issue for it if you think it might be useful
    – ydaetskcoR
    Nov 14, 2017 at 7:39
3

The hashicorp/kubernetes provider does not support raw YAML/JSON, and they have no intention of implementing it.

The possible solutions are:

  • K2tf, a tool for converting Kubernetes RAW YAML manifests into Terraform HCL for the Kubernetes provider.
  • Use an alternative community Kubernetes provider, such as gavinbunny/kubectl, which does support raw YAML and can track each resource and the attributes in Terraform state, unlike the kubernetes-alpha provider.
  • Another solution is to use the hashicorp/kubernetes-alpha provider, you can pass in either a Terraform object or convert raw YAML manifest into a TF object for using in the provider resource. The downside is that the attributes are not tracked as individual objects and thus a change will cause the entire resource to be tainted.

Using the kubectl provider.

This core of this provider is the kubectl_manifest resource, allowing free-form yaml to be processed and applied against Kubernetes. This yaml object is then tracked and handles creation, updates and deleted seamlessly - including drift detection. This provider is ideal if you want to track the manifest in Terraform:

resource "kubectl_manifest" "test" {
    yaml_body = file("path/to/manifest.yaml")
}

Using the kubernetes-alpha provider

The kubernetes_manifest represents one Kubernetes resource as described in the manifest attribute. The manifest value is the HCL transcription of a regular Kubernetes YAML manifest. To transcribe an existing manifest from YAML to HCL, use the Terrafrom built-in function yamldecode(), or use the tfk8s tool to convert YAML into manifest attributes for the kubernetes-alpha provider manifest resource.

Example using yamldecode:

resource "kubernetes_manifest" "service" {
  provider = kubernetes-alpha

  manifest = yamldecode(file("path/to/manifest.yaml"))
}

Why doesn't the kubernetes provider support RAW YAML?

Supporting YAML/JSON in hashicorp/kubernetes was considered before (the very first proposal of K8S provider was exactly that) and during the initial implementation of this provider and we decided not to do it.

The reason is that you can't accurately track resources created from RAW YAML as Terraform objects.

From Terraform's developer perspective it is very tricky to get around the way K8S API works where you send an array [a, b, c] to the Create API and then you Get back [a, b, c, d]. This happens for example with pods that get some secret volumes attached automatically, but happens with most other resources I had the chance to play with. The whitelisting/blacklisting is tricky part.

2
1

You may also be interested in the following project, which allows you to convert YAML files to Terraform's HCL.

https://github.com/sl1pm4t/k2tf

Description:

A tool for converting Kubernetes API Objects (in YAML format) into HashiCorp's Terraform configuration language.

The converted .tf files are suitable for use with the Terraform Kubernetes Provider

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.