forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_concurrency_test.go
1045 lines (953 loc) · 30.9 KB
/
operator_concurrency_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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package controller
import (
"context"
"encoding/json"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
argoErr "github.com/argoproj/argo-workflows/v3/errors"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/sync"
)
const configMap = `
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
workflow: "2"
template: "1"
step: "1"
`
const wfWithSemaphore = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: hello-world
namespace: default
spec:
entrypoint: whalesay
templates:
-
synchronization:
semaphore:
configMapKeyRef:
key: template
name: my-config
container:
args:
- "hello world"
command:
- cowsay
image: "docker/whalesay:latest"
name: whalesay
`
const ScriptWfWithSemaphore = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: script-wf
namespace: default
spec:
entrypoint: scriptTmpl
templates:
- name: scriptTmpl
synchronization:
semaphore:
configMapKeyRef:
key: template
name: my-config
script:
image: python:alpine3.6
command: ["python"]
# fail with a 66% probability
source: |
import random;
import sys;
exit_code = random.choice([0, 1, 1]);
sys.exit(exit_code)
`
const ScriptWfWithSemaphoreDifferentNamespace = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: script-wf
namespace: default
spec:
entrypoint: scriptTmpl
templates:
- name: scriptTmpl
synchronization:
semaphore:
namespace: other
configMapKeyRef:
key: template
name: my-config
script:
image: python:alpine3.6
command: ["python"]
# fail with a 66% probability
source: |
import random;
import sys;
exit_code = random.choice([0, 1, 1]);
sys.exit(exit_code)
`
const ResourceWfWithSemaphore = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: resource-wf
namespace: default
spec:
entrypoint: resourceTmpl
templates:
- name: resourceTmpl
synchronization:
semaphore:
configMapKeyRef:
key: template
name: my-config
resource:
action: create
manifest: |
apiVersion: v1
kind: ConfigMap
metadata:
name: workflow-controller-configmap1
`
var workflowExistenceFunc = func(key string) bool {
return true
}
func GetSyncLimitFunc(ctx context.Context, kube kubernetes.Interface) func(string) (int, error) {
syncLimitConfig := func(lockName string) (int, error) {
items := strings.Split(lockName, "/")
if len(items) < 4 {
return 0, argoErr.New(argoErr.CodeBadRequest, "Invalid Config Map Key")
}
configMap, err := kube.CoreV1().ConfigMaps(items[0]).Get(ctx, items[2], metav1.GetOptions{})
if err != nil {
return 0, err
}
value, found := configMap.Data[items[3]]
if !found {
return 0, argoErr.New(argoErr.CodeBadRequest, "Invalid Sync configuration Key")
}
return strconv.Atoi(value)
}
return syncLimitConfig
}
func TestSemaphoreTmplLevel(t *testing.T) {
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
var cm v1.ConfigMap
wfv1.MustUnmarshal([]byte(configMap), &cm)
_, err := controller.kubeclientset.CoreV1().ConfigMaps("default").Create(ctx, &cm, metav1.CreateOptions{})
assert.NoError(t, err)
t.Run("TmplLevelAcquireAndRelease", func(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(wfWithSemaphore)
wf.Name = "one"
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(t, err)
woc := newWorkflowOperationCtx(wf, controller)
// acquired the lock
woc.operate(ctx)
assert.NotNil(t, woc.wf.Status.Synchronization)
assert.NotNil(t, woc.wf.Status.Synchronization.Semaphore)
assert.Equal(t, 1, len(woc.wf.Status.Synchronization.Semaphore.Holding))
for _, node := range woc.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Try to Acquire the lock, But lock is not available
wf_Two := wf.DeepCopy()
wf_Two.Name = "two"
wf_Two, err = controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf_Two, metav1.CreateOptions{})
assert.NoError(t, err)
woc_two := newWorkflowOperationCtx(wf_Two, controller)
// Try Acquire the lock
woc_two.operate(ctx)
// Check Node status
err = woc_two.podReconciliation(ctx)
assert.NoError(t, err)
for _, node := range woc_two.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Updating Pod state
makePodsPhase(ctx, woc, v1.PodFailed)
// Release the lock
woc = newWorkflowOperationCtx(woc.wf, controller)
woc.operate(ctx)
assert.Nil(t, woc.wf.Status.Synchronization)
// Try to acquired the lock
woc_two = newWorkflowOperationCtx(woc_two.wf, controller)
woc_two.operate(ctx)
assert.NotNil(t, woc_two.wf.Status.Synchronization)
assert.NotNil(t, woc_two.wf.Status.Synchronization.Semaphore)
assert.Equal(t, 1, len(woc_two.wf.Status.Synchronization.Semaphore.Holding))
})
}
func TestSemaphoreScriptTmplLevel(t *testing.T) {
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
var cm v1.ConfigMap
wfv1.MustUnmarshal([]byte(configMap), &cm)
_, err := controller.kubeclientset.CoreV1().ConfigMaps("default").Create(ctx, &cm, metav1.CreateOptions{})
assert.NoError(t, err)
t.Run("ScriptTmplLevelAcquireAndRelease", func(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(ScriptWfWithSemaphore)
wf.Name = "one"
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(t, err)
woc := newWorkflowOperationCtx(wf, controller)
// acquired the lock
woc.operate(ctx)
assert.NotNil(t, woc.wf.Status.Synchronization)
assert.NotNil(t, woc.wf.Status.Synchronization.Semaphore)
assert.Equal(t, 1, len(woc.wf.Status.Synchronization.Semaphore.Holding))
for _, node := range woc.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Try to Acquire the lock, But lock is not available
wf_Two := wf.DeepCopy()
wf_Two.Name = "two"
wf_Two, err = controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf_Two, metav1.CreateOptions{})
assert.NoError(t, err)
woc_two := newWorkflowOperationCtx(wf_Two, controller)
// Try Acquire the lock
woc_two.operate(ctx)
// Check Node status
err = woc_two.podReconciliation(ctx)
assert.NoError(t, err)
for _, node := range woc_two.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Updating Pod state
makePodsPhase(ctx, woc, v1.PodFailed)
// Release the lock
woc = newWorkflowOperationCtx(woc.wf, controller)
woc.operate(ctx)
assert.Nil(t, woc.wf.Status.Synchronization)
// Try to acquired the lock
woc_two = newWorkflowOperationCtx(woc_two.wf, controller)
woc_two.operate(ctx)
assert.NotNil(t, woc_two.wf.Status.Synchronization)
assert.NotNil(t, woc_two.wf.Status.Synchronization.Semaphore)
assert.Equal(t, 1, len(woc_two.wf.Status.Synchronization.Semaphore.Holding))
})
}
func TestSemaphoreScriptConfigMapInDifferentNamespace(t *testing.T) {
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
var cm v1.ConfigMap
wfv1.MustUnmarshal([]byte(configMap), &cm)
_, err := controller.kubeclientset.CoreV1().ConfigMaps("other").Create(ctx, &cm, metav1.CreateOptions{})
assert.NoError(t, err)
t.Run("ScriptTmplLevelAcquireAndRelease", func(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(ScriptWfWithSemaphoreDifferentNamespace)
wf.Name = "one"
wf.Namespace = "namespace-one"
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(t, err)
woc := newWorkflowOperationCtx(wf, controller)
// acquired the lock
woc.operate(ctx)
assert.NotNil(t, woc.wf.Status.Synchronization)
assert.NotNil(t, woc.wf.Status.Synchronization.Semaphore)
assert.Equal(t, 1, len(woc.wf.Status.Synchronization.Semaphore.Holding))
for _, node := range woc.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Try to Acquire the lock, But lock is not available
wf_Two := wf.DeepCopy()
wf_Two.Name = "two"
wf_Two.Namespace = "namespace-two"
wf_Two, err = controller.wfclientset.ArgoprojV1alpha1().Workflows(wf_Two.Namespace).Create(ctx, wf_Two, metav1.CreateOptions{})
assert.NoError(t, err)
woc_two := newWorkflowOperationCtx(wf_Two, controller)
// Try Acquire the lock
woc_two.operate(ctx)
// Check Node status
err = woc_two.podReconciliation(ctx)
assert.NoError(t, err)
for _, node := range woc_two.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Updating Pod state
makePodsPhase(ctx, woc, v1.PodFailed)
// Release the lock
woc = newWorkflowOperationCtx(woc.wf, controller)
woc.operate(ctx)
assert.Nil(t, woc.wf.Status.Synchronization)
// Try to acquired the lock
woc_two = newWorkflowOperationCtx(woc_two.wf, controller)
woc_two.operate(ctx)
assert.NotNil(t, woc_two.wf.Status.Synchronization)
assert.NotNil(t, woc_two.wf.Status.Synchronization.Semaphore)
assert.Equal(t, 1, len(woc_two.wf.Status.Synchronization.Semaphore.Holding))
})
}
func TestSemaphoreResourceTmplLevel(t *testing.T) {
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
var cm v1.ConfigMap
wfv1.MustUnmarshal([]byte(configMap), &cm)
_, err := controller.kubeclientset.CoreV1().ConfigMaps("default").Create(ctx, &cm, metav1.CreateOptions{})
assert.NoError(t, err)
t.Run("ResourceTmplLevelAcquireAndRelease", func(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(ResourceWfWithSemaphore)
wf.Name = "one"
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(t, err)
woc := newWorkflowOperationCtx(wf, controller)
// acquired the lock
woc.operate(ctx)
assert.NotNil(t, woc.wf.Status.Synchronization)
assert.NotNil(t, woc.wf.Status.Synchronization.Semaphore)
assert.Equal(t, 1, len(woc.wf.Status.Synchronization.Semaphore.Holding))
for _, node := range woc.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Try to Acquire the lock, But lock is not available
wf_Two := wf.DeepCopy()
wf_Two.Name = "two"
wf_Two, err = controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf_Two, metav1.CreateOptions{})
assert.NoError(t, err)
woc_two := newWorkflowOperationCtx(wf_Two, controller)
// Try Acquire the lock
woc_two.operate(ctx)
// Check Node status
err = woc_two.podReconciliation(ctx)
assert.NoError(t, err)
for _, node := range woc_two.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Updating Pod state
makePodsPhase(ctx, woc, v1.PodFailed)
// Release the lock
woc = newWorkflowOperationCtx(woc.wf, controller)
woc.operate(ctx)
assert.Nil(t, woc.wf.Status.Synchronization)
// Try to acquired the lock
woc_two = newWorkflowOperationCtx(woc_two.wf, controller)
woc_two.operate(ctx)
assert.NotNil(t, woc_two.wf.Status.Synchronization)
assert.NotNil(t, woc_two.wf.Status.Synchronization.Semaphore)
assert.Equal(t, 1, len(woc_two.wf.Status.Synchronization.Semaphore.Holding))
})
}
func TestSemaphoreWithOutConfigMap(t *testing.T) {
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
t.Run("SemaphoreRefWithOutConfigMap", func(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(wfWithSemaphore)
wf.Name = "one"
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(t, err)
woc := newWorkflowOperationCtx(wf, controller)
err = woc.podReconciliation(ctx)
assert.NoError(t, err)
for _, node := range woc.wf.Status.Nodes {
assert.Equal(t, wfv1.NodePending, node.Phase)
}
// Acquire the lock
woc.operate(ctx)
assert.Nil(t, woc.wf.Status.Synchronization)
for _, node := range woc.wf.Status.Nodes {
assert.Equal(t, wfv1.NodeError, node.Phase)
}
})
}
var DAGWithMutex = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: dag-mutex
namespace: default
spec:
entrypoint: diamond
templates:
- name: diamond
dag:
tasks:
- name: A
template: mutex
- name: B
depends: A
template: mutex
- name: mutex
synchronization:
mutex:
name: welcome
container:
image: alpine:3.7
command: [sh, -c, "exit 0"]
`
func TestMutexInDAG(t *testing.T) {
assert := assert.New(t)
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
t.Run("MutexWithDAG", func(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(DAGWithMutex)
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(err)
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
for _, node := range woc.wf.Status.Nodes {
if node.Name == "dag-mutex.A" {
assert.Equal(wfv1.NodePending, node.Phase)
}
}
assert.Equal(wfv1.WorkflowRunning, woc.wf.Status.Phase)
makePodsPhase(ctx, woc, v1.PodSucceeded)
woc1 := newWorkflowOperationCtx(woc.wf, controller)
woc1.operate(ctx)
for _, node := range woc1.wf.Status.Nodes {
if node.Name == "dag-mutex.B" {
assert.Nil(node.SynchronizationStatus)
assert.Equal(wfv1.NodePending, node.Phase)
}
}
})
}
var DAGWithInterpolatedMutex = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: dag-mutex
namespace: default
spec:
entrypoint: diamond
templates:
- name: diamond
dag:
tasks:
- name: A
template: mutex
arguments:
parameters:
- name: message
value: foo/bar
- name: B
depends: A
template: mutex
arguments:
parameters:
- name: message
value: foo/bar
- name: mutex
synchronization:
mutex:
name: '{{=sprig.replace("/", "-", inputs.parameters.message)}}'
inputs:
parameters:
- name: message
container:
image: alpine:3.7
command: [sh, -c, "echo {{inputs.parameters.message}}"]
`
func TestMutexInDAGWithInterpolation(t *testing.T) {
assert := assert.New(t)
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
t.Run("InterpolatedMutexWithDAG", func(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(DAGWithInterpolatedMutex)
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(err)
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
for _, node := range woc.wf.Status.Nodes {
if node.Name == "dag-mutex.A" {
assert.Equal(wfv1.NodePending, node.Phase)
}
}
assert.Equal(wfv1.WorkflowRunning, woc.wf.Status.Phase)
makePodsPhase(ctx, woc, v1.PodSucceeded)
woc1 := newWorkflowOperationCtx(woc.wf, controller)
woc1.operate(ctx)
for _, node := range woc1.wf.Status.Nodes {
assert.NotEqual(wfv1.NodeError, node.Phase)
if node.Name == "dag-mutex.B" {
assert.Nil(node.SynchronizationStatus)
assert.Equal(wfv1.NodePending, node.Phase)
}
}
})
}
const RetryWfWithSemaphore = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: script-wf
namespace: default
spec:
entrypoint: step1
retryStrategy:
limit: 10
templates:
- name: step1
steps:
- - name: hello1
template: whalesay
- - name: hello2
template: whalesay
- name: whalesay
daemon: true
synchronization:
semaphore:
configMapKeyRef:
key: template
name: my-config
container:
args:
- "hello world"
command:
- cowsay
image: "docker/whalesay:latest"
`
func TestSynchronizationWithRetry(t *testing.T) {
assert := assert.New(t)
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
var cm v1.ConfigMap
wfv1.MustUnmarshal([]byte(configMap), &cm)
_, err := controller.kubeclientset.CoreV1().ConfigMaps("default").Create(ctx, &cm, metav1.CreateOptions{})
assert.NoError(err)
t.Run("WorkflowWithRetry", func(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(RetryWfWithSemaphore)
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(err)
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
for _, node := range woc.wf.Status.Nodes {
if node.Name == "hello1" {
assert.Equal(wfv1.NodePending, node.Phase)
}
}
// Updating Pod state
makePodsPhase(ctx, woc, v1.PodSucceeded)
// Release the lock from hello1
woc = newWorkflowOperationCtx(woc.wf, controller)
woc.operate(ctx)
for _, node := range woc.wf.Status.Nodes {
if node.Name == "hello1" {
assert.Equal(wfv1.NodeSucceeded, node.Phase)
}
if node.Name == "hello2" {
assert.Equal(wfv1.NodePending, node.Phase)
}
}
// Updating Pod state
makePodsPhase(ctx, woc, v1.PodSucceeded)
// Release the lock from hello2
woc = newWorkflowOperationCtx(woc.wf, controller)
woc.operate(ctx)
// Nobody is waiting for the lock
assert.Nil(woc.wf.Status.Synchronization)
})
}
const StepWithSync = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: steps-jklcl
namespace: default
spec:
entrypoint: hello-hello-hello
templates:
-
name: hello-hello-hello
steps:
- - arguments:
parameters:
- name: message
value: hello1
name: hello1
template: whalesay
synchronization:
semaphore:
configMapKeyRef:
key: step
name: my-config
-
container:
args:
- '{{inputs.parameters.message}}'
command:
- cowsay
image: docker/whalesay
inputs:
parameters:
- name: message
name: whalesay
`
const StepWithSyncStatus = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: steps-jklcl
namespace: default
spec:
entrypoint: hello-hello-hello
templates:
- inputs: {}
name: hello-hello-hello
steps:
- - arguments:
parameters:
- name: message
value: hello1
name: hello1
template: whalesay
synchronization:
semaphore:
configMapKeyRef:
key: step
name: my-config
- container:
args:
- '{{inputs.parameters.message}}'
command:
- cowsay
image: docker/whalesay
resources: {}
inputs:
parameters:
- name: message
name: whalesay
status:
artifactRepositoryRef:
configMap: artifact-repositories
key: default-v1
namespace: argo
conditions:
- status: "False"
type: PodRunning
- status: "True"
type: Completed
finishedAt: "2021-02-11T19:46:55Z"
nodes:
steps-jklcl:
children:
- steps-jklcl-3895081407
displayName: steps-jklcl
finishedAt: "2021-02-11T19:46:55Z"
id: steps-jklcl
name: steps-jklcl
outboundNodes:
- steps-jklcl-969694128
phase: Running
progress: 1/1
resourcesDuration:
cpu: 7
memory: 4
startedAt: "2021-02-11T19:46:33Z"
templateName: hello-hello-hello
templateScope: local/steps-jklcl
type: Steps
steps-jklcl-969694128:
boundaryID: steps-jklcl
displayName: hello1
finishedAt: "2021-02-11T19:46:44Z"
id: steps-jklcl-969694128
inputs:
parameters:
- name: message
value: hello1
name: steps-jklcl[0].hello1
outputs:
artifacts:
- archiveLogs: true
name: main-logs
s3:
accessKeySecret:
key: accesskey
name: my-minio-cred
bucket: my-bucket
endpoint: minio:9000
insecure: true
key: steps-jklcl/steps-jklcl-969694128/main.log
secretKeySecret:
key: secretkey
name: my-minio-cred
exitCode: "0"
phase: Succeeded
progress: 1/1
resourcesDuration:
cpu: 7
memory: 4
startedAt: "2021-02-11T19:46:33Z"
templateName: whalesay
templateScope: local/steps-jklcl
type: Pod
steps-jklcl-3895081407:
boundaryID: steps-jklcl
children:
- steps-jklcl-969694128
displayName: '[0]'
finishedAt: "2021-02-11T19:46:55Z"
id: steps-jklcl-3895081407
name: steps-jklcl[0]
phase: Succeeded
progress: 1/1
resourcesDuration:
cpu: 7
memory: 4
startedAt: "2021-02-11T19:46:33Z"
templateScope: local/steps-jklcl
type: StepGroup
phase: Succeeded
progress: 1/1
resourcesDuration:
cpu: 7
memory: 4
startedAt: "2021-02-11T19:46:33Z"
`
func TestSynchronizationWithStep(t *testing.T) {
assert := assert.New(t)
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
var cm v1.ConfigMap
wfv1.MustUnmarshal([]byte(configMap), &cm)
_, err := controller.kubeclientset.CoreV1().ConfigMaps("default").Create(ctx, &cm, metav1.CreateOptions{})
assert.NoError(err)
t.Run("StepWithSychronization", func(t *testing.T) {
// First workflow Acquire the lock
wf := wfv1.MustUnmarshalWorkflow(StepWithSync)
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows("default").Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(err)
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
assert.NotNil(woc.wf.Status.Synchronization)
assert.NotNil(woc.wf.Status.Synchronization.Semaphore)
assert.Len(woc.wf.Status.Synchronization.Semaphore.Holding, 1)
// Second workflow try to acquire the lock and wait for lock
wf1 := wfv1.MustUnmarshalWorkflow(StepWithSync)
wf1.Name = "step2"
wf1, err = controller.wfclientset.ArgoprojV1alpha1().Workflows("default").Create(ctx, wf1, metav1.CreateOptions{})
assert.NoError(err)
woc1 := newWorkflowOperationCtx(wf1, controller)
woc1.operate(ctx)
assert.NotNil(woc1.wf.Status.Synchronization)
assert.NotNil(woc1.wf.Status.Synchronization.Semaphore)
assert.Nil(woc1.wf.Status.Synchronization.Semaphore.Holding)
assert.Len(woc1.wf.Status.Synchronization.Semaphore.Waiting, 1)
// Finished all StepGroup in step
wf = wfv1.MustUnmarshalWorkflow(StepWithSyncStatus)
woc = newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
assert.Nil(woc.wf.Status.Synchronization)
// Second workflow acquire the lock
woc1 = newWorkflowOperationCtx(woc1.wf, controller)
woc1.operate(ctx)
assert.NotNil(woc1.wf.Status.Synchronization)
assert.NotNil(woc1.wf.Status.Synchronization.Semaphore)
assert.NotNil(woc1.wf.Status.Synchronization.Semaphore.Holding)
assert.Len(woc1.wf.Status.Synchronization.Semaphore.Holding, 1)
})
}
const wfWithStepRetry = `apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: my-workflow-
spec:
entrypoint: step-entry
templates:
- name: step-entry
steps:
- - name: step1
template: sleep
- name: sleep
retryStrategy:
limit: 5
retryPolicy: Always
synchronization:
semaphore:
configMapKeyRef:
name: my-config
key: template
container:
image: alpine:3.6
command: [sh, -c]
args: ["sleep 300"]`
func TestSynchronizationWithStepRetry(t *testing.T) {
assert := assert.New(t)
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
var cm v1.ConfigMap
wfv1.MustUnmarshal([]byte(configMap), &cm)
_, err := controller.kubeclientset.CoreV1().ConfigMaps("default").Create(ctx, &cm, metav1.CreateOptions{})
assert.NoError(err)
t.Run("StepRetryWithSynchronization", func(t *testing.T) {
// First workflow Acquire the lock
wf := wfv1.MustUnmarshalWorkflow(wfWithStepRetry)
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows("default").Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(err)
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
for _, n := range woc.wf.Status.Nodes {
if n.Name == "[0].step1(0)" {
assert.Equal(n.Phase, wfv1.NodePending)
}
}
// Updating Pod state
makePodsPhase(ctx, woc, v1.PodRunning)
woc.operate(ctx)
for _, n := range woc.wf.Status.Nodes {
if n.Name == "[0].step1(0)" {
assert.Equal(n.Phase, wfv1.NodeRunning)
}
}
makePodsPhase(ctx, woc, v1.PodFailed)
woc.operate(ctx)
for _, n := range woc.wf.Status.Nodes {
if n.Name == "[0].step1(0)" {
assert.Equal(n.Phase, wfv1.NodeFailed)
}
if n.Name == "[0].step1(1)" {
assert.Equal(n.Phase, wfv1.NodePending)
}
}
})
}
const pendingWfWithShutdownStrategy = `apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: synchronization-wf-level
namespace: default
spec:
entrypoint: whalesay
onExit: whalesay
synchronization:
mutex:
name: test
templates:
- name: whalesay
container:
image: docker/whalesay:latest
command: [sh, -c]
args: ["sleep 99999"]`
func TestSynchronizationForPendingShuttingdownWfs(t *testing.T) {
cancel, controller := newController()
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
}, workflowExistenceFunc)
t.Run("PendingShuttingdownTerminatingWf", func(t *testing.T) {
// Create and acquire the lock for the first workflow
wf := wfv1.MustUnmarshalWorkflow(pendingWfWithShutdownStrategy)
wf.Name = "one-terminating"
wf.Spec.Synchronization.Mutex.Name = "terminating-test"
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(t, err)
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
assert.NotNil(t, woc.wf.Status.Synchronization)
assert.NotNil(t, woc.wf.Status.Synchronization.Mutex)
assert.Equal(t, 1, len(woc.wf.Status.Synchronization.Mutex.Holding))
// Create the second workflow and try to acquire the lock, which should not be available.
wfTwo := wf.DeepCopy()
wfTwo.Name = "two-terminating"
wfTwo, err = controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wfTwo, metav1.CreateOptions{})
assert.NoError(t, err)
// This workflow should be pending since the first workflow still holds the lock.
wocTwo := newWorkflowOperationCtx(wfTwo, controller)
wocTwo.operate(ctx)
assert.Equal(t, wfv1.WorkflowPending, wocTwo.wf.Status.Phase)
// Shutdown the second workflow that's pending.
patchObj := map[string]interface{}{
"spec": map[string]interface{}{
"shutdown": wfv1.ShutdownStrategyTerminate,
},
}
patch, err := json.Marshal(patchObj)
assert.NoError(t, err)
wfTwo, err = controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Patch(ctx, wfTwo.Name, types.MergePatchType, patch, metav1.PatchOptions{})
assert.NoError(t, err)
// The pending workflow that's being shutdown should have succeeded and released the lock.
wocTwo = newWorkflowOperationCtx(wfTwo, controller)
wocTwo.operate(ctx)
assert.Equal(t, wfv1.WorkflowSucceeded, wocTwo.execWf.Status.Phase)
assert.Nil(t, wocTwo.wf.Status.Synchronization)
})
t.Run("PendingShuttingdownStoppingWf", func(t *testing.T) {
// Create and acquire the lock for the first workflow
wf := wfv1.MustUnmarshalWorkflow(pendingWfWithShutdownStrategy)
wf.Name = "one-stopping"
wf.Spec.Synchronization.Mutex.Name = "stopping-test"
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})