How to cordon a Kubernetes node using golang client

7/6/2020

I want to use golang Kubernetes client SDK to cordon specific nodes in my Kubernetes cluster.

According to other posts, I need to pass the following:

PATCH /api/v1/nodes/node-name

Request Body: {"spec":{"unschedulable":true}} Content-Type: "application/strategic-merge-patch+json"

However, I am not familiar with how to pass that.

I have the following, but not sure if those values are correct

type patchStringValue struct {
	Op    string `json:"op"`
	Path  string `json:"path"`
	Value string `json:"value"`
}

func k8NodeCordon() {
	clientSet := k8ClientInit()

	payload := []patchStringValue{{
		Op:    "replace",
		Path:  "/spec/unschedulable",
		Value: "true",
	}}
	payloadBytes, _ := json.Marshal(payload)
	_, err := clientSet.
		CoreV1().Nodes().Patch()
	return err
}
-- popopanda
go
kubernetes

2 Answers

7/7/2020

Looks like I just needed to add the following:

_, err := clientSet.CoreV1().Nodes().Patch("<node_name>", types.JSONPatchType, payloadBytes)
-- popopanda
Source: StackOverflow

4/2/2021
type patchStringValue struct {
    Op    string `json:"op"`
    Path  string `json:"path"`
    Value bool   `json:"value"`
}

func k8NodeCordon() {
    clientSet := k8ClientInit()

    payload := []patchStringValue{{
        Op:    "replace",
        Path:  "/spec/unschedulable",
        Value: true,
    }}
    payloadBytes, _ := json.Marshal(payload)
    _, err := clientSet.CoreV1().Nodes().Patch("<node_name>", types.JSONPatchType, payloadBytes)
    return err
}
-- sully
Source: StackOverflow