Skip to content

Commit

Permalink
Enable otel-agent using DDA annotation (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
levan-m authored Oct 22, 2024
1 parent ae5b9e1 commit 86c5b1d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/datadoghq/v2alpha1/test/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
60 changes: 59 additions & 1 deletion internal/controller/datadogagent/feature/test/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}{
Expand All @@ -36,6 +38,7 @@ func TestBuilder(t *testing.T) {
common.TraceAgentContainerName: true,
common.SystemProbeContainerName: false,
common.SecurityAgentContainerName: false,
common.OtelAgent: false,
},
},
{
Expand All @@ -50,6 +53,7 @@ func TestBuilder(t *testing.T) {
common.TraceAgentContainerName: false,
common.SystemProbeContainerName: false,
common.SecurityAgentContainerName: false,
common.OtelAgent: false,
},
},
{
Expand All @@ -64,6 +68,7 @@ func TestBuilder(t *testing.T) {
common.TraceAgentContainerName: true,
common.SystemProbeContainerName: false,
common.SecurityAgentContainerName: false,
common.OtelAgent: false,
},
},
{
Expand All @@ -79,6 +84,7 @@ func TestBuilder(t *testing.T) {
common.TraceAgentContainerName: false,
common.SystemProbeContainerName: false,
common.SecurityAgentContainerName: false,
common.OtelAgent: false,
},
},
{
Expand All @@ -94,6 +100,7 @@ func TestBuilder(t *testing.T) {
common.TraceAgentContainerName: true,
common.SystemProbeContainerName: true,
common.SecurityAgentContainerName: false,
common.OtelAgent: false,
},
},
{
Expand All @@ -110,6 +117,7 @@ func TestBuilder(t *testing.T) {
common.TraceAgentContainerName: true,
common.SystemProbeContainerName: true,
common.SecurityAgentContainerName: false,
common.OtelAgent: false,
},
},
{
Expand All @@ -126,6 +134,7 @@ func TestBuilder(t *testing.T) {
common.TraceAgentContainerName: true,
common.SystemProbeContainerName: true,
common.SecurityAgentContainerName: true,
common.OtelAgent: false,
},
},
{
Expand All @@ -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)

Expand Down

0 comments on commit 86c5b1d

Please sign in to comment.