e2e/framework/metrics: handle index out of bounds panic
Signed-off-by: Daman Arora <aroradaman@gmail.com>
This commit is contained in:
		| @@ -17,6 +17,8 @@ limitations under the License. | |||||||
| package metrics | package metrics | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"fmt" | ||||||
|  |  | ||||||
| 	"k8s.io/component-base/metrics/testutil" | 	"k8s.io/component-base/metrics/testutil" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -24,8 +26,11 @@ import ( | |||||||
| type KubeProxyMetrics testutil.Metrics | type KubeProxyMetrics testutil.Metrics | ||||||
|  |  | ||||||
| // GetCounterMetricValue returns value for metric type counter. | // GetCounterMetricValue returns value for metric type counter. | ||||||
| func (m *KubeProxyMetrics) GetCounterMetricValue(metricName string) float64 { | func (m *KubeProxyMetrics) GetCounterMetricValue(metricName string) (float64, error) { | ||||||
| 	return float64(testutil.Metrics(*m)[metricName][0].Value) | 	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 { | func newKubeProxyMetricsMetrics() KubeProxyMetrics { | ||||||
|   | |||||||
| @@ -299,7 +299,8 @@ var _ = common.SIGDescribe("KubeProxy", func() { | |||||||
| 		// get value of target metric before accessing localhost nodeports | 		// get value of target metric before accessing localhost nodeports | ||||||
| 		metrics, err := metricsGrabber.GrabFromKubeProxy(ctx, nodeName) | 		metrics, err := metricsGrabber.GrabFromKubeProxy(ctx, nodeName) | ||||||
| 		framework.ExpectNoError(err) | 		framework.ExpectNoError(err) | ||||||
| 		targetMetricBefore := metrics.GetCounterMetricValue(metricName) | 		targetMetricBefore, err := metrics.GetCounterMetricValue(metricName) | ||||||
|  | 		framework.ExpectNoError(err) | ||||||
|  |  | ||||||
| 		// create pod | 		// create pod | ||||||
| 		ginkgo.By("creating test 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) { | 		if err := wait.PollUntilContextTimeout(ctx, 10*time.Second, 2*time.Minute, true, func(_ context.Context) (bool, error) { | ||||||
| 			metrics, err := metricsGrabber.GrabFromKubeProxy(ctx, nodeName) | 			metrics, err := metricsGrabber.GrabFromKubeProxy(ctx, nodeName) | ||||||
| 			framework.ExpectNoError(err) | 			framework.ExpectNoError(err) | ||||||
| 			targetMetricAfter := metrics.GetCounterMetricValue(metricName) | 			targetMetricAfter, err := metrics.GetCounterMetricValue(metricName) | ||||||
|  | 			framework.ExpectNoError(err) | ||||||
| 			return targetMetricAfter > targetMetricBefore, nil | 			return targetMetricAfter > targetMetricBefore, nil | ||||||
| 		}); err != nil { | 		}); err != nil { | ||||||
| 			framework.Failf("expected %s metric to be updated after accessing endpoints via localhost nodeports", metricName) | 			framework.Failf("expected %s metric to be updated after accessing endpoints via localhost nodeports", metricName) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Daman Arora
					Daman Arora