Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential out of bounds panic #1266

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading