diff --git a/controllers/datadogagent/controller_reconcile_agent.go b/controllers/datadogagent/controller_reconcile_agent.go index 7997b42a0..f4e987aac 100644 --- a/controllers/datadogagent/controller_reconcile_agent.go +++ b/controllers/datadogagent/controller_reconcile_agent.go @@ -7,7 +7,6 @@ package datadogagent import ( "context" - "fmt" "time" apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common" @@ -273,7 +272,7 @@ func (r *Reconciler) handleProfiles(ctx context.Context, profilesByNode map[stri return nil } -// labelNodesWithProfiles sets the "agent.datadoghq.com/profile" label only in +// labelNodesWithProfiles sets the "agent.datadoghq.com/datadogagentprofile" 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 { @@ -286,27 +285,24 @@ func (r *Reconciler) labelNodesWithProfiles(ctx context.Context, profilesByNode _, profileLabelExists := node.Labels[agentprofile.ProfileLabelKey] - var newLabels map[string]string + 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 + } + } // If the profile is the default one and the label exists in the node, // it should be removed. if isDefaultProfile && profileLabelExists { - newLabels = make(map[string]string, len(node.Labels)-1) - for label, value := range node.Labels { - if label != agentprofile.ProfileLabelKey { - newLabels[label] = value - } - } + delete(newLabels, agentprofile.ProfileLabelKey) } // 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 = 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) + newLabels[agentprofile.ProfileLabelKey] = profileNamespacedName.Name } if len(newLabels) == 0 { @@ -356,7 +352,7 @@ func (r *Reconciler) cleanupPodsForProfilesThatNoLongerApply(ctx context.Context } isDefaultProfile := agentprofile.IsDefaultProfile(profileNamespacedName.Namespace, profileNamespacedName.Name) - expectedProfileLabelValue := fmt.Sprintf("%s-%s", profileNamespacedName.Namespace, profileNamespacedName.Name) + expectedProfileLabelValue := profileNamespacedName.Name profileLabelValue, profileLabelExists := agentPod.Labels[agentprofile.ProfileLabelKey] diff --git a/controllers/datadogagent/controller_reconcile_v2.go b/controllers/datadogagent/controller_reconcile_v2.go index dbf339659..f3eb27513 100644 --- a/controllers/datadogagent/controller_reconcile_v2.go +++ b/controllers/datadogagent/controller_reconcile_v2.go @@ -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", "name", profile.Name, "namespace", profile.Namespace) + logger.Error(err, "profile cannot be applied", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace) continue } profileListToApply = append(profileListToApply, profile) diff --git a/controllers/datadogagent/controller_reconcile_v2_common.go b/controllers/datadogagent/controller_reconcile_v2_common.go index 89db8897b..b5ba2d060 100644 --- a/controllers/datadogagent/controller_reconcile_v2_common.go +++ b/controllers/datadogagent/controller_reconcile_v2_common.go @@ -11,6 +11,7 @@ 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" @@ -215,6 +216,9 @@ 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) diff --git a/controllers/datadogagent/controller_test.go b/controllers/datadogagent/controller_test.go index 4c1fd662b..54ef0c156 100644 --- a/controllers/datadogagent/controller_test.go +++ b/controllers/datadogagent/controller_test.go @@ -2924,8 +2924,8 @@ func Test_LabelNodesWithProfiles(t *testing.T) { }, }, expectProfileLabel: map[string]string{ - "node-1": "ns-1-profile-1", - "node-2": "ns-2-profile-2", + "node-1": "profile-1", + "node-2": "profile-2", }, }, { @@ -2956,7 +2956,7 @@ func Test_LabelNodesWithProfiles(t *testing.T) { }, expectProfileLabel: map[string]string{ "node-1": "", - "node-2": "ns-2-profile-2", + "node-2": "profile-2", }, }, { diff --git a/controllers/datadogagent/finalizer.go b/controllers/datadogagent/finalizer.go index 80187619b..19a207b5a 100644 --- a/controllers/datadogagent/finalizer.go +++ b/controllers/datadogagent/finalizer.go @@ -165,15 +165,19 @@ func (r *Reconciler) profilesCleanup() error { } for _, node := range nodeList.Items { - if _, profileLabelExists := node.Labels[agentprofile.ProfileLabelKey]; !profileLabelExists { + _, profileLabelExists := node.Labels[agentprofile.ProfileLabelKey] + _, oldProfileLabelExists := node.Labels[agentprofile.OldProfileLabelKey] + if !profileLabelExists && !oldProfileLabelExists { continue } - newLabels := make(map[string]string, len(node.Labels)-1) - for label, value := range node.Labels { - if label != agentprofile.ProfileLabelKey { - newLabels[label] = value + newLabels := map[string]string{} + for k, v := range node.Labels { + // Remove profile labels from nodes + if k == agentprofile.OldProfileLabelKey || k == agentprofile.ProfileLabelKey { + continue } + newLabels[k] = v } patch := corev1.Node{ diff --git a/pkg/agentprofile/agent_profile.go b/pkg/agentprofile/agent_profile.go index 78f96bade..8959018b6 100644 --- a/pkg/agentprofile/agent_profile.go +++ b/pkg/agentprofile/agent_profile.go @@ -24,7 +24,9 @@ import ( ) const ( - ProfileLabelKey = "agent.datadoghq.com/profile" + ProfileLabelKey = "agent.datadoghq.com/datadogagentprofile" + // OldProfileLabelKey was deprecated in operator v1.8.0 + OldProfileLabelKey = "agent.datadoghq.com/profile" defaultProfileName = "default" daemonSetNamePrefix = "datadog-agent-with-profile-" ) @@ -37,13 +39,13 @@ 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", "name", profile.Name, "namespace", profile.Namespace) + logger.Error(err, "couldn't generate hash for profile", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace) } else { profileStatus.CurrentHash = hash } if err := datadoghqv1alpha1.ValidateDatadogAgentProfileSpec(&profile.Spec); err != nil { - logger.Error(err, "profile spec is invalid, skipping", "name", profile.Name, "namespace", profile.Namespace) + logger.Error(err, "profile spec is invalid, skipping", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace) profileStatus.Conditions = SetDatadogAgentProfileCondition(profileStatus.Conditions, NewDatadogAgentProfileCondition(ValidConditionType, metav1.ConditionFalse, now, InvalidConditionReason, err.Error())) profileStatus.Valid = metav1.ConditionFalse UpdateProfileStatus(profile, profileStatus, now) @@ -53,7 +55,7 @@ func ProfileToApply(logger logr.Logger, profile *datadoghqv1alpha1.DatadogAgentP for _, node := range nodes { matchesNode, err := profileMatchesNode(profile, node.Labels) if err != nil { - logger.Error(err, "profile selector is invalid, skipping", "name", profile.Name, "namespace", profile.Namespace) + logger.Error(err, "profile selector is invalid, skipping", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace) profileStatus.Conditions = SetDatadogAgentProfileCondition(profileStatus.Conditions, NewDatadogAgentProfileCondition(ValidConditionType, metav1.ConditionFalse, now, InvalidConditionReason, err.Error())) profileStatus.Valid = metav1.ConditionFalse UpdateProfileStatus(profile, profileStatus, now) @@ -185,7 +187,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/profile label. +// all nodes that don't have the agent.datadoghq.com/datadogagentprofile label. func affinityOverrideForDefaultProfile() *v1.Affinity { return &v1.Affinity{ NodeAffinity: &v1.NodeAffinity{ @@ -269,9 +271,7 @@ func labelsOverride(profile *datadoghqv1alpha1.DatadogAgentProfile) map[string]s } return map[string]string{ - // Can't use the namespaced name because it includes "/" which is not - // accepted in labels. - ProfileLabelKey: fmt.Sprintf("%s-%s", profile.Namespace, profile.Name), + ProfileLabelKey: profile.Name, } } diff --git a/pkg/agentprofile/agent_profile_test.go b/pkg/agentprofile/agent_profile_test.go index a569a7038..5921c046d 100644 --- a/pkg/agentprofile/agent_profile_test.go +++ b/pkg/agentprofile/agent_profile_test.go @@ -282,7 +282,7 @@ func TestOverrideFromProfile(t *testing.T) { expectedOverride: v2alpha1.DatadogAgentComponentOverride{ Name: &overrideNameForExampleProfile, Labels: map[string]string{ - "agent.datadoghq.com/profile": fmt.Sprintf("%s-%s", "default", "example"), + "agent.datadoghq.com/datadogagentprofile": "example", }, Affinity: &v1.Affinity{ PodAntiAffinity: &v1.PodAntiAffinity{ @@ -352,7 +352,7 @@ func TestOverrideFromProfile(t *testing.T) { }, }, Labels: map[string]string{ - "agent.datadoghq.com/profile": fmt.Sprintf("%s-%s", "default", "linux"), + "agent.datadoghq.com/datadogagentprofile": "linux", }, }, },