Skip to content

Commit

Permalink
Fix DetectAgentMode so auto resolves to onPrem if not ec2. (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Dec 14, 2022
1 parent 39372d5 commit 24420d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
18 changes: 10 additions & 8 deletions translator/util/sdkutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ const (
DEFAULT_PROFILE = "AmazonCloudWatchAgent"
)

var DetectRegion func(mode string, credsConfig map[string]string) string = detectRegion
var DetectCredentialsPath func() string = detectCredentialsPath
var DetectRegion = detectRegion
var DetectCredentialsPath = detectCredentialsPath
var DefaultEC2Region = defaultEC2Region
var DefaultECSRegion = defaultECSRegion
var runInAws = os.Getenv(config.RUN_IN_AWS)

func DetectAgentMode(configuredMode string) string {
Expand All @@ -36,17 +38,17 @@ func DetectAgentMode(configuredMode string) string {
return config.ModeEC2
}

if defaultEC2Region() != "" {
if DefaultEC2Region() != "" {
fmt.Println("I! Detected the instance is EC2")
return config.ModeEC2
}

if defaultECSRegion() != "" {
if DefaultECSRegion() != "" {
fmt.Println("I! Detected the instance is ECS")
return config.ModeEC2
}
fmt.Println("I! Detected the instance is OnPremise")
return configuredMode
return config.ModeOnPrem
}

func SDKRegionWithCredsMap(mode string, credsConfig map[string]string) (region string) {
Expand Down Expand Up @@ -93,15 +95,15 @@ func detectRegion(mode string, credsConfig map[string]string) (region string) {
// For ec2, fallback to metadata when no region info found in credential profile.
if region == "" && mode == config.ModeEC2 {
fmt.Println("I! Trying to detect region from ec2")
region = defaultEC2Region()
region = DefaultEC2Region()
}

// try to get region from ecs metadata
if region == "" && mode == config.ModeEC2 {
fmt.Println("I! Trying to detect region from ecs")
region = defaultECSRegion()
region = DefaultECSRegion()
}

return
}

Expand Down
31 changes: 31 additions & 0 deletions translator/util/sdkutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package util

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/aws/amazon-cloudwatch-agent/translator/config"
)

func TestDetectAgentModeAuto(t *testing.T) {
testCases := map[string]struct {
runInAws string
ec2Region string
ecsRegion string
wantMode string
}{
"WithRunInAWS": {runInAws: config.RUN_IN_AWS_TRUE, wantMode: config.ModeEC2},
"WithEC2Region": {ec2Region: "us-east-1", wantMode: config.ModeEC2},
"WithECSRegion": {ecsRegion: "us-east-1", wantMode: config.ModeEC2},
"WithNone": {wantMode: config.ModeOnPrem},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
runInAws = testCase.runInAws
DefaultEC2Region = func() string { return testCase.ec2Region }
DefaultECSRegion = func() string { return testCase.ecsRegion }
require.Equal(t, testCase.wantMode, DetectAgentMode("auto"))
})
}
}

0 comments on commit 24420d8

Please sign in to comment.