<p>I'm trying to write Helm Charts for our product. The images are stored on GCR private repo. The charts for all components are ready, but I'm trying to write a YAML file in a chart for imagePullSecrets. I've read chart tips from <a href="https://github.com/kubernetes/helm/blob/master/docs/charts_tips_and_tricks.md#creating-image-pull-secrets" rel="nofollow noreferrer">here</a>,</p>

<p>I also know how to create imagePullSecret with:</p>

<pre><code>kubectl create secret docker-registry mydockercfg \
        --docker-server "https://eu.gcr.io" \
        --docker-username _json_key \
        --docker-email not@val.id \
        --docker-password=$(cat your_service_account.json)
</code></pre>

<p>But I don't know how to fill the content of "your_service_account.json" to password of values.yaml of that Chart. It's better I can change the name "your_service_account.json" to update the password of values.yaml.</p>

<p>Currently, My implementation is as follows:</p>

<pre><code>$ cat values.yaml
secretName: gcr-json-key-test
imageCredentials:
  registry: us.gcr.io/xxxxx
  username: _json_key
  password:
</code></pre>

<p>Contents of secrets.yaml:</p>

<pre><code>$ cat templates/secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Values.secretName }}
    labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
type: kubernetes.io/dockercfg
data:
  .dockerconfigjson: {{ template "imagePullSecret" . }}
</code></pre>

<p>Contents of _helpers.tpl:</p>

<pre><code>$ cat templates/_helpers.tpl
{{/*
Expand the name of the chart.
*/}}
{{- define "name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited 
to this (by the DNS naming spec).
 */}}
{{- define "fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- define "imagePullSecret" }}
{{- printf "{\"auths\": {\"%s\": {\"auth\": \"%s\"}}}" .Values.imageCredentials.registry (printf "%s:%s" .Values.imageCredentials.username .Values.imageCredentials.password | b64enc) | b64enc }}
{{- end }}
</code></pre>

<p>And then using</p>

<pre><code>$ helm install ./secrets --set imageCredentials.password "$(cat ./my_service_account.json)"
</code></pre>

<p>Will result an error:</p>

<blockquote>
  <p>Error: This command needs 1 argument: chart name</p>
</blockquote>

<p>How can I solve this problem?</p>

<p>It can be created and deployed using the following steps:</p>

<p><strong>Steps:</strong></p>

<ol>
<li><p>Create base64 encoded string using your docker_username and docker_password</p>

<pre><code>$ echo -n "docker_username:docker_password" | base64
ZG9rY2VyX3VzZXI6ZG9ja2VyX3Bhc3N3b3Jk
</code></pre></li>
<li><p>Place the encoded string obtained in the <strong>Step 1</strong> as value for <strong>auth</strong> key in the following Json and fill the required details. </p>

<pre><code>{
  "https://eu.gcr.io":
   { 
     "username":"docker_user",
     "password":"docker_password",
     "email":"docker@gamil.com",
     "auth":"ZG9rY2VyX3VzZXI6ZG9ja2VyX3Bhc3N3b3Jk",
   }
 }
</code></pre></li>
<li><p>Reduce this <strong>json</strong> into a <strong>string</strong> enclosed by single quote:</p>

<pre><code>'{"https://eu.gcr.io":{"username":"docker_user","password":"docker_password","email":"docker@gamil.com","auth":"ZG9rY2VyX3VzZXI6ZG9ja2VyX3Bhc3N3b3Jk"}}'
</code></pre></li>
<li><p>Create base64 encoded string for the above Json string as follows:</p>

<pre><code>$ echo -n '{"https://eu.gcr.io":{"username":"docker_user","password":"docker_password","email":"docker@gamil.com","auth":"ZG9rY2VyX3VzZXI6ZG9ja2VyX3Bhc3N3b3Jk"}}' | base64 
eyJodHRwczovL2V1Lmdjci5pbyI6eyJ1c2VybmFtZSI6ImRva2Nlcl91c2VyIiwicGFzc3dvcmQiOiJkb2NrZXJfcGFzc3dvcmQiLCJlbWFpbCI6ImRvY2tlckBnYW1pbC5jb20iLCJhdXRoIjoiWkc5clkyVnlYM1Z6WlhJNlpHOWphMlZ5WDNCaGMzTjNiM0prIn19
</code></pre></li>
<li><p>Create secret.yml in the following format:</p>

<pre><code>$ cat templates/secrets.yaml
apiVersion: v1
kind: Secret
metadata:
   name: {{ .Values.secretName }}
   labels:
     app: {{ template "fullname" . }}
     chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
     release: "{{ .Release.Name }}"
     heritage: "{{ .Release.Service }}"
type: kubernetes.io/dockercfg
data:
  .dockercfg: {{ .Values.dockercfg }}
</code></pre></li>
<li><p>Place the encoded string obtained in <strong>Step 4</strong> in the value.yaml:</p>

<pre><code>$ cat values.yaml
secretName: gcr-json-key-test
dockercfg:
</code></pre></li>
<li><p>Install the chart using the following command:</p>

<pre><code>$ helm install ./secrets -n release_name --set dockecfg="eyJodHRwczovL2V1Lmdjci5pbyI6eyJ1c2VybmFtZSI6ImRva2Nlcl91c2VyIiwicGFzc3dvcmQiOiJkb2NrZXJfcGFzc3dvcmQiLCJlbWFpbCI6ImRvY2tlckBnYW1pbC5jb20iLCJhdXRoIjoiWkc5clkyVnlYM1Z6WlhJNlpHOWphMlZ5WDNCaGMzTjNiM0prIn19" --debug
</code></pre>

<p>or store it in a file ( .dockercfg ) and use the following command</p>

<pre><code>$ helm install ./secrets -n release_name --set dockecfg="$(cat ./.dockercfg )"
</code></pre></li>
</ol>

<p>Hope this will be useful...!! :)</p>
