<p>I want to change (edit/add) labels on pods with specific labels. I have the following code:</p>

<pre><code>public void changeLabelsForPodFilterByLabels(String namespace, Map&lt;String, String&gt; filterByLabels, Map&lt;String, String&gt; newLabels) {
    try {
        V1PodList podList = coreV1Api.listNamespacedPod(namespace, null, null, null, null, null, null, null, false);
        List&lt;V1Pod&gt; podsFilteredByLabel = filterPodsByLabels(podList.getItems(), filterByLabels);
        List&lt;V1Pod&gt; labelModifiedPods = addLabels(podsFilteredByLabel, newLabels);
        List&lt;V1Pod&gt; updatedPods = updatePods(namespace, labelModifiedPods);
    } catch (ApiException e) {
        logger.error("API Exception", e);
        logger.error(e.getMessage());
        logger.error(e.getResponseBody());
    }
}


private List&lt;V1Pod&gt; filterPodsByLabels(List&lt;V1Pod&gt; pods, Map&lt;String, String&gt; labels) {
    List&lt;V1Pod&gt; filteredPods = new ArrayList&lt;&gt;();
    logger.info("Input labels: " + labels.toString());
    for(V1Pod pod: pods) {
        logger.info("Pod: " + pod.getMetadata().getName());
        Map&lt;String, String&gt; podLabels = pod.getMetadata().getLabels();
        logger.info("Labels:" + podLabels.toString());
        if (podLabels.entrySet().containsAll(labels.entrySet())) {
            filteredPods.add(pod);
        }
    }
    return filteredPods;
}


private List&lt;V1Pod&gt; addLabels(List&lt;V1Pod&gt; pods, Map&lt;String, String&gt; newLabels) {
    List&lt;V1Pod&gt; updatedPods = new ArrayList&lt;&gt;();
    for (V1Pod pod: pods) {
        pod.getMetadata().setLabels(newLabels);
        updatedPods.add(pod);
    }
    return updatedPods;
}


private List&lt;V1Pod&gt; updatePods(String namespace, List&lt;V1Pod&gt; updatedPods) throws ApiException {

    for (V1Pod pod: updatedPods){
        V1Patch patch = new V1Patch();
        coreV1Api.patchNamespacedPod(pod.getMetadata().getName(), namespace, null, null, null, null, false);
    }
    return null;
}
</code></pre>

<p>Based on the example from <a href="https://github.com/Agirish/kubernetes-client-java/blob/master/kubernetes/docs/CoreV1Api.md#patchNamespacedPod" rel="nofollow noreferrer">here</a> and from <a href="https://stackoverflow.com/questions/36147137/kubernetes-api-add-label-to-pod">this thread</a> and <a href="https://stackoverflow.com/questions/57310483/whats-the-shortest-way-to-add-a-label-to-a-pod-using-the-kubernetes-go-client">this thread</a> I understand that a body should be provided as parameter to <em>coreV1Api.patchNamespacedPod</em> method. </p>

<p>Can someone provide an example on how a body should look like? Further, in the examples linked above I see that there is the notion of operation (defined by op field). What are the available operations (link to the docs would help)?</p>

<p>Dependencies:</p>

<pre><code>&lt;dependency&gt;
    &lt;groupId&gt;io.kubernetes&lt;/groupId&gt;
    &lt;artifactId&gt;client-java&lt;/artifactId&gt;
    &lt;version&gt;6.0.1&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>

<p><strong>Later edit</strong></p>

<p>I've changed the library version to 8.0.2 and now I was able to change the code as follows:</p>

<pre><code>private List&lt;V1Pod&gt; updatePods(String namespace, List&lt;V1Pod&gt; updatedPods) throws ApiException {

    for (V1Pod pod: updatedPods){
        String body = generateBody("demo", "myvalue");
        V1Patch patch = new V1Patch(body);
        coreV1Api.patchNamespacedPod(pod.getMetadata().getName(), namespace, patch, null, null, null, false);
    }
    return null;
}

public String generateBody(String labelKey, String newLabelValue){
    return String.format("[{\"op\": \"add\", \"path\": \"/metadata/labels/%s\", \"value\": \"%s\"}]", labelKey, newLabelValue);
}
</code></pre>

<p>The above questions are still available because now I receive errors regarding the bad format/form of the body.</p>

<blockquote>
  <p>io.kubernetes.client.openapi.ApiException: Unprocessable Entity</p>
</blockquote>

florin
<p>You could use <strong>JsonObject</strong> instead of String for <em>body</em> argument.<br />
For example:</p>
<pre><code>JsonObject item = new JsonObject();
item.add(&quot;op&quot;, new JsonPrimitive(&quot;add&quot;));
item.add(&quot;path&quot;, new JsonPrimitive(&quot;/metadata/labels&quot;));

JsonObject label = new JsonObject();
label.addProperty(&quot;demo&quot;, &quot;myvalue&quot;);
item.add(&quot;value&quot;, label);
JsonArray body = new JsonArray();
body.add(item);

coreV1Api.patchNamespacedPod(pod.getMetadata().getName(), namespace, body, null, null, null, false);
</code></pre>
