【k8s】curl命令修改node status里的conditions字段

2022-09-08 21:32:23 浏览数 (1)

简介

使用kubectl edit命令可以更新node字段,但是无法更新nodestatus字段。本文实践几种修改k8s节点status里面的字段的方法,比如修改conditions里面的字段。

实践

nodestatus的API

  • k8s nodestatus的API参考官网:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#patch-status-node-v1-core。
  • API接口支持PATCH,GET,PUT三种方法

基于client-go编码修改

可以写代码使用client-go中的nodestatus更新来更新节点的status字段。支持patchStatus和updateStatus

  • 测试代码如下,测试样例是把第0个condition的Status字段由False改为True。
代码语言:javascript复制
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)
}

  • 测试结果如下。表示condition修改成功
代码语言:javascript复制
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命令修改

  • 用户名密码
代码语言:javascript复制
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"}]'

测试命令

测试结果

  • token
代码语言:javascript复制
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"}]'

  • 证书秘钥
代码语言:javascript复制
提前准备好证书秘钥写好文件
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"}]'

0 人点赞