如何使用 golang 客户端封锁 Kubernetes 节点



我想使用 golang Kubernetes 客户端 SDK 来封锁我的 Kubernetes 集群中的特定节点。

根据其他帖子,我需要通过以下内容:

PATCH /api/v1/nodes/node-name
Request Body: {"spec":{"unschedulable":true}} Content-Type: "application/strategic-merge-patch+json"

但是,我不熟悉如何通过它。

我有以下内容,但不确定这些值是否正确

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
}
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
}

看起来我只需要添加以下内容:

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

最新更新