Skip to content

Commit

Permalink
Add kubernetes modes and use eksdetector on startup (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisguo authored Mar 1, 2024
1 parent 8f1a815 commit 90caaca
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 162 deletions.
5 changes: 4 additions & 1 deletion cmd/config-translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ func initFlags() {
}
translatorUtil.SetProxyEnv(ctx.Proxy())
translatorUtil.SetSSLEnv(ctx.SSL())
ctx.SetMode(translatorUtil.DetectAgentMode(*inputMode))

mode := translatorUtil.DetectAgentMode(*inputMode)
ctx.SetMode(mode)
ctx.SetKubernetesMode(translatorUtil.DetectKubernetesMode(mode))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"k8s.io/client-go/tools/clientcmd"

attr "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/internal/attributes"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
"github.com/aws/amazon-cloudwatch-agent/translator/util/eksdetector"
)

const (
Expand Down Expand Up @@ -680,7 +680,7 @@ func (h *kubernetesHostedInAttributeResolver) Process(attributes, resourceAttrib
}
}

if isEks := common.IsEKS(); isEks.Value {
if isEks := eksdetector.IsEKS(); isEks.Value {
attributes.PutStr(attr.HostedInClusterNameEKS, h.clusterName)
} else {
attributes.PutStr(attr.HostedInClusterNameK8s, h.clusterName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

attr "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/internal/attributes"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
"github.com/aws/amazon-cloudwatch-agent/translator/util/eksdetector"
)

// MockDeleter deletes a key immediately, useful for testing.
Expand Down Expand Up @@ -817,8 +817,8 @@ func TestEksResolver(t *testing.T) {
}

func TestHostedInEksResolver(t *testing.T) {
common.NewDetector = common.TestEKSDetector
common.IsEKS = common.TestIsEKSCacheEKS
eksdetector.NewDetector = eksdetector.TestEKSDetector
eksdetector.IsEKS = eksdetector.TestIsEKSCacheEKS
// helper function to get string values from the attributes
getStrAttr := func(attributes pcommon.Map, key string, t *testing.T) string {
if value, ok := attributes.Get(key); ok {
Expand Down Expand Up @@ -850,8 +850,8 @@ func TestHostedInEksResolver(t *testing.T) {
}

func TestHostedInNativeK8sEC2Resolver(t *testing.T) {
common.NewDetector = common.TestK8sDetector
common.IsEKS = common.TestIsEKSCacheK8s
eksdetector.NewDetector = eksdetector.TestK8sDetector
eksdetector.IsEKS = eksdetector.TestIsEKSCacheK8s
// helper function to get string values from the attributes
getStrAttr := func(attributes pcommon.Map, key string, t *testing.T) string {
if value, ok := attributes.Get(key); ok {
Expand Down Expand Up @@ -883,7 +883,7 @@ func TestHostedInNativeK8sEC2Resolver(t *testing.T) {
}

func TestHostedInNativeK8sOnPremResolver(t *testing.T) {
common.NewDetector = common.TestK8sDetector
eksdetector.NewDetector = eksdetector.TestK8sDetector
// helper function to get string values from the attributes
getStrAttr := func(attributes pcommon.Map, key string, t *testing.T) string {
if value, ok := attributes.Get(key); ok {
Expand Down
15 changes: 12 additions & 3 deletions translator/config/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ const (
)

const (
ShortModeEC2 = "EC2"
ShortModeOnPrem = "OP"
ShortModeWithIRSA = "WI"
ModeEKS = "EKS"
ModeK8sEC2 = "K8sEC2"
ModeK8sOnPrem = "K8sOnPrem"
)

const (
ShortModeEC2 = "EC2"
ShortModeOnPrem = "OP"
ShortModeWithIRSA = "WI"
ShortModeEKS = "EKS"
ShortModeK8sEC2 = "K8E"
ShortModeK8sOnPrem = "K8OP"
)
23 changes: 22 additions & 1 deletion translator/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Context struct {
multiConfig string
outputTomlFilePath string
mode string
kubernetesMode string
shortMode string
credentials map[string]string
proxy map[string]string
Expand Down Expand Up @@ -97,6 +98,10 @@ func (ctx *Context) Mode() string {
return ctx.mode
}

func (ctx *Context) KubernetesMode() string {
return ctx.kubernetesMode
}

func (ctx *Context) ShortMode() string {
return ctx.shortMode
}
Expand Down Expand Up @@ -128,7 +133,23 @@ func (ctx *Context) SetMode(mode string) {
ctx.mode = config.ModeWithIRSA
ctx.shortMode = config.ShortModeWithIRSA
default:
log.Panicf("Invalid mode %s. Valid mode values are %s, %s, %s and %s.", mode, config.ModeEC2, config.ModeOnPrem, config.ModeOnPremise, config.ModeWithIRSA)
log.Panicf("Invalid mode %s. Valid mode values are %s, %s, %s, and %s.", mode, config.ModeEC2, config.ModeOnPrem, config.ModeOnPremise, config.ModeWithIRSA)
}
}

func (ctx *Context) SetKubernetesMode(mode string) {
switch mode {
case config.ModeEKS:
ctx.kubernetesMode = config.ModeEKS
ctx.shortMode = config.ShortModeEKS
case config.ModeK8sEC2:
ctx.kubernetesMode = config.ModeK8sEC2
ctx.shortMode = config.ShortModeK8sEC2
case config.ModeK8sOnPrem:
ctx.kubernetesMode = config.ModeK8sOnPrem
ctx.shortMode = config.ShortModeK8sOnPrem
default:
ctx.kubernetesMode = ""
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,15 @@ extensions:
operations:
- PutLogEvents
usage_flags:
mode: EC2
mode: EKS
region_type: ACJ
agenthealth/traces:
is_usage_data_enabled: true
stats:
operations:
- PutTraceSegments
usage_flags:
mode: EC2
mode: EKS
region_type: ACJ
awsproxy/app_signals:
aws_endpoint: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,15 @@ extensions:
operations:
- PutLogEvents
usage_flags:
mode: EC2
mode: K8E
region_type: ACJ
agenthealth/traces:
is_usage_data_enabled: true
stats:
operations:
- PutTraceSegments
usage_flags:
mode: EC2
mode: K8E
region_type: ACJ
awsproxy/app_signals:
aws_endpoint: ""
Expand Down
25 changes: 16 additions & 9 deletions translator/tocwconfig/tocwconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"k8s.io/client-go/kubernetes/fake"

"github.com/aws/amazon-cloudwatch-agent/cfg/commonconfig"
"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
Expand All @@ -39,6 +38,7 @@ import (
"github.com/aws/amazon-cloudwatch-agent/translator/translate/agent"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
"github.com/aws/amazon-cloudwatch-agent/translator/util"
"github.com/aws/amazon-cloudwatch-agent/translator/util/eksdetector"
)

const (
Expand Down Expand Up @@ -81,25 +81,32 @@ func TestGenericAppSignalsConfig(t *testing.T) {
checkTranslation(t, "base_appsignals_config", "windows", expectedEnvVars, "")
}

func TestAppSignalsAndKubernetesConfig(t *testing.T) {
func TestAppSignalsAndEKSConfig(t *testing.T) {
resetContext(t)
context.CurrentContext().SetRunInContainer(true)
context.CurrentContext().SetMode(config.ModeEC2)
t.Setenv(config.HOST_NAME, "host_name_from_env")
t.Setenv(config.HOST_IP, "127.0.0.1")
t.Setenv(common.KubernetesEnvVar, "use_appsignals_eks_config")
common.NewDetector = common.TestEKSDetector
common.IsEKS = common.TestIsEKSCacheEKS
eksdetector.NewDetector = eksdetector.TestEKSDetector
context.CurrentContext().SetMode(config.ModeEC2)
context.CurrentContext().SetKubernetesMode(config.ModeEKS)

expectedEnvVars := map[string]string{}
checkTranslation(t, "appsignals_and_eks_config", "linux", expectedEnvVars, "")
checkTranslation(t, "appsignals_and_eks_config", "windows", expectedEnvVars, "")
}

common.NewDetector = func() (common.Detector, error) {
return &common.EksDetector{Clientset: fake.NewSimpleClientset()}, nil
}
common.IsEKS = common.TestIsEKSCacheK8s
func TestAppSignalsAndNativeKubernetesConfig(t *testing.T) {
resetContext(t)
context.CurrentContext().SetRunInContainer(true)
t.Setenv(config.HOST_NAME, "host_name_from_env")
t.Setenv(config.HOST_IP, "127.0.0.1")
t.Setenv(common.KubernetesEnvVar, "use_appsignals_k8s_config")
eksdetector.IsEKS = eksdetector.TestIsEKSCacheK8s
context.CurrentContext().SetMode(config.ModeEC2)
context.CurrentContext().SetKubernetesMode(config.ModeK8sEC2)

expectedEnvVars := map[string]string{}
checkTranslation(t, "appsignals_and_k8s_config", "linux", expectedEnvVars, "")
checkTranslation(t, "appsignals_and_k8s_config", "windows", expectedEnvVars, "")
}
Expand Down
15 changes: 7 additions & 8 deletions translator/translate/otel/exporter/awsemf/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,14 @@ func (t *translator) Translate(c *confmap.Conf) (component.Config, error) {
}

func getAppSignalsConfig() string {
if common.IsAppSignalsKubernetes() {
isEks := common.IsEKS()
if isEks.Value {
return appSignalsConfigEks
}
return appSignalsConfigK8s
}
ctx := context.CurrentContext()
if ctx.Mode() == config.ModeEC2 {
kubernetesMode := ctx.KubernetesMode()

if kubernetesMode == config.ModeEKS {
return appSignalsConfigEks
} else if kubernetesMode == config.ModeK8sEC2 || kubernetesMode == config.ModeK8sOnPrem {
return appSignalsConfigK8s
} else if ctx.Mode() == config.ModeEC2 {
return appSignalsConfigEC2
}
return appSignalsConfigGeneric
Expand Down
39 changes: 13 additions & 26 deletions translator/translate/otel/exporter/awsemf/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,8 @@ func TestTranslateAppSignals(t *testing.T) {
input map[string]any
want *confmap.Conf
wantErr error
isKubernetes bool
isEC2 bool
detector func() (common.Detector, error)
isEKSDataStore func() common.IsEKSCache
kubernetesMode string
mode string
}{
"WithAppSignalsEnabledEKS": {
input: map[string]any{
Expand All @@ -730,14 +728,13 @@ func TestTranslateAppSignals(t *testing.T) {
},
}},
want: testutil.GetConfWithOverrides(t, filepath.Join("appsignals_config_eks.yaml"), map[string]any{
"local_mode": "true",
"local_mode": "false",
"region": "us-east-1",
"role_arn": "global_arn",
"certificate_file_path": "/ca/bundle",
}),
isKubernetes: true,
detector: common.TestEKSDetector,
isEKSDataStore: common.TestIsEKSCacheEKS,
kubernetesMode: config.ModeEKS,
mode: config.ModeEC2,
},
"WithAppSignalsEnabledK8s": {
input: map[string]any{
Expand All @@ -752,9 +749,8 @@ func TestTranslateAppSignals(t *testing.T) {
"role_arn": "global_arn",
"certificate_file_path": "/ca/bundle",
}),
isKubernetes: true,
detector: common.TestK8sDetector,
isEKSDataStore: common.TestIsEKSCacheK8s,
kubernetesMode: config.ModeK8sOnPrem,
mode: config.ModeOnPrem,
},
"WithAppSignalsEnabledGeneric": {
input: map[string]any{
Expand All @@ -769,8 +765,8 @@ func TestTranslateAppSignals(t *testing.T) {
"role_arn": "global_arn",
"certificate_file_path": "/ca/bundle",
}),
isKubernetes: false,
isEC2: false,
kubernetesMode: "",
mode: config.ModeOnPrem,
},
"WithAppSignalsEnabledEC2": {
input: map[string]any{
Expand All @@ -785,24 +781,15 @@ func TestTranslateAppSignals(t *testing.T) {
"role_arn": "global_arn",
"certificate_file_path": "/ca/bundle",
}),
isEC2: true,
kubernetesMode: "",
mode: config.ModeEC2,
},
}
factory := awsemfexporter.NewFactory()
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
if testCase.isKubernetes {
t.Setenv(common.KubernetesEnvVar, "TEST")
}
if testCase.isEC2 {
ctx := context.CurrentContext()
ctx.SetMode(config.ModeEC2)
} else {
ctx := context.CurrentContext()
ctx.SetMode(config.ModeOnPrem)
}
common.NewDetector = testCase.detector
common.IsEKS = testCase.isEKSDataStore
context.CurrentContext().SetKubernetesMode(testCase.kubernetesMode)
context.CurrentContext().SetMode(testCase.mode)
conf := confmap.NewFromStringMap(testCase.input)
got, err := tt.Translate(conf)
assert.Equal(t, testCase.wantErr, err)
Expand Down
21 changes: 8 additions & 13 deletions translator/translate/otel/exporter/awsxray/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,15 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
cfg := t.factory.CreateDefaultConfig().(*awsxrayexporter.Config)

if isAppSignals(conf) {
if common.IsAppSignalsKubernetes() {
isEks := common.IsEKS()
if isEks.Value {
cfg.IndexedAttributes = indexedAttributesEKS
} else {
cfg.IndexedAttributes = indexedAttributesK8s
}
ctx := context.CurrentContext()
if ctx.KubernetesMode() == config.ModeEKS {
cfg.IndexedAttributes = indexedAttributesEKS
} else if ctx.KubernetesMode() == config.ModeK8sEC2 || ctx.KubernetesMode() == config.ModeK8sOnPrem {
cfg.IndexedAttributes = indexedAttributesK8s
} else if ctx.Mode() == config.ModeEC2 {
cfg.IndexedAttributes = indexedAttributesEC2
} else {
ctx := context.CurrentContext()
if ctx.Mode() == config.ModeEC2 {
cfg.IndexedAttributes = indexedAttributesEC2
} else {
cfg.IndexedAttributes = indexedAttributesGeneric
}
cfg.IndexedAttributes = indexedAttributesGeneric
}
}

Expand Down
Loading

0 comments on commit 90caaca

Please sign in to comment.