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
27 changes: 20 additions & 7 deletions internal/controller/datadogagent/override/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ func Container(containerName apicommon.AgentContainerName, manager feature.PodTe
addEnvsToInitContainer(containerName, manager, override.Env)
addVolMountsToInitContainer(containerName, manager, override.VolumeMounts)

overrideSeccompProfile(containerName, manager, override)
overrideAppArmorProfile(containerName, manager, override)

for i, container := range manager.PodTemplateSpec().Spec.Containers {
if container.Name == string(containerName) {
overrideContainer(&manager.PodTemplateSpec().Spec.Containers[i], override)
Expand All @@ -58,6 +55,9 @@ func Container(containerName apicommon.AgentContainerName, manager feature.PodTe
overrideInitContainer(&manager.PodTemplateSpec().Spec.InitContainers[i], override)
}
}

overrideSeccompProfile(containerName, manager, override)
overrideAppArmorProfile(containerName, manager, override)
}

func overrideLogLevel(containerName apicommon.AgentContainerName, manager feature.PodTemplateManagers, logLevel string) {
Expand Down Expand Up @@ -234,13 +234,26 @@ func overrideSeccompProfile(containerName apicommon.AgentContainerName, manager

func overrideAppArmorProfile(containerName apicommon.AgentContainerName, manager feature.PodTemplateManagers, override *v2alpha1.DatadogAgentGenericContainer) {
if override.AppArmorProfileName != nil {
var annotation string
effectiveName := string(containerName)
if override.Name != nil {
annotation = fmt.Sprintf("%s/%s", common.AppArmorAnnotationKey, *override.Name)
} else {
annotation = fmt.Sprintf("%s/%s", common.AppArmorAnnotationKey, containerName)
effectiveName = *override.Name
}

// Only add the AppArmor annotation if the container actually exists in the pod spec.
// This avoids invalid DaemonSet configurations when a container is not present
// (e.g. security-agent is absent when directSendFromSystemProbe is enabled).
containerExists := false
for _, c := range manager.PodTemplateSpec().Spec.Containers {
if c.Name == effectiveName {
Comment on lines +246 to +247
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check AppArmor existence using the current container name

When override.Name is set with AppArmorProfileName, this check compares effectiveName (the future renamed name) against the current Spec.Containers list before Container() applies the rename, so containerExists is false and the function returns early. That drops the AppArmor annotation for valid renamed containers, silently ignoring a requested security profile.

Useful? React with 👍 / 👎.

containerExists = true
break
}
}
if !containerExists {
return
}

annotation := fmt.Sprintf("%s/%s", common.AppArmorAnnotationKey, effectiveName)
manager.Annotation().AddAnnotation(annotation, *override.AppArmorProfileName)
}
}
Expand Down
20 changes: 20 additions & 0 deletions internal/controller/datadogagent/override/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,26 @@ func TestContainer(t *testing.T) {
})
},
},
{
name: "override app armor profile for non-existing container does not add annotation",
containerName: apicommon.SecurityAgentContainerName,
existingManager: func() *fake.PodTemplateManagers {
// Pod spec does not contain the security-agent container
return fake.NewPodTemplateManagers(t, corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{*agentContainer},
},
})
},
override: v2alpha1.DatadogAgentGenericContainer{
AppArmorProfileName: apiutils.NewStringPointer("my-app-armor-profile"),
},
validateManager: func(t *testing.T, manager *fake.PodTemplateManagers, containerName string) {
annotation := fmt.Sprintf("%s/%s", common.AppArmorAnnotationKey, apicommon.SecurityAgentContainerName)
_, found := manager.AnnotationMgr.Annotations[annotation]
assert.False(t, found, "AppArmor annotation should not be added when container does not exist in pod spec")
},
},
{
name: "seccomp inline ConfigData adds checksum annotation",
containerName: apicommon.SystemProbeContainerName,
Expand Down
Loading