Skip to content

Commit 172e504

Browse files
committed
- fix double "/" in endpoint URL leading to 404 errors on some metrics API (was not visible in E2E tests as server ran by ghcr.io/kedacore/tests-metrics-api seems to accomodate it anyway)
- metrics_api_test.go : fix getUpdateUrlsForAllMetricAllMetricsServerReplicas() to retry more transient failure cases & fail test after 5 failed retries Signed-off-by: julian GUINARD <[email protected]>
1 parent 67a3eb4 commit 172e504

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

pkg/scalers/metrics_api_scaler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ func (s *metricsAPIScaler) getEndpointsUrlsFromServiceURL(ctx context.Context, s
500500
}
501501
for _, address := range subset.Addresses {
502502
if address.NodeName != nil {
503-
endpointUrls = append(endpointUrls, fmt.Sprintf("%s://%s%s/%s", url.Scheme, address.IP, foundPort, url.Path))
503+
endpointUrls = append(endpointUrls, fmt.Sprintf("%s://%s%s%s", url.Scheme, address.IP, foundPort, url.Path))
504504
}
505505
}
506506
}

tests/scalers/metrics_api/metrics_api_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ func getSplitArrayFromAverage(t *testing.T, average, splitInNValues int) []int {
264264
return result
265265
}
266266

267-
func getUpdateUrlsForAllMetricAllMetricsServerReplicas(t *testing.T, kc *kubernetes.Clientset, expectedAverageMetric int, nbReplicasForMetricsServer int) map[string]string {
267+
func getUpdateUrlsForAllMetricAllMetricsServerReplicas(t *testing.T, kc *kubernetes.Clientset, expectedAverageMetric int, nbReplicasForMetricsServer int, nbRetry int) map[string]string {
268+
nbRetriesMax := 5
268269
// get an array for which all elements' average would give expectedAverageMetric without any of its elements being exactly expectedAverageMetric
269270
individualMetrics := getSplitArrayFromAverage(t, expectedAverageMetric, nbReplicasForMetricsServer)
270271
// use kc to curl all metrics-server replicas
@@ -277,22 +278,27 @@ func getUpdateUrlsForAllMetricAllMetricsServerReplicas(t *testing.T, kc *kuberne
277278
return nil
278279
}
279280

281+
retryFunc := func(message string) map[string]string {
282+
if nbRetry >= nbRetriesMax {
283+
t.Fatal(message)
284+
return nil
285+
}
286+
t.Logf("%s. Retry calling getUpdateUrlsForAllMetricAllMetricsServerReplicas() after 1 second", message)
287+
time.Sleep(1 * time.Second)
288+
return getUpdateUrlsForAllMetricAllMetricsServerReplicas(t, kc, expectedAverageMetric, nbReplicasForMetricsServer, nbRetry+1)
289+
}
280290
if len(pods.Items) == 0 {
281-
t.Fatalf("No pods found with the given selector.")
282-
return nil
291+
return retryFunc(fmt.Sprintf("No pods found with the given selector."))
283292
}
284293

285294
if len(pods.Items) != nbReplicasForMetricsServer {
286-
t.Fatalf("Number of replicas of metrics server (%d) does not match expected value (%d).", len(pods.Items), nbReplicasForMetricsServer)
287-
return nil
295+
return retryFunc(fmt.Sprintf("Number of replicas of metrics server (%d) does not match expected value (%d).", len(pods.Items), nbReplicasForMetricsServer))
288296
}
289297
postUrls := make(map[string]string, nbReplicasForMetricsServer)
290298
// Iterate through the pods and send HTTP requests
291299
for nbPod, pod := range pods.Items {
292300
if pod.Status.Phase != v1.PodRunning || pod.Status.PodIP == "" {
293-
t.Logf("Pod %s was expected to be running and to have an IP. Retry calling getUpdateUrlsForAllMetricAllMetricsServerReplicas() after 1 second", pod.Name)
294-
time.Sleep(1 * time.Second)
295-
return getUpdateUrlsForAllMetricAllMetricsServerReplicas(t, kc, expectedAverageMetric, nbReplicasForMetricsServer)
301+
return retryFunc(fmt.Sprintf("Pod %s was expected to be running and to have an IP.", pod.Name))
296302
}
297303
url := fmt.Sprintf("http://%s:8080/api/value/%d", pod.Status.PodIP, individualMetrics[nbPod])
298304
postUrls[pod.Name] = url
@@ -301,7 +307,7 @@ func getUpdateUrlsForAllMetricAllMetricsServerReplicas(t *testing.T, kc *kuberne
301307
}
302308

303309
func updateAllMetricsServerReplicas(t *testing.T, kc *kubernetes.Clientset, data templateData, metricValue int, nbReplicasForMetricsServer int) {
304-
for targetPodName, urlToPost := range getUpdateUrlsForAllMetricAllMetricsServerReplicas(t, kc, metricValue, nbReplicasForMetricsServer) {
310+
for targetPodName, urlToPost := range getUpdateUrlsForAllMetricAllMetricsServerReplicas(t, kc, metricValue, nbReplicasForMetricsServer, 0) {
305311
if urlToPost == "" {
306312
t.Fatalf("target pod %s should have non emoty url but got one", targetPodName)
307313
return

0 commit comments

Comments
 (0)