<p>Using <code>kubectl</code> we can create <code>docker registry authentication secret</code> as follows</p>

<pre><code>kubectl create secret docker-registry regsecret \
--docker-server=docker.example.com \
--docker-username=kube \
--docker-password=PW_STRING \
--docker-email=my@email.com \
</code></pre>

<p>How do i create this <code>secret</code> using <code>terraform</code>, i saw this <a href="https://www.terraform.io/docs/providers/kubernetes/r/secret.html#example-usage" rel="nofollow noreferrer">link</a>, it has <code>data</code>, in the flow of <code>terraform</code> the <code>kubernetes</code> instance is being created in <code>azure</code> and i get the data required from there and i created something like below</p>

<pre><code>resource "kubernetes_secret" "docker-registry" {
  metadata {
    name = "registry-credentials"
  }

  data = {
    docker-server = data.azurerm_container_registry.docker_registry_data.login_server
    docker-username = data.azurerm_container_registry.docker_registry_data.admin_username
    docker-password = data.azurerm_container_registry.docker_registry_data.admin_password
  }


}
</code></pre>

<p>It seems that it is wrong as the images are not being pulled. What am i missing here.</p>

<p>If you run following command</p>

<pre><code>kubectl create secret docker-registry regsecret \
--docker-server=docker.example.com \
--docker-username=kube \
--docker-password=PW_STRING \
--docker-email=my@email.com 
</code></pre>

<p>It will create a secret like following</p>

<pre><code>$ kubectl get secrets regsecret -o yaml
apiVersion: v1
data:
  .dockerconfigjson: eyJhdXRocyI6eyJkb2NrZXIuZXhhbXBsZS5jb20iOnsidXNlcm5hbWUiOiJrdWJlIiwicGFzc3dvcmQiOiJQV19TVFJJTkciLCJlbWFpbCI6Im15QGVtYWlsLmNvbSIsImF1dGgiOiJhM1ZpWlRwUVYxOVRWRkpKVGtjPSJ9fX0=
kind: Secret
metadata:
  creationTimestamp: "2020-06-01T18:31:07Z"
  name: regsecret
  namespace: default
  resourceVersion: "42304"
  selfLink: /api/v1/namespaces/default/secrets/regsecret
  uid: 59054483-2789-4dd2-9321-74d911eef610
type: kubernetes.io/dockerconfigjson

</code></pre>

<p>If we decode <code>.dockerconfigjson</code> we will get</p>

<pre><code>{"auths":{"docker.example.com":{"username":"kube","password":"PW_STRING","email":"my@email.com","auth":"a3ViZTpQV19TVFJJTkc="}}}
</code></pre>

<p>So, how can we do that using terraform?</p>

<p>I created a file <code>config.json</code> with following data</p>

<pre><code>{"auths":{"${docker-server}":{"username":"${docker-username}","password":"${docker-password}","email":"${docker-email}","auth":"${auth}"}}}
</code></pre>

<p>Then in <code>main.tf</code> file</p>

<pre><code>resource "kubernetes_secret" "docker-registry" {
  metadata {
    name = "regsecret"
  }

  data = {
    ".dockerconfigjson" = "${data.template_file.docker_config_script.rendered}"
  }

  type = "kubernetes.io/dockerconfigjson"
}


data "template_file" "docker_config_script" {
  template = "${file("${path.module}/config.json")}"
  vars = {
    docker-username           = "${var.docker-username}"
    docker-password           = "${var.docker-password}"
    docker-server             = "${var.docker-server}"
    docker-email              = "${var.docker-email}"
    auth                      = base64encode("${var.docker-username}:${var.docker-password}")
  }
}
</code></pre>

<p>then run </p>

<pre><code>$ terraform apply
</code></pre>

<p>This will generate same secrets. Hope it will helps</p>
