From 5145c61c8fb14aad43f2c7841e08e29e2a8ff68c Mon Sep 17 00:00:00 2001 From: zyy17 Date: Tue, 3 Dec 2024 19:00:48 +0800 Subject: [PATCH] refactor(apiserver): don't need to fail the request when querying podmetrics failed Signed-off-by: zyy17 --- pkg/apiserver/apiserver.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 11b28af..713face 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -22,6 +22,7 @@ import ( "github.com/gin-gonic/gin" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog/v2" podmetricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1" @@ -327,14 +328,11 @@ func (s *Server) getPods(ctx context.Context, namespace, name string, kind grept for _, pod := range internalPods.Items { podMetrics, err := s.getPodMetrics(ctx, namespace, pod.Name) if err != nil { - return err + // Logs the error and continue to get the next pod metrics. We don't want the error of getting podmetrics to block the whole API. + klog.Errorf("failed to get pod metrics for pod %s/%s: %v", namespace, pod.Name, err) } metrics = append(metrics, podMetrics) } - - if len(metrics) != len(internalPods.Items) { - return fmt.Errorf("the number of metrics is not equal to the number of pods") - } } var pods []*Pod @@ -398,7 +396,14 @@ func (s *Server) buildPod(internalPod *corev1.Pod, podMetrics *podmetricsv1beta1 func (s *Server) getPodMetrics(ctx context.Context, namespace, name string) (*podmetricsv1beta1.PodMetrics, error) { var podMetrics podmetricsv1beta1.PodMetrics - if err := s.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, &podMetrics); err != nil { + err := s.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, &podMetrics) + + // It's possible when the pod just started, the metrics-server hasn't collected the metrics yet. + if apierrors.IsNotFound(err) { + return nil, nil + } + + if err != nil { return nil, err } return &podMetrics, nil