<p>Is there some python/go library to generate Terraform code from a JSON file, or is there any templating language to generate terraform code.</p>

<p><strong>Use case:</strong></p>

<p>I need to register all the Kubernetes services of type node port on my cluster, on external load balancer, and there is a terraform provider for that external load balancer that takes the service and namespace name and some other parameters and registers the service on a pool, once the pool is created with correct service , and namespace name, then the load balancer automatically do discovery of nodeports and node IPs and keep the pool updated all the time. Currently, I have to edit the terraform code manually to add the service names and other stuff, to update the load balancer config to create the pool for k8s service.</p>

<p>Once I figure out how to generate/update the terraform file from the given state of the cluster, I will put this process as cluster daemon in a pod, that will run the script every X interval so that the load balancer is updated when users create and delete k8s services.</p>

<p><strong>Example record in terraform file:</strong></p>

<pre><code>   # Pool automatically populated by K8s Service Discovery
resource "vtm_pool" "&lt;cluster name&gt;_&lt;k8s namsepace&gt;_&lt;k8s service name&gt;" {
  name                          = ""&lt;cluster name&gt;_&lt;k8s namsepace&gt;_&lt;service name&gt;""
  monitors                      = ["Ping"]
  service_discovery_enabled     = "true"
  service_discovery_interval    = "15"
  service_discovery_plugin      = "${var.k8s_discovery_plugin}"
  service_discovery_plugin_args = "-s &lt;k8s service name &gt; -n &lt;k8s namsepace&gt; -c &lt;kubeconf file name&gt;"
}
</code></pre>

Ijaz Ahmad
<p>Terraform has a couple of hook points that might work for you.</p>

<p>The overall approach you're suggesting will probably work fine.  The HCL syntax itself isn't that complicated, and you could just write it out.  Hashicorp has <a href="https://github.com/hashicorp/hcl" rel="nofollow noreferrer">a Go library</a> that can read and write it.</p>

<p>Terraform also <a href="https://www.terraform.io/docs/configuration/syntax.html#json-syntax" rel="nofollow noreferrer">directly supports JSON input</a> as well.  The syntax is harder to hand-write but probably easier to machine-generate.  There are a couple of subtleties in how the HCL syntax converts to JSON and it's probably worth reading through that whole page (even the HCL part), but this might be easiest to machine-generate.</p>

<p>If I had to do this, I might reach for a Terraform <a href="https://www.terraform.io/docs/providers/external/data_source.html" rel="nofollow noreferrer">external data source</a>.  That can run any program that produces JSON output, but once you have that it's "natively" in Terraform space.  You could write something like (untested):</p>

<pre><code>data "external" "services" {
  program = ["kubectl", "get", "service",
             "-o", "json",
             "--field-selector", "spec.type==LoadBalancer"]
}
resource "vtm_pool" "k8s_services" {
  count = "${length(data.external.services.result)}"
  name = "${data.external.services.*.metadata.name[count.index]}"
}
</code></pre>

<p>(In the past, I've had trouble where Terraform can get very fixated on specific indices, and so if you do something like this and the <code>kubectl</code> output returns the Service objects in a different order, it might want to go off and swap which load balancer is which; or if something gets deleted, every other load balancer might get reassigned.)</p>

<p>The "most right" (but hardest) answer would be to teach Kubernetes about your cloud load balancer.  There is a standard (Go) <a href="https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/cloud-provider" rel="nofollow noreferrer">k8s.io/cloud-provider</a> interface you can implement, and a <a href="https://github.com/kubernetes/kubernetes/tree/master/pkg/cloudprovider/providers" rel="nofollow noreferrer">handful of providers</a> in the main Kubernetes source tree.  I'd guess the <a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/cloudprovider/providers/fake/fake.go" rel="nofollow noreferrer">fake cloud provider</a> is as good a starting point as any.</p>
