Skip to content

Commit

Permalink
Revert "[CECO-1240] Rename profile label key (#1230)"
Browse files Browse the repository at this point in the history
This reverts commit a3ce2ae.
  • Loading branch information
khewonc committed Jul 24, 2024
1 parent 7b3a6ed commit d5ae274
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
26 changes: 15 additions & 11 deletions controllers/datadogagent/controller_reconcile_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package datadogagent

import (
"context"
"fmt"
"time"

apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
Expand Down Expand Up @@ -272,7 +273,7 @@ func (r *Reconciler) handleProfiles(ctx context.Context, profilesByNode map[stri
return nil
}

// labelNodesWithProfiles sets the "agent.datadoghq.com/datadogagentprofile" label only in
// labelNodesWithProfiles sets the "agent.datadoghq.com/profile" label only in
// the nodes where a profile is applied
func (r *Reconciler) labelNodesWithProfiles(ctx context.Context, profilesByNode map[string]types.NamespacedName) error {
for nodeName, profileNamespacedName := range profilesByNode {
Expand All @@ -285,24 +286,27 @@ func (r *Reconciler) labelNodesWithProfiles(ctx context.Context, profilesByNode

_, profileLabelExists := node.Labels[agentprofile.ProfileLabelKey]

newLabels := map[string]string{}
for k, v := range node.Labels {
// If the profile uses the old profile label key, it should be removed
if k != agentprofile.OldProfileLabelKey {
newLabels[k] = v
}
}
var newLabels map[string]string

// If the profile is the default one and the label exists in the node,
// it should be removed.
if isDefaultProfile && profileLabelExists {
delete(newLabels, agentprofile.ProfileLabelKey)
newLabels = make(map[string]string, len(node.Labels)-1)
for label, value := range node.Labels {
if label != agentprofile.ProfileLabelKey {
newLabels[label] = value
}
}
}

// If the profile is not the default one and the label does not exist in
// the node, it should be added.
if !isDefaultProfile && !profileLabelExists {
newLabels[agentprofile.ProfileLabelKey] = profileNamespacedName.Name
newLabels = make(map[string]string, len(node.Labels)+1)
for label, value := range node.Labels {
newLabels[label] = value
}
newLabels[agentprofile.ProfileLabelKey] = fmt.Sprintf("%s-%s", profileNamespacedName.Namespace, profileNamespacedName.Name)
}

if len(newLabels) == 0 {
Expand Down Expand Up @@ -352,7 +356,7 @@ func (r *Reconciler) cleanupPodsForProfilesThatNoLongerApply(ctx context.Context
}

isDefaultProfile := agentprofile.IsDefaultProfile(profileNamespacedName.Namespace, profileNamespacedName.Name)
expectedProfileLabelValue := profileNamespacedName.Name
expectedProfileLabelValue := fmt.Sprintf("%s-%s", profileNamespacedName.Namespace, profileNamespacedName.Name)

profileLabelValue, profileLabelExists := agentPod.Labels[agentprofile.ProfileLabelKey]

Expand Down
2 changes: 1 addition & 1 deletion controllers/datadogagent/controller_reconcile_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (r *Reconciler) profilesToApply(ctx context.Context, logger logr.Logger, no
r.updateDAPStatus(logger, &profile)
if err != nil {
// profile is invalid or conflicts
logger.Error(err, "profile cannot be applied", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace)
logger.Error(err, "profile cannot be applied", "name", profile.Name, "namespace", profile.Namespace)
continue
}
profileListToApply = append(profileListToApply, profile)
Expand Down
4 changes: 0 additions & 4 deletions controllers/datadogagent/controller_reconcile_v2_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

datadoghqv2alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
"github.com/DataDog/datadog-operator/pkg/agentprofile"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
"github.com/DataDog/datadog-operator/pkg/controller/utils/datadog"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
Expand Down Expand Up @@ -216,9 +215,6 @@ func (r *Reconciler) createOrUpdateDaemonset(parentLogger logr.Logger, dda *data
updateDaemonset.Spec = *daemonset.Spec.DeepCopy()
updateDaemonset.Annotations = mergeAnnotationsLabels(logger, currentDaemonset.GetAnnotations(), daemonset.GetAnnotations(), keepAnnotationsFilter)
updateDaemonset.Labels = mergeAnnotationsLabels(logger, currentDaemonset.GetLabels(), daemonset.GetLabels(), keepLabelsFilter)
// manually remove the old profile label because mergeAnnotationsLabels
// won't filter labels with "datadoghq.com" in the key
delete(updateDaemonset.Labels, agentprofile.OldProfileLabelKey)

now := metav1.NewTime(time.Now())
err = kubernetes.UpdateFromObject(context.TODO(), r.client, updateDaemonset, currentDaemonset.ObjectMeta)
Expand Down
6 changes: 3 additions & 3 deletions controllers/datadogagent/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2929,8 +2929,8 @@ func Test_LabelNodesWithProfiles(t *testing.T) {
},
},
expectProfileLabel: map[string]string{
"node-1": "profile-1",
"node-2": "profile-2",
"node-1": "ns-1-profile-1",
"node-2": "ns-2-profile-2",
},
},
{
Expand Down Expand Up @@ -2961,7 +2961,7 @@ func Test_LabelNodesWithProfiles(t *testing.T) {
},
expectProfileLabel: map[string]string{
"node-1": "",
"node-2": "profile-2",
"node-2": "ns-2-profile-2",
},
},
{
Expand Down
14 changes: 5 additions & 9 deletions controllers/datadogagent/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,15 @@ func (r *Reconciler) profilesCleanup() error {
}

for _, node := range nodeList.Items {
_, profileLabelExists := node.Labels[agentprofile.ProfileLabelKey]
_, oldProfileLabelExists := node.Labels[agentprofile.OldProfileLabelKey]
if !profileLabelExists && !oldProfileLabelExists {
if _, profileLabelExists := node.Labels[agentprofile.ProfileLabelKey]; !profileLabelExists {
continue
}

newLabels := map[string]string{}
for k, v := range node.Labels {
// Remove profile labels from nodes
if k == agentprofile.OldProfileLabelKey || k == agentprofile.ProfileLabelKey {
continue
newLabels := make(map[string]string, len(node.Labels)-1)
for label, value := range node.Labels {
if label != agentprofile.ProfileLabelKey {
newLabels[label] = value
}
newLabels[k] = v
}

patch := corev1.Node{
Expand Down
8 changes: 3 additions & 5 deletions pkg/agentprofile/agent_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import (
)

const (
ProfileLabelKey = "agent.datadoghq.com/datadogagentprofile"
// OldProfileLabelKey was deprecated in operator v1.8.0
OldProfileLabelKey = "agent.datadoghq.com/profile"
ProfileLabelKey = "agent.datadoghq.com/profile"
defaultProfileName = "default"
daemonSetNamePrefix = "datadog-agent-with-profile-"
labelValueMaxLength = 63
Expand All @@ -42,7 +40,7 @@ func ProfileToApply(logger logr.Logger, profile *datadoghqv1alpha1.DatadogAgentP
profileStatus := datadoghqv1alpha1.DatadogAgentProfileStatus{}

if hash, err := comparison.GenerateMD5ForSpec(profile.Spec); err != nil {
logger.Error(err, "couldn't generate hash for profile", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace)
logger.Error(err, "couldn't generate hash for profile", "name", profile.Name, "namespace", profile.Namespace)
} else {
profileStatus.CurrentHash = hash
}
Expand Down Expand Up @@ -201,7 +199,7 @@ func affinityOverride(profile *datadoghqv1alpha1.DatadogAgentProfile) *v1.Affini

// affinityOverrideForDefaultProfile returns the affinity override that should
// be applied to the default profile. The default profile should be applied to
// all nodes that don't have the agent.datadoghq.com/datadogagentprofile label.
// all nodes that don't have the agent.datadoghq.com/profile label.
func affinityOverrideForDefaultProfile() *v1.Affinity {
return &v1.Affinity{
NodeAffinity: &v1.NodeAffinity{
Expand Down
4 changes: 2 additions & 2 deletions pkg/agentprofile/agent_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func TestOverrideFromProfile(t *testing.T) {
expectedOverride: v2alpha1.DatadogAgentComponentOverride{
Name: &overrideNameForExampleProfile,
Labels: map[string]string{
"agent.datadoghq.com/datadogagentprofile": "example",
"agent.datadoghq.com/profile": fmt.Sprintf("%s-%s", "default", "example"),
},
Affinity: &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
Expand Down Expand Up @@ -352,7 +352,7 @@ func TestOverrideFromProfile(t *testing.T) {
},
},
Labels: map[string]string{
"agent.datadoghq.com/datadogagentprofile": "linux",
"agent.datadoghq.com/profile": fmt.Sprintf("%s-%s", "default", "linux"),
},
},
},
Expand Down

0 comments on commit d5ae274

Please sign in to comment.