Skip to content

Commit

Permalink
Fix potential out of bounds panic (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
dricross authored Aug 9, 2024
1 parent c3f1e5f commit 630e834
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
16 changes: 10 additions & 6 deletions translator/translate/logs/util/get_eks_cluster_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
const (
EKSClusterNameTagKeyPrefix = "kubernetes.io/cluster/"
defaultRetryCount = 5
defaultBackoffDuration = time.Duration(1 * time.Minute)
)

var (
Expand Down Expand Up @@ -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
}
25 changes: 25 additions & 0 deletions translator/translate/logs/util/get_eks_cluster_name_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 630e834

Please sign in to comment.