From 05be39862d3a4142a902a6747007a372dea20a83 Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Tue, 17 Oct 2023 11:48:26 -0600 Subject: [PATCH] node metrics --- pkg/handlers/helmvm_get.go | 2 +- pkg/helmvm/helmvm_nodes.go | 49 ++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/pkg/handlers/helmvm_get.go b/pkg/handlers/helmvm_get.go index cd440d116f..91133a03b6 100644 --- a/pkg/handlers/helmvm_get.go +++ b/pkg/handlers/helmvm_get.go @@ -16,7 +16,7 @@ func (h *Handler) GetHelmVMNodes(w http.ResponseWriter, r *http.Request) { return } - nodes, err := helmvm.GetNodes(client) + nodes, err := helmvm.GetNodes(r.Context(), client) if err != nil { logger.Error(err) w.WriteHeader(http.StatusInternalServerError) diff --git a/pkg/helmvm/helmvm_nodes.go b/pkg/helmvm/helmvm_nodes.go index e00dca2108..ccf570c9eb 100644 --- a/pkg/helmvm/helmvm_nodes.go +++ b/pkg/helmvm/helmvm_nodes.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "encoding/json" "fmt" + "github.com/replicatedhq/kots/pkg/k8sutil" "io" "math" "net/http" @@ -14,20 +15,30 @@ import ( "github.com/pkg/errors" "github.com/replicatedhq/kots/pkg/helmvm/types" - "github.com/replicatedhq/kots/pkg/logger" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" statsv1alpha1 "k8s.io/kubelet/pkg/apis/stats/v1alpha1" + metricsv "k8s.io/metrics/pkg/client/clientset/versioned" ) // GetNodes will get a list of nodes with stats -func GetNodes(client kubernetes.Interface) (*types.HelmVMNodes, error) { - nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) +func GetNodes(ctx context.Context, client kubernetes.Interface) (*types.HelmVMNodes, error) { + nodes, err := client.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) if err != nil { return nil, errors.Wrap(err, "list nodes") } + clientConfig, err := k8sutil.GetClusterConfig() + if err != nil { + return nil, errors.Wrap(err, "failed to get cluster config") + } + + metricsClient, err := metricsv.NewForConfig(clientConfig) + if err != nil { + return nil, errors.Wrap(err, "failed to create metrics client") + } + toReturn := types.HelmVMNodes{} for _, node := range nodes.Items { @@ -44,32 +55,24 @@ func GetNodes(client kubernetes.Interface) (*types.HelmVMNodes, error) { podCapacity.Capacity = float64(node.Status.Capacity.Pods().Value()) - nodeIP := "" - for _, address := range node.Status.Addresses { - if address.Type == corev1.NodeInternalIP { - nodeIP = address.Address - } + nodeMetrics, err := metricsClient.MetricsV1beta1().NodeMetricses().Get(ctx, node.Name, metav1.GetOptions{}) + if err != nil { + return nil, errors.Wrap(err, "list pod metrics") } - if nodeIP == "" { - logger.Infof("Did not find address for node %s, %+v", node.Name, node.Status.Addresses) - } else { - nodeMetrics, err := getNodeMetrics(nodeIP) - if err != nil { - logger.Infof("Got error retrieving stats for node %q: %v", node.Name, err) - } else { - if nodeMetrics.Node.Memory != nil && nodeMetrics.Node.Memory.AvailableBytes != nil { - memoryCapacity.Available = float64(*nodeMetrics.Node.Memory.AvailableBytes) / math.Pow(2, 30) - } + str, _ := json.Marshal(nodeMetrics) + fmt.Printf("node %s metrics: %s\n", node.Name, str) - if nodeMetrics.Node.CPU != nil && nodeMetrics.Node.CPU.UsageNanoCores != nil { - cpuCapacity.Available = cpuCapacity.Capacity - (float64(*nodeMetrics.Node.CPU.UsageNanoCores) / math.Pow(10, 9)) - } + if nodeMetrics.Usage.Memory() != nil { + memoryCapacity.Available = float64(nodeMetrics.Usage.Memory().Value()) / math.Pow(2, 30) + } - podCapacity.Available = podCapacity.Capacity - float64(len(nodeMetrics.Pods)) - } + if nodeMetrics.Usage.Cpu() != nil { + cpuCapacity.Available = cpuCapacity.Capacity - float64(nodeMetrics.Usage.Cpu().Value()) } + podCapacity.Available = podCapacity.Capacity - float64(nodeMetrics.Usage.Pods().Value()) + nodeLabelArray := []string{} for k, v := range node.Labels { nodeLabelArray = append(nodeLabelArray, fmt.Sprintf("%s:%s", k, v))