Replace resources api to return the max allocatable memory (#264)
This commit is contained in:
@@ -18,12 +18,13 @@ package restapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"sort"
|
||||
|
||||
"github.com/minio/console/cluster"
|
||||
|
||||
"errors"
|
||||
|
||||
"github.com/go-openapi/runtime/middleware"
|
||||
"github.com/go-openapi/swag"
|
||||
"github.com/minio/console/models"
|
||||
@@ -35,73 +36,94 @@ import (
|
||||
)
|
||||
|
||||
func registerNodesHandlers(api *operations.ConsoleAPI) {
|
||||
api.AdminAPIGetClusterResourcesHandler = admin_api.GetClusterResourcesHandlerFunc(func(params admin_api.GetClusterResourcesParams, session *models.Principal) middleware.Responder {
|
||||
resp, err := getClusterResourcesResponse(session)
|
||||
api.AdminAPIGetMaxAllocatableMemHandler = admin_api.GetMaxAllocatableMemHandlerFunc(func(params admin_api.GetMaxAllocatableMemParams, principal *models.Principal) middleware.Responder {
|
||||
resp, err := getMaxAllocatableMemoryResponse(principal, params.NumNodes)
|
||||
if err != nil {
|
||||
return admin_api.NewGetClusterResourcesDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())})
|
||||
return admin_api.NewGetMaxAllocatableMemDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())})
|
||||
}
|
||||
return admin_api.NewGetClusterResourcesOK().WithPayload(resp)
|
||||
return admin_api.NewGetMaxAllocatableMemOK().WithPayload(resp)
|
||||
})
|
||||
}
|
||||
|
||||
// getClusterResources get cluster nodes and collects taints, available and allocatable resources of the node
|
||||
func getClusterResources(ctx context.Context, clientset v1.CoreV1Interface) (*models.ClusterResources, error) {
|
||||
// getMaxAllocatableMemory get max allocatable memory given a desired number of nodes
|
||||
func getMaxAllocatableMemory(ctx context.Context, clientset v1.CoreV1Interface, numNodes int32) (*models.MaxAllocatableMemResponse, error) {
|
||||
if numNodes == 0 {
|
||||
return nil, errors.New("error NumNodes must be greated than 0")
|
||||
}
|
||||
|
||||
// get all nodes from cluster
|
||||
nodes, err := clientset.Nodes().List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// construct ClusterResources response
|
||||
res := &models.ClusterResources{}
|
||||
|
||||
availableMemSizes := []int64{}
|
||||
OUTER:
|
||||
for _, n := range nodes.Items {
|
||||
// Get Total Resources
|
||||
totalResources := make(map[string]int64)
|
||||
for resource, quantity := range n.Status.Capacity {
|
||||
totalResources[string(resource)] = quantity.Value()
|
||||
}
|
||||
|
||||
// Get Allocatable Resources
|
||||
allocatableResources := make(map[string]int64)
|
||||
for resource, quantity := range n.Status.Allocatable {
|
||||
allocatableResources[string(resource)] = quantity.Value()
|
||||
}
|
||||
|
||||
// Get Node taints and split them by effect
|
||||
taints := &models.NodeTaints{}
|
||||
// Don't consider node if it has a NoSchedule or NoExecute Taint
|
||||
for _, t := range n.Spec.Taints {
|
||||
var taint string
|
||||
// when value is not defined the taint string is created without `=`
|
||||
if strings.TrimSpace(t.Value) != "" {
|
||||
taint = fmt.Sprintf("%s=%s:%s", t.Key, t.Value, t.Effect)
|
||||
} else {
|
||||
taint = fmt.Sprintf("%s:%s", t.Key, t.Effect)
|
||||
}
|
||||
switch t.Effect {
|
||||
case corev1.TaintEffectNoSchedule:
|
||||
taints.NoSchedule = append(taints.NoSchedule, taint)
|
||||
continue OUTER
|
||||
case corev1.TaintEffectNoExecute:
|
||||
taints.NoExecute = append(taints.NoExecute, taint)
|
||||
case corev1.TaintEffectPreferNoSchedule:
|
||||
taints.PreferNoSchedule = append(taints.PreferNoSchedule, taint)
|
||||
continue OUTER
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// create node object an add it to the nodes list
|
||||
nodeInfo := &models.NodeInfo{
|
||||
Name: n.Name,
|
||||
Taints: taints,
|
||||
AllocatableResources: allocatableResources,
|
||||
TotalResources: totalResources,
|
||||
if quantity, ok := n.Status.Allocatable[corev1.ResourceMemory]; ok {
|
||||
availableMemSizes = append(availableMemSizes, quantity.Value())
|
||||
}
|
||||
res.Nodes = append(res.Nodes, nodeInfo)
|
||||
}
|
||||
|
||||
maxAllocatableMemory := getMaxClusterMemory(numNodes, availableMemSizes)
|
||||
|
||||
res := &models.MaxAllocatableMemResponse{
|
||||
MaxMemory: maxAllocatableMemory,
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func getClusterResourcesResponse(session *models.Principal) (*models.ClusterResources, error) {
|
||||
// getMaxClusterMemory returns the maximum memory size that can be used
|
||||
// across numNodes (number of nodes)
|
||||
func getMaxClusterMemory(numNodes int32, nodesMemorySizes []int64) int64 {
|
||||
if int32(len(nodesMemorySizes)) < numNodes || numNodes == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// sort nodesMemorySizes int64 array
|
||||
sort.Slice(nodesMemorySizes, func(i, j int) bool { return nodesMemorySizes[i] < nodesMemorySizes[j] })
|
||||
maxIndex := 0
|
||||
maxAllocatableMemory := nodesMemorySizes[maxIndex]
|
||||
|
||||
for i, size := range nodesMemorySizes {
|
||||
// maxAllocatableMemory is the minimum value of nodesMemorySizes array
|
||||
// only within the size of numNodes, if more nodes are available
|
||||
// then the maxAllocatableMemory is equal to the next minimum value
|
||||
// on the sorted nodesMemorySizes array.
|
||||
// e.g. with numNodes = 4;
|
||||
// maxAllocatableMemory of [2,4,8,8] => 2
|
||||
// maxAllocatableMemory of [2,4,8,8,16] => 4
|
||||
if int32(i) < numNodes {
|
||||
maxAllocatableMemory = min(maxAllocatableMemory, size)
|
||||
} else {
|
||||
maxIndex++
|
||||
maxAllocatableMemory = nodesMemorySizes[maxIndex]
|
||||
}
|
||||
}
|
||||
return maxAllocatableMemory
|
||||
}
|
||||
|
||||
// min returns the smaller of x or y.
|
||||
func min(x, y int64) int64 {
|
||||
if x > y {
|
||||
return y
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func getMaxAllocatableMemoryResponse(session *models.Principal, numNodes int32) (*models.MaxAllocatableMemResponse, error) {
|
||||
ctx := context.Background()
|
||||
client, err := cluster.K8sClient(session.SessionToken)
|
||||
if err != nil {
|
||||
@@ -109,7 +131,7 @@ func getClusterResourcesResponse(session *models.Principal) (*models.ClusterReso
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clusterResources, err := getClusterResources(ctx, client.CoreV1())
|
||||
clusterResources, err := getMaxAllocatableMemory(ctx, client.CoreV1(), numNodes)
|
||||
if err != nil {
|
||||
log.Println("error getting cluster's resources:", err)
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user