Search code examples
gokuberneteskubernetes-go-client

How to get the status of Kubernetes nodes via the client?


I want to get the list of the nodes and their status in Kubernetes via the go client. I am using clientset.CoreV1().Nodes().List(metav1.ListOptions{}) and I am able to get the list of nodes and some information such as node lables but I cannot find the status.phase there (for pods that give me the status).

I searched and I found NodeCondition (https://github.com/kubernetes/api/blob/9b64426eca51a74faa7cc9bd732a533d339c69c2/core/v1/types.go#L4911) but I cannot find any documentation regarding how to use it and I couldn't make it work myself. Can someone please let me know how I can get the status of Nodes via the client?


Solution

  • You can access it through node.Status.Condition

    nodes, _ := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
    
    for _, node := range nodes.Items {
        fmt.Printf("%s\n", node.Name)
        for _, condition := range node.Status.Conditions {
            fmt.Printf("\t%s: %s\n", condition.Type, condition.Status)
        }
    }
    

    Prints:

    dev-master-01
            NetworkUnavailable: False
            MemoryPressure: False
            DiskPressure: False
            PIDPressure: False
            Ready: True
    dev-master-02
            NetworkUnavailable: False
            MemoryPressure: False
            DiskPressure: False
            PIDPressure: False
            Ready: True
    dev-master-03
            NetworkUnavailable: False
            MemoryPressure: False
            DiskPressure: False
            PIDPressure: False
            Ready: True