<p>Assume I know the following secret parameters:</p>
<pre class="lang-json prettyprint-override"><code>&quot;name&quot;:            &quot;aaa&quot;,
&quot;docker-server&quot;:   &quot;a.b.com&quot;,
&quot;docker-username&quot;: &quot;aaa&quot;,
&quot;docker-password&quot;: &quot;aaaa&quot;,
&quot;docker-email&quot;:    &quot;aaa@gmail.com&quot;
</code></pre>
<p>Then I want to use client-go to create a pull-image secret</p>
<pre class="lang-golang prettyprint-override"><code>secret := &amp;corev1.Secret{
    ObjectMeta: metav1.ObjectMeta{
        Name:      &quot;pull-image-secret&quot;,
        Namespace: &quot;aaaaaa&quot;,
    },
    Type: &quot;kubernetes.io/dockerconfigjson&quot;,
    Data: map[string][]byte{&quot;.dockerconfigjson&quot;: []byte(secretData)},
}
err = k8sClient.Create(context.Background(), secret)

</code></pre>
<p>My question is, how to convert secret parameters into secretData?</p>

Joe
<p>From <a href="https://kubernetes.io/docs/concepts/configuration/secret/#docker-config-secrets" rel="nofollow noreferrer">docs</a>:</p>
<blockquote>
<p>the data field of the Secret object must contain a .dockerconfigjson key, in which the content for the ~/.docker/config.json file is provided as a base64 encoded string</p>
</blockquote>
<p>So if you want to use <code>Data</code> field you need to modify code to base64 encode secret data, something like that should work:</p>
<pre class="lang-golang prettyprint-override"><code>import b64 &quot;encoding/base64&quot;

...

base64EncodedData := make([]byte, b64.StdEncoding.EncodedLen(len(secretData)))
b64.StdEncoding.Encode(base64EncodedData, []byte(secretData))

secret := &amp;corev1.Secret{
    ObjectMeta: metav1.ObjectMeta{
        Name:      &quot;pull-image-secret&quot;,
        Namespace: &quot;aaaaaa&quot;,
    },
    Type: &quot;kubernetes.io/dockerconfigjson&quot;,
    Data: map[string][]byte{&quot;.dockerconfigjson&quot;: base64EncodedData},
}
</code></pre>
<p>Otherwise, you can try to use <code>StringData</code> field without base64 encoding:</p>
<pre class="lang-golang prettyprint-override"><code>secret := &amp;corev1.Secret{
    ObjectMeta: metav1.ObjectMeta{
        Name:      &quot;pull-image-secret&quot;,
        Namespace: &quot;aaaaaa&quot;,
    },
    Type: &quot;kubernetes.io/dockerconfigjson&quot;,
    StringData: map[string]string{&quot;.dockerconfigjson&quot;: secretData},
}
</code></pre>
