<p>I have a configmap <code>my-config</code> in a namespace and need to make a copy (part of some temporary experimentation) but with another name so I end up with :</p>
<pre><code>my-config
my-config-copy
</code></pre>
<p>I can do this with:</p>
<pre><code>kubectl get cm my-config -o yaml &gt; my-config-copy.yaml
</code></pre>
<p>edit the name manually followed by:</p>
<pre><code>kubectl create -f my-config-copy.yaml
</code></pre>
<p>But is there a way to do it automatically in one line?</p>
<p>I can get some of the way with:</p>
<pre><code>kubectl get cm my-config --export -o yaml | kubectl apply -f -
</code></pre>
<p>but I am missing the part with the new name (since names are immutable I know this is not standard behavior).</p>
<p>Also preferably without using export since:</p>
<p><em>Flag --export has been deprecated, This flag is deprecated and will be removed in future.</em></p>
<p>Any suggestions?</p>

u123
<p>You can achieve this by combining kubectl's <code>patch</code> and <code>apply</code> functions.</p>
<pre><code> kubectl patch cm source-cm -p '{&quot;metadata&quot;:{ &quot;name&quot;:&quot;target-cm&quot;}}' --dry-run=client -o yaml | kubectl apply -f -
</code></pre>
<p><code>source-cm</code> and <code>target-cm</code> are the config map names</p>
