<p>I have an existing generic Kubernetes secret that exported as YAML (using <code>kubectl get secret -o yaml &gt; secret.yaml</code>), looks like this:</p>
<pre class="lang-yaml prettyprint-override"><code>kind: Secret
apiVersion: v1
type: Opaque
metadata:
  name: some-secret-key-files
data:
  host1.example.com.key: c2VjcmV0IG51bWJlciBvbmUK
  host2.example.com.key: c2VjcmV0IG51bWJlciB0d28K
</code></pre>
<p>Now I have a new key file named <code>host3.example.com.key</code>, with these contents:</p>
<pre><code>secret number three
</code></pre>
<p>What is easiest way to add the contents of this file base64-encoded to <code>secret.yaml</code>, so that in the end it looks like this:</p>
<pre class="lang-yaml prettyprint-override"><code>kind: Secret
apiVersion: v1
type: Opaque
metadata:
  name: some-secret-key-files
data:
  host1.example.com.key: c2VjcmV0IG51bWJlciBvbmUK
  host2.example.com.key: c2VjcmV0IG51bWJlciB0d28K
  host3.example.com.key: c2VjcmV0IG51bWJlciB0aHJlZQo=
</code></pre>

hvtilborg
<p>In the end, exporting the secret to a YAML file was not needed at all. With <code>kubectl patch secret</code> this can be done 'online' like this:</p>
<pre class="lang-bash prettyprint-override"><code>kubectl patch secret some-secret-key-files --patch=&quot;{\&quot;data\&quot;: {\&quot;host3.example.com.key\&quot;: \&quot;$(base64 -w0 host3.example.com.key)\&quot;}}&quot;
</code></pre>
<p>This will add a new file entry to the existing secret <code>some-secret-key-files</code>, and use <code>base64(1)</code> to base64 encode the contents of the local <code>host3.example.com.key</code> file.</p>
