Skip to content

Commit

Permalink
[profiles] Reduce patch frequency (#1317)
Browse files Browse the repository at this point in the history
* Reduce patch requests

* Loop through all profilesByNode

* Refactor

* go lint
  • Loading branch information
khewonc authored and levan-m committed Jul 26, 2024
1 parent 665fbbd commit 5f3b7a8
Show file tree
Hide file tree
Showing 2 changed files with 461 additions and 15 deletions.
45 changes: 30 additions & 15 deletions controllers/datadogagent/controller_reconcile_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,45 @@ func (r *Reconciler) labelNodesWithProfiles(ctx context.Context, profilesByNode
return err
}

_, 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
}
}
labelsToRemove := map[string]bool{}
labelsToAddOrChange := 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)
if isDefaultProfile {
if _, profileLabelExists := node.Labels[agentprofile.ProfileLabelKey]; profileLabelExists {
labelsToRemove[agentprofile.ProfileLabelKey] = true
}
} else {
// If the profile is not the default one and the label does not exist in
// the node, it should be added. If the label value is outdated, it
// should be updated.
if profileLabelValue := node.Labels[agentprofile.ProfileLabelKey]; profileLabelValue != profileNamespacedName.Name {
labelsToAddOrChange[agentprofile.ProfileLabelKey] = profileNamespacedName.Name
}
}

// Remove old profile label key if it is present
if _, oldProfileLabelExists := node.Labels[agentprofile.OldProfileLabelKey]; oldProfileLabelExists {
labelsToRemove[agentprofile.OldProfileLabelKey] = true
}

// 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
if len(labelsToRemove) > 0 || len(labelsToAddOrChange) > 0 {
for k, v := range node.Labels {
if _, ok := labelsToRemove[k]; ok {
continue
}
newLabels[k] = v
}

for k, v := range labelsToAddOrChange {
newLabels[k] = v
}
}

if len(newLabels) == 0 {
return nil
continue
}

modifiedNode := node.DeepCopy()
Expand Down
Loading

0 comments on commit 5f3b7a8

Please sign in to comment.