Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CECO-1240] Rename profile label key #1230

Merged
merged 3 commits into from
Jul 10, 2024
Merged
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
26 changes: 11 additions & 15 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"

apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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]

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", "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)
Expand Down
4 changes: 4 additions & 0 deletions controllers/datadogagent/controller_reconcile_v2_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
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 @@ -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",
},
},
{
Expand Down Expand Up @@ -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",
},
},
{
Expand Down
14 changes: 9 additions & 5 deletions controllers/datadogagent/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
16 changes: 8 additions & 8 deletions pkg/agentprofile/agent_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-"
)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
}
}

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/profile": fmt.Sprintf("%s-%s", "default", "example"),
"agent.datadoghq.com/datadogagentprofile": "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/profile": fmt.Sprintf("%s-%s", "default", "linux"),
"agent.datadoghq.com/datadogagentprofile": "linux",
},
},
},
Expand Down
Loading