<p>I have a configmap that looks like this:</p>
<pre><code>apiVersion: v1
data:
  nginx.conf: &quot;events {worker_connections  1024;
.
.
.
}&quot;
kind: ConfigMap
metadata:
  name: nginx-cfg
  namespace: nginx
</code></pre>
<p>I want to add a new line of text to the begining of nginx.conf:</p>
<pre><code>apiVersion: v1
data:
   nginx.conf: &quot; &lt;some line of new text here&gt;
   events {worker_connections  1024;
  .
  .
  .
  }&quot;
kind: ConfigMap
metadata:
   name: nginx-cfg
   namespace: nginx
</code></pre>
<p>I use this patch command to make changes:</p>
<pre><code>kubectl patch configmap/nginx-cfg \
      -n nginx \
      --type merge \
      -p '{&quot;data&quot;:{&quot;nginx.conf&quot;:{&quot;load_module /usr/lib/nginx/modules/ngx_http_vhost_traffic_status_module.so&quot;}}}'
</code></pre>
<p>but got an error: <strong>Error from server: Invalid JSON Patch</strong>. What shoud I do to fix the error?</p>
<p>Thank you for your help!</p>

Danil Savchenko
<p>The error is because you're trying to go into file block. If you remove the braces:</p>
<p><code>-p '{&quot;data&quot;:{&quot;nginx.conf&quot;:{&quot;load_module /usr/lib/nginx/modules/ngx_http_vhost_traffic_status_module.so&quot;}}}'</code></p>
<p>to:</p>
<p><code>-p '{&quot;data&quot;:{&quot;nginx.conf&quot;:&quot;load_module /usr/lib/nginx/modules/ngx_http_vhost_traffic_status_module.so&quot;}}'</code></p>
<p>Then the patch will then succeed, but would overwrite the config file contents:</p>
<pre><code>$ kubectl patch configmap/nginx-cfg --type merge -p '{&quot;data&quot;:{&quot;nginx.conf&quot;:&quot;load_module /usr/lib/nginx/modules/ngx_http_vhost_traffic_status_module.so&quot;}}' -o yaml
apiVersion: v1
data:
  nginx.conf: load_module /usr/lib/nginx/modules/ngx_http_vhost_traffic_status_module.so
kind: ConfigMap
metadata:
  name: nginx-cfg

</code></pre>
<p>If you want to patch the contents of that field, then you have a few options:</p>
<p>a) Dump the configmap, modify the confimap locally using <code>sed</code> or similar tools, then <code>kubectl replace</code> it back. Note the <code>&quot;\ \ \ \ &quot;</code> is intentional to pad the line within the yaml</p>
<pre><code>kubectl get configmap nginx-cfg -o yaml &gt;nginx-cfg.yaml

sed -i '/nginx\.conf.*/a  \ \ \ \ load_module /usr/lib/nginx/modules/ngx_http_vhost_traffic_status_module.so nginx-cfg.yaml #Or whatever edits you need to make

kubectl replace -f nginx-cfg.yaml

</code></pre>
<p>b) Extract the config block itself using <code>kubectl get configmap nginx-cfg -o jsonpath=&quot;{.data.nginx\.conf}&quot;</code>, dump it to disk, modify the block, then <code>kubectl create configmap</code> using that and replace</p>
<pre><code>kubectl get configmap nginx-cfg -o jsonpath=&quot;{.data.nginx\.conf}&quot; &gt;nginx.conf

sed -i '1 i\load_module /usr/lib/nginx/modules/ngx_http_vhost_traffic_status_module.so' nginx.conf #Or whatever edits you need to make

kubectl create configmap --dry-run=client --from-file=nginx.conf nginx-cfg -o yaml | kubectl replace -f -
</code></pre>
<p>c) Using some pipes to do the edits in-line (only useful if it's single line change. Otherwise, it's worth doing one of the above instead). This gets the yaml, edits it in-line, then feeds it back into kubectl to replace the existing configmap</p>
<pre><code>kubectl get configmap nginx-cfg -o yaml | \
  sed '/nginx\.conf.*/a  \ \ \ \ load_module /usr/lib/nginx/modules/ngx_http_vhost_traffic_status_module.so' | \
  kubectl replace -f -

</code></pre>
