<p>I created a secret like this:  </p>

<pre><code>kubectl create secret generic test --from-literal=username=testuser --from-literal=password=12345
</code></pre>

<p>I want to update the username to <code>testuser2</code> but I want to do it only with <code>kubectl patch --type='json'</code>.  </p>

<p>This is how I tried to do it:  </p>

<pre><code>kubectl patch secret test --type='json' -p='[{"data":{"username": "testuser 2"}}]' -v=1  
</code></pre>

<p>But I received:  </p>

<blockquote>
  <p>The  "" is invalid</p>
</blockquote>

<p>Remember, I want to do it with the option of <code>--type='json'</code>, no other workarounds.</p>

<p>I found how to do it after I read <a href="https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment" rel="noreferrer">here</a> that referred me to <a href="http://erosb.github.io/post/json-patch-vs-merge-patch/" rel="noreferrer">this</a> great article.<br>
This is the JSON secret:  </p>

<pre><code>{
    "apiVersion": "v1",
    "data": {
        "password": "aWx1dnRlc3Rz",
        "username": "dGVzdHVzZXI="
    },
    "kind": "Secret",
    "metadata": {
        "creationTimestamp": "2019-04-18T11:37:09Z",
        "name": "test",
        "namespace": "default",
        "resourceVersion": "3017",
        "selfLink": "/api/v1/namespaces/default/secrets/test",
        "uid": "4d0a763e-61ce-11e9-92b6-0242ac110015"
    },
    "type": "Opaque"
}
</code></pre>

<p>Therefore, to update the user's field I needed to create the JSON Patch format:  </p>

<pre><code>[
    {
        "op" : "replace" ,
        "path" : "/data/username" ,
        "value" : "dGVzdHVzZXIy" # testuser2 in base64
    }
]
</code></pre>

<p>Notice that the value should be in base64.  </p>

<p>The result is:  </p>

<pre><code>kubectl patch secret test --type='json' -p='[{"op" : "replace" ,"path" : "/data/username" ,"value" : "dGVzdHVzZXIy"}]'
</code></pre>
