<p>I'm using the terraform <a href="https://www.terraform.io/docs/providers/kubernetes" rel="noreferrer">kubernetes-provider</a> and I'd like to translate something like this <code>kubectl</code> command into TF:</p>

<pre><code>kubectl create secret generic my-secret --from-file mysecret.json
</code></pre>

<p>It seems, however the <code>secret</code> resource's <code>data</code> field <a href="https://www.terraform.io/docs/providers/kubernetes/d/secret.html#data" rel="noreferrer">expects only a TF map</a>.</p>

<p>I've tried something like </p>

<pre><code>data "template_file" "my-secret" {
  template = "${file("${path.module}/my-secret.json")}"
}

resource "kubernetes_secret" "sgw-config" {
   metadata {
     name = "my-secret"
   }
   type = "Opaque"
   data = "{data.template_file.my-secret.template}"
}
</code></pre>

<p>But it complains that this is <em>not</em> a map.  So, I can do something like this:</p>

<pre><code>   data = {
      "my-secret.json" = "{data.template_file.my-secret.template}"
   }
</code></pre>

<p>But this will write the secret with a top-level field named <code>my-secret.json</code> and when I volume mount it, it won't work with other resources.</p>

<p>What is the trick here?</p>

Davis Ford
<p>as long the file is UTF-8 encoded  you can use something like this</p>

<pre><code>resource "kubernetes_secret" "some-secret" {

      metadata {
        name      = "some-secret"
        namespace = kubernetes_namespace.some-ns.metadata.0.name
        labels = {
          "sensitive" = "true"
          "app"       = "my-app"
        }
      }
      data = {
        "file.txt" = file("${path.cwd}/your/relative/path/to/file.txt")
      }
    }
</code></pre>

<p>If the file is a binary one you will have an error like</p>

<blockquote>
  <p>Call to function "file" failed: contents of
  /your/relative/path/to/file.txt are not valid UTF-8; use the
  filebase64 function to obtain the Base64 encoded contents or the other
  file functions (e.g. filemd5, filesha256) to obtain file hashing
  results instead.</p>
</blockquote>

<p>I tried encoding the file in base64 but then the problem is that the resulting text will be re-encoded in base64 by the provider. So I guess there is no solution for binary files at the moment...
I'll edit with what I find next for binaries.</p>
