Skip to content

Commit

Permalink
Do not error if pod needing deletion is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
khewonc committed Jan 7, 2025
1 parent 87f8143 commit 1790b63
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -379,7 +380,9 @@ func (r *Reconciler) cleanupPodsForProfilesThatNoLongerApply(ctx context.Context
},
}
if err = r.client.Delete(ctx, &toDelete); err != nil {
return err
if !apierrors.IsNotFound(err) {
return err
}
}
}
}
Expand Down
223 changes: 223 additions & 0 deletions internal/controller/datadogagent/controller_reconcile_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1841,3 +1841,226 @@ func Test_labelNodesWithProfiles(t *testing.T) {
})
}
}

func Test_cleanupPodsForProfilesThatNoLongerApply(t *testing.T) {
sch := runtime.NewScheme()
_ = scheme.AddToScheme(sch)
ctx := context.Background()

testCases := []struct {
name string
description string
profilesByNode map[string]types.NamespacedName
ddaNamespace string
existingPods []client.Object
wantPods []corev1.Pod
}{
{
name: "delete agent pod that shouldn't be running",
description: "pod-2 should be deleted",
profilesByNode: map[string]types.NamespacedName{
"node-1": {
Namespace: "foo",
Name: "profile-1",
},
"node-2": {
Namespace: "foo",
Name: "profile-2",
},
"node-default": {
Namespace: "",
Name: "default",
},
},
ddaNamespace: "foo",
existingPods: []client.Object{
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
agentprofile.ProfileLabelKey: "profile-1",
},
},
Spec: corev1.PodSpec{
NodeName: "node-1",
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-2",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
agentprofile.ProfileLabelKey: "profile-1",
},
},
Spec: corev1.PodSpec{
NodeName: "node-2",
},
},
},
wantPods: []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
agentprofile.ProfileLabelKey: "profile-1",
},
ResourceVersion: "999",
},
Spec: corev1.PodSpec{
NodeName: "node-1",
},
},
},
},
{
name: "delete default agent on profile node",
description: "pod-2 should be deleted",
profilesByNode: map[string]types.NamespacedName{
"node-1": {
Namespace: "foo",
Name: "profile-1",
},
"node-2": {
Namespace: "foo",
Name: "profile-2",
},
"node-default": {
Namespace: "",
Name: "default",
},
},
ddaNamespace: "foo",
existingPods: []client.Object{
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
agentprofile.ProfileLabelKey: "profile-1",
},
},
Spec: corev1.PodSpec{
NodeName: "node-1",
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-default",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
},
},
Spec: corev1.PodSpec{
NodeName: "node-2",
},
},
},
wantPods: []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
agentprofile.ProfileLabelKey: "profile-1",
},
ResourceVersion: "999",
},
Spec: corev1.PodSpec{
NodeName: "node-1",
},
},
},
},
{
name: "delete profile agent on default node",
description: "pod-2 should be deleted",
profilesByNode: map[string]types.NamespacedName{
"node-1": {
Namespace: "foo",
Name: "profile-1",
},
"node-2": {
Namespace: "foo",
Name: "profile-2",
},
"node-default": {
Namespace: "",
Name: "default",
},
},
ddaNamespace: "foo",
existingPods: []client.Object{
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
agentprofile.ProfileLabelKey: "profile-1",
},
},
Spec: corev1.PodSpec{
NodeName: "node-1",
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-2",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
agentprofile.ProfileLabelKey: "profile-2",
},
},
Spec: corev1.PodSpec{
NodeName: "node-default",
},
},
},
wantPods: []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "foo",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: constants.DefaultAgentResourceSuffix,
agentprofile.ProfileLabelKey: "profile-1",
},
ResourceVersion: "999",
},
Spec: corev1.PodSpec{
NodeName: "node-1",
},
},
},
},
}

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

r := &Reconciler{
client: fakeClient,
}

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

podList := &corev1.PodList{}
err = fakeClient.List(ctx, podList)
assert.NoError(t, err)
assert.Len(t, podList.Items, len(tt.wantPods))
assert.Equal(t, tt.wantPods, podList.Items)
})
}
}

0 comments on commit 1790b63

Please sign in to comment.