<p>I'm playing around with GitOps and ArgoCD in Redhat Openshift. My goal is to switch a worker node to an infra node.</p>

<p>I want to do this with descriptive YAML Files, and NOT manually by using the command line (that's easy with kubectl label node ...)</p>

<p>In order to do make the node an infra node, I want to add a label "infra" and take the label "worker" from it. Before, the object looks like this (irrelevant labels omitted): </p>

<pre><code>apiVersion: v1
kind: Node
metadata:
  labels:
    node-role.kubernetes.io/infra: ""
  name: node6.example.com
spec: {}
</code></pre>

<p>After applying a YAML File, it's supposed to look like that: </p>

<pre><code>apiVersion: v1
kind: Node
metadata:
  labels:
    node-role.kubernetes.io/worker: ""
  name: node6.example.com
spec: {}
</code></pre>

<p>If I put the latter config in a file, and do "kubectl apply -f ", the node has both infra and worker labels. So adding a label or changing the value of a label is easy, but is there a way to remove a label in an objects metadata by applying a YAML file ?</p>

tvitt
<p>I would say it's not possible to do with <code>kubectl apply</code>, at least I tried and couldn't find any informations about that.</p>

<p>As @Petr Kotas mentioned you can always use </p>

<pre><code>kubectl label node node6.example.com node-role.kubernetes.io/infra-
</code></pre>

<p>But I see you're looking for something else</p>

<blockquote>
  <p>I want to do this with descriptive YAML Files, and NOT manually by using the command line (that's easy with kubectl label node ...)</p>
</blockquote>

<hr>

<p>So maybe the answer could be to use API clients, for example <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">python</a>? I have found this example <a href="https://stackoverflow.com/a/54168783/11977760">here</a>, made by @Prafull Ladha</p>

<blockquote>
  <p>As already mentioned, correct kubectl example to delete label, but there is no mention of removing labels using API clients. if you want to remove label using the API, then you need to provide a new body with the <code>labelname: None</code> and then patch that body to the node or pod. I am using the kubernetes python client API for example purpose</p>
</blockquote>

<pre><code>from pprint import pprint
from kubernetes import client, config

config.load_kube_config()
client.configuration.debug = True

api_instance = client.CoreV1Api()

body = {
    "metadata": {
        "labels": {
            "label-name": None}
        }
}

api_response = api_instance.patch_node("minikube", body)

print(api_response)
</code></pre>
