MCPcopy
hub / github.com/1Panel-dev/KubePi / getExtraClusterInfo

Function getExtraClusterInfo

internal/api/v1/cluster/cluster.go:389–448  ·  view source on GitHub ↗
(context goContext.Context, client kubernetes.Interface)

Source from the content-addressed store, hash-verified

387 }
388}
389func getExtraClusterInfo(context goContext.Context, client kubernetes.Interface) (ExtraClusterInfo, error) {
390 err := client.Ping()
391 if err != nil {
392 return ExtraClusterInfo{Health: false, Message: err.Error()}, err
393 }
394 c, err := client.Client()
395 if err != nil {
396 return ExtraClusterInfo{Health: false, Message: err.Error()}, err
397 }
398 nodesList, err := c.CoreV1().Nodes().List(context, metav1.ListOptions{})
399 if err != nil {
400 return ExtraClusterInfo{Health: true, Message: err.Error()}, err
401 }
402 nodes := nodesList.Items
403
404 totalCpu := float64(0)
405 totalMemory := float64(0)
406 usedCpu := float64(0)
407 usedMemory := float64(0)
408 readyNodes := 0
409 for i := range nodes {
410 conditions := nodes[i].Status.Conditions
411 for i := range conditions {
412 if conditions[i].Type == "Ready" {
413 if conditions[i].Status == "True" {
414 readyNodes += 1
415 }
416 }
417 }
418 cpu := nodes[i].Status.Allocatable.Cpu().AsApproximateFloat64()
419 totalCpu += cpu
420 memory := nodes[i].Status.Allocatable.Memory().AsApproximateFloat64()
421 totalMemory += memory
422 }
423 podsList, err := c.CoreV1().Pods("").List(goContext.TODO(), metav1.ListOptions{})
424 if err != nil {
425 return ExtraClusterInfo{Health: true, Message: err.Error()}, err
426 }
427 pods := podsList.Items
428 for i := range pods {
429 for j := range pods[i].Spec.Containers {
430 cpu := pods[i].Spec.Containers[j].Resources.Requests.Cpu().AsApproximateFloat64()
431 usedCpu += cpu
432 memory := pods[i].Spec.Containers[j].Resources.Requests.Memory().AsApproximateFloat64()
433 usedMemory += memory
434
435 }
436 }
437 result := ExtraClusterInfo{
438 Health: true,
439 TotalNodeNum: len(nodes),
440 ReadyNodeNum: readyNodes,
441 CPUAllocatable: totalCpu,
442 CPURequested: usedCpu,
443 MemoryAllocatable: totalMemory,
444 MemoryRequested: usedMemory,
445 }
446 return result, nil

Callers 1

SearchClustersMethod · 0.85

Calls 5

ErrorMethod · 0.80
MemoryMethod · 0.80
PingMethod · 0.65
ClientMethod · 0.65
ListMethod · 0.65

Tested by

no test coverage detected