|
| 1 | +package evict |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + awssdk "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/eks" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/runtime" |
| 17 | + "k8s.io/client-go/kubernetes/fake" |
| 18 | + clienttesting "k8s.io/client-go/testing" |
| 19 | + |
| 20 | + commonaws "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/common/aws" |
| 21 | +) |
| 22 | + |
| 23 | +type stubEKS struct { |
| 24 | + gotInputs []*eks.UpdateNodegroupConfigInput |
| 25 | + err error |
| 26 | +} |
| 27 | + |
| 28 | +func (s *stubEKS) UpdateNodegroupConfig(_ context.Context, in *eks.UpdateNodegroupConfigInput, _ ...func(*eks.Options)) (*eks.UpdateNodegroupConfigOutput, error) { |
| 29 | + s.gotInputs = append(s.gotInputs, in) |
| 30 | + return &eks.UpdateNodegroupConfigOutput{}, s.err |
| 31 | +} |
| 32 | + |
| 33 | +func mngDrainOpts() nodeDrainOptions { |
| 34 | + return nodeDrainOptions{ |
| 35 | + EvictionTimeout: time.Second, |
| 36 | + NodeTimeout: 5 * time.Second, |
| 37 | + PollInterval: 10 * time.Millisecond, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestEvictEKSManagedNodeGroup(t *testing.T) { |
| 42 | + const cluster, mng, nodeName = "my-cluster", "my-mng", "ip-1" |
| 43 | + newMngNode := func() *corev1.Node { |
| 44 | + return &corev1.Node{ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Name: nodeName, |
| 46 | + Labels: map[string]string{commonaws.LabelEKSNodegroup: mng}, |
| 47 | + }} |
| 48 | + } |
| 49 | + podOnNode := corev1.Pod{ |
| 50 | + ObjectMeta: metav1.ObjectMeta{Name: "app", Namespace: "default"}, |
| 51 | + Spec: corev1.PodSpec{NodeName: nodeName}, |
| 52 | + } |
| 53 | + |
| 54 | + // installEvictionReactor makes the fake accept Eviction creates (echoing |
| 55 | + // the object) or fail them with evictErr. |
| 56 | + installEvictionReactor := func(client *fake.Clientset, evictErr error) { |
| 57 | + client.PrependReactor("create", "pods", func(a clienttesting.Action) (bool, runtime.Object, error) { |
| 58 | + ca, ok := a.(clienttesting.CreateAction) |
| 59 | + if !ok || ca.GetSubresource() != "eviction" { |
| 60 | + return false, nil, nil |
| 61 | + } |
| 62 | + if evictErr != nil { |
| 63 | + return true, nil, evictErr |
| 64 | + } |
| 65 | + return true, ca.GetObject(), nil |
| 66 | + }) |
| 67 | + } |
| 68 | + // installPodListReactor returns the pod list for each 1-based List call, so a |
| 69 | + // case can drain a node across successive polls. |
| 70 | + installPodListReactor := func(client *fake.Clientset, lists func(call int) []corev1.Pod) { |
| 71 | + var n int |
| 72 | + client.PrependReactor("list", "pods", func(_ clienttesting.Action) (bool, runtime.Object, error) { |
| 73 | + n++ |
| 74 | + return true, &corev1.PodList{Items: lists(n)}, nil |
| 75 | + }) |
| 76 | + } |
| 77 | + // installNodeListReactor drives waitEKSNodegroupEmpty's Nodes().List (by |
| 78 | + // nodegroup label). It does not affect cordonNodes, which uses Get + Update. |
| 79 | + installNodeListReactor := func(client *fake.Clientset, items []corev1.Node) { |
| 80 | + client.PrependReactor("list", "nodes", func(_ clienttesting.Action) (bool, runtime.Object, error) { |
| 81 | + return true, &corev1.NodeList{Items: items}, nil |
| 82 | + }) |
| 83 | + } |
| 84 | + assertScaledToZero := func(t *testing.T, in *eks.UpdateNodegroupConfigInput) { |
| 85 | + t.Helper() |
| 86 | + assert.Equal(t, cluster, awssdk.ToString(in.ClusterName)) |
| 87 | + assert.Equal(t, mng, awssdk.ToString(in.NodegroupName)) |
| 88 | + require.NotNil(t, in.ScalingConfig) |
| 89 | + assert.Equal(t, int32(0), awssdk.ToInt32(in.ScalingConfig.MinSize)) |
| 90 | + assert.Equal(t, int32(0), awssdk.ToInt32(in.ScalingConfig.DesiredSize)) |
| 91 | + // MaxSize is deliberately omitted so EKS preserves the existing ceiling |
| 92 | + // (partial ScalingConfig update). |
| 93 | + assert.Nil(t, in.ScalingConfig.MaxSize) |
| 94 | + } |
| 95 | + |
| 96 | + t.Run("dry-run cordons nothing and skips Update", func(t *testing.T) { |
| 97 | + stub := &stubEKS{} |
| 98 | + client := fake.NewClientset(newMngNode()) |
| 99 | + opts := mngDrainOpts() |
| 100 | + opts.DryRun = true |
| 101 | + |
| 102 | + err := evictEKSManagedNodeGroup(t.Context(), stub, client, cluster, mng, []string{nodeName}, opts) |
| 103 | + require.NoError(t, err) |
| 104 | + assert.Empty(t, stub.gotInputs) |
| 105 | + got, getErr := client.CoreV1().Nodes().Get(t.Context(), nodeName, metav1.GetOptions{}) |
| 106 | + require.NoError(t, getErr) |
| 107 | + assert.False(t, got.Spec.Unschedulable, "dry-run must not cordon") |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("cordons, drains gracefully, then scales to zero", func(t *testing.T) { |
| 111 | + stub := &stubEKS{} |
| 112 | + client := fake.NewClientset(newMngNode()) |
| 113 | + installEvictionReactor(client, nil) |
| 114 | + // The node still hosts the pod when drainNode enumerates it (call 1); |
| 115 | + // the eviction takes effect by the time waitForNodeEmpty polls (call 2+). |
| 116 | + installPodListReactor(client, func(call int) []corev1.Pod { |
| 117 | + if call == 1 { |
| 118 | + return []corev1.Pod{podOnNode} |
| 119 | + } |
| 120 | + return nil |
| 121 | + }) |
| 122 | + // EKS has terminated the instance by the time we poll for nodes. |
| 123 | + installNodeListReactor(client, nil) |
| 124 | + |
| 125 | + err := evictEKSManagedNodeGroup(t.Context(), stub, client, cluster, mng, []string{nodeName}, mngDrainOpts()) |
| 126 | + require.NoError(t, err) |
| 127 | + require.Len(t, stub.gotInputs, 1) |
| 128 | + assertScaledToZero(t, stub.gotInputs[0]) |
| 129 | + got, getErr := client.CoreV1().Nodes().Get(t.Context(), nodeName, metav1.GetOptions{}) |
| 130 | + require.NoError(t, getErr) |
| 131 | + assert.True(t, got.Spec.Unschedulable, "the node must be cordoned before draining") |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("drain failure keeps PDBs and skips scale-down", func(t *testing.T) { |
| 135 | + stub := &stubEKS{} |
| 136 | + // The seeded pod is never removed, so the node never empties. |
| 137 | + client := fake.NewClientset(newMngNode(), &podOnNode) |
| 138 | + installEvictionReactor(client, apierrors.NewTooManyRequests("PDB blocked", 1)) |
| 139 | + opts := mngDrainOpts() |
| 140 | + opts.EvictionTimeout = 30 * time.Millisecond |
| 141 | + opts.NodeTimeout = 60 * time.Millisecond |
| 142 | + |
| 143 | + err := evictEKSManagedNodeGroup(t.Context(), stub, client, cluster, mng, []string{nodeName}, opts) |
| 144 | + require.Error(t, err) |
| 145 | + assert.ErrorIs(t, err, errEKSDrainIncomplete) |
| 146 | + assert.Empty(t, stub.gotInputs, "must not scale down while a node still holds workloads") |
| 147 | + }) |
| 148 | + |
| 149 | + t.Run("cordon failure keeps PDBs and skips scale-down", func(t *testing.T) { |
| 150 | + stub := &stubEKS{} |
| 151 | + client := fake.NewClientset(newMngNode()) |
| 152 | + client.PrependReactor("update", "nodes", func(_ clienttesting.Action) (bool, runtime.Object, error) { |
| 153 | + return true, nil, apierrors.NewInternalError(errors.New("cordon boom")) |
| 154 | + }) |
| 155 | + |
| 156 | + err := evictEKSManagedNodeGroup(t.Context(), stub, client, cluster, mng, []string{nodeName}, mngDrainOpts()) |
| 157 | + require.Error(t, err) |
| 158 | + assert.ErrorIs(t, err, errEKSDrainIncomplete) |
| 159 | + assert.Empty(t, stub.gotInputs) |
| 160 | + }) |
| 161 | + |
| 162 | + t.Run("UpdateNodegroupConfig failure after drain does not wrap the sentinel", func(t *testing.T) { |
| 163 | + stub := &stubEKS{err: errors.New("api error")} |
| 164 | + client := fake.NewClientset(newMngNode()) // no pods ⇒ the node drains immediately |
| 165 | + |
| 166 | + err := evictEKSManagedNodeGroup(t.Context(), stub, client, cluster, mng, []string{nodeName}, mngDrainOpts()) |
| 167 | + require.Error(t, err) |
| 168 | + assert.Contains(t, err.Error(), "UpdateNodegroupConfig") |
| 169 | + assert.NotErrorIs(t, err, errEKSDrainIncomplete) |
| 170 | + require.Len(t, stub.gotInputs, 1) |
| 171 | + }) |
| 172 | + |
| 173 | + t.Run("post-scale wait timeout wraps the sentinel", func(t *testing.T) { |
| 174 | + stub := &stubEKS{} |
| 175 | + client := fake.NewClientset(newMngNode()) // no pods ⇒ the node drains immediately |
| 176 | + // The instance never terminates, so waitEKSNodegroupEmpty times out. |
| 177 | + installNodeListReactor(client, []corev1.Node{*newMngNode()}) |
| 178 | + opts := mngDrainOpts() |
| 179 | + opts.NodeTimeout = 60 * time.Millisecond |
| 180 | + |
| 181 | + err := evictEKSManagedNodeGroup(t.Context(), stub, client, cluster, mng, []string{nodeName}, opts) |
| 182 | + require.Error(t, err) |
| 183 | + assert.ErrorIs(t, err, errEKSDrainIncomplete) |
| 184 | + assert.Contains(t, err.Error(), mng) |
| 185 | + require.Len(t, stub.gotInputs, 1) |
| 186 | + }) |
| 187 | + |
| 188 | + t.Run("post-scale node-list error keeps the PDBs (wraps the sentinel)", func(t *testing.T) { |
| 189 | + stub := &stubEKS{} |
| 190 | + client := fake.NewClientset(newMngNode()) // no pods ⇒ the node drains immediately |
| 191 | + // The scale-down succeeded, but the Nodes().List used to confirm the |
| 192 | + // group emptied errors out, so the drain cannot be confirmed complete. |
| 193 | + client.PrependReactor("list", "nodes", func(_ clienttesting.Action) (bool, runtime.Object, error) { |
| 194 | + return true, nil, apierrors.NewInternalError(errors.New("apiserver unreachable")) |
| 195 | + }) |
| 196 | + |
| 197 | + err := evictEKSManagedNodeGroup(t.Context(), stub, client, cluster, mng, []string{nodeName}, mngDrainOpts()) |
| 198 | + require.Error(t, err) |
| 199 | + assert.ErrorIs(t, err, errEKSDrainIncomplete) |
| 200 | + require.Len(t, stub.gotInputs, 1) |
| 201 | + }) |
| 202 | +} |
0 commit comments