Skip to content

Commit

Permalink
[profiles] Add label to DaemonSets (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor authored Nov 28, 2023
1 parent 48aa275 commit cceca2f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
17 changes: 1 addition & 16 deletions controllers/datadogagent/controller_reconcile_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package datadogagent

import (
"context"
"fmt"
"time"

"github.com/go-logr/logr"
Expand All @@ -28,7 +27,6 @@ import (
"github.com/DataDog/datadog-operator/controllers/datadogagent/override"
"github.com/DataDog/datadog-operator/pkg/agentprofile"
"github.com/DataDog/datadog-operator/pkg/controller/utils/datadog"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
edsv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
)

Expand Down Expand Up @@ -223,23 +221,10 @@ func (r *Reconciler) cleanupDaemonSetsForProfilesThatNoLongerApply(ctx context.C
return nil
}

// TODO: add specific labels to the daemonset created by the operator that belong to a profile so that we can filter more easily
func (r *Reconciler) agentDaemonSetsCreatedByOperator(ctx context.Context) ([]appsv1.DaemonSet, error) {
daemonSetList := appsv1.DaemonSetList{}

err := r.client.List(
ctx,
&daemonSetList,
client.HasLabels{
fmt.Sprintf(
"%s=%s,%s=%s",
kubernetes.AppKubernetesNameLabelKey,
"datadog-agent-deployment",
kubernetes.AppKubernetesManageByLabelKey,
"datadog-operator",
),
},
)
err := r.client.List(ctx, &daemonSetList, client.HasLabels{agentprofile.DaemonSetLabelKey})
if err != nil {
return nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/agentprofile/agent_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package agentprofile

import (
"fmt"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -16,6 +18,7 @@ import (
)

const (
DaemonSetLabelKey = "agent.datadoghq.com/profile"
defaultProfileName = "default"
daemonSetNamePrefix = "datadog-agent-with-profile-"
)
Expand Down Expand Up @@ -50,6 +53,7 @@ func ComponentOverrideFromProfile(profile *datadoghqv1alpha1.DatadogAgentProfile
Name: &overrideDSName,
Affinity: affinityOverride(profile),
Containers: containersOverride(profile),
Labels: labelsOverride(profile),
}
}

Expand Down Expand Up @@ -171,3 +175,15 @@ func containersOverride(profile *datadoghqv1alpha1.DatadogAgentProfile) map[comm

return res
}

func labelsOverride(profile *datadoghqv1alpha1.DatadogAgentProfile) map[string]string {
if profile.Name == defaultProfileName {
return nil
}

return map[string]string{
// Can't use the namespaced name because it includes "/" which is not
// accepted in labels.
DaemonSetLabelKey: fmt.Sprintf("%s-%s", profile.Namespace, profile.Name),
}
}
7 changes: 7 additions & 0 deletions pkg/agentprofile/agent_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package agentprofile

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -100,6 +101,9 @@ func TestComponentOverrideFromProfile(t *testing.T) {
},
expectedOverride: v2alpha1.DatadogAgentComponentOverride{
Name: &overrideNameForExampleProfile,
Labels: map[string]string{
"agent.datadoghq.com/profile": fmt.Sprintf("%s-%s", "default", "example"),
},
},
},
{
Expand Down Expand Up @@ -133,6 +137,9 @@ func TestComponentOverrideFromProfile(t *testing.T) {
},
},
},
Labels: map[string]string{
"agent.datadoghq.com/profile": fmt.Sprintf("%s-%s", "default", "linux"),
},
},
},
}
Expand Down

0 comments on commit cceca2f

Please sign in to comment.