<p>I am working on automating stuffs on Kubernetes cluster and have a requirement of creating an API to cordon a node. Basically this API should not allow any new pods to enter the cordoned node.</p>

<p>I went through below stack-overflow discussion but couldn't figure out the APIs needed to cordon (and then drain) a node:
<a href="https://stackoverflow.com/questions/52325091/how-to-access-the-kubernetes-api-in-go-and-run-kubectl-commands">How to access the Kubernetes API in Go and run kubectl commands</a></p>

martand pal
<p>In order to findout API involved in particular kubectl commmand, use kubectl with flag <code>--v=9</code> which displays HTTP request made to API server with their response (<a href="https://kubernetes.io/docs/reference/kubectl/cheatsheet/#kubectl-output-verbosity-and-debugging" rel="noreferrer">verbose mode</a>)</p>

<h2>API involved in <code>kubectl cordon nodename</code>:</h2>

<pre><code>GET /api/v1/nodes/node-name
PATCH /api/v1/nodes/node-name
</code></pre>

<p>In HTTP PATCH Request, 
<code>Request Body: {"spec":{"unschedulable":true}}</code>
<code>Content-Type: "application/strategic-merge-patch+json"</code></p>

<p>Under the hood, the Golang client will simply make similar HTTP calls. Refer <a href="https://stackoverflow.com/a/43418833/4989632">here</a> for making HTTP PATCH request in golang client.</p>

<h2>API involved in <code>kubectl drain &lt;nodename&gt; --ignore-daemonsets</code>:</h2>

<pre><code>PATCH /api/v1/nodes/node-name -&gt; Request Body: {"spec":{"unschedulable":true}}
GET /api/v1/pods?fieldSelector=spec.nodeName%3Dnode-name -&gt; Get Podlist
POST /api/v1/namespaces/kube-system/pods/coredns-7b5c8bfcfc-s94bs/eviction
GET /api/v1/namespaces/kube-system/pods/coredns-7b5c8bfcfc-s94bs -&gt; If API call returns 404 means Pod is successfully evicted.
</code></pre>

<p>Basically, drain command, first cordons the node, then evict the Daemonset Pod(s) from that node.</p>
