-
Notifications
You must be signed in to change notification settings - Fork 327
/
validateWorkloadActions.ts
2632 lines (2329 loc) · 86.5 KB
/
validateWorkloadActions.ts
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
import { affinityType } from './../constants/Config';
import * as ActionType from '../constants/ActionType';
import {
RootState,
WorkloadEdit,
MountItem,
LimitItem,
HealthCheckItem,
WorkloadLabel,
HpaMetrics,
VolumeItem,
ContainerItem,
ImagePullSecrets,
ServiceEdit,
ContainerEnv
} from '../models';
import { cloneDeep } from '../../common/utils';
import { workloadEditActions } from './workloadEditActions';
import { validateServiceActions } from './validateServiceActions';
import { AffinityRule, CronMetrics } from '../models/WorkloadEdit';
import { t, Trans } from '@tencent/tea-app/lib/i18n';
import { TappGrayUpdateEditItem } from '../models/ResourceDetailState';
type GetState = () => RootState;
export const validateWorkloadActions = {
/** 校验 workload名称是否正确 */
_validateWorkloadName(name: string) {
let reg = /^[a-z]([-a-z0-9]*[a-z0-9])?$/,
status = 0,
message = '';
// 验证workload名称
if (!name) {
status = 2;
message = t('Workload名称不能为空');
} else if (name.length > 63) {
status = 2;
message = t('Workload名称不能超过63个字符');
} else if (!reg.test(name)) {
status = 2;
message = t('Workload名称格式不正确');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateWorkloadName() {
return async (dispatch, getState: GetState) => {
const { workloadName } = getState().subRoot.workloadEdit;
const result = await validateWorkloadActions._validateWorkloadName(workloadName);
dispatch({
type: ActionType.WV_WorkloadName,
payload: result
});
};
},
/** 校验执行策略是否正确 */
_validateCronSchedule(schedule: string) {
// cronjob的规则 * * * * * 分别代表 minute(0-59)、 hour(0-23)、 day(1-31)、 month(1-12)、 day of week(0-6)
let status = 0,
message = '',
reg = /^(\*|([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])|\*\/([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])) (\*|([0-9]|1[0-9]|2[0-3])|\*\/([0-9]|1[0-9]|2[0-3])) (\*|([1-9]|1[0-9]|2[0-9]|3[0-1])|\*\/([1-9]|1[0-9]|2[0-9]|3[0-1])) (\*|([1-9]|1[0-2])|\*\/([1-9]|1[0-2])) (\*|([0-6])|\*\/([0-6]))$/;
if (!schedule) {
status = 2;
message = t('执行策略不能为空');
} else if (!reg.test(schedule)) {
status = 2;
message = t('执行策略格式不正确');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateCronSchedule() {
return async (dispatch, getState: GetState) => {
const { cronSchedule } = getState().subRoot.workloadEdit;
const result = validateWorkloadActions._validateCronSchedule(cronSchedule);
dispatch({
type: ActionType.WV_CronSchedule,
payload: result
});
};
},
/** 校验重复次数是否正确 */
_validateJobCompletion(completion: number) {
let status = 0,
message = '',
reg = /^\d+$/;
if (completion < 1) {
status = 2;
message = t('执行次数至少为1');
} else if (!reg.test(completion + '')) {
status = 2;
message = t('格式不正确,只能为数值');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateJobCompletion() {
return async (dispatch, getState: GetState) => {
const { completion } = getState().subRoot.workloadEdit;
const result = validateWorkloadActions._validateJobCompletion(+completion);
dispatch({
type: ActionType.WV_Completion,
payload: result
});
};
},
/** 校验job并行度 */
_validateJobParallel(parallel: number) {
let status = 0,
message = '',
reg = /^\d+$/;
if (parallel < 1) {
status = 2;
message = t('Job并行度至少为1');
} else if (!reg.test(parallel + '')) {
status = 2;
message = t('格式不正确,只能为数值');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateJobParallel() {
return async (dispatch, getState: GetState) => {
const { parallelism } = getState().subRoot.workloadEdit;
const result = validateWorkloadActions._validateJobParallel(+parallelism);
dispatch({
type: ActionType.WV_Parallelism,
payload: result
});
};
},
/** 校验 workload 描述 */
_validateWorkloadDesp(desp: string) {
let status = 0,
message = '';
// 验证ingress描述
if (desp && desp.length > 1000) {
status = 2;
message = t('Workload描述不能超过1000个字符');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateWorkloadDesp() {
return async (dispatch, getState: GetState) => {
const { description } = getState().subRoot.workloadEdit;
const result = validateWorkloadActions._validateWorkloadDesp(description);
dispatch({
type: ActionType.WV_Description,
payload: result
});
};
},
/** 校验标签是否正确 */
_validateWorkloadLabelKey(name: string, labels: WorkloadLabel[]) {
// label 支持 [A-Z0-9a-z]开头和结尾,中间还可以有 -_.
let reg = /^[A-Za-z0-9][-A-Za-z0-9_\./]*?[A-Za-z0-9]$/,
status = 0,
message = '';
// 验证label
if (!name) {
status = 2;
message = t('Key不能为空');
} else if (!reg.test(name)) {
status = 2;
message = t('Key格式不正确');
} else if (labels.filter(label => label.labelKey === name).length > 1) {
status = 2;
message = t('Key名称不能重复');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateWorkloadLabelKey(name: string, labelId: string) {
return async (dispatch, getState: GetState) => {
const labels: WorkloadLabel[] = cloneDeep(getState().subRoot.workloadEdit.workloadLabels),
labelIndex = labels.findIndex(x => x.id === labelId),
result = validateWorkloadActions._validateWorkloadLabelKey(name, labels);
labels[labelIndex]['v_labelKey'] = result;
dispatch({
type: ActionType.W_WorkloadLabels,
payload: labels
});
};
},
_validateAllWorkloadLabelKey(labels: WorkloadLabel[]) {
let result = true;
labels.forEach(label => {
result = result && validateWorkloadActions._validateWorkloadLabelKey(label.labelKey, labels).status === 1;
});
return result;
},
validateAllWorkloadLabelKey() {
return async (dispatch, getState: GetState) => {
getState().subRoot.workloadEdit.workloadLabels.forEach(label => {
dispatch(validateWorkloadActions.validateWorkloadLabelKey(label.labelKey, label.id + ''));
});
};
},
/** 校验标签的key是否正确 */
_validateWorkloadLabelValue(value: string) {
let status = 0,
message = '',
reg = /^[a-z]([-a-z0-9]*[a-z0-9])?$/;
// 验证label
if (!value) {
status = 2;
message = t('Value不能为空');
} else if (!reg.test(value)) {
status = 2;
message = t('格式不正确');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateWorkloadLabelValue(value: string, labelId: string) {
return async (dispatch, getState: GetState) => {
const labels: WorkloadLabel[] = cloneDeep(getState().subRoot.workloadEdit.workloadLabels),
labelIndex = labels.findIndex(x => x.id === labelId),
result = validateWorkloadActions._validateWorkloadLabelValue(value);
labels[labelIndex]['v_labelValue'] = result;
dispatch({
type: ActionType.W_WorkloadLabels,
payload: labels
});
};
},
_validateAllWorkloadLabelValue(labels: WorkloadLabel[]) {
let result = true;
labels.forEach(label => {
result = result && validateWorkloadActions._validateWorkloadLabelKey(label.labelKey, labels).status === 1;
});
return result;
},
validateAllWorkloadLabelValue() {
return async (dispatch, getState: GetState) => {
getState().subRoot.workloadEdit.workloadLabels.forEach(label => {
dispatch(validateWorkloadActions.validateWorkloadLabelValue(label.labelValue, label.id + ''));
});
};
},
/** 校验annotations的key、value是否正确 */
_validateWorkloadAnnotationsKey(name: string, annotations: WorkloadLabel[]) {
let status = 0,
message = '',
reg = /^([A-Za-z0-9][-A-Za-z0-9_\.\/]*)?[A-Za-z0-9]$/;
// 验证key
if (!name) {
status = 2;
message = t('Key不能为空');
} else if (!reg.test(name)) {
status = 2;
message = t('Key格式不正确');
} else if (annotations.filter(annotataion => annotataion.labelKey === name).length > 1) {
status = 2;
message = t('Key名称不能重复');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateWorkloadAnnotationsKey(value: string, aId: string) {
return async (dispatch: Redux.Dispatch, getState: GetState) => {
const annotations: WorkloadLabel[] = cloneDeep(getState().subRoot.workloadEdit.workloadAnnotations),
aIndex = annotations.findIndex(x => x.id === aId),
result = validateWorkloadActions._validateWorkloadAnnotationsKey(value, annotations);
annotations[aIndex]['v_labelKey'] = result;
dispatch({
type: ActionType.W_WorkloadAnnotations,
payload: annotations
});
};
},
_validateAllWorkloadAnnotationsKey(annotations: WorkloadLabel[]) {
let result = true;
annotations.forEach(annotation => {
result =
result &&
validateWorkloadActions._validateWorkloadAnnotationsKey(annotation.labelKey, annotations).status === 1;
});
return result;
},
validateAllWorkloadAnnotationsKey() {
return async (dispatch: Redux.Dispatch, getState: GetState) => {
getState().subRoot.workloadEdit.workloadAnnotations.forEach(annotation => {
dispatch(validateWorkloadActions.validateWorkloadAnnotationsKey(annotation.labelKey, annotation.id + ''));
});
};
},
/** 校验annotations的value是否正确 */
_validateWorkloadAnnotationsValue(value: string) {
let status = 0,
message = '';
// reg = /^([A-Za-z0-9][-A-Za-z0-9_\.]*)?[A-Za-z0-9]$/;
// 验证key
if (!value) {
status = 2;
message = t('Value不能为空');
// } else if (!reg.test(value)) {
// status = 2;
// message = t('Value格式不正确');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateWorkloadAnnotationsValue(value: string, aId: string) {
return async (dispatch: Redux.Dispatch, getState: GetState) => {
const annotations: WorkloadLabel[] = cloneDeep(getState().subRoot.workloadEdit.workloadAnnotations),
aIndex = annotations.findIndex(x => x.id === aId),
result = validateWorkloadActions._validateWorkloadAnnotationsValue(value);
annotations[aIndex]['v_labelValue'] = result;
dispatch({
type: ActionType.W_WorkloadAnnotations,
payload: annotations
});
};
},
_validateAllWorkloadAnnotationsValue(annotations: WorkloadLabel[]) {
let result = true;
annotations.forEach(annotation => {
result = result && validateWorkloadActions._validateWorkloadAnnotationsValue(annotation.labelValue).status === 1;
});
return result;
},
validateAllWorkloadAnnotationsValue() {
return async (dispatch: Redux.Dispatch, getState: GetState) => {
getState().subRoot.workloadEdit.workloadAnnotations.forEach(annotation => {
dispatch(validateWorkloadActions.validateWorkloadAnnotationsValue(annotation.labelValue, annotation.id + ''));
});
};
},
/** 校验命名空间 */
_validateNamespace(namespace: string) {
let status = 0,
message = '';
// 验证命名空间的选择
if (!namespace) {
status = 2;
message = t('命名空间不能为空');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateNamespace() {
return async (dispatch, getState: GetState) => {
const { namespace } = getState().subRoot.workloadEdit;
const result = validateWorkloadActions._validateNamespace(namespace);
dispatch({
type: ActionType.WV_Namespace,
payload: result
});
};
},
/** 校验数据卷的名称 */
_validateVolumeName(name: string, volumes: VolumeItem[]) {
let reg = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/,
status = 0,
message = '';
if (!name) {
status = 2;
message = t('请填写数据卷名称');
} else if (name.length > 63) {
status = 2;
message = t('数据卷名称不能超过63个字符');
} else if (!reg.test(name)) {
status = 2;
message = t('只能包含小写字母、数字及分隔符("-"),且不能以分隔符开头或结尾');
} else if (name && volumes.filter(vol => vol.name === name).length > 1) {
status = 2;
message = t('数据卷名称不能重复');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateVolumeName(name: string, vId: string) {
return async (dispatch, getState: GetState) => {
const volumes: VolumeItem[] = cloneDeep(getState().subRoot.workloadEdit.volumes),
vIndex = volumes.findIndex(vol => vol.id === vId),
result = validateWorkloadActions._validateVolumeName(name, volumes);
volumes[vIndex]['v_name'] = result;
dispatch({
type: ActionType.W_UpdateVolumes,
payload: volumes
});
};
},
_validateAllVolumeName(volumes: VolumeItem[]) {
let result = true;
volumes.forEach(volume => {
result = result && validateWorkloadActions._validateVolumeName(volume.name, volumes).status === 1;
});
return result;
},
validateAllVolumeName() {
return async (dispatch, getState: GetState) => {
const volumes = getState().subRoot.workloadEdit.volumes;
volumes.forEach(volume => {
dispatch(validateWorkloadActions.validateVolumeName(volume.name, volume.id + ''));
});
};
},
/** 校验pvc的相关选择 */
_validateVolumePvc(pvcName: string, volumes: VolumeItem[]) {
let status = 0,
message = '';
if (!pvcName) {
status = 2;
message = t('请选择pvc');
} else if (volumes.filter(vol => vol.volumeType === 'pvc' && vol.pvcSelection === pvcName).length > 1) {
status = 2;
message = t('pvc不可重复');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateVolumePvc(pvcName: string, vId: string) {
return async (dispatch, getState: GetState) => {
const volumes: VolumeItem[] = cloneDeep(getState().subRoot.workloadEdit.volumes),
vIndex = volumes.findIndex(vol => vol.id === vId),
result = validateWorkloadActions._validateVolumePvc(pvcName, volumes);
volumes[vIndex]['v_pvcSelection'] = result;
dispatch({
type: ActionType.W_UpdateVolumes,
payload: volumes
});
};
},
_validateAllPvcSelection(volumes: VolumeItem[]) {
let result = true;
volumes.forEach(volume => {
if (volume.volumeType === 'pvc') {
result = result && validateWorkloadActions._validateVolumePvc(volume.pvcSelection, volumes).status === 1;
}
});
return result;
},
validateAllPvcSelection() {
return async (dispatch, getState: GetState) => {
const volumes = getState().subRoot.workloadEdit.volumes;
volumes.forEach(volume => {
volume.volumeType === 'pvc' &&
dispatch(validateWorkloadActions.validateVolumePvc(volume.pvcSelection, volume.id + ''));
});
};
},
/** 校验数据卷 nfs路径 */
_validateNfsPath(path: string, vId: string, volumes: VolumeItem[]) {
const volume = volumes.find(vol => vol.id === vId);
let reg = /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/,
status = 0,
message = '';
if (volume.volumeType === 'nfsDisk') {
if (path) {
if (!reg.test(path)) {
status = 2;
message = t('NFS路径格式不正确');
} else if (volumes.filter(vol => vol.nfsPath === path && vol.volumeType === 'nfsDisk').length > 1) {
status = 2;
message = t('NFS路径称不可重复');
} else {
status = 1;
message = '';
}
} else {
status = 2;
message = t('NFS路径不能为空');
}
} else {
status = 1;
message = '';
}
return { status, message };
},
validateNfsPath(path: string, vId: string) {
return async (dispatch, getState: GetState) => {
const volumes: VolumeItem[] = cloneDeep(getState().subRoot.workloadEdit.volumes),
vIndex = volumes.findIndex(v => v.id === vId),
result = validateWorkloadActions._validateNfsPath(path, vId, volumes);
volumes[vIndex]['v_nfsPath'] = result;
dispatch({
type: ActionType.W_UpdateVolumes,
payload: volumes
});
};
},
_validateAllNfsPath(volumes: VolumeItem[]) {
let result = true;
volumes.forEach(volume => {
result = result && validateWorkloadActions._validateNfsPath(volume.nfsPath, volume.id + '', volumes).status === 1;
});
return result;
},
validateAllNfsPath() {
return async (dispatch, getState: GetState) => {
const volumes = getState().subRoot.workloadEdit.volumes;
volumes.forEach(volume => {
dispatch(validateWorkloadActions.validateNfsPath(volume.nfsPath, volume.id + ''));
});
};
},
/** 校验hostPath是否正确 */
_validateHostPath(path: string) {
let status = 0,
message = '',
reg = /^\/[\/\w-_\.]*$/;
if (!path) {
status = 2;
message = t('HostPath路径不能为空');
} else if (!reg.test(path)) {
status = 2;
message = t('HostPath格式不正确');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateHostPath(path: string, vId: string) {
return async (dispatch, getState: GetState) => {
const volumes: VolumeItem[] = cloneDeep(getState().subRoot.workloadEdit.volumes),
vIndex = volumes.findIndex(v => v.id === vId),
result = validateWorkloadActions._validateHostPath(path);
volumes[vIndex]['v_hostPath'] = result;
dispatch({
type: ActionType.W_UpdateVolumes,
payload: volumes
});
};
},
_validateAllHostPath(volumes: VolumeItem[]) {
let result = true;
volumes.forEach(volume => {
if (volume.volumeType === 'hostPath') {
result = result && validateWorkloadActions._validateHostPath(volume.hostPath).status === 1;
}
});
return result;
},
validateAllHosPath() {
return async (dispatch, getState: GetState) => {
const volumes = getState().subRoot.workloadEdit.volumes;
volumes.forEach(volume => {
volume.volumeType === 'hostPath' &&
dispatch(validateWorkloadActions.validateHostPath(volume.hostPath, volume.id + ''));
});
};
},
_validateAllVolumeIsMounted(volumes: VolumeItem[], containers: ContainerItem[]) {
let allIsMounted = true,
validateVolumes = {};
// 已经挂载的数据卷的名称数组
const mounts: MountItem[] = [];
containers.forEach(c => {
mounts.push(...c.mounts);
});
const mountsName = mounts.map(m => m.volume);
volumes.forEach(v => {
const isMounted = !!mountsName.find(m => m === v.name);
allIsMounted = allIsMounted && isMounted;
validateVolumes[v.id + ''] = isMounted;
});
return { allIsMounted, validateVolumes };
},
validateAllVolumeIsMounted() {
return async (dispatch, getState: GetState) => {
const { volumes, containers } = getState().subRoot.workloadEdit;
const result = validateWorkloadActions._validateAllVolumeIsMounted(volumes, containers);
// 更新全部是否ok的标志
dispatch({
type: ActionType.W_IsAllVolumeIsMounted,
payload: result.allIsMounted
});
// 更新每一个挂在项的校验状态
const newVolumes: VolumeItem[] = cloneDeep(volumes),
validateVolumes = result.validateVolumes;
newVolumes.forEach(v => {
v['isMounted'] = validateVolumes[v.id + ''];
});
dispatch({
type: ActionType.W_UpdateVolumes,
payload: newVolumes
});
};
},
/** 判断是否可以新增数据卷,对数据卷中的属性状态进行相关的判断 */
_canAddVolume(volumes: VolumeItem[]) {
let result = true;
volumes.forEach(volume => {
result = result && validateWorkloadActions._validateVolumeName(volume.name, volumes).status === 1;
if (volume.volumeType === 'pvc') {
result = result && validateWorkloadActions._validateVolumePvc(volume.pvcSelection, volumes).status === 1;
} else if (volume.volumeType === 'hostPath') {
result = result && validateWorkloadActions._validateHostPath(volume.hostPath).status === 1;
}
});
return result;
},
/** 校验容器名称是否正确 */
_validateContainerName(name: string, containers: ContainerItem[]) {
let reg = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/,
status = 0,
message = '';
// 验证容器服务名称
if (!name) {
status = 2;
message = t('容器名称不能为空');
} else if (containers.filter(c => c.name === name).length > 1) {
status = 2;
message = t('容器名称不能重复');
} else if (name.length > 63) {
status = 2;
message = t('容器名称不能超过63个字符');
} else if (!reg.test(name)) {
status = 2;
message = t('容器名称格式不正确');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateContainerName(name: string, cKey: string) {
return async (dispatch, getState: GetState) => {
const containers: ContainerItem[] = cloneDeep(getState().subRoot.workloadEdit.containers),
cIndex = containers.findIndex(item => item.id === cKey),
result = validateWorkloadActions._validateContainerName(name, containers);
containers[cIndex]['v_name'] = result;
dispatch({
type: ActionType.W_UpdateContainers,
payload: containers
});
};
},
validateAllContainerName() {
return async (dispatch, getState: GetState) => {
const containers: ContainerItem[] = getState().subRoot.workloadEdit.containers;
containers.forEach(container => {
dispatch(validateWorkloadActions.validateContainerName(container.name, container.id + ''));
});
};
},
/** 校验镜像名称是否合法 */
_validateRegistrySelection(registry: string) {
let status = 2,
message = '';
// 验证容器的镜像
if (!registry) {
status = 2;
message = t('镜像不能为空');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateRegistrySelection(registry: string, cKey: string) {
return async (dispatch, getState: GetState) => {
const containers: ContainerItem[] = cloneDeep(getState().subRoot.workloadEdit.containers),
cIndex = containers.findIndex(c => c.id === cKey),
result = validateWorkloadActions._validateRegistrySelection(registry);
containers[cIndex]['v_registry'] = result;
dispatch({
type: ActionType.W_UpdateContainers,
payload: containers
});
};
},
validateGrayUpdateRegistrySelection(index_in: number, imageName: string) {
return async (dispatch, getState: GetState) => {
const { editTappGrayUpdate } = getState().subRoot.resourceDetailState;
const target: TappGrayUpdateEditItem = cloneDeep(editTappGrayUpdate);
target.containers[index_in].v_imageName = validateWorkloadActions._validateRegistrySelection(
imageName
);
dispatch({
type: ActionType.W_TappGrayUpdate,
payload: target
});
};
},
validateGrayUpdate() {
return async (dispatch, getState: GetState) => {
const { editTappGrayUpdate } = getState().subRoot.resourceDetailState;
editTappGrayUpdate.containers.forEach((container, index_in) => {
dispatch(
validateWorkloadActions.validateGrayUpdateRegistrySelection(index_in, container.imageName)
);
});
};
},
/** 校验数据卷挂在是否正确 */
_validateVolumeMount(volumeName: string, mId: string, mounts: MountItem[], volumes: VolumeItem[]) {
let mount = mounts.find(m => m.id === mId),
status = 0,
message = '';
if (!volumeName && mount.mountPath) {
status = 2;
message = t('请选择数据卷');
} else if (!volumes.find(v => v.name === volumeName)) {
status = 2;
message = t('数据卷不存在');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateVolumeMount(volumeName: string, cId: string, mId: string) {
return async (dispatch, getState: GetState) => {
const { containers, volumes } = getState().subRoot.workloadEdit;
const newContainers: ContainerItem[] = cloneDeep(containers),
cIndex = newContainers.findIndex(c => c.id === cId),
mIndex = newContainers[cIndex]['mounts'].findIndex(m => m.id === mId),
result = validateWorkloadActions._validateVolumeMount(
volumeName,
mId,
newContainers[cIndex]['mounts'],
volumes
);
newContainers[cIndex]['mounts'][mIndex]['v_volume'] = result;
dispatch({
type: ActionType.W_UpdateContainers,
payload: newContainers
});
};
},
_validateAllVolumeMount(mounts: MountItem[], volumes: VolumeItem[]) {
let result = true;
mounts.forEach(m => {
if (m.mountPath) {
result =
result && validateWorkloadActions._validateVolumeMount(m.volume, m.id + '', mounts, volumes).status === 1;
}
});
return result;
},
validateAllVolumeMount(container: ContainerItem) {
return async (dispatch, getState: GetState) => {
container.mounts.forEach(mount => {
if (mount.mountPath) {
dispatch(validateWorkloadActions.validateVolumeMount(mount.volume, container.id + '', mount.id + ''));
}
});
};
},
/** 校验挂载的挂载路径 */
_validateVolumeMountPath(mountPath: string, mId: string, mounts: MountItem[]) {
const mount = mounts.find(m => m.id === mId);
let status = 0,
message = '',
reg = /:/;
if (!mountPath && mount.volume) {
status = 2;
message = t('请输入目标路径');
} else if (reg.test(mountPath)) {
status = 2;
message = t('目标路径不可包含 ":" ');
} else if (mountPath && mounts.filter(m => m.mountPath === mountPath).length > 1) {
status = 2;
message = t('目标路径不可重复');
} else {
status = 1;
message = '';
}
return { status, message };
},
validateVolumeMountPath(mountPath: string, cId: string, mId: string) {
return async (dispatch, getState: GetState) => {
const { containers, volumes } = getState().subRoot.workloadEdit;
const newContainers: ContainerItem[] = cloneDeep(containers),
cIndex = newContainers.findIndex(c => c.id === cId),
mIndex = newContainers[cIndex]['mounts'].findIndex(m => m.id === mId),
result = validateWorkloadActions._validateVolumeMountPath(mountPath, mId, newContainers[cIndex]['mounts']);
newContainers[cIndex]['mounts'][mIndex]['v_mountPath'] = result;
dispatch({
type: ActionType.W_UpdateContainers,
payload: newContainers
});
};
},
_validateAllVolumeMountPath(mounts: MountItem[]) {
let result = true;
mounts.forEach(mount => {
if (mount.volume) {
result =
result &&
validateWorkloadActions._validateVolumeMountPath(mount.mountPath, mount.id + '', mounts).status === 1;
}
});
return result;
},
validateAllVolumeMountPath(container: ContainerItem) {
return async (dispatch, getState: GetState) => {
container.mounts.forEach(item => {
if (item.volume) {
dispatch(validateWorkloadActions.validateVolumeMountPath(item.mountPath, container.id + '', item.id + ''));
}
});
};
},
/** 校验容器 CPU限制 */
_validateCpuLimit(cpu: number, type: string, cpuLimit: LimitItem[]) {
let reg = /^\d+(\.\d{1,3})?$/,
status = 0,
message = '';
// 验证CPU限制
if (isNaN(cpu)) {
status = 2;
message = t('数据格式不正确,CPU限制只能是小数,且只能精确到0.01');
} else if (!cpu) {
status = 1;
message = '';
} else if (!reg.test(cpu + '')) {
status = 2;
message = t('数据格式不正确,CPU限制只能是小数,且只能精确到0.01');
} else if (cpu < 0.01) {
status = 2;
message = t('CPU限制最小为0.01');
} else {
if (type === 'limit') {
status = 1;
message = '';
} else {
const limit = cpuLimit.find(cpu => cpu.type === 'limit');
if (!limit || (limit && cpu - +limit.value <= 0) || !limit.value) {
status = 1;
message = '';
} else {
status = 2;
message = t('request限制不能超过limit限制');
}
}
}
return { status, message };
},
validateCpuLimit(cpu: string, cId: string, cpuId: string) {
return async (dispatch, getState: GetState) => {
const containers: ContainerItem[] = cloneDeep(getState().subRoot.workloadEdit.containers),
cIndex = containers.findIndex(c => c.id === cId),
cpuLimit = containers[cIndex].cpuLimit,
cpuIndex = cpuLimit.findIndex(c => c.id === cpuId),
result = validateWorkloadActions._validateCpuLimit(+cpu, cpuLimit[cpuIndex].type, cpuLimit);
containers[cIndex]['cpuLimit'][cpuIndex]['v_value'] = result;
dispatch({
type: ActionType.W_UpdateContainers,
payload: containers
});
};
},
_validateAllCpuLimit(cpuLimit: LimitItem[]) {
let result = true;