Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the available RAM using GO API

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.

like image 332
Ashwin Avatar asked Mar 09 '18 11:03

Ashwin


1 Answers

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)
}
like image 138
Ashwin Avatar answered Nov 12 '22 21:11

Ashwin