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.
I went through below stack-overflow discussion but couldn't figure out the APIs needed to cordon (and then drain) a node: How to access the Kubernetes API in Go and run kubectl commands
In order to findout API involved in particular kubectl commmand, use kubectl with flag --v=9
which displays HTTP request made to API server with their response (verbose mode)
kubectl cordon nodename
:GET /api/v1/nodes/node-name
PATCH /api/v1/nodes/node-name
In HTTP PATCH Request, Request Body: {"spec":{"unschedulable":true}}
Content-Type: "application/strategic-merge-patch+json"
Under the hood, the Golang client will simply make similar HTTP calls. Refer here for making HTTP PATCH request in golang client.
kubectl drain <nodename> --ignore-daemonsets
:PATCH /api/v1/nodes/node-name -> Request Body: {"spec":{"unschedulable":true}}
GET /api/v1/pods?fieldSelector=spec.nodeName%3Dnode-name -> Get Podlist
POST /api/v1/namespaces/kube-system/pods/coredns-7b5c8bfcfc-s94bs/eviction
GET /api/v1/namespaces/kube-system/pods/coredns-7b5c8bfcfc-s94bs -> If API call returns 404 means Pod is successfully evicted.
Basically, drain command, first cordons the node, then evict the Daemonset Pod(s) from that node.