-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconciler_test.go
More file actions
343 lines (297 loc) · 8.09 KB
/
reconciler_test.go
File metadata and controls
343 lines (297 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"context"
"testing"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
func newTestScheme() *runtime.Scheme {
s := runtime.NewScheme()
_ = clientgoscheme.AddToScheme(s)
return s
}
func newNode(name string, ready corev1.ConditionStatus, taints ...corev1.Taint) *corev1.Node {
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: corev1.NodeSpec{
Taints: taints,
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: ready,
},
},
},
}
}
func outOfServiceTaint() corev1.Taint {
return corev1.Taint{
Key: TaintKey,
Value: TaintValue,
Effect: TaintEffect,
}
}
func TestIsNodeReady(t *testing.T) {
r := &NodeTaintReconciler{}
tests := []struct {
name string
node *corev1.Node
expected bool
}{
{
name: "node is ready",
node: newNode("test", corev1.ConditionTrue),
expected: true,
},
{
name: "node is not ready",
node: newNode("test", corev1.ConditionFalse),
expected: false,
},
{
name: "node ready status unknown",
node: newNode("test", corev1.ConditionUnknown),
expected: false,
},
{
name: "node has no ready condition",
node: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
Status: corev1.NodeStatus{Conditions: []corev1.NodeCondition{}},
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := r.isNodeReady(tt.node); got != tt.expected {
t.Errorf("isNodeReady() = %v, want %v", got, tt.expected)
}
})
}
}
func TestHasOutOfServiceTaint(t *testing.T) {
r := &NodeTaintReconciler{}
tests := []struct {
name string
node *corev1.Node
expected bool
}{
{
name: "has out-of-service taint",
node: newNode("test", corev1.ConditionFalse, outOfServiceTaint()),
expected: true,
},
{
name: "no taints",
node: newNode("test", corev1.ConditionFalse),
expected: false,
},
{
name: "has other taint",
node: newNode("test", corev1.ConditionFalse, corev1.Taint{
Key: "node.kubernetes.io/unschedulable",
Effect: corev1.TaintEffectNoSchedule,
}),
expected: false,
},
{
name: "has taint with same key but different effect",
node: newNode("test", corev1.ConditionFalse, corev1.Taint{
Key: TaintKey,
Value: TaintValue,
Effect: corev1.TaintEffectNoSchedule,
}),
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := r.hasOutOfServiceTaint(tt.node); got != tt.expected {
t.Errorf("hasOutOfServiceTaint() = %v, want %v", got, tt.expected)
}
})
}
}
func TestTrackNotReady(t *testing.T) {
r := &NodeTaintReconciler{
NotReadySince: make(map[string]time.Time),
}
// First call should return 0 and start tracking
d1 := r.trackNotReady("node1")
if d1 != 0 {
t.Errorf("first trackNotReady() = %v, want 0", d1)
}
if _, ok := r.NotReadySince["node1"]; !ok {
t.Error("node1 should be tracked")
}
// Small sleep to ensure time passes
time.Sleep(10 * time.Millisecond)
// Second call should return positive duration
d2 := r.trackNotReady("node1")
if d2 <= 0 {
t.Errorf("second trackNotReady() = %v, want > 0", d2)
}
// Clear tracking
r.clearTracking("node1")
if _, ok := r.NotReadySince["node1"]; ok {
t.Error("node1 should not be tracked after clear")
}
}
func TestReconcile_NotReadyNodeGetsTainted(t *testing.T) {
node := newNode("test-node", corev1.ConditionFalse)
client := fake.NewClientBuilder().
WithScheme(newTestScheme()).
WithObjects(node).
Build()
r := &NodeTaintReconciler{
Client: client,
NotReadyThreshold: 50 * time.Millisecond,
ReconcileInterval: 30 * time.Second,
NotReadySince: make(map[string]time.Time),
}
ctx := context.Background()
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "test-node"}}
// First reconcile: starts tracking, doesn't taint yet
result, err := r.Reconcile(ctx, req)
if err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
if result.RequeueAfter <= 0 {
t.Error("expected positive RequeueAfter")
}
var updated corev1.Node
if err := client.Get(ctx, req.NamespacedName, &updated); err != nil {
t.Fatal(err)
}
if r.hasOutOfServiceTaint(&updated) {
t.Error("node should not be tainted yet")
}
// Wait for threshold
time.Sleep(60 * time.Millisecond)
// Second reconcile: should apply taint
_, err = r.Reconcile(ctx, req)
if err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
if err := client.Get(ctx, req.NamespacedName, &updated); err != nil {
t.Fatal(err)
}
if !r.hasOutOfServiceTaint(&updated) {
t.Error("node should be tainted after threshold")
}
}
func TestReconcile_ReadyNodeGetsTaintRemoved(t *testing.T) {
node := newNode("test-node", corev1.ConditionTrue, outOfServiceTaint())
client := fake.NewClientBuilder().
WithScheme(newTestScheme()).
WithObjects(node).
Build()
r := &NodeTaintReconciler{
Client: client,
NotReadyThreshold: 5 * time.Minute,
ReconcileInterval: 30 * time.Second,
NotReadySince: make(map[string]time.Time),
}
ctx := context.Background()
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "test-node"}}
_, err := r.Reconcile(ctx, req)
if err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
var updated corev1.Node
if err := client.Get(ctx, req.NamespacedName, &updated); err != nil {
t.Fatal(err)
}
if r.hasOutOfServiceTaint(&updated) {
t.Error("taint should be removed from ready node")
}
}
func TestReconcile_ReadyNodeClearsTracking(t *testing.T) {
node := newNode("test-node", corev1.ConditionTrue)
client := fake.NewClientBuilder().
WithScheme(newTestScheme()).
WithObjects(node).
Build()
r := &NodeTaintReconciler{
Client: client,
NotReadyThreshold: 5 * time.Minute,
ReconcileInterval: 30 * time.Second,
NotReadySince: map[string]time.Time{
"test-node": time.Now().Add(-10 * time.Minute),
},
}
ctx := context.Background()
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "test-node"}}
_, err := r.Reconcile(ctx, req)
if err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
if _, ok := r.NotReadySince["test-node"]; ok {
t.Error("tracking should be cleared for ready node")
}
}
func TestReconcile_AlreadyTaintedNodeStaysTainted(t *testing.T) {
node := newNode("test-node", corev1.ConditionFalse, outOfServiceTaint())
client := fake.NewClientBuilder().
WithScheme(newTestScheme()).
WithObjects(node).
Build()
r := &NodeTaintReconciler{
Client: client,
NotReadyThreshold: 5 * time.Minute,
ReconcileInterval: 30 * time.Second,
NotReadySince: make(map[string]time.Time),
}
ctx := context.Background()
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "test-node"}}
_, err := r.Reconcile(ctx, req)
if err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
var updated corev1.Node
if err := client.Get(ctx, req.NamespacedName, &updated); err != nil {
t.Fatal(err)
}
taintCount := 0
for _, taint := range updated.Spec.Taints {
if taint.Key == TaintKey {
taintCount++
}
}
if taintCount != 1 {
t.Errorf("expected exactly 1 out-of-service taint, got %d", taintCount)
}
}
func TestReconcile_DeletedNodeClearsTracking(t *testing.T) {
client := fake.NewClientBuilder().
WithScheme(newTestScheme()).
Build()
r := &NodeTaintReconciler{
Client: client,
NotReadyThreshold: 5 * time.Minute,
ReconcileInterval: 30 * time.Second,
NotReadySince: map[string]time.Time{
"deleted-node": time.Now().Add(-10 * time.Minute),
},
}
ctx := context.Background()
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "deleted-node"}}
_, err := r.Reconcile(ctx, req)
if err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
if _, ok := r.NotReadySince["deleted-node"]; ok {
t.Error("tracking should be cleared for deleted node")
}
}