forked from longhorn/longhorn-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplica_scheduler_test.go
736 lines (690 loc) · 23.7 KB
/
replica_scheduler_test.go
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
package scheduler
import (
"context"
"fmt"
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/kubernetes/pkg/controller"
"github.com/longhorn/longhorn-manager/datastore"
"github.com/longhorn/longhorn-manager/types"
"github.com/longhorn/longhorn-manager/util"
longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
lhfake "github.com/longhorn/longhorn-manager/k8s/pkg/client/clientset/versioned/fake"
lhinformerfactory "github.com/longhorn/longhorn-manager/k8s/pkg/client/informers/externalversions"
. "gopkg.in/check.v1"
)
const (
TestNamespace = "default"
TestIP1 = "1.2.3.4"
TestIP2 = "5.6.7.8"
TestIP3 = "9.10.11.12"
TestNode1 = "test-node-name-1"
TestNode2 = "test-node-name-2"
TestNode3 = "test-node-name-3"
TestOwnerID1 = TestNode1
TestEngineImage = "longhorn-engine:latest"
TestVolumeName = "test-volume"
TestVolumeSize = 1073741824
TestVolumeStaleTimeout = 60
TestDefaultDataPath = "/var/lib/longhorn"
TestDaemon1 = "longhorn-manager-1"
TestDaemon2 = "longhorn-manager-2"
TestDaemon3 = "longhorn-manager-3"
// TestDiskID1 = "diskID1"
// TestDiskID2 = "diskID2"
TestDiskSize = 5000000000
TestDiskAvailableSize = 3000000000
)
var longhornFinalizerKey = longhorn.SchemeGroupVersion.Group
func newReplicaScheduler(lhInformerFactory lhinformerfactory.SharedInformerFactory, kubeInformerFactory informers.SharedInformerFactory,
lhClient *lhfake.Clientset, kubeClient *fake.Clientset) *ReplicaScheduler {
fmt.Printf("testing NewReplicaScheduler\n")
ds := datastore.NewDataStore(lhInformerFactory, lhClient, kubeInformerFactory, kubeClient, TestNamespace)
return NewReplicaScheduler(ds)
}
func newDaemonPod(phase v1.PodPhase, name, namespace, nodeID, podIP string) *v1.Pod {
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "longhorn-manager",
},
},
Spec: v1.PodSpec{
NodeName: nodeID,
},
Status: v1.PodStatus{
Phase: phase,
PodIP: podIP,
},
}
}
func newNode(name, namespace string, allowScheduling bool, status longhorn.ConditionStatus) *longhorn.Node {
return &longhorn.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: longhorn.NodeSpec{
AllowScheduling: allowScheduling,
},
Status: longhorn.NodeStatus{
Conditions: []longhorn.Condition{
newCondition(longhorn.NodeConditionTypeSchedulable, status),
newCondition(longhorn.NodeConditionTypeReady, status),
},
},
}
}
func newEngineImage(image string, state longhorn.EngineImageState) *longhorn.EngineImage {
return &longhorn.EngineImage{
ObjectMeta: metav1.ObjectMeta{
Name: types.GetEngineImageChecksumName(image),
Namespace: TestNamespace,
UID: uuid.NewUUID(),
Finalizers: []string{longhornFinalizerKey},
},
Spec: longhorn.EngineImageSpec{
Image: image,
},
Status: longhorn.EngineImageStatus{
OwnerID: TestNode1,
State: state,
EngineVersionDetails: longhorn.EngineVersionDetails{
Version: "latest",
GitCommit: "latest",
CLIAPIVersion: 4,
CLIAPIMinVersion: 3,
ControllerAPIVersion: 3,
ControllerAPIMinVersion: 3,
DataFormatVersion: 1,
DataFormatMinVersion: 1,
},
Conditions: []longhorn.Condition{
{
Type: longhorn.EngineImageConditionTypeReady,
Status: longhorn.ConditionStatusTrue,
},
},
NodeDeploymentMap: map[string]bool{},
},
}
}
func newCondition(conditionType string, status longhorn.ConditionStatus) longhorn.Condition {
return longhorn.Condition{
Type: conditionType,
Status: status,
Reason: "",
Message: "",
}
}
func newDisk(path string, allowScheduling bool, storageReserved int64) longhorn.DiskSpec {
return longhorn.DiskSpec{
Path: path,
AllowScheduling: allowScheduling,
StorageReserved: storageReserved,
}
}
func newVolume(name string, replicaCount int) *longhorn.Volume {
return &longhorn.Volume{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Finalizers: []string{
longhorn.SchemeGroupVersion.Group,
},
},
Spec: longhorn.VolumeSpec{
NumberOfReplicas: replicaCount,
Size: TestVolumeSize,
StaleReplicaTimeout: TestVolumeStaleTimeout,
EngineImage: TestEngineImage,
},
Status: longhorn.VolumeStatus{
OwnerID: TestOwnerID1,
},
}
}
func newReplicaForVolume(v *longhorn.Volume) *longhorn.Replica {
return &longhorn.Replica{
ObjectMeta: metav1.ObjectMeta{
Name: v.Name + "-r-" + util.RandomID(),
Labels: map[string]string{
"longhornvolume": v.Name,
},
},
Spec: longhorn.ReplicaSpec{
InstanceSpec: longhorn.InstanceSpec{
VolumeName: v.Name,
VolumeSize: v.Spec.Size,
EngineImage: TestEngineImage,
DesireState: longhorn.InstanceStateStopped,
},
},
Status: longhorn.ReplicaStatus{
InstanceStatus: longhorn.InstanceStatus{
OwnerID: v.Status.OwnerID,
},
},
}
}
func getDiskID(nodeID, index string) string {
return fmt.Sprintf("%s-disk%s", nodeID, index)
}
func initSettings(name, value string) *longhorn.Setting {
setting := &longhorn.Setting{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Value: value,
}
return setting
}
func Test(t *testing.T) { TestingT(t) }
type TestSuite struct {
}
var _ = Suite(&TestSuite{})
func (s *TestSuite) SetUpTest(c *C) {
}
type ReplicaSchedulerTestCase struct {
volume *longhorn.Volume
replicas map[string]*longhorn.Replica
daemons []*v1.Pod
nodes map[string]*longhorn.Node
engineImage *longhorn.EngineImage
storageOverProvisioningPercentage string
storageMinimalAvailablePercentage string
replicaNodeSoftAntiAffinity string
// schedule state
expectedNodes map[string]*longhorn.Node
// scheduler exception
err bool
// couldn't schedule replica
isNilReplica bool
}
func generateSchedulerTestCase() *ReplicaSchedulerTestCase {
v := newVolume(TestVolumeName, 2)
replica1 := newReplicaForVolume(v)
replica2 := newReplicaForVolume(v)
replicas := map[string]*longhorn.Replica{
replica1.Name: replica1,
replica2.Name: replica2,
}
engineImage := newEngineImage(TestEngineImage, longhorn.EngineImageStateDeployed)
return &ReplicaSchedulerTestCase{
volume: v,
replicas: replicas,
engineImage: engineImage,
}
}
func (s *TestSuite) TestReplicaScheduler(c *C) {
testCases := map[string]*ReplicaSchedulerTestCase{}
// Test only node1 could schedule replica
tc := generateSchedulerTestCase()
daemon1 := newDaemonPod(v1.PodRunning, TestDaemon1, TestNamespace, TestNode1, TestIP1)
daemon2 := newDaemonPod(v1.PodRunning, TestDaemon2, TestNamespace, TestNode2, TestIP2)
daemon3 := newDaemonPod(v1.PodRunning, TestDaemon3, TestNamespace, TestNode3, TestIP3)
tc.daemons = []*v1.Pod{
daemon1,
daemon2,
daemon3,
}
node1 := newNode(TestNode1, TestNamespace, true, longhorn.ConditionStatusTrue)
disk := newDisk(TestDefaultDataPath, true, 0)
node1.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode1, "1"): disk,
}
node1.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode1, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode1, "1"),
},
}
tc.engineImage.Status.NodeDeploymentMap[node1.Name] = true
node2 := newNode(TestNode2, TestNamespace, false, longhorn.ConditionStatusTrue)
disk = newDisk(TestDefaultDataPath, true, 0)
node2.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode2, "1"): disk,
}
node2.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode2, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
DiskUUID: getDiskID(TestNode2, "1"),
},
}
tc.engineImage.Status.NodeDeploymentMap[node2.Name] = true
node3 := newNode(TestNode3, TestNamespace, true, longhorn.ConditionStatusFalse)
disk = newDisk(TestDefaultDataPath, true, 0)
node3.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode3, "1"): disk,
}
node3.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode3, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
DiskUUID: getDiskID(TestNode3, "1"),
},
}
tc.engineImage.Status.NodeDeploymentMap[node3.Name] = true
nodes := map[string]*longhorn.Node{
TestNode1: node1,
TestNode2: node2,
TestNode3: node3,
}
tc.nodes = nodes
expectedNodes := map[string]*longhorn.Node{
TestNode1: node1,
}
tc.expectedNodes = expectedNodes
tc.err = false
tc.isNilReplica = false
// Set replica node soft anti-affinity
tc.replicaNodeSoftAntiAffinity = "true"
testCases["nodes could not schedule"] = tc
// Test no disks on each nodes, volume should not schedule to any node
tc = generateSchedulerTestCase()
daemon1 = newDaemonPod(v1.PodRunning, TestDaemon1, TestNamespace, TestNode1, TestIP1)
daemon2 = newDaemonPod(v1.PodRunning, TestDaemon2, TestNamespace, TestNode2, TestIP2)
tc.daemons = []*v1.Pod{
daemon1,
daemon2,
}
node1 = newNode(TestNode1, TestNamespace, true, longhorn.ConditionStatusTrue)
tc.engineImage.Status.NodeDeploymentMap[node1.Name] = true
node2 = newNode(TestNode2, TestNamespace, true, longhorn.ConditionStatusTrue)
tc.engineImage.Status.NodeDeploymentMap[node2.Name] = true
nodes = map[string]*longhorn.Node{
TestNode1: node1,
TestNode2: node2,
}
tc.nodes = nodes
expectedNodes = map[string]*longhorn.Node{}
tc.expectedNodes = expectedNodes
tc.err = false
tc.isNilReplica = true
testCases["there's no disk for replica"] = tc
// Test engine image is not deployed on any node
tc = generateSchedulerTestCase()
daemon1 = newDaemonPod(v1.PodRunning, TestDaemon1, TestNamespace, TestNode1, TestIP1)
daemon2 = newDaemonPod(v1.PodRunning, TestDaemon2, TestNamespace, TestNode2, TestIP2)
tc.daemons = []*v1.Pod{
daemon1,
daemon2,
}
node1 = newNode(TestNode1, TestNamespace, true, longhorn.ConditionStatusTrue)
disk = newDisk(TestDefaultDataPath, true, 0)
node1.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode1, "1"): disk,
}
node1.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode1, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode1, "1"),
},
}
node2 = newNode(TestNode2, TestNamespace, true, longhorn.ConditionStatusTrue)
disk = newDisk(TestDefaultDataPath, true, 0)
node2.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode2, "1"): disk,
}
node2.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode2, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode2, "1"),
},
}
nodes = map[string]*longhorn.Node{
TestNode1: node1,
TestNode2: node2,
}
tc.nodes = nodes
expectedNodes = map[string]*longhorn.Node{}
tc.expectedNodes = expectedNodes
tc.err = false
tc.isNilReplica = true
testCases["there's no engine image deployed on any node"] = tc
// Test anti affinity nodes, replica should schedule to both node1 and node2
tc = generateSchedulerTestCase()
daemon1 = newDaemonPod(v1.PodRunning, TestDaemon1, TestNamespace, TestNode1, TestIP1)
daemon2 = newDaemonPod(v1.PodRunning, TestDaemon2, TestNamespace, TestNode2, TestIP2)
tc.daemons = []*v1.Pod{
daemon1,
daemon2,
}
node1 = newNode(TestNode1, TestNamespace, true, longhorn.ConditionStatusTrue)
tc.engineImage.Status.NodeDeploymentMap[node1.Name] = true
disk = newDisk(TestDefaultDataPath, true, 0)
disk2 := newDisk(TestDefaultDataPath, true, TestDiskSize)
node1.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode1, "1"): disk,
getDiskID(TestNode1, "2"): disk2,
}
node1.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode1, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode1, "1"),
},
getDiskID(TestNode1, "2"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode1, "2"),
},
}
expectNode1 := newNode(TestNode1, TestNamespace, true, longhorn.ConditionStatusTrue)
expectNode1.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode1, "1"): disk,
}
node2 = newNode(TestNode2, TestNamespace, true, longhorn.ConditionStatusTrue)
tc.engineImage.Status.NodeDeploymentMap[node2.Name] = true
disk = newDisk(TestDefaultDataPath, true, 0)
disk2 = newDisk(TestDefaultDataPath, true, 0)
node2.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode2, "1"): disk,
getDiskID(TestNode2, "2"): disk2,
}
node2.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode2, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode2, "1"),
},
getDiskID(TestNode2, "2"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusFalse),
},
DiskUUID: getDiskID(TestNode2, "2"),
},
}
expectNode2 := newNode(TestNode2, TestNamespace, true, longhorn.ConditionStatusTrue)
expectNode2.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode2, "1"): disk,
}
nodes = map[string]*longhorn.Node{
TestNode1: node1,
TestNode2: node2,
}
tc.nodes = nodes
expectedNodes = map[string]*longhorn.Node{
TestNode1: expectNode1,
TestNode2: expectNode2,
}
tc.expectedNodes = expectedNodes
tc.err = false
tc.isNilReplica = false
testCases["anti-affinity nodes"] = tc
// Test scheduler error when replica.NodeID is not ""
tc = generateSchedulerTestCase()
replicas := tc.replicas
for _, replica := range replicas {
replica.Spec.NodeID = TestNode1
}
tc.err = true
tc.isNilReplica = true
testCases["scheduler error when replica has NodeID"] = tc
// Test no available disks
tc = generateSchedulerTestCase()
daemon1 = newDaemonPod(v1.PodRunning, TestDaemon1, TestNamespace, TestNode1, TestIP1)
daemon2 = newDaemonPod(v1.PodRunning, TestDaemon2, TestNamespace, TestNode2, TestIP2)
tc.daemons = []*v1.Pod{
daemon1,
daemon2,
}
node1 = newNode(TestNode1, TestNamespace, true, longhorn.ConditionStatusTrue)
tc.engineImage.Status.NodeDeploymentMap[node1.Name] = true
disk = newDisk(TestDefaultDataPath, true, TestDiskSize)
node1.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode1, "1"): disk,
}
node1.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode1, "1"): {
StorageAvailable: 0,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode1, "1"),
},
}
node2 = newNode(TestNode2, TestNamespace, true, longhorn.ConditionStatusTrue)
tc.engineImage.Status.NodeDeploymentMap[node1.Name] = true
disk = newDisk(TestDefaultDataPath, true, 0)
disk2 = newDisk(TestDefaultDataPath, true, 0)
node2.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode2, "1"): disk,
getDiskID(TestNode2, "2"): disk2,
}
node2.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode2, "1"): {
StorageAvailable: 0,
StorageScheduled: TestDiskAvailableSize,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode2, "1"),
},
getDiskID(TestNode2, "2"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusFalse),
},
DiskUUID: getDiskID(TestNode2, "2"),
},
}
nodes = map[string]*longhorn.Node{
TestNode1: node1,
TestNode2: node2,
}
tc.nodes = nodes
expectedNodes = map[string]*longhorn.Node{}
tc.expectedNodes = expectedNodes
tc.err = false
tc.isNilReplica = true
tc.storageOverProvisioningPercentage = "0"
tc.storageMinimalAvailablePercentage = "100"
testCases["there's no available disks for scheduling"] = tc
// Test no available disks due to volume.Status.ActualSize
tc = generateSchedulerTestCase()
daemon1 = newDaemonPod(v1.PodRunning, TestDaemon1, TestNamespace, TestNode1, TestIP1)
daemon2 = newDaemonPod(v1.PodRunning, TestDaemon2, TestNamespace, TestNode2, TestIP2)
tc.daemons = []*v1.Pod{
daemon1,
daemon2,
}
node1 = newNode(TestNode1, TestNamespace, true, longhorn.ConditionStatusTrue)
tc.engineImage.Status.NodeDeploymentMap[node1.Name] = true
disk = newDisk(TestDefaultDataPath, true, TestDiskSize)
node1.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode1, "1"): disk,
}
node1.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode1, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode1, "1"),
},
}
node2 = newNode(TestNode2, TestNamespace, true, longhorn.ConditionStatusTrue)
tc.engineImage.Status.NodeDeploymentMap[node2.Name] = true
disk = newDisk(TestDefaultDataPath, true, 0)
disk2 = newDisk(TestDefaultDataPath, true, 0)
node2.Spec.Disks = map[string]longhorn.DiskSpec{
getDiskID(TestNode2, "1"): disk,
getDiskID(TestNode2, "2"): disk2,
}
node2.Status.DiskStatus = map[string]*longhorn.DiskStatus{
getDiskID(TestNode2, "1"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode2, "1"),
},
getDiskID(TestNode2, "2"): {
StorageAvailable: TestDiskAvailableSize,
StorageScheduled: 0,
StorageMaximum: TestDiskSize,
Conditions: []longhorn.Condition{
newCondition(longhorn.DiskConditionTypeSchedulable, longhorn.ConditionStatusTrue),
},
DiskUUID: getDiskID(TestNode2, "2"),
},
}
nodes = map[string]*longhorn.Node{
TestNode1: node1,
TestNode2: node2,
}
tc.nodes = nodes
tc.volume.Status.ActualSize = TestDiskAvailableSize - TestDiskSize*0.2
expectedNodes = map[string]*longhorn.Node{}
tc.expectedNodes = expectedNodes
tc.err = false
tc.isNilReplica = true
tc.storageOverProvisioningPercentage = "200"
tc.storageMinimalAvailablePercentage = "20"
testCases["there's no available disks for scheduling due to required storage"] = tc
for name, tc := range testCases {
fmt.Printf("testing %v\n", name)
kubeClient := fake.NewSimpleClientset()
kubeInformerFactory := informers.NewSharedInformerFactory(kubeClient, controller.NoResyncPeriodFunc())
lhClient := lhfake.NewSimpleClientset()
lhInformerFactory := lhinformerfactory.NewSharedInformerFactory(lhClient, controller.NoResyncPeriodFunc())
vIndexer := lhInformerFactory.Longhorn().V1beta2().Volumes().Informer().GetIndexer()
rIndexer := lhInformerFactory.Longhorn().V1beta2().Replicas().Informer().GetIndexer()
nIndexer := lhInformerFactory.Longhorn().V1beta2().Nodes().Informer().GetIndexer()
eiIndexer := lhInformerFactory.Longhorn().V1beta2().EngineImages().Informer().GetIndexer()
sIndexer := lhInformerFactory.Longhorn().V1beta2().Settings().Informer().GetIndexer()
pIndexer := kubeInformerFactory.Core().V1().Pods().Informer().GetIndexer()
s := newReplicaScheduler(lhInformerFactory, kubeInformerFactory, lhClient, kubeClient)
// create daemon pod
for _, daemon := range tc.daemons {
p, err := kubeClient.CoreV1().Pods(TestNamespace).Create(context.TODO(), daemon, metav1.CreateOptions{})
c.Assert(err, IsNil)
err = pIndexer.Add(p)
c.Assert(err, IsNil)
}
// create node
for _, node := range tc.nodes {
n, err := lhClient.LonghornV1beta2().Nodes(TestNamespace).Create(context.TODO(), node, metav1.CreateOptions{})
c.Assert(err, IsNil)
c.Assert(n, NotNil)
err = nIndexer.Add(n)
c.Assert(err, IsNil)
}
// Create engine image
ei, err := lhClient.LonghornV1beta2().EngineImages(TestNamespace).Create(context.TODO(), tc.engineImage, metav1.CreateOptions{})
c.Assert(err, IsNil)
c.Assert(ei, NotNil)
err = eiIndexer.Add(ei)
c.Assert(err, IsNil)
// create volume
volume, err := lhClient.LonghornV1beta2().Volumes(TestNamespace).Create(context.TODO(), tc.volume, metav1.CreateOptions{})
c.Assert(err, IsNil)
c.Assert(volume, NotNil)
err = vIndexer.Add(volume)
c.Assert(err, IsNil)
// set settings
if tc.storageOverProvisioningPercentage != "" && tc.storageMinimalAvailablePercentage != "" {
s := initSettings(string(types.SettingNameStorageOverProvisioningPercentage), tc.storageOverProvisioningPercentage)
setting, err := lhClient.LonghornV1beta2().Settings(TestNamespace).Create(context.TODO(), s, metav1.CreateOptions{})
c.Assert(err, IsNil)
err = sIndexer.Add(setting)
c.Assert(err, IsNil)
s = initSettings(string(types.SettingNameStorageMinimalAvailablePercentage), tc.storageMinimalAvailablePercentage)
setting, err = lhClient.LonghornV1beta2().Settings(TestNamespace).Create(context.TODO(), s, metav1.CreateOptions{})
c.Assert(err, IsNil)
err = sIndexer.Add(setting)
c.Assert(err, IsNil)
}
// Set replica node soft anti-affinity setting
if tc.replicaNodeSoftAntiAffinity != "" {
s := initSettings(
string(types.SettingNameReplicaSoftAntiAffinity),
tc.replicaNodeSoftAntiAffinity)
setting, err :=
lhClient.LonghornV1beta2().Settings(TestNamespace).Create(context.TODO(), s, metav1.CreateOptions{})
c.Assert(err, IsNil)
err = sIndexer.Add(setting)
c.Assert(err, IsNil)
}
// validate scheduler
for _, replica := range tc.replicas {
r, err := lhClient.LonghornV1beta2().Replicas(TestNamespace).Create(context.TODO(), replica, metav1.CreateOptions{})
c.Assert(err, IsNil)
c.Assert(r, NotNil)
err = rIndexer.Add(r)
c.Assert(err, IsNil)
sr, err := s.ScheduleReplica(r, tc.replicas, volume)
if tc.err {
c.Assert(err, NotNil)
} else {
if tc.isNilReplica {
c.Assert(sr, IsNil)
} else {
c.Assert(err, IsNil)
c.Assert(sr, NotNil)
c.Assert(sr.Spec.NodeID, Not(Equals), "")
c.Assert(sr.Spec.DiskID, Not(Equals), "")
c.Assert(sr.Spec.DiskPath, Not(Equals), "")
c.Assert(sr.Spec.DataDirectoryName, Not(Equals), "")
tc.replicas[sr.Name] = sr
// check expected node
for nname, node := range tc.expectedNodes {
if sr.Spec.NodeID == nname {
c.Assert(sr.Spec.DiskPath, Equals, node.Spec.Disks[sr.Spec.DiskID].Path)
delete(tc.expectedNodes, nname)
}
}
}
}
}
c.Assert(len(tc.expectedNodes), Equals, 0)
}
}