volcano.sh/volcano@v1.9.0/example/kubecon-2019-china/scripts/node-info.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	v1 "k8s.io/api/core/v1"
     8  
     9  	"volcano.sh/volcano/pkg/scheduler/api"
    10  
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/client-go/kubernetes"
    13  	"k8s.io/client-go/tools/clientcmd"
    14  )
    15  
    16  func main() {
    17  	config, err := clientcmd.BuildConfigFromFlags("", "C:/Users/m00483107/.kube/config")
    18  	if err != nil {
    19  		panic(err)
    20  	}
    21  	kubeClient := kubernetes.NewForConfigOrDie(config)
    22  	nodes, err := kubeClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  	pods, err := kubeClient.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
    27  	if err != nil {
    28  		panic(err)
    29  	}
    30  
    31  	fmt.Print("\n--------------------------------------------------------------------------------------------------------------\n")
    32  	fmt.Printf(" %-13s  |", " ")
    33  	for _, n := range nodes.Items {
    34  		fmt.Printf(" %-20s  |", n.Name)
    35  	}
    36  	fmt.Print("\n--------------------------------------------------------------------------------------------------------------\n")
    37  	fmt.Printf(" %-13s  |", "Alloctable")
    38  	for _, n := range nodes.Items {
    39  		res := fmt.Sprintf("cpu: %s", n.Status.Allocatable.Cpu())
    40  		fmt.Printf(" %-20s  |", res)
    41  	}
    42  	fmt.Println()
    43  	fmt.Printf(" %-13s  |", " ")
    44  	for _, n := range nodes.Items {
    45  		res := fmt.Sprintf("mem: %s", n.Status.Allocatable.Memory())
    46  		fmt.Printf(" %-20s  |", res)
    47  	}
    48  	fmt.Print("\n--------------------------------------------------------------------------------------------------------------\n")
    49  
    50  	podMap := map[string]*api.Resource{}
    51  
    52  	for _, p := range pods.Items {
    53  		nodeName := p.Spec.NodeName
    54  		// Only account running pods here.
    55  		if p.Status.Phase == v1.PodSucceeded || p.Status.Phase == v1.PodFailed {
    56  			continue
    57  		}
    58  		if len(nodeName) == 0 {
    59  			continue
    60  		}
    61  		if _, found := podMap[nodeName]; !found {
    62  			podMap[nodeName] = api.EmptyResource()
    63  		}
    64  		res := api.GetPodResourceWithoutInitContainers(&p)
    65  		podMap[nodeName].Add(res)
    66  	}
    67  
    68  	fmt.Printf(" %-13s  |", "Idle")
    69  	for _, n := range nodes.Items {
    70  		allocate := n.Status.Allocatable.DeepCopy()
    71  		c := allocate.Cpu()
    72  		if r, found := podMap[n.Name]; found {
    73  			cpu := c.MilliValue() - int64(r.MilliCPU)
    74  			c.SetMilli(cpu)
    75  		}
    76  
    77  		res := fmt.Sprintf("cpu: %s", c)
    78  		fmt.Printf(" %-20s  |", res)
    79  	}
    80  	fmt.Println()
    81  	fmt.Printf(" %-13s  |", " ")
    82  	for _, n := range nodes.Items {
    83  		allocate := n.Status.Allocatable.DeepCopy()
    84  		c := allocate.Memory()
    85  		if r, found := podMap[n.Name]; found {
    86  			cpu := c.Value() - int64(r.Memory)
    87  			c.Set(cpu)
    88  		}
    89  
    90  		res := fmt.Sprintf("mem: %s", c)
    91  		fmt.Printf(" %-20s  |", res)
    92  	}
    93  	fmt.Print("\n--------------------------------------------------------------------------------------------------------------\n")
    94  }