Skip to content
Open
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
1 change: 1 addition & 0 deletions cfg/envconfig/envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
AWS_USE_DUALSTACK_ENDPOINT = "AWS_USE_DUALSTACK_ENDPOINT" //nolint:revive
CWAGENT_USER_AGENT = "CWAGENT_USER_AGENT" //nolint:revive
CWAGENT_LOG_LEVEL = "CWAGENT_LOG_LEVEL" //nolint:revive
CWAGENT_MODE = "CWAGENT_MODE" //nolint:revive
CWAGENT_ROLE = "CWAGENT_ROLE" //nolint:revive
CWAGENT_USAGE_DATA = "CWAGENT_USAGE_DATA" //nolint:revive
IMDS_NUMBER_RETRY = "IMDS_NUMBER_RETRY" //nolint:revive
Expand Down
11 changes: 10 additions & 1 deletion translator/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"

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

Expand Down Expand Up @@ -90,7 +91,15 @@ func (ctx *Context) SetOutputTomlFilePath(outputTomlFilePath string) {

func (ctx *Context) Mode() string {
if ctx.mode == "" {
ctx.mode = config.ModeEC2
// Try to read mode from environment variable first (set by env-config.json)
// This is used when the agent starts up and reads the persisted mode
envMode := os.Getenv(envconfig.CWAGENT_MODE)
if envMode != "" {
return envMode
}
// Default to EC2 mode if not set
// Note: We return directly without caching to allow SetMode() to override later
return config.ModeEC2
}
return ctx.mode
}
Expand Down
17 changes: 16 additions & 1 deletion translator/tocwconfig/tocwconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestBaseContainerInsightsConfig(t *testing.T) {
t.Setenv(envconfig.AWS_CA_BUNDLE, "/etc/test/ca_bundle.pem")
expectedEnvVars := map[string]string{
"AWS_CA_BUNDLE": "/etc/test/ca_bundle.pem",
"CWAGENT_MODE": "ec2",
}
checkTranslation(t, "base_container_insights_config", "linux", expectedEnvVars, "")
checkTranslation(t, "base_container_insights_config", "darwin", nil, "")
Expand Down Expand Up @@ -1039,7 +1040,21 @@ func checkIfEnvTranslateSucceed(t *testing.T, jsonStr string, targetOs string, e
var actualEnvVars = make(map[string]string)
err := json.Unmarshal(envVarsBytes, &actualEnvVars)
assert.NoError(t, err)
assert.Equal(t, expectedEnvVars, actualEnvVars, "Expect to be equal")

// Automatically add the current mode to expected values since it's now always persisted
expectedWithMode := make(map[string]string)
for k, v := range expectedEnvVars {
expectedWithMode[k] = v
}
// Add the mode from context if not already specified in expected values
if _, exists := expectedWithMode[envconfig.CWAGENT_MODE]; !exists {
currentMode := context.CurrentContext().Mode()
if currentMode != "" {
expectedWithMode[envconfig.CWAGENT_MODE] = currentMode
}
}

assert.Equal(t, expectedWithMode, actualEnvVars, "Expect to be equal")
} else {
t.Logf("Got error %v", err)
t.Fail()
Expand Down
7 changes: 7 additions & 0 deletions translator/tocwconfig/toenvconfig/toEnvConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
debugKey = "debug"
awsSdkLogLevelKey = "aws_sdk_log_level"
usageDataKey = "usage_data"
modeKey = "mode"
)

func ToEnvConfig(jsonConfigValue map[string]interface{}) []byte {
Expand Down Expand Up @@ -76,6 +77,12 @@ func ToEnvConfig(jsonConfigValue map[string]interface{}) []byte {
envVars[envconfig.CWAgentLogsBackpressureMode] = backpressureMode
}

// Persist the agent mode so it can be used when the agent starts
mode := context.CurrentContext().Mode()
if mode != "" {
envVars[envconfig.CWAGENT_MODE] = mode
}

bytes, err := json.MarshalIndent(envVars, "", "\t")
if err != nil {
log.Panicf("Failed to create json map for environment variables. Reason: %s", err.Error())
Expand Down
87 changes: 69 additions & 18 deletions translator/tocwconfig/toenvconfig/toEnvConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ func TestToEnvConfig(t *testing.T) {
contextSetup func()
}{
{
name: "empty config",
input: map[string]interface{}{},
envVars: map[string]string{},
expectedEnv: map[string]string{},
name: "empty config",
input: map[string]interface{}{},
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "ec2", // Default mode
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
Expand All @@ -50,8 +53,10 @@ func TestToEnvConfig(t *testing.T) {
envconfig.CWAGENT_LOG_LEVEL: "DEBUG",
envconfig.AWS_SDK_LOG_LEVEL: "DEBUG",
envconfig.CWAGENT_USAGE_DATA: "FALSE",
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
Expand All @@ -66,8 +71,10 @@ func TestToEnvConfig(t *testing.T) {
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.AWS_USE_DUALSTACK_ENDPOINT: "true",
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
Expand All @@ -82,8 +89,10 @@ func TestToEnvConfig(t *testing.T) {
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.AWS_USE_DUALSTACK_ENDPOINT: "false",
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
Expand All @@ -106,8 +115,10 @@ func TestToEnvConfig(t *testing.T) {
envconfig.AWS_USE_DUALSTACK_ENDPOINT: "true",
envconfig.HTTP_PROXY: "http://proxy.test",
envconfig.AWS_CA_BUNDLE: "/test/ca-bundle.pem",
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{
"http_proxy": "http://proxy.test",
})
Expand All @@ -123,8 +134,11 @@ func TestToEnvConfig(t *testing.T) {
agent.UseDualStackEndpointKey: "true",
},
},
expectedEnv: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
Expand All @@ -136,8 +150,11 @@ func TestToEnvConfig(t *testing.T) {
agent.UseDualStackEndpointKey: 1,
},
},
expectedEnv: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
Expand All @@ -149,8 +166,11 @@ func TestToEnvConfig(t *testing.T) {
agent.UseDualStackEndpointKey: nil,
},
},
expectedEnv: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
Expand All @@ -161,11 +181,13 @@ func TestToEnvConfig(t *testing.T) {
input: map[string]interface{}{},
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.HTTP_PROXY: "http://proxy.example.com",
envconfig.HTTPS_PROXY: "https://proxy.example.com",
envconfig.NO_PROXY: "localhost,127.0.0.1",
envconfig.HTTP_PROXY: "http://proxy.example.com",
envconfig.HTTPS_PROXY: "https://proxy.example.com",
envconfig.NO_PROXY: "localhost,127.0.0.1",
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{
"http_proxy": "http://proxy.example.com",
"https_proxy": "https://proxy.example.com",
Expand All @@ -180,8 +202,10 @@ func TestToEnvConfig(t *testing.T) {
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.AWS_CA_BUNDLE: "/path/to/ca-bundle.pem",
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{
"ca_bundle_path": "/path/to/ca-bundle.pem",
Expand All @@ -196,8 +220,10 @@ func TestToEnvConfig(t *testing.T) {
},
expectedEnv: map[string]string{
envconfig.CWAgentLogsBackpressureMode: "fd_release",
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
Expand All @@ -216,8 +242,10 @@ func TestToEnvConfig(t *testing.T) {
envconfig.CWAGENT_LOG_LEVEL: "DEBUG",
envconfig.HTTP_PROXY: "http://proxy.test",
envconfig.AWS_CA_BUNDLE: "/test/ca-bundle.pem",
envconfig.CWAGENT_MODE: "ec2",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{
"http_proxy": "http://proxy.test",
})
Expand All @@ -226,6 +254,20 @@ func TestToEnvConfig(t *testing.T) {
})
},
},
{
name: "mode persistence",
input: map[string]interface{}{},
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "onPremise",
},
contextSetup: func() {
context.ResetContext()
context.CurrentContext().SetMode("onPremise")
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -262,8 +304,10 @@ func TestToEnvConfig_TypeAssertions(t *testing.T) {
input: map[string]interface{}{
agent.SectionKey: "invalid",
},
envVars: map[string]string{},
expectedEnv: map[string]string{},
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "ec2",
},
},
{
name: "invalid user_agent type",
Expand All @@ -272,8 +316,10 @@ func TestToEnvConfig_TypeAssertions(t *testing.T) {
userAgentKey: 123,
},
},
envVars: map[string]string{},
expectedEnv: map[string]string{},
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "ec2",
},
},
{
name: "invalid debug type",
Expand All @@ -282,16 +328,20 @@ func TestToEnvConfig_TypeAssertions(t *testing.T) {
debugKey: "true",
},
},
envVars: map[string]string{},
expectedEnv: map[string]string{},
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "ec2",
},
},
{
name: "invalid logs section type",
input: map[string]interface{}{
logs.SectionKey: "invalid",
},
envVars: map[string]string{},
expectedEnv: map[string]string{},
envVars: map[string]string{},
expectedEnv: map[string]string{
envconfig.CWAGENT_MODE: "ec2",
},
},
}

Expand All @@ -307,6 +357,7 @@ func TestToEnvConfig_TypeAssertions(t *testing.T) {
}
}()

context.ResetContext()
context.CurrentContext().SetProxy(map[string]string{})
context.CurrentContext().SetSSL(map[string]string{})
result := ToEnvConfig(tt.input)
Expand Down
Loading