type patchStringValue struct {
Op string `json:"op"`
Path string `json:"path"`
Value bool `json:"value"`
}
// 调度开关
func CordonNode(clusterID, nodeName string) error {
node := strings.Replace(nodeName, "_", ".", -1)
clientSet, err := GetClientSet(clusterID)
if err != nil {
log.Errorf("[CordonNode] get clientSet failed, %s", err)
return err
}
cluster, err := GetCluster(clusterID)
if err != nil {
return err
}
fmt.Println(cluster.Nodes[nodeName].Unschedulable)
payload := []patchStringValue{{
Op: "replace",
Path: "/spec/unschedulable",
Value: !cluster.Nodes[nodeName].Unschedulable,
}}
payloadBytes, err := json.Marshal(payload)
if err != nil {
log.Errorf("[CordonNode] json marshal failed, %s", err)
return err
}
nodeInfo, err := clientSet.CoreV1().Nodes().Get(context.TODO(), node, metav1.GetOptions{})
if err != nil {
log.Errorf("[CordonNode] get node info failed %s", err)
return err
}
if GetNodeRole(nodeInfo) == "master" {
log.Errorf("[CordonNode] master %s node not set unschedulable", node)
return fmt.Errorf("master node not set unschedulable")
}
if _, err := clientSet.CoreV1().Nodes().Patch(context.TODO(), node, types.JSONPatchType, payloadBytes, metav1.PatchOptions{}); err != nil {
log.Errorf("[CordonNode] node %s cordon failed %s", node, err)
return err
}
update := bson.D{
{Key: "$set", Value: bson.D{
{Key: fmt.Sprintf("nodes.%s.unschedulable", nodeName), Value: !cluster.Nodes[nodeName].Unschedulable},
},
}}
if _, err := model.ClusterCollection.UpdateOne(context.TODO(), bson.D{{Key: "_id", Value: cluster.ID}}, update); err != nil {
log.Errorf("[updateCluster] Failed to update cluster node %s,%s", node, err.Error())
return err
}
log.Infof("[CordonNode] node %s cordon successful", node)
return nil
}
k8s node cordon
原创
©著作权归作者所有:来自51CTO博客作者qinshixu的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:go-ldap v3 操作AD域
下一篇:修改k8s 节点label
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章