diff --git a/translator/translate/logs/util/get_eks_cluster_name.go b/translator/translate/logs/util/get_eks_cluster_name.go index 356892ad51..f5aa8539af 100644 --- a/translator/translate/logs/util/get_eks_cluster_name.go +++ b/translator/translate/logs/util/get_eks_cluster_name.go @@ -19,6 +19,7 @@ import ( const ( EKSClusterNameTagKeyPrefix = "kubernetes.io/cluster/" defaultRetryCount = 5 + defaultBackoffDuration = time.Duration(1 * time.Minute) ) var ( @@ -111,12 +112,15 @@ func callFuncWithRetries(fn func(input *ec2.DescribeTagsInput) (*ec2.DescribeTag // sleep some back off time before retries. func backoffSleep(i int) { - //save the sleep time for the last occurrence since it will exit the loop immediately after the sleep - backoffDuration := time.Duration(time.Minute * 1) - if i <= defaultRetryCount { - backoffDuration = sleeps[i] - } - + backoffDuration := getBackoffDuration(i) log.Printf("W! It is the %v time, going to sleep %v before retrying.", i, backoffDuration) time.Sleep(backoffDuration) } + +func getBackoffDuration(i int) time.Duration { + backoffDuration := defaultBackoffDuration + if i >= 0 && i < len(sleeps) { + backoffDuration = sleeps[i] + } + return backoffDuration +} diff --git a/translator/translate/logs/util/get_eks_cluster_name_test.go b/translator/translate/logs/util/get_eks_cluster_name_test.go new file mode 100644 index 0000000000..e3b8dd0d80 --- /dev/null +++ b/translator/translate/logs/util/get_eks_cluster_name_test.go @@ -0,0 +1,25 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT + +package util + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetBackoffDuration(t *testing.T) { + t.Parallel() + + duration := getBackoffDuration(-1) + assert.Equal(t, defaultBackoffDuration, duration) + + for i := range sleeps { + duration := getBackoffDuration(i) + assert.Equal(t, sleeps[i], duration) + } + + duration = getBackoffDuration(len(sleeps)) + assert.Equal(t, defaultBackoffDuration, duration) +}