<p>I have the <a href="https://stackoverflow.com/questions/57310483/whats-the-shortest-way-to-add-a-label-to-a-pod-using-the-kubernetes-go-client">following code</a> that is working fine. It adds the label <code>example: yes</code> in the kubernetes object:</p>
<pre><code>package main

import (
    &quot;fmt&quot;
    &quot;encoding/json&quot;
    &quot;k8s.io/apimachinery/pkg/types&quot;

    eksauth &quot;github.com/chankh/eksutil/pkg/auth&quot;
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
)

type patchStringValue struct {
    Op    string `json:&quot;op&quot;`
    Path  string `json:&quot;path&quot;`
    Value string `json:&quot;value&quot;`
}

func main() {
    var updateErr error

    cfg := &amp;eksauth.ClusterConfig{ClusterName: &quot;my cluster name&quot;}
    clientset, _ := eksauth.NewAuthClient(cfg)
    api := clientset.CoreV1()

    // Get all pods from all namespaces without the &quot;sent_alert_emailed&quot; label.
    pods, _ := api.Pods(&quot;&quot;).List(metav1.ListOptions{})

    for i, pod := range pods.Items {

        payload := []patchStringValue{{
            Op:    &quot;replace&quot;,
            Path:  &quot;/metadata/labels/example&quot;,
            Value: &quot;yes&quot;,
        }}
        payloadBytes, _ := json.Marshal(payload)

        _, updateErr = api.Pods(pod.GetNamespace()).Patch(pod.GetName(), types.JSONPatchType, payloadBytes)
        if updateErr == nil {
            fmt.Println(fmt.Sprintf(&quot;Pod %s labelled successfully.&quot;, pod.GetName()))
        } else {
            fmt.Println(updateErr)
        }
    }
}
</code></pre>
<p>The problem is that I need to add the label <code>example/test</code>, that contains the character <code>/</code>, which I think is the origin of my problem. When executing the previous code with the payload:</p>
<pre><code>        payload := []patchStringValue{{
            Op:    &quot;replace&quot;,
            Path:  &quot;/metadata/labels/test/example&quot;,
            Value: &quot;yes&quot;,
        }}
</code></pre>
<p>I get the error: <code>&quot;the server rejected our request due to an error in our request&quot;</code>.</p>
<p>I know an alternative way is using <code>Update</code> instead of <code>Patch</code>. But is there any solution of this problem using <code>Patch</code>?</p>

pcampana
<p>According to <a href="https://www.rfc-editor.org/rfc/rfc6901#section-3" rel="nofollow noreferrer">JSON pointer notation spec</a> which JSON patch uses, you need to use <code>~1</code> to encode <code>/</code>. So your payload would become as follows:</p>
<pre><code>        payload := []patchStringValue{{
            Op:    &quot;replace&quot;,
            Path:  &quot;/metadata/labels/test~1example&quot;,
            Value: &quot;yes&quot;,
        }}
</code></pre>
<pre><code># kubectl patch deploy mydeployment --type='json' -p='[{&quot;op&quot;: &quot;replace&quot;, &quot;path&quot;: &quot;/metadata/labels/example~1test&quot;, &quot;value&quot;:&quot;yes&quot;}]'
deployment.apps/mydeployment patched


# kubectl get deploy mydeployment -o=jsonpath='{@.metadata.labels}'
map[example/test:yes]
</code></pre>
