使用kubectl edit命令可以更新node字段,但是无法更新nodestatus字段。本文实践几种修改k8s节点status里面的字段的方法,比如修改conditions里面的字段。
可以写代码使用client-go中的nodestatus更新来更新节点的status字段。支持patchStatus和updateStatus
package main
import (
"context"
"log"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main(){
kubeconfigFile := "/Users/williamji/Downloads/cls-jf1v5w0p-config"
nodeName := "10.111.0.14"
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigFile)
if err != nil {
log.Printf("failed to config using kubeconfig: %s with error: %v", kubeconfigFile, err)
os.Exit(1)
}
k8sClient, err := kubernetes.NewForConfig(config)
if err != nil {
log.Printf("failed to get k8s client with error: %v", err)
os.Exit(1)
}
getNode, err := k8sClient.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
if err != nil {
log.Printf("failed to get node: %s with error: %v", nodeName, err)
os.Exit(1)
}
// modify one condition Status False => True
if getNode.Status.Conditions[0].Status == "False"{
log.Printf("0 condition Status is False, now change it to True")
getNode.Status.Conditions[0].Status = "True"
}
modifyNode, err := k8sClient.CoreV1().Nodes().UpdateStatus(context.Background(), getNode, metav1.UpdateOptions{})
if err != nil {
log.Printf("failed to update nodestatus: %s with error: %v", nodeName, err)
os.Exit(1)
}
log.Printf("0 condition Status is %s", modifyNode.Status.Conditions[0].Status)
}
2022/09/08 17:18:39 0 condition Status is False, now change it to True
2022/09/08 17:18:39 0 condition Status is True
curl -u User:Paasword -k -H "Content-Type: application/json-patch+json" -X PATCH https://<host>:<port>/api/v1/nodes/<nodename>/status --data '[{ "op": "replace", "path": "/status/conditions/0/status", "value": "True"}]'
cred= echo "User:Password" | base64
curl -H "Authorization: Basic $cred" -k -H "Content-Type: application/json-patch+json" -X PATCH https://<host>:<port>/api/v1/nodes/<nodename>/status --data '[{ "op": "replace", "path": "/status/conditions/0/status", "value": "True"}]'
测试命令
测试结果
curl -H "Authorization: Bearer <ACCESS_TOKEN>" -k -H "Content-Type: application/json-patch+json" -X PATCH https://<host>:<port>/api/v1/nodes/<nodename>/status --data '[{ "op": "replace", "path": "/status/conditions/0/status", "value": "True"}]'
提前准备好证书秘钥写好文件
curl --cert <证书文件全路径> --key <秘钥文件全路径> -k -H "Content-Type: application/json-patch+json" -X PATCH https://<host>:<port>/api/v1/nodes/<nodename>/status --data '[{ "op": "replace", "path": "/status/conditions/0/status", "value": "True"}]'
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有