使用GO API查找可用的RAM [英] Find the available RAM using GO API
本文介绍了使用GO API查找可用的RAM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Minikube环境,并且已经使用定义了最大内存
I am using the Minikube environment and I have defined the max memory using
$] minikube config set memory 2048
现在,我想使用GO中的Kubernetes API调用获取此内存值. 我尝试了以下方法,
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")
但是输出不是可读的.
推荐答案
此代码将获取第一个集群的可用内存
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)
}
这篇关于使用GO API查找可用的RAM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文