Skip to content

Commit

Permalink
Add label overrides for profiles (#1291)
Browse files Browse the repository at this point in the history
  • Loading branch information
khewonc authored Jul 16, 2024
1 parent f9aef45 commit 78cefd3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
7 changes: 6 additions & 1 deletion apis/datadoghq/v1alpha1/datadogagentprofile_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Config struct {
type Override struct {
// Configure the basic configurations for an Agent container
// Valid Agent container names are: `agent`
// +optional
Containers map[commonv1.AgentContainerName]*Container `json:"containers,omitempty"`

// If specified, indicates the pod's priority. "system-node-critical" and
Expand All @@ -46,11 +47,15 @@ type Override struct {
// default.
// +optional
PriorityClassName *string `json:"priorityClassName,omitempty"`

// Labels provide labels that are added to the Datadog Agent pods.
// +optional
Labels map[string]string `json:"labels,omitempty"`
}

type Container struct {
// Specify additional environment variables in the container.
// See also: https://docs.datadoghq.com/agent/kubernetes/?tab=helm#environment-variables
// See also: https://docs.datadoghq.com/agent/guide/environment-variables/
//
// +optional
// +listType=map
Expand Down
7 changes: 7 additions & 0 deletions apis/datadoghq/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion config/crd/bases/v1/datadoghq.com_datadogagentprofiles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ spec:
env:
description: |-
Specify additional environment variables in the container.
See also: https://docs.datadoghq.com/agent/kubernetes/?tab=helm#environment-variables
See also: https://docs.datadoghq.com/agent/guide/environment-variables/
items:
description: EnvVar represents an environment variable present in a Container.
properties:
Expand Down Expand Up @@ -230,6 +230,11 @@ spec:
Configure the basic configurations for an Agent container
Valid Agent container names are: `agent`
type: object
labels:
additionalProperties:
type: string
description: Labels provide labels that are added to the Datadog Agent pods.
type: object
priorityClassName:
description: |-
If specified, indicates the pod's priority. "system-node-critical" and
Expand Down
14 changes: 12 additions & 2 deletions pkg/agentprofile/agent_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,19 @@ func labelsOverride(profile *datadoghqv1alpha1.DatadogAgentProfile) map[string]s
return nil
}

return map[string]string{
ProfileLabelKey: profile.Name,
labels := map[string]string{}

if profile.Spec.Config != nil {
if nodeAgentOverride, ok := profile.Spec.Config.Override[datadoghqv1alpha1.NodeAgentComponentName]; ok {
for labelName, labelVal := range nodeAgentOverride.Labels {
labels[labelName] = labelVal
}
}
}

labels[ProfileLabelKey] = profile.Name

return labels
}

func priorityClassNameOverride(profile *datadoghqv1alpha1.DatadogAgentProfile) *string {
Expand Down
90 changes: 90 additions & 0 deletions pkg/agentprofile/agent_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,96 @@ func TestPriorityClassNameOverride(t *testing.T) {
})
}
}
func Test_labelsOverride(t *testing.T) {
tests := []struct {
name string
profile v1alpha1.DatadogAgentProfile
expectedLabels map[string]string
}{
{
name: "default profile",
profile: v1alpha1.DatadogAgentProfile{
ObjectMeta: metav1.ObjectMeta{
Name: defaultProfileName,
},
},
expectedLabels: nil,
},
{
name: "profile with no label overrides",
profile: v1alpha1.DatadogAgentProfile{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: "foo",
},
Spec: v1alpha1.DatadogAgentProfileSpec{
Config: &v1alpha1.Config{
Override: map[v1alpha1.ComponentName]*v1alpha1.Override{
v1alpha1.NodeAgentComponentName: {},
},
},
},
},
expectedLabels: map[string]string{
ProfileLabelKey: "foo",
},
},
{
name: "profile with label overrides",
profile: v1alpha1.DatadogAgentProfile{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: "foo",
},
Spec: v1alpha1.DatadogAgentProfileSpec{
Config: &v1alpha1.Config{
Override: map[v1alpha1.ComponentName]*v1alpha1.Override{
v1alpha1.NodeAgentComponentName: {
Labels: map[string]string{
"foo": "bar",
},
},
},
},
},
},
expectedLabels: map[string]string{
ProfileLabelKey: "foo",
"foo": "bar",
},
},
{
// ProfileLabelKey should not be overriden by a user-created profile
name: "profile with label overriding ProfileLabelKey",
profile: v1alpha1.DatadogAgentProfile{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: "foo",
},
Spec: v1alpha1.DatadogAgentProfileSpec{
Config: &v1alpha1.Config{
Override: map[v1alpha1.ComponentName]*v1alpha1.Override{
v1alpha1.NodeAgentComponentName: {
Labels: map[string]string{
ProfileLabelKey: "bar",
},
},
},
},
},
},
expectedLabels: map[string]string{
ProfileLabelKey: "foo",
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedLabels, labelsOverride(&test.profile))
})
}
}

func exampleProfileForLinux() v1alpha1.DatadogAgentProfile {
return v1alpha1.DatadogAgentProfile{
Expand Down

0 comments on commit 78cefd3

Please sign in to comment.