From a99645832425a8872413924d24c586dc814ef147 Mon Sep 17 00:00:00 2001 From: lucming <2876757716@qq.com> Date: Mon, 24 Jul 2023 18:59:20 +0800 Subject: [PATCH] aggregate errors --- pkg/descheduler/node/node.go | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/pkg/descheduler/node/node.go b/pkg/descheduler/node/node.go index afa8fc1b88..a7889571e8 100644 --- a/pkg/descheduler/node/node.go +++ b/pkg/descheduler/node/node.go @@ -24,6 +24,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" + kerr "k8s.io/apimachinery/pkg/util/errors" clientset "k8s.io/client-go/kubernetes" listersv1 "k8s.io/client-go/listers/core/v1" "k8s.io/klog/v2" @@ -120,8 +121,7 @@ func NodeFit(nodeIndexer podutil.GetPodsAssignedToNodeFunc, pod *v1.Pod, node *v } // Check if the pod can fit on a node based off it's requests if pod.Spec.NodeName == "" || pod.Spec.NodeName != node.Name { - ok, reqErrors := fitsRequest(nodeIndexer, pod, node) - if !ok { + if ok, reqErrors := fitsRequest(nodeIndexer, pod, node); !ok { errors = append(errors, reqErrors...) } } @@ -146,13 +146,11 @@ func PodFitsAnyOtherNode(nodeIndexer podutil.GetPodsAssignedToNodeFunc, pod *v1. if len(errors) == 0 { klog.V(4).InfoS("Pod fits on node", "pod", klog.KObj(pod), "node", klog.KObj(node)) return true - } else { - klog.V(4).InfoS("Pod does not fit on node", "pod", klog.KObj(pod), "node", klog.KObj(node)) - for _, err := range errors { - klog.V(4).InfoS(err.Error()) - } } + klog.V(4).InfoS("Pod does not fit on node", + "pod:", klog.KObj(pod), "node:", klog.KObj(node), "error:", kerr.NewAggregate(errors).Error()) } + return false } @@ -164,13 +162,11 @@ func PodFitsAnyNode(nodeIndexer podutil.GetPodsAssignedToNodeFunc, pod *v1.Pod, if len(errors) == 0 { klog.V(4).InfoS("Pod fits on node", "pod", klog.KObj(pod), "node", klog.KObj(node)) return true - } else { - klog.V(4).InfoS("Pod does not fit on node", "pod", klog.KObj(pod), "node", klog.KObj(node)) - for _, err := range errors { - klog.V(4).InfoS(err.Error()) - } } + klog.V(4).InfoS("Pod does not fit on node", + "pod:", klog.KObj(pod), "node:", klog.KObj(node), "error:", kerr.NewAggregate(errors).Error()) } + return false } @@ -181,12 +177,11 @@ func PodFitsCurrentNode(nodeIndexer podutil.GetPodsAssignedToNodeFunc, pod *v1.P if len(errors) == 0 { klog.V(4).InfoS("Pod fits on node", "pod", klog.KObj(pod), "node", klog.KObj(node)) return true - } else { - klog.V(4).InfoS("Pod does not fit on node", "pod", klog.KObj(pod), "node", klog.KObj(node)) - for _, err := range errors { - klog.V(4).InfoS(err.Error()) - } } + + klog.V(4).InfoS("Pod does not fit on node", + "pod:", klog.KObj(pod), "node:", klog.KObj(node), "error:", kerr.NewAggregate(errors).Error()) + return false }