From 9fd1a2169369af81137de2f68da826194fb85897 Mon Sep 17 00:00:00 2001 From: Daman Arora Date: Thu, 13 Jun 2024 10:53:20 +0530 Subject: [PATCH] e2e/framework/metrics: handle index out of bounds panic Signed-off-by: Daman Arora --- test/e2e/framework/metrics/kube_proxy_metrics.go | 9 +++++++-- test/e2e/network/kube_proxy.go | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/test/e2e/framework/metrics/kube_proxy_metrics.go b/test/e2e/framework/metrics/kube_proxy_metrics.go index 66cdada9d62..6c415626c5a 100644 --- a/test/e2e/framework/metrics/kube_proxy_metrics.go +++ b/test/e2e/framework/metrics/kube_proxy_metrics.go @@ -17,6 +17,8 @@ limitations under the License. package metrics import ( + "fmt" + "k8s.io/component-base/metrics/testutil" ) @@ -24,8 +26,11 @@ import ( type KubeProxyMetrics testutil.Metrics // GetCounterMetricValue returns value for metric type counter. -func (m *KubeProxyMetrics) GetCounterMetricValue(metricName string) float64 { - return float64(testutil.Metrics(*m)[metricName][0].Value) +func (m *KubeProxyMetrics) GetCounterMetricValue(metricName string) (float64, error) { + if len(testutil.Metrics(*m)[metricName]) == 0 { + return 0, fmt.Errorf("metric '%s' not found", metricName) + } + return float64(testutil.Metrics(*m)[metricName][0].Value), nil } func newKubeProxyMetricsMetrics() KubeProxyMetrics { diff --git a/test/e2e/network/kube_proxy.go b/test/e2e/network/kube_proxy.go index 3435122d942..d15f9b44332 100644 --- a/test/e2e/network/kube_proxy.go +++ b/test/e2e/network/kube_proxy.go @@ -299,7 +299,8 @@ var _ = common.SIGDescribe("KubeProxy", func() { // get value of target metric before accessing localhost nodeports metrics, err := metricsGrabber.GrabFromKubeProxy(ctx, nodeName) framework.ExpectNoError(err) - targetMetricBefore := metrics.GetCounterMetricValue(metricName) + targetMetricBefore, err := metrics.GetCounterMetricValue(metricName) + framework.ExpectNoError(err) // create pod ginkgo.By("creating test pod") @@ -359,7 +360,8 @@ var _ = common.SIGDescribe("KubeProxy", func() { if err := wait.PollUntilContextTimeout(ctx, 10*time.Second, 2*time.Minute, true, func(_ context.Context) (bool, error) { metrics, err := metricsGrabber.GrabFromKubeProxy(ctx, nodeName) framework.ExpectNoError(err) - targetMetricAfter := metrics.GetCounterMetricValue(metricName) + targetMetricAfter, err := metrics.GetCounterMetricValue(metricName) + framework.ExpectNoError(err) return targetMetricAfter > targetMetricBefore, nil }); err != nil { framework.Failf("expected %s metric to be updated after accessing endpoints via localhost nodeports", metricName)