<p>I am trying to update a pvc label using go library's patch option from <a href="https://github.com/kubernetes/client-go/blob/b72204b2445de5ac815ae2bb993f6182d271fdb4/kubernetes/typed/core/v1/persistentvolume.go#L40" rel="nofollow noreferrer">this interface</a>.</p>
<p>Is there any example on how to use this? Also if the label is not there will patch also add the label?</p>
<p>I am looking to update/add my_label in below object:</p>
<pre><code>kind: PersistentVolumeClaim
metadata:
  annotations:
    pv.kubernetes.io/bind-completed: &quot;yes&quot;
    pv.kubernetes.io/bound-by-controller: &quot;yes&quot;
    volume.beta.kubernetes.io/storage-class: &quot;&quot;
  creationTimestamp: &quot;2021-01-25T18:53:02Z&quot;
  finalizers:
  - kubernetes.io/pvc-protection
  labels:
      my_label: my_value
</code></pre>

codec
<p>It could be done the following way:</p>
<pre class="lang-golang prettyprint-override"><code>    persVolC := client.CoreV1().PersistentVolumeClaims(&quot;default&quot;)

    data := `
    [
      { &quot;op&quot;: &quot;add&quot;, &quot;path&quot;: &quot;/metadata/labels/mylabel&quot;, &quot;value&quot;: &quot;myvalue&quot; }
    ]
    `
    updatedPvc, err := persVolC.Patch(ctx, &quot;name-of-pvc&quot;, types.JSONPatchType, []byte(data), meta_v1.PatchOptions{})
    if err != nil {
        log.Fatal(err)
    }
</code></pre>
<p>Full example at <a href="https://play.golang.org/p/-UyHUU7Viv5" rel="nofollow noreferrer">go playground</a>.</p>
<p>And <a href="https://www.rfc-editor.org/rfc/rfc6902" rel="nofollow noreferrer">here</a> you can read about JSON patch standard.</p>
