kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.96k stars 2.93k forks source link

Nodes CPU and Memory #1068

Closed rodrigoscferraz closed 2 years ago

rodrigoscferraz commented 2 years ago

Hello folks, Basically the same question as here #667

How can i get the :

I can get this information through the kubectl describe node but what about client-go?

rodrigoscferraz commented 2 years ago

I reallized that we can use

nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) to get this info

forbearing commented 2 years ago

const (
    // LabelNodeRolePrefix is a label prefix for node roles
    // It's copied over to here until it's merged in core: https://github.com/kubernetes/kubernetes/pull/39112
    LabelNodeRolePrefix = "node-role.kubernetes.io/"

    // LabelNodeRole specifies the role of a node
    LabelNodeRole = "kubernetes.io/role"

    //LabelNodeRoleMaster       = "kubernetes.io/role=master"
    //LabelNodeRoleControlPlane = "kubernetes.io/role=control-plane"
    //LabelNodeRoleWorker       = "!kubernetes.io/role=master"

    NodeRoleMaster       = "master"
    NodeRoleControlPlane = "control-plane"
)

type NodeInfo struct {
    Hostname           string
    IPAddress          string
    AllocatableCpu     string
    AllocatableMemory  string
    AllocatableStorage string
    TotalCpu           string
    TotalMemory        string
    TotalStorage       string

    Architecture            string
    BootID                  string
    ContainerRuntimeVersion string
    KernelVersion           string
    KubeProxyVersion        string
    KubeletVersion          string
    MachineID               string
    OperatingSystem         string
    OSImage                 string
    SystemUUID              string
}

// get all master node info
func (n *Node) GetMasterInfo() ([]NodeInfo, error) {
    var nodeInfo NodeInfo
    var nodeInfoList []NodeInfo

    masterNodes, err := n.List(LabelNodeRolePrefix + "master")
    if err != nil {
        return nil, err
    }
    for _, node := range masterNodes.Items {
        nodeInfo.Hostname = node.ObjectMeta.Name
        nodeInfo.IPAddress, _ = n.GetIP(nodeInfo.Hostname)
        nodeInfo.AllocatableCpu = node.Status.Allocatable.Cpu().String()
        nodeInfo.AllocatableMemory = node.Status.Allocatable.Memory().String()
        nodeInfo.AllocatableStorage = node.Status.Allocatable.StorageEphemeral().String()
        nodeInfo.Architecture = node.Status.NodeInfo.Architecture
        nodeInfo.TotalCpu = node.Status.Capacity.Cpu().String()
        nodeInfo.TotalMemory = node.Status.Capacity.Memory().String()
        nodeInfo.TotalStorage = node.Status.Capacity.StorageEphemeral().String()
        nodeInfo.BootID = node.Status.NodeInfo.BootID
        nodeInfo.ContainerRuntimeVersion = node.Status.NodeInfo.ContainerRuntimeVersion
        nodeInfo.KernelVersion = node.Status.NodeInfo.KernelVersion
        nodeInfo.KubeProxyVersion = node.Status.NodeInfo.KubeProxyVersion
        nodeInfo.KubeletVersion = node.Status.NodeInfo.KubeletVersion
        nodeInfo.MachineID = node.Status.NodeInfo.MachineID
        nodeInfo.OperatingSystem = node.Status.NodeInfo.OperatingSystem
        nodeInfo.OSImage = node.Status.NodeInfo.OSImage
        nodeInfo.SystemUUID = node.Status.NodeInfo.SystemUUID
        nodeInfoList = append(nodeInfoList, nodeInfo)
    }

    return nodeInfoList, nil
}

// list node by label
func (n *Node) List(labelSelector string) (*corev1.NodeList, error) {
    n.Options.ListOptions.LabelSelector = labelSelector
    return n.clientset.CoreV1().Nodes().List(n.ctx, n.Options.ListOptions)
}
`