From 2c91caad6b88b69d2e5d6e10339f823bb7c9a283 Mon Sep 17 00:00:00 2001 From: Levan Machablishvili Date: Mon, 21 Oct 2024 13:51:00 -0400 Subject: [PATCH] Enable otel-agent using DDA annotation --- api/datadoghq/v2alpha1/test/builder.go | 6 ++ .../feature/enabledefault/feature.go | 8 +++ .../datadogagent/feature/test/factory_test.go | 60 ++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/api/datadoghq/v2alpha1/test/builder.go b/api/datadoghq/v2alpha1/test/builder.go index ba7b4c7f8..776e40477 100644 --- a/api/datadoghq/v2alpha1/test/builder.go +++ b/api/datadoghq/v2alpha1/test/builder.go @@ -22,6 +22,7 @@ type DatadogAgentBuilder struct { // NewDatadogAgentBuilder creates DatadogAgent and initializes Global, Features, Override properties func NewDatadogAgentBuilder() *DatadogAgentBuilder { dda := &v2alpha1.DatadogAgent{ + ObjectMeta: metav1.ObjectMeta{}, Spec: v2alpha1.DatadogAgentSpec{ Global: &v2alpha1.GlobalConfig{}, Features: &v2alpha1.DatadogFeatures{}, @@ -71,6 +72,11 @@ func (builder *DatadogAgentBuilder) WithName(name string) *DatadogAgentBuilder { return builder } +func (builder *DatadogAgentBuilder) WithAnnotations(annotations map[string]string) *DatadogAgentBuilder { + builder.datadogAgent.ObjectMeta.Annotations = annotations + return builder +} + // Global environment variable func (builder *DatadogAgentBuilder) WithEnvVars(envs []corev1.EnvVar) *DatadogAgentBuilder { builder.datadogAgent.Spec.Global.Env = envs diff --git a/internal/controller/datadogagent/feature/enabledefault/feature.go b/internal/controller/datadogagent/feature/enabledefault/feature.go index 61156d247..1c23e261b 100644 --- a/internal/controller/datadogagent/feature/enabledefault/feature.go +++ b/internal/controller/datadogagent/feature/enabledefault/feature.go @@ -27,6 +27,10 @@ import ( "k8s.io/apimachinery/pkg/util/errors" ) +const ( + enableOtelAnnotation = "agent.datadoghq.com/otel-agent-enabled" +) + func init() { err := feature.Register(feature.DefaultIDType, buildDefaultFeature) if err != nil { @@ -126,6 +130,10 @@ func (f *defaultFeature) Configure(dda *v2alpha1.DatadogAgent) feature.RequiredC f.agent.serviceAccountAnnotations = v2alpha1.GetAgentServiceAccountAnnotations(dda) f.clusterChecksRunner.serviceAccountAnnotations = v2alpha1.GetClusterChecksRunnerServiceAccountAnnotations(dda) + if dda.ObjectMeta.Annotations != nil { + f.otelAgentEnabled = f.otelAgentEnabled || dda.ObjectMeta.Annotations[enableOtelAnnotation] == "true" + } + if dda.Spec.Global != nil { if dda.Spec.Global.DisableNonResourceRules != nil && *dda.Spec.Global.DisableNonResourceRules { f.disableNonResourceRules = true diff --git a/internal/controller/datadogagent/feature/test/factory_test.go b/internal/controller/datadogagent/feature/test/factory_test.go index 9a613f567..69d96f2e5 100644 --- a/internal/controller/datadogagent/feature/test/factory_test.go +++ b/internal/controller/datadogagent/feature/test/factory_test.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature" _ "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature/apm" _ "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature/cspm" + _ "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature/enabledefault" _ "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature/livecontainer" _ "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature/npm" ) @@ -20,6 +21,7 @@ func TestBuilder(t *testing.T) { tests := []struct { name string dda *v2alpha1.DatadogAgent + featureOptions feature.Options wantCoreAgentComponent bool wantAgentContainer map[common.AgentContainerName]bool }{ @@ -36,6 +38,7 @@ func TestBuilder(t *testing.T) { common.TraceAgentContainerName: true, common.SystemProbeContainerName: false, common.SecurityAgentContainerName: false, + common.OtelAgent: false, }, }, { @@ -50,6 +53,7 @@ func TestBuilder(t *testing.T) { common.TraceAgentContainerName: false, common.SystemProbeContainerName: false, common.SecurityAgentContainerName: false, + common.OtelAgent: false, }, }, { @@ -64,6 +68,7 @@ func TestBuilder(t *testing.T) { common.TraceAgentContainerName: true, common.SystemProbeContainerName: false, common.SecurityAgentContainerName: false, + common.OtelAgent: false, }, }, { @@ -79,6 +84,7 @@ func TestBuilder(t *testing.T) { common.TraceAgentContainerName: false, common.SystemProbeContainerName: false, common.SecurityAgentContainerName: false, + common.OtelAgent: false, }, }, { @@ -94,6 +100,7 @@ func TestBuilder(t *testing.T) { common.TraceAgentContainerName: true, common.SystemProbeContainerName: true, common.SecurityAgentContainerName: false, + common.OtelAgent: false, }, }, { @@ -110,6 +117,7 @@ func TestBuilder(t *testing.T) { common.TraceAgentContainerName: true, common.SystemProbeContainerName: true, common.SecurityAgentContainerName: false, + common.OtelAgent: false, }, }, { @@ -126,6 +134,7 @@ func TestBuilder(t *testing.T) { common.TraceAgentContainerName: true, common.SystemProbeContainerName: true, common.SecurityAgentContainerName: true, + common.OtelAgent: false, }, }, { @@ -143,13 +152,62 @@ func TestBuilder(t *testing.T) { common.TraceAgentContainerName: true, common.SystemProbeContainerName: true, common.SecurityAgentContainerName: true, + common.OtelAgent: false, + }, + }, + { + name: "Default DDA, default feature Option, otel-agent-enabled annotation true", + dda: v2alpha1test.NewDatadogAgentBuilder(). + WithAnnotations(map[string]string{"agent.datadoghq.com/otel-agent-enabled": "true"}). + BuildWithDefaults(), + wantAgentContainer: map[common.AgentContainerName]bool{ + common.UnprivilegedSingleAgentContainerName: false, + common.CoreAgentContainerName: true, + common.ProcessAgentContainerName: true, + common.TraceAgentContainerName: true, + common.SystemProbeContainerName: false, + common.SecurityAgentContainerName: false, + common.OtelAgent: true, + }, + }, + { + name: "Default DDA, default feature Option, otel-agent-enabled annotation false", + dda: v2alpha1test.NewDatadogAgentBuilder(). + WithAnnotations(map[string]string{"agent.datadoghq.com/otel-agent-enabled": "false"}). + BuildWithDefaults(), + wantAgentContainer: map[common.AgentContainerName]bool{ + common.UnprivilegedSingleAgentContainerName: false, + common.CoreAgentContainerName: true, + common.ProcessAgentContainerName: true, + common.TraceAgentContainerName: true, + common.SystemProbeContainerName: false, + common.SecurityAgentContainerName: false, + common.OtelAgent: false, + }, + }, + { + name: "Default DDA, no otel annotation, Operator option enabled", + dda: v2alpha1test.NewDatadogAgentBuilder(). + WithAnnotations(map[string]string{"agent.datadoghq.com/otel-agent-enabled": "false"}). + BuildWithDefaults(), + featureOptions: feature.Options{ + OtelAgentEnabled: true, + }, + wantAgentContainer: map[common.AgentContainerName]bool{ + common.UnprivilegedSingleAgentContainerName: false, + common.CoreAgentContainerName: true, + common.ProcessAgentContainerName: true, + common.TraceAgentContainerName: true, + common.SystemProbeContainerName: false, + common.SecurityAgentContainerName: false, + common.OtelAgent: true, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, requiredComponents := feature.BuildFeatures(tt.dda, &feature.Options{}) + _, requiredComponents := feature.BuildFeatures(tt.dda, &tt.featureOptions) assert.True(t, *requiredComponents.Agent.IsRequired)