获取指定 Node 详细信息

package main

import (
	"context"
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

func main() {
	configPath := "etc/bfl.conf"
	config, err := clientcmd.BuildConfigFromFlags("", configPath)
	if err != nil {
		fmt.Println(err)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		fmt.Println(err)
	}

	node, err := clientset.CoreV1().Nodes().Get(context.Background(), "kube-node1", metav1.GetOptions{})
	if err != nil {
		fmt.Println(err)
	}
	// 获取指定 Node 详细信息

	// 获取节点名字
	fmt.Printf("Name: %s\n", node.Name)
	// 获取节点 状态
	fmt.Printf("STATUS: %s\n", node.Status.Conditions[len(node.Status.Conditions)-1].Type)
	// 创建时间
	fmt.Printf("CreateTime: %s\n", node.CreationTimestamp)
	//Kubelet 版本
	fmt.Printf("KubeletVersion: %s\n", node.Status.NodeInfo.KubeletVersion)
	// Kernel 版本
	fmt.Printf("KernelVersion: %s\n", node.Status.NodeInfo.KernelVersion)
	// OS-IMAGE
	fmt.Printf("OS-IMAGE: %s\n", node.Status.NodeInfo.OSImage)
	//CONTAINER-RUNTIME
	fmt.Printf("ContainerRuntimeVersion: %s\n", node.Status.NodeInfo.ContainerRuntimeVersion)
	// InternalIP
	fmt.Printf("InternalIP: %s\n", node.Status.Addresses[0].Address)
	//CPU
	fmt.Println(node.Status.Capacity.Cpu())
	//Mem
	fmt.Println(node.Status.Capacity.Memory().String())
}

更新中…