-
Notifications
You must be signed in to change notification settings - Fork 261
Support CWAGENT_ROLE env var for Container Insights mode in JSON v2 config #2185
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
Changes from all commits
2534ea1
2dc544e
151ea2d
3af68f3
4b9fed3
42e39af
84b4ccc
ddf4eb9
207b239
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Missing a unit test to cover the case where the role is not leader/node. func TestGetMode_UnrecognizedRole(t *testing.T) {
cfg := confmap.NewFromStringMap(map[string]interface{}{
"opentelemetry": map[string]interface{}{
"collect": map[string]interface{}{
"container_insights": map[string]interface{}{},
},
},
})
t.Setenv(envconfig.CWAGENT_ROLE, "WORKER")
assert.Equal(t, modeNode, getMode(cfg))
} |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,14 @@ | |||||||
|
|
||||||||
| package containerinsights | ||||||||
|
|
||||||||
| import "testing" | ||||||||
| import ( | ||||||||
| "testing" | ||||||||
|
|
||||||||
| "github.com/stretchr/testify/assert" | ||||||||
| "go.opentelemetry.io/collector/confmap" | ||||||||
|
|
||||||||
| "github.com/aws/amazon-cloudwatch-agent/cfg/envconfig" | ||||||||
| ) | ||||||||
|
|
||||||||
| func TestEscapeDollarDigit(t *testing.T) { | ||||||||
| tests := []struct { | ||||||||
|
|
@@ -35,3 +42,100 @@ func TestEscapeDollarDigit(t *testing.T) { | |||||||
| }) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| func TestGetMode_JSONConfig(t *testing.T) { | ||||||||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||||||||
| "opentelemetry": map[string]interface{}{ | ||||||||
| "collect": map[string]interface{}{ | ||||||||
| "container_insights": map[string]interface{}{ | ||||||||
| "mode": "cluster", | ||||||||
| }, | ||||||||
| }, | ||||||||
| }, | ||||||||
| }) | ||||||||
| assert.Equal(t, modeCluster, getMode(cfg)) | ||||||||
| } | ||||||||
|
|
||||||||
| func TestGetMode_EnvVarFallback(t *testing.T) { | ||||||||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||||||||
| "opentelemetry": map[string]interface{}{ | ||||||||
| "collect": map[string]interface{}{ | ||||||||
| "container_insights": map[string]interface{}{}, | ||||||||
| }, | ||||||||
| }, | ||||||||
| }) | ||||||||
|
|
||||||||
| t.Setenv(envconfig.CWAGENT_ROLE, envconfig.LEADER) | ||||||||
| assert.Equal(t, modeCluster, getMode(cfg)) | ||||||||
|
|
||||||||
| t.Setenv(envconfig.CWAGENT_ROLE, envconfig.NODE) | ||||||||
| assert.Equal(t, modeNode, getMode(cfg)) | ||||||||
| } | ||||||||
|
|
||||||||
| func TestGetMode_DefaultsToNode(t *testing.T) { | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Consider clearing the environment variable before the assertion to ensure it isn't set. Also in
Suggested change
|
||||||||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||||||||
| "opentelemetry": map[string]interface{}{ | ||||||||
| "collect": map[string]interface{}{ | ||||||||
| "container_insights": map[string]interface{}{}, | ||||||||
| }, | ||||||||
| }, | ||||||||
| }) | ||||||||
| assert.Equal(t, modeNode, getMode(cfg)) | ||||||||
| } | ||||||||
|
|
||||||||
| func TestGetMode_EnvVarCaseInsensitive(t *testing.T) { | ||||||||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||||||||
| "opentelemetry": map[string]interface{}{ | ||||||||
| "collect": map[string]interface{}{ | ||||||||
| "container_insights": map[string]interface{}{}, | ||||||||
| }, | ||||||||
| }, | ||||||||
| }) | ||||||||
|
|
||||||||
| t.Setenv(envconfig.CWAGENT_ROLE, "leader") // lowercase | ||||||||
| assert.Equal(t, modeCluster, getMode(cfg)) | ||||||||
|
|
||||||||
| t.Setenv(envconfig.CWAGENT_ROLE, "node") // lowercase | ||||||||
| assert.Equal(t, modeNode, getMode(cfg)) | ||||||||
|
|
||||||||
| t.Setenv(envconfig.CWAGENT_ROLE, "Leader") // mixed case | ||||||||
| assert.Equal(t, modeCluster, getMode(cfg)) | ||||||||
| } | ||||||||
|
|
||||||||
| func TestGetMode_JSONOverridesEnv(t *testing.T) { | ||||||||
| t.Setenv(envconfig.CWAGENT_ROLE, envconfig.NODE) | ||||||||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||||||||
| "opentelemetry": map[string]interface{}{ | ||||||||
| "collect": map[string]interface{}{ | ||||||||
| "container_insights": map[string]interface{}{ | ||||||||
| "mode": "cluster", | ||||||||
| }, | ||||||||
| }, | ||||||||
| }, | ||||||||
| }) | ||||||||
| assert.Equal(t, modeCluster, getMode(cfg)) | ||||||||
| } | ||||||||
|
|
||||||||
| func TestLogsEnabled(t *testing.T) { | ||||||||
| tests := []struct { | ||||||||
| name string | ||||||||
| cfg *confmap.Conf | ||||||||
| want bool | ||||||||
| }{ | ||||||||
| {"nil config", nil, false}, | ||||||||
| {"not set", confmap.NewFromStringMap(map[string]interface{}{ | ||||||||
| "opentelemetry": map[string]interface{}{"collect": map[string]interface{}{"container_insights": map[string]interface{}{}}}, | ||||||||
| }), false}, | ||||||||
| {"enabled true", confmap.NewFromStringMap(map[string]interface{}{ | ||||||||
| "opentelemetry": map[string]interface{}{"collect": map[string]interface{}{"container_insights": map[string]interface{}{"logs": map[string]interface{}{"enabled": true}}}}, | ||||||||
| }), true}, | ||||||||
| {"enabled false", confmap.NewFromStringMap(map[string]interface{}{ | ||||||||
| "opentelemetry": map[string]interface{}{"collect": map[string]interface{}{"container_insights": map[string]interface{}{"logs": map[string]interface{}{"enabled": false}}}}, | ||||||||
| }), false}, | ||||||||
| } | ||||||||
| for _, tt := range tests { | ||||||||
| t.Run(tt.name, func(t *testing.T) { | ||||||||
| assert.Equal(t, tt.want, logsEnabled(tt.cfg)) | ||||||||
| }) | ||||||||
| } | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package containerinsights | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "go.opentelemetry.io/collector/confmap" | ||
|
|
||
| "github.com/aws/amazon-cloudwatch-agent/cfg/envconfig" | ||
| ) | ||
|
|
||
| func TestNewTranslators_MissingKey(t *testing.T) { | ||
| // nil config - should return 0 translators (no container_insights key present) | ||
| assert.Equal(t, 0, NewTranslators(nil).Len()) | ||
| // empty config - should return 0 translators | ||
| assert.Equal(t, 0, NewTranslators(confmap.NewFromStringMap(map[string]interface{}{})).Len()) | ||
| } | ||
|
|
||
| func TestNewTranslators_ModeNode(t *testing.T) { | ||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||
| "opentelemetry": map[string]interface{}{ | ||
| "collect": map[string]interface{}{ | ||
| "container_insights": map[string]interface{}{ | ||
| "cluster_name": "test-cluster", | ||
| "mode": "node", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| translators := NewTranslators(cfg) | ||
| // node mode: kubeletstats, cadvisor, node_exporter, dcgm, neuron, efa, ebs_csi, lis_csi = 8 pipelines | ||
| assert.Equal(t, 8, translators.Len()) | ||
| } | ||
|
|
||
| func TestNewTranslators_ModeNodeWithLogs(t *testing.T) { | ||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||
| "opentelemetry": map[string]interface{}{ | ||
| "collect": map[string]interface{}{ | ||
| "container_insights": map[string]interface{}{ | ||
| "cluster_name": "test-cluster", | ||
| "mode": "node", | ||
| "logs": map[string]interface{}{ | ||
| "enabled": true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| translators := NewTranslators(cfg) | ||
| // node mode + logs: 8 metric pipelines + 2 log pipelines = 10 | ||
| assert.Equal(t, 10, translators.Len()) | ||
| } | ||
|
|
||
| func TestNewTranslators_ModeCluster(t *testing.T) { | ||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||
| "opentelemetry": map[string]interface{}{ | ||
| "collect": map[string]interface{}{ | ||
| "container_insights": map[string]interface{}{ | ||
| "cluster_name": "test-cluster", | ||
| "mode": "cluster", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| translators := NewTranslators(cfg) | ||
| // cluster mode: apiserver, kube_state_metrics = 2 pipelines | ||
| assert.Equal(t, 2, translators.Len()) | ||
| } | ||
|
|
||
| func TestNewTranslators_DefaultMode(t *testing.T) { | ||
| // No mode specified, no env var - should default to node | ||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||
| "opentelemetry": map[string]interface{}{ | ||
| "collect": map[string]interface{}{ | ||
| "container_insights": map[string]interface{}{ | ||
| "cluster_name": "test-cluster", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| translators := NewTranslators(cfg) | ||
| // defaults to node mode: 8 pipelines | ||
| assert.Equal(t, 8, translators.Len()) | ||
| } | ||
|
|
||
| func TestNewTranslators_EnvVarFallback_Node(t *testing.T) { | ||
| // No mode in config, CWAGENT_ROLE=NODE | ||
| t.Setenv(envconfig.CWAGENT_ROLE, envconfig.NODE) | ||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||
| "opentelemetry": map[string]interface{}{ | ||
| "collect": map[string]interface{}{ | ||
| "container_insights": map[string]interface{}{ | ||
| "cluster_name": "test-cluster", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| translators := NewTranslators(cfg) | ||
| // env var NODE -> node mode: 8 pipelines | ||
| assert.Equal(t, 8, translators.Len()) | ||
| } | ||
|
|
||
| func TestNewTranslators_EnvVarFallback_Leader(t *testing.T) { | ||
| // No mode in config, CWAGENT_ROLE=LEADER | ||
| t.Setenv(envconfig.CWAGENT_ROLE, envconfig.LEADER) | ||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||
| "opentelemetry": map[string]interface{}{ | ||
| "collect": map[string]interface{}{ | ||
| "container_insights": map[string]interface{}{ | ||
| "cluster_name": "test-cluster", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| translators := NewTranslators(cfg) | ||
| // env var LEADER -> cluster mode: 2 pipelines | ||
| assert.Equal(t, 2, translators.Len()) | ||
| } | ||
|
|
||
| func TestNewTranslators_JSONConfigOverridesEnvVar(t *testing.T) { | ||
| // JSON says cluster, env var says NODE -> JSON wins | ||
| t.Setenv(envconfig.CWAGENT_ROLE, envconfig.NODE) | ||
| cfg := confmap.NewFromStringMap(map[string]interface{}{ | ||
| "opentelemetry": map[string]interface{}{ | ||
| "collect": map[string]interface{}{ | ||
| "container_insights": map[string]interface{}{ | ||
| "cluster_name": "test-cluster", | ||
| "mode": "cluster", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| translators := NewTranslators(cfg) | ||
| // JSON config wins: cluster mode = 2 pipelines | ||
| assert.Equal(t, 2, translators.Len()) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.