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

[profiles] Reduce patch frequency #1317

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 23 additions & 8 deletions controllers/datadogagent/controller_reconcile_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,29 +284,44 @@ func (r *Reconciler) labelNodesWithProfiles(ctx context.Context, profilesByNode
}

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

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 {
delete(newLabels, agentprofile.ProfileLabelKey)
for k, v := range node.Labels {
if k != agentprofile.ProfileLabelKey {
newLabels[k] = v
}
}
}

// If the profile is not the default one and the label does not exist in
// the node, it should be added.
if !isDefaultProfile && !profileLabelExists {
for k, v := range node.Labels {
newLabels[k] = v
}
newLabels[agentprofile.ProfileLabelKey] = profileNamespacedName.Name
}

// Remove old profile label key if it is present
if oldProfileLabelExists {
if len(newLabels) > 0 {
delete(newLabels, agentprofile.OldProfileLabelKey)
} else {
for k, v := range node.Labels {
if k != agentprofile.OldProfileLabelKey {
newLabels[k] = v
}
}
}
}

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

patch := corev1.Node{
Expand Down
201 changes: 201 additions & 0 deletions controllers/datadogagent/controller_reconcile_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
apiutils "github.com/DataDog/datadog-operator/apis/utils"
"github.com/DataDog/datadog-operator/controllers/datadogagent/component/agent"
"github.com/DataDog/datadog-operator/pkg/agentprofile"
"github.com/DataDog/datadog-operator/pkg/kubernetes"

edsdatadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
Expand All @@ -18,6 +19,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -1534,3 +1536,202 @@ func Test_removeStaleStatus(t *testing.T) {
})
}
}

func Test_labelNodesWithProfiles(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding comprehensive test!

sch := runtime.NewScheme()
_ = scheme.AddToScheme(sch)
ctx := context.Background()

testCases := []struct {
name string
description string
profilesByNode map[string]types.NamespacedName
nodes []client.Object
wantNodeLabels map[string]map[string]string
}{
{
name: "label multiple profile nodes and default node",
description: "node-1 and node-2 should be labeled with profile name, node-default should stay nil",
profilesByNode: map[string]types.NamespacedName{
"node-1": {
Namespace: "foo",
Name: "profile-1",
},
"node-2": {
Namespace: "foo",
Name: "profile-2",
},
"node-default": {
Namespace: "",
Name: "default",
},
},
nodes: []client.Object{
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
Labels: map[string]string{
"1": "1",
},
},
},
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-2",
},
},
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-default",
},
},
},
wantNodeLabels: map[string]map[string]string{
"node-1": {
agentprofile.ProfileLabelKey: "profile-1",
"1": "1",
},
"node-2": {
agentprofile.ProfileLabelKey: "profile-2",
},
"node-default": nil,
},
},
{
name: "label multiple profile nodes, default node has profile label",
description: "node-1 and node-2 should be labeled with profile name, profile label should be removed from node-default",
profilesByNode: map[string]types.NamespacedName{
"node-1": {
Namespace: "foo",
Name: "profile-1",
},
"node-2": {
Namespace: "foo",
Name: "profile-2",
},
"node-default": {
Namespace: "",
Name: "default",
},
},
nodes: []client.Object{
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
Labels: map[string]string{
"1": "1",
},
},
},
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-2",
},
},
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-default",
Labels: map[string]string{
agentprofile.ProfileLabelKey: "profile-1",
"foo": "bar",
},
},
},
},
wantNodeLabels: map[string]map[string]string{
"node-1": {
agentprofile.ProfileLabelKey: "profile-1",
"1": "1",
},
"node-2": {
agentprofile.ProfileLabelKey: "profile-2",
},
"node-default": {
"foo": "bar",
},
},
},
{
name: "remove old profile label",
description: "old profile label should be removed from node-2 and node-default",
profilesByNode: map[string]types.NamespacedName{
"node-1": {
Namespace: "foo",
Name: "profile-1",
},
"node-2": {
Namespace: "foo",
Name: "profile-2",
},
"node-default": {
Namespace: "",
Name: "default",
},
},
nodes: []client.Object{
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
Labels: map[string]string{
"1": "1",
},
},
},
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-2",
Labels: map[string]string{
agentprofile.OldProfileLabelKey: "profile-2",
},
},
},
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-default",
Labels: map[string]string{
agentprofile.ProfileLabelKey: "profile-1",
agentprofile.OldProfileLabelKey: "profile-2",
"foo": "bar",
},
},
},
},
wantNodeLabels: map[string]map[string]string{
"node-1": {
agentprofile.ProfileLabelKey: "profile-1",
"1": "1",
},
"node-2": {
agentprofile.ProfileLabelKey: "profile-2",
},
"node-default": {
"foo": "bar",
},
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(sch).WithObjects(tt.nodes...).Build()

r := &Reconciler{
client: fakeClient,
}

err := r.labelNodesWithProfiles(ctx, tt.profilesByNode)
assert.NoError(t, err)

nodeList := &corev1.NodeList{}
err = fakeClient.List(ctx, nodeList)
assert.NoError(t, err)
assert.Len(t, nodeList.Items, len(tt.wantNodeLabels))

for _, node := range nodeList.Items {
expectedNodeLabels, ok := tt.wantNodeLabels[node.Name]
assert.True(t, ok)
assert.Equal(t, expectedNodeLabels, node.Labels)
}
})
}
}
Loading