Find the available RAM using GO API

时光怂恿深爱的人放手 提交于 2019-12-07 02:42:48

问题


I am using the Minikube environment and I have defined the max memory using

$] minikube config set memory 2048

Now I want to get this memory value using the Kubernetes API call in GO. I have tried the following,

import (
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/rest"
)

availableMem := kubernetes.Interface.StorageV1beta1().RESTClient().Get().Name("config")

But the output is not in readable manner.


回答1:


This code will fetch the available memory of the first cluster

nodeList, err := f.KubeClient.CoreV1().Nodes().List(metav1.ListOptions{})

if err == nil {
   if len(nodeList.Items) > 0 {
        node := &nodeList.Items[0]
        memQuantity := node.Status.Allocatable[v1.ResourceMemory]
        totalMemAvail = int(memQuantity.Value() >> 20)
    } else {
        t.Fatal("Unable to read node list")
        return
    }
} else {
    t.Fatalf("Error while reading node list data: %v", err)
}


来源:https://stackoverflow.com/questions/49192508/find-the-available-ram-using-go-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!