1
2
3
4
5
6
7
8
9
10
11
12
13
14 package v1alpha1
15
16 import (
17 "encoding/json"
18 "reflect"
19 "time"
20
21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22 )
23
24 const KindAWSChaos = "AWSChaos"
25
26
27 func (in *AWSChaos) IsDeleted() bool {
28 return !in.DeletionTimestamp.IsZero()
29 }
30
31
32 func (in *AWSChaos) IsPaused() bool {
33 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
34 return false
35 }
36 return true
37 }
38
39
40 func (in *AWSChaos) GetObjectMeta() *metav1.ObjectMeta {
41 return &in.ObjectMeta
42 }
43
44
45 func (in *AWSChaosSpec) GetDuration() (*time.Duration, error) {
46 if in.Duration == nil {
47 return nil, nil
48 }
49 duration, err := time.ParseDuration(*in.Duration)
50 if err != nil {
51 return nil, err
52 }
53 return &duration, nil
54 }
55
56
57 func (in *AWSChaos) GetChaos() *ChaosInstance {
58 instance := &ChaosInstance{
59 Name: in.Name,
60 Namespace: in.Namespace,
61 Kind: KindAWSChaos,
62 StartTime: in.CreationTimestamp.Time,
63 Action: "",
64 UID: string(in.UID),
65 Status: in.Status.ChaosStatus,
66 }
67
68 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
69 if action.IsValid() {
70 instance.Action = action.String()
71 }
72 if in.Spec.Duration != nil {
73 instance.Duration = *in.Spec.Duration
74 }
75 if in.DeletionTimestamp != nil {
76 instance.EndTime = in.DeletionTimestamp.Time
77 }
78 return instance
79 }
80
81
82 func (in *AWSChaos) GetStatus() *ChaosStatus {
83 return &in.Status.ChaosStatus
84 }
85
86
87 func (in *AWSChaos) GetSpecAndMetaString() (string, error) {
88 spec, err := json.Marshal(in.Spec)
89 if err != nil {
90 return "", err
91 }
92
93 meta := in.ObjectMeta.DeepCopy()
94 meta.SetResourceVersion("")
95 meta.SetGeneration(0)
96
97 return string(spec) + meta.String(), nil
98 }
99
100
101
102
103 type AWSChaosList struct {
104 metav1.TypeMeta `json:",inline"`
105 metav1.ListMeta `json:"metadata,omitempty"`
106 Items []AWSChaos `json:"items"`
107 }
108
109
110 func (in *AWSChaosList) ListChaos() []*ChaosInstance {
111 res := make([]*ChaosInstance, 0, len(in.Items))
112 for _, item := range in.Items {
113 res = append(res, item.GetChaos())
114 }
115 return res
116 }
117
118 func (in *AWSChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
119 duration, err := in.Spec.GetDuration()
120 if err != nil {
121 return false, 0, err
122 }
123
124 if duration != nil {
125 stopTime := in.GetCreationTimestamp().Add(*duration)
126 if stopTime.Before(now) {
127 return true, 0, nil
128 }
129
130 return false, stopTime.Sub(now), nil
131 }
132
133 return false, 0, nil
134 }
135
136 func (in *AWSChaos) IsOneShot() bool {
137
138 if in.Spec.Action==Ec2Restart {
139 return true
140 }
141
142 return false
143
144 }
145
146 const KindDNSChaos = "DNSChaos"
147
148
149 func (in *DNSChaos) IsDeleted() bool {
150 return !in.DeletionTimestamp.IsZero()
151 }
152
153
154 func (in *DNSChaos) IsPaused() bool {
155 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
156 return false
157 }
158 return true
159 }
160
161
162 func (in *DNSChaos) GetObjectMeta() *metav1.ObjectMeta {
163 return &in.ObjectMeta
164 }
165
166
167 func (in *DNSChaosSpec) GetDuration() (*time.Duration, error) {
168 if in.Duration == nil {
169 return nil, nil
170 }
171 duration, err := time.ParseDuration(*in.Duration)
172 if err != nil {
173 return nil, err
174 }
175 return &duration, nil
176 }
177
178
179 func (in *DNSChaos) GetChaos() *ChaosInstance {
180 instance := &ChaosInstance{
181 Name: in.Name,
182 Namespace: in.Namespace,
183 Kind: KindDNSChaos,
184 StartTime: in.CreationTimestamp.Time,
185 Action: "",
186 UID: string(in.UID),
187 Status: in.Status.ChaosStatus,
188 }
189
190 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
191 if action.IsValid() {
192 instance.Action = action.String()
193 }
194 if in.Spec.Duration != nil {
195 instance.Duration = *in.Spec.Duration
196 }
197 if in.DeletionTimestamp != nil {
198 instance.EndTime = in.DeletionTimestamp.Time
199 }
200 return instance
201 }
202
203
204 func (in *DNSChaos) GetStatus() *ChaosStatus {
205 return &in.Status.ChaosStatus
206 }
207
208
209 func (in *DNSChaos) GetSpecAndMetaString() (string, error) {
210 spec, err := json.Marshal(in.Spec)
211 if err != nil {
212 return "", err
213 }
214
215 meta := in.ObjectMeta.DeepCopy()
216 meta.SetResourceVersion("")
217 meta.SetGeneration(0)
218
219 return string(spec) + meta.String(), nil
220 }
221
222
223
224
225 type DNSChaosList struct {
226 metav1.TypeMeta `json:",inline"`
227 metav1.ListMeta `json:"metadata,omitempty"`
228 Items []DNSChaos `json:"items"`
229 }
230
231
232 func (in *DNSChaosList) ListChaos() []*ChaosInstance {
233 res := make([]*ChaosInstance, 0, len(in.Items))
234 for _, item := range in.Items {
235 res = append(res, item.GetChaos())
236 }
237 return res
238 }
239
240 func (in *DNSChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
241 duration, err := in.Spec.GetDuration()
242 if err != nil {
243 return false, 0, err
244 }
245
246 if duration != nil {
247 stopTime := in.GetCreationTimestamp().Add(*duration)
248 if stopTime.Before(now) {
249 return true, 0, nil
250 }
251
252 return false, stopTime.Sub(now), nil
253 }
254
255 return false, 0, nil
256 }
257
258 func (in *DNSChaos) IsOneShot() bool {
259
260 return false
261
262 }
263
264 const KindGCPChaos = "GCPChaos"
265
266
267 func (in *GCPChaos) IsDeleted() bool {
268 return !in.DeletionTimestamp.IsZero()
269 }
270
271
272 func (in *GCPChaos) IsPaused() bool {
273 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
274 return false
275 }
276 return true
277 }
278
279
280 func (in *GCPChaos) GetObjectMeta() *metav1.ObjectMeta {
281 return &in.ObjectMeta
282 }
283
284
285 func (in *GCPChaosSpec) GetDuration() (*time.Duration, error) {
286 if in.Duration == nil {
287 return nil, nil
288 }
289 duration, err := time.ParseDuration(*in.Duration)
290 if err != nil {
291 return nil, err
292 }
293 return &duration, nil
294 }
295
296
297 func (in *GCPChaos) GetChaos() *ChaosInstance {
298 instance := &ChaosInstance{
299 Name: in.Name,
300 Namespace: in.Namespace,
301 Kind: KindGCPChaos,
302 StartTime: in.CreationTimestamp.Time,
303 Action: "",
304 UID: string(in.UID),
305 Status: in.Status.ChaosStatus,
306 }
307
308 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
309 if action.IsValid() {
310 instance.Action = action.String()
311 }
312 if in.Spec.Duration != nil {
313 instance.Duration = *in.Spec.Duration
314 }
315 if in.DeletionTimestamp != nil {
316 instance.EndTime = in.DeletionTimestamp.Time
317 }
318 return instance
319 }
320
321
322 func (in *GCPChaos) GetStatus() *ChaosStatus {
323 return &in.Status.ChaosStatus
324 }
325
326
327 func (in *GCPChaos) GetSpecAndMetaString() (string, error) {
328 spec, err := json.Marshal(in.Spec)
329 if err != nil {
330 return "", err
331 }
332
333 meta := in.ObjectMeta.DeepCopy()
334 meta.SetResourceVersion("")
335 meta.SetGeneration(0)
336
337 return string(spec) + meta.String(), nil
338 }
339
340
341
342
343 type GCPChaosList struct {
344 metav1.TypeMeta `json:",inline"`
345 metav1.ListMeta `json:"metadata,omitempty"`
346 Items []GCPChaos `json:"items"`
347 }
348
349
350 func (in *GCPChaosList) ListChaos() []*ChaosInstance {
351 res := make([]*ChaosInstance, 0, len(in.Items))
352 for _, item := range in.Items {
353 res = append(res, item.GetChaos())
354 }
355 return res
356 }
357
358 func (in *GCPChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
359 duration, err := in.Spec.GetDuration()
360 if err != nil {
361 return false, 0, err
362 }
363
364 if duration != nil {
365 stopTime := in.GetCreationTimestamp().Add(*duration)
366 if stopTime.Before(now) {
367 return true, 0, nil
368 }
369
370 return false, stopTime.Sub(now), nil
371 }
372
373 return false, 0, nil
374 }
375
376 func (in *GCPChaos) IsOneShot() bool {
377
378 if in.Spec.Action==NodeReset {
379 return true
380 }
381
382 return false
383
384 }
385
386 const KindHTTPChaos = "HTTPChaos"
387
388
389 func (in *HTTPChaos) IsDeleted() bool {
390 return !in.DeletionTimestamp.IsZero()
391 }
392
393
394 func (in *HTTPChaos) IsPaused() bool {
395 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
396 return false
397 }
398 return true
399 }
400
401
402 func (in *HTTPChaos) GetObjectMeta() *metav1.ObjectMeta {
403 return &in.ObjectMeta
404 }
405
406
407 func (in *HTTPChaosSpec) GetDuration() (*time.Duration, error) {
408 if in.Duration == nil {
409 return nil, nil
410 }
411 duration, err := time.ParseDuration(*in.Duration)
412 if err != nil {
413 return nil, err
414 }
415 return &duration, nil
416 }
417
418
419 func (in *HTTPChaos) GetChaos() *ChaosInstance {
420 instance := &ChaosInstance{
421 Name: in.Name,
422 Namespace: in.Namespace,
423 Kind: KindHTTPChaos,
424 StartTime: in.CreationTimestamp.Time,
425 Action: "",
426 UID: string(in.UID),
427 Status: in.Status.ChaosStatus,
428 }
429
430 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
431 if action.IsValid() {
432 instance.Action = action.String()
433 }
434 if in.Spec.Duration != nil {
435 instance.Duration = *in.Spec.Duration
436 }
437 if in.DeletionTimestamp != nil {
438 instance.EndTime = in.DeletionTimestamp.Time
439 }
440 return instance
441 }
442
443
444 func (in *HTTPChaos) GetStatus() *ChaosStatus {
445 return &in.Status.ChaosStatus
446 }
447
448
449 func (in *HTTPChaos) GetSpecAndMetaString() (string, error) {
450 spec, err := json.Marshal(in.Spec)
451 if err != nil {
452 return "", err
453 }
454
455 meta := in.ObjectMeta.DeepCopy()
456 meta.SetResourceVersion("")
457 meta.SetGeneration(0)
458
459 return string(spec) + meta.String(), nil
460 }
461
462
463
464
465 type HTTPChaosList struct {
466 metav1.TypeMeta `json:",inline"`
467 metav1.ListMeta `json:"metadata,omitempty"`
468 Items []HTTPChaos `json:"items"`
469 }
470
471
472 func (in *HTTPChaosList) ListChaos() []*ChaosInstance {
473 res := make([]*ChaosInstance, 0, len(in.Items))
474 for _, item := range in.Items {
475 res = append(res, item.GetChaos())
476 }
477 return res
478 }
479
480 func (in *HTTPChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
481 duration, err := in.Spec.GetDuration()
482 if err != nil {
483 return false, 0, err
484 }
485
486 if duration != nil {
487 stopTime := in.GetCreationTimestamp().Add(*duration)
488 if stopTime.Before(now) {
489 return true, 0, nil
490 }
491
492 return false, stopTime.Sub(now), nil
493 }
494
495 return false, 0, nil
496 }
497
498 func (in *HTTPChaos) IsOneShot() bool {
499
500 return false
501
502 }
503
504 const KindIOChaos = "IOChaos"
505
506
507 func (in *IOChaos) IsDeleted() bool {
508 return !in.DeletionTimestamp.IsZero()
509 }
510
511
512 func (in *IOChaos) IsPaused() bool {
513 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
514 return false
515 }
516 return true
517 }
518
519
520 func (in *IOChaos) GetObjectMeta() *metav1.ObjectMeta {
521 return &in.ObjectMeta
522 }
523
524
525 func (in *IOChaosSpec) GetDuration() (*time.Duration, error) {
526 if in.Duration == nil {
527 return nil, nil
528 }
529 duration, err := time.ParseDuration(*in.Duration)
530 if err != nil {
531 return nil, err
532 }
533 return &duration, nil
534 }
535
536
537 func (in *IOChaos) GetChaos() *ChaosInstance {
538 instance := &ChaosInstance{
539 Name: in.Name,
540 Namespace: in.Namespace,
541 Kind: KindIOChaos,
542 StartTime: in.CreationTimestamp.Time,
543 Action: "",
544 UID: string(in.UID),
545 Status: in.Status.ChaosStatus,
546 }
547
548 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
549 if action.IsValid() {
550 instance.Action = action.String()
551 }
552 if in.Spec.Duration != nil {
553 instance.Duration = *in.Spec.Duration
554 }
555 if in.DeletionTimestamp != nil {
556 instance.EndTime = in.DeletionTimestamp.Time
557 }
558 return instance
559 }
560
561
562 func (in *IOChaos) GetStatus() *ChaosStatus {
563 return &in.Status.ChaosStatus
564 }
565
566
567 func (in *IOChaos) GetSpecAndMetaString() (string, error) {
568 spec, err := json.Marshal(in.Spec)
569 if err != nil {
570 return "", err
571 }
572
573 meta := in.ObjectMeta.DeepCopy()
574 meta.SetResourceVersion("")
575 meta.SetGeneration(0)
576
577 return string(spec) + meta.String(), nil
578 }
579
580
581
582
583 type IOChaosList struct {
584 metav1.TypeMeta `json:",inline"`
585 metav1.ListMeta `json:"metadata,omitempty"`
586 Items []IOChaos `json:"items"`
587 }
588
589
590 func (in *IOChaosList) ListChaos() []*ChaosInstance {
591 res := make([]*ChaosInstance, 0, len(in.Items))
592 for _, item := range in.Items {
593 res = append(res, item.GetChaos())
594 }
595 return res
596 }
597
598 func (in *IOChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
599 duration, err := in.Spec.GetDuration()
600 if err != nil {
601 return false, 0, err
602 }
603
604 if duration != nil {
605 stopTime := in.GetCreationTimestamp().Add(*duration)
606 if stopTime.Before(now) {
607 return true, 0, nil
608 }
609
610 return false, stopTime.Sub(now), nil
611 }
612
613 return false, 0, nil
614 }
615
616 func (in *IOChaos) IsOneShot() bool {
617
618 return false
619
620 }
621
622 const KindJVMChaos = "JVMChaos"
623
624
625 func (in *JVMChaos) IsDeleted() bool {
626 return !in.DeletionTimestamp.IsZero()
627 }
628
629
630 func (in *JVMChaos) IsPaused() bool {
631 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
632 return false
633 }
634 return true
635 }
636
637
638 func (in *JVMChaos) GetObjectMeta() *metav1.ObjectMeta {
639 return &in.ObjectMeta
640 }
641
642
643 func (in *JVMChaosSpec) GetDuration() (*time.Duration, error) {
644 if in.Duration == nil {
645 return nil, nil
646 }
647 duration, err := time.ParseDuration(*in.Duration)
648 if err != nil {
649 return nil, err
650 }
651 return &duration, nil
652 }
653
654
655 func (in *JVMChaos) GetChaos() *ChaosInstance {
656 instance := &ChaosInstance{
657 Name: in.Name,
658 Namespace: in.Namespace,
659 Kind: KindJVMChaos,
660 StartTime: in.CreationTimestamp.Time,
661 Action: "",
662 UID: string(in.UID),
663 Status: in.Status.ChaosStatus,
664 }
665
666 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
667 if action.IsValid() {
668 instance.Action = action.String()
669 }
670 if in.Spec.Duration != nil {
671 instance.Duration = *in.Spec.Duration
672 }
673 if in.DeletionTimestamp != nil {
674 instance.EndTime = in.DeletionTimestamp.Time
675 }
676 return instance
677 }
678
679
680 func (in *JVMChaos) GetStatus() *ChaosStatus {
681 return &in.Status.ChaosStatus
682 }
683
684
685 func (in *JVMChaos) GetSpecAndMetaString() (string, error) {
686 spec, err := json.Marshal(in.Spec)
687 if err != nil {
688 return "", err
689 }
690
691 meta := in.ObjectMeta.DeepCopy()
692 meta.SetResourceVersion("")
693 meta.SetGeneration(0)
694
695 return string(spec) + meta.String(), nil
696 }
697
698
699
700
701 type JVMChaosList struct {
702 metav1.TypeMeta `json:",inline"`
703 metav1.ListMeta `json:"metadata,omitempty"`
704 Items []JVMChaos `json:"items"`
705 }
706
707
708 func (in *JVMChaosList) ListChaos() []*ChaosInstance {
709 res := make([]*ChaosInstance, 0, len(in.Items))
710 for _, item := range in.Items {
711 res = append(res, item.GetChaos())
712 }
713 return res
714 }
715
716 func (in *JVMChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
717 duration, err := in.Spec.GetDuration()
718 if err != nil {
719 return false, 0, err
720 }
721
722 if duration != nil {
723 stopTime := in.GetCreationTimestamp().Add(*duration)
724 if stopTime.Before(now) {
725 return true, 0, nil
726 }
727
728 return false, stopTime.Sub(now), nil
729 }
730
731 return false, 0, nil
732 }
733
734 func (in *JVMChaos) IsOneShot() bool {
735
736 return false
737
738 }
739
740 const KindKernelChaos = "KernelChaos"
741
742
743 func (in *KernelChaos) IsDeleted() bool {
744 return !in.DeletionTimestamp.IsZero()
745 }
746
747
748 func (in *KernelChaos) IsPaused() bool {
749 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
750 return false
751 }
752 return true
753 }
754
755
756 func (in *KernelChaos) GetObjectMeta() *metav1.ObjectMeta {
757 return &in.ObjectMeta
758 }
759
760
761 func (in *KernelChaosSpec) GetDuration() (*time.Duration, error) {
762 if in.Duration == nil {
763 return nil, nil
764 }
765 duration, err := time.ParseDuration(*in.Duration)
766 if err != nil {
767 return nil, err
768 }
769 return &duration, nil
770 }
771
772
773 func (in *KernelChaos) GetChaos() *ChaosInstance {
774 instance := &ChaosInstance{
775 Name: in.Name,
776 Namespace: in.Namespace,
777 Kind: KindKernelChaos,
778 StartTime: in.CreationTimestamp.Time,
779 Action: "",
780 UID: string(in.UID),
781 Status: in.Status.ChaosStatus,
782 }
783
784 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
785 if action.IsValid() {
786 instance.Action = action.String()
787 }
788 if in.Spec.Duration != nil {
789 instance.Duration = *in.Spec.Duration
790 }
791 if in.DeletionTimestamp != nil {
792 instance.EndTime = in.DeletionTimestamp.Time
793 }
794 return instance
795 }
796
797
798 func (in *KernelChaos) GetStatus() *ChaosStatus {
799 return &in.Status.ChaosStatus
800 }
801
802
803 func (in *KernelChaos) GetSpecAndMetaString() (string, error) {
804 spec, err := json.Marshal(in.Spec)
805 if err != nil {
806 return "", err
807 }
808
809 meta := in.ObjectMeta.DeepCopy()
810 meta.SetResourceVersion("")
811 meta.SetGeneration(0)
812
813 return string(spec) + meta.String(), nil
814 }
815
816
817
818
819 type KernelChaosList struct {
820 metav1.TypeMeta `json:",inline"`
821 metav1.ListMeta `json:"metadata,omitempty"`
822 Items []KernelChaos `json:"items"`
823 }
824
825
826 func (in *KernelChaosList) ListChaos() []*ChaosInstance {
827 res := make([]*ChaosInstance, 0, len(in.Items))
828 for _, item := range in.Items {
829 res = append(res, item.GetChaos())
830 }
831 return res
832 }
833
834 func (in *KernelChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
835 duration, err := in.Spec.GetDuration()
836 if err != nil {
837 return false, 0, err
838 }
839
840 if duration != nil {
841 stopTime := in.GetCreationTimestamp().Add(*duration)
842 if stopTime.Before(now) {
843 return true, 0, nil
844 }
845
846 return false, stopTime.Sub(now), nil
847 }
848
849 return false, 0, nil
850 }
851
852 func (in *KernelChaos) IsOneShot() bool {
853
854 return false
855
856 }
857
858 const KindNetworkChaos = "NetworkChaos"
859
860
861 func (in *NetworkChaos) IsDeleted() bool {
862 return !in.DeletionTimestamp.IsZero()
863 }
864
865
866 func (in *NetworkChaos) IsPaused() bool {
867 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
868 return false
869 }
870 return true
871 }
872
873
874 func (in *NetworkChaos) GetObjectMeta() *metav1.ObjectMeta {
875 return &in.ObjectMeta
876 }
877
878
879 func (in *NetworkChaosSpec) GetDuration() (*time.Duration, error) {
880 if in.Duration == nil {
881 return nil, nil
882 }
883 duration, err := time.ParseDuration(*in.Duration)
884 if err != nil {
885 return nil, err
886 }
887 return &duration, nil
888 }
889
890
891 func (in *NetworkChaos) GetChaos() *ChaosInstance {
892 instance := &ChaosInstance{
893 Name: in.Name,
894 Namespace: in.Namespace,
895 Kind: KindNetworkChaos,
896 StartTime: in.CreationTimestamp.Time,
897 Action: "",
898 UID: string(in.UID),
899 Status: in.Status.ChaosStatus,
900 }
901
902 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
903 if action.IsValid() {
904 instance.Action = action.String()
905 }
906 if in.Spec.Duration != nil {
907 instance.Duration = *in.Spec.Duration
908 }
909 if in.DeletionTimestamp != nil {
910 instance.EndTime = in.DeletionTimestamp.Time
911 }
912 return instance
913 }
914
915
916 func (in *NetworkChaos) GetStatus() *ChaosStatus {
917 return &in.Status.ChaosStatus
918 }
919
920
921 func (in *NetworkChaos) GetSpecAndMetaString() (string, error) {
922 spec, err := json.Marshal(in.Spec)
923 if err != nil {
924 return "", err
925 }
926
927 meta := in.ObjectMeta.DeepCopy()
928 meta.SetResourceVersion("")
929 meta.SetGeneration(0)
930
931 return string(spec) + meta.String(), nil
932 }
933
934
935
936
937 type NetworkChaosList struct {
938 metav1.TypeMeta `json:",inline"`
939 metav1.ListMeta `json:"metadata,omitempty"`
940 Items []NetworkChaos `json:"items"`
941 }
942
943
944 func (in *NetworkChaosList) ListChaos() []*ChaosInstance {
945 res := make([]*ChaosInstance, 0, len(in.Items))
946 for _, item := range in.Items {
947 res = append(res, item.GetChaos())
948 }
949 return res
950 }
951
952 func (in *NetworkChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
953 duration, err := in.Spec.GetDuration()
954 if err != nil {
955 return false, 0, err
956 }
957
958 if duration != nil {
959 stopTime := in.GetCreationTimestamp().Add(*duration)
960 if stopTime.Before(now) {
961 return true, 0, nil
962 }
963
964 return false, stopTime.Sub(now), nil
965 }
966
967 return false, 0, nil
968 }
969
970 func (in *NetworkChaos) IsOneShot() bool {
971
972 return false
973
974 }
975
976 const KindPodChaos = "PodChaos"
977
978
979 func (in *PodChaos) IsDeleted() bool {
980 return !in.DeletionTimestamp.IsZero()
981 }
982
983
984 func (in *PodChaos) IsPaused() bool {
985 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
986 return false
987 }
988 return true
989 }
990
991
992 func (in *PodChaos) GetObjectMeta() *metav1.ObjectMeta {
993 return &in.ObjectMeta
994 }
995
996
997 func (in *PodChaosSpec) GetDuration() (*time.Duration, error) {
998 if in.Duration == nil {
999 return nil, nil
1000 }
1001 duration, err := time.ParseDuration(*in.Duration)
1002 if err != nil {
1003 return nil, err
1004 }
1005 return &duration, nil
1006 }
1007
1008
1009 func (in *PodChaos) GetChaos() *ChaosInstance {
1010 instance := &ChaosInstance{
1011 Name: in.Name,
1012 Namespace: in.Namespace,
1013 Kind: KindPodChaos,
1014 StartTime: in.CreationTimestamp.Time,
1015 Action: "",
1016 UID: string(in.UID),
1017 Status: in.Status.ChaosStatus,
1018 }
1019
1020 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
1021 if action.IsValid() {
1022 instance.Action = action.String()
1023 }
1024 if in.Spec.Duration != nil {
1025 instance.Duration = *in.Spec.Duration
1026 }
1027 if in.DeletionTimestamp != nil {
1028 instance.EndTime = in.DeletionTimestamp.Time
1029 }
1030 return instance
1031 }
1032
1033
1034 func (in *PodChaos) GetStatus() *ChaosStatus {
1035 return &in.Status.ChaosStatus
1036 }
1037
1038
1039 func (in *PodChaos) GetSpecAndMetaString() (string, error) {
1040 spec, err := json.Marshal(in.Spec)
1041 if err != nil {
1042 return "", err
1043 }
1044
1045 meta := in.ObjectMeta.DeepCopy()
1046 meta.SetResourceVersion("")
1047 meta.SetGeneration(0)
1048
1049 return string(spec) + meta.String(), nil
1050 }
1051
1052
1053
1054
1055 type PodChaosList struct {
1056 metav1.TypeMeta `json:",inline"`
1057 metav1.ListMeta `json:"metadata,omitempty"`
1058 Items []PodChaos `json:"items"`
1059 }
1060
1061
1062 func (in *PodChaosList) ListChaos() []*ChaosInstance {
1063 res := make([]*ChaosInstance, 0, len(in.Items))
1064 for _, item := range in.Items {
1065 res = append(res, item.GetChaos())
1066 }
1067 return res
1068 }
1069
1070 func (in *PodChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
1071 duration, err := in.Spec.GetDuration()
1072 if err != nil {
1073 return false, 0, err
1074 }
1075
1076 if duration != nil {
1077 stopTime := in.GetCreationTimestamp().Add(*duration)
1078 if stopTime.Before(now) {
1079 return true, 0, nil
1080 }
1081
1082 return false, stopTime.Sub(now), nil
1083 }
1084
1085 return false, 0, nil
1086 }
1087
1088 func (in *PodChaos) IsOneShot() bool {
1089
1090 if in.Spec.Action==PodKillAction || in.Spec.Action==ContainerKillAction {
1091 return true
1092 }
1093
1094 return false
1095
1096 }
1097
1098 const KindStressChaos = "StressChaos"
1099
1100
1101 func (in *StressChaos) IsDeleted() bool {
1102 return !in.DeletionTimestamp.IsZero()
1103 }
1104
1105
1106 func (in *StressChaos) IsPaused() bool {
1107 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
1108 return false
1109 }
1110 return true
1111 }
1112
1113
1114 func (in *StressChaos) GetObjectMeta() *metav1.ObjectMeta {
1115 return &in.ObjectMeta
1116 }
1117
1118
1119 func (in *StressChaosSpec) GetDuration() (*time.Duration, error) {
1120 if in.Duration == nil {
1121 return nil, nil
1122 }
1123 duration, err := time.ParseDuration(*in.Duration)
1124 if err != nil {
1125 return nil, err
1126 }
1127 return &duration, nil
1128 }
1129
1130
1131 func (in *StressChaos) GetChaos() *ChaosInstance {
1132 instance := &ChaosInstance{
1133 Name: in.Name,
1134 Namespace: in.Namespace,
1135 Kind: KindStressChaos,
1136 StartTime: in.CreationTimestamp.Time,
1137 Action: "",
1138 UID: string(in.UID),
1139 Status: in.Status.ChaosStatus,
1140 }
1141
1142 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
1143 if action.IsValid() {
1144 instance.Action = action.String()
1145 }
1146 if in.Spec.Duration != nil {
1147 instance.Duration = *in.Spec.Duration
1148 }
1149 if in.DeletionTimestamp != nil {
1150 instance.EndTime = in.DeletionTimestamp.Time
1151 }
1152 return instance
1153 }
1154
1155
1156 func (in *StressChaos) GetStatus() *ChaosStatus {
1157 return &in.Status.ChaosStatus
1158 }
1159
1160
1161 func (in *StressChaos) GetSpecAndMetaString() (string, error) {
1162 spec, err := json.Marshal(in.Spec)
1163 if err != nil {
1164 return "", err
1165 }
1166
1167 meta := in.ObjectMeta.DeepCopy()
1168 meta.SetResourceVersion("")
1169 meta.SetGeneration(0)
1170
1171 return string(spec) + meta.String(), nil
1172 }
1173
1174
1175
1176
1177 type StressChaosList struct {
1178 metav1.TypeMeta `json:",inline"`
1179 metav1.ListMeta `json:"metadata,omitempty"`
1180 Items []StressChaos `json:"items"`
1181 }
1182
1183
1184 func (in *StressChaosList) ListChaos() []*ChaosInstance {
1185 res := make([]*ChaosInstance, 0, len(in.Items))
1186 for _, item := range in.Items {
1187 res = append(res, item.GetChaos())
1188 }
1189 return res
1190 }
1191
1192 func (in *StressChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
1193 duration, err := in.Spec.GetDuration()
1194 if err != nil {
1195 return false, 0, err
1196 }
1197
1198 if duration != nil {
1199 stopTime := in.GetCreationTimestamp().Add(*duration)
1200 if stopTime.Before(now) {
1201 return true, 0, nil
1202 }
1203
1204 return false, stopTime.Sub(now), nil
1205 }
1206
1207 return false, 0, nil
1208 }
1209
1210 func (in *StressChaos) IsOneShot() bool {
1211
1212 return false
1213
1214 }
1215
1216 const KindTimeChaos = "TimeChaos"
1217
1218
1219 func (in *TimeChaos) IsDeleted() bool {
1220 return !in.DeletionTimestamp.IsZero()
1221 }
1222
1223
1224 func (in *TimeChaos) IsPaused() bool {
1225 if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
1226 return false
1227 }
1228 return true
1229 }
1230
1231
1232 func (in *TimeChaos) GetObjectMeta() *metav1.ObjectMeta {
1233 return &in.ObjectMeta
1234 }
1235
1236
1237 func (in *TimeChaosSpec) GetDuration() (*time.Duration, error) {
1238 if in.Duration == nil {
1239 return nil, nil
1240 }
1241 duration, err := time.ParseDuration(*in.Duration)
1242 if err != nil {
1243 return nil, err
1244 }
1245 return &duration, nil
1246 }
1247
1248
1249 func (in *TimeChaos) GetChaos() *ChaosInstance {
1250 instance := &ChaosInstance{
1251 Name: in.Name,
1252 Namespace: in.Namespace,
1253 Kind: KindTimeChaos,
1254 StartTime: in.CreationTimestamp.Time,
1255 Action: "",
1256 UID: string(in.UID),
1257 Status: in.Status.ChaosStatus,
1258 }
1259
1260 action := reflect.ValueOf(in).Elem().FieldByName("Spec").FieldByName("Action")
1261 if action.IsValid() {
1262 instance.Action = action.String()
1263 }
1264 if in.Spec.Duration != nil {
1265 instance.Duration = *in.Spec.Duration
1266 }
1267 if in.DeletionTimestamp != nil {
1268 instance.EndTime = in.DeletionTimestamp.Time
1269 }
1270 return instance
1271 }
1272
1273
1274 func (in *TimeChaos) GetStatus() *ChaosStatus {
1275 return &in.Status.ChaosStatus
1276 }
1277
1278
1279 func (in *TimeChaos) GetSpecAndMetaString() (string, error) {
1280 spec, err := json.Marshal(in.Spec)
1281 if err != nil {
1282 return "", err
1283 }
1284
1285 meta := in.ObjectMeta.DeepCopy()
1286 meta.SetResourceVersion("")
1287 meta.SetGeneration(0)
1288
1289 return string(spec) + meta.String(), nil
1290 }
1291
1292
1293
1294
1295 type TimeChaosList struct {
1296 metav1.TypeMeta `json:",inline"`
1297 metav1.ListMeta `json:"metadata,omitempty"`
1298 Items []TimeChaos `json:"items"`
1299 }
1300
1301
1302 func (in *TimeChaosList) ListChaos() []*ChaosInstance {
1303 res := make([]*ChaosInstance, 0, len(in.Items))
1304 for _, item := range in.Items {
1305 res = append(res, item.GetChaos())
1306 }
1307 return res
1308 }
1309
1310 func (in *TimeChaos) DurationExceeded(now time.Time) (bool, time.Duration, error) {
1311 duration, err := in.Spec.GetDuration()
1312 if err != nil {
1313 return false, 0, err
1314 }
1315
1316 if duration != nil {
1317 stopTime := in.GetCreationTimestamp().Add(*duration)
1318 if stopTime.Before(now) {
1319 return true, 0, nil
1320 }
1321
1322 return false, stopTime.Sub(now), nil
1323 }
1324
1325 return false, 0, nil
1326 }
1327
1328 func (in *TimeChaos) IsOneShot() bool {
1329
1330 return false
1331
1332 }
1333
1334 func init() {
1335
1336 SchemeBuilder.Register(&AWSChaos{}, &AWSChaosList{})
1337 all.register(KindAWSChaos, &ChaosKind{
1338 Chaos: &AWSChaos{},
1339 ChaosList: &AWSChaosList{},
1340 })
1341
1342 SchemeBuilder.Register(&DNSChaos{}, &DNSChaosList{})
1343 all.register(KindDNSChaos, &ChaosKind{
1344 Chaos: &DNSChaos{},
1345 ChaosList: &DNSChaosList{},
1346 })
1347
1348 SchemeBuilder.Register(&GCPChaos{}, &GCPChaosList{})
1349 all.register(KindGCPChaos, &ChaosKind{
1350 Chaos: &GCPChaos{},
1351 ChaosList: &GCPChaosList{},
1352 })
1353
1354 SchemeBuilder.Register(&HTTPChaos{}, &HTTPChaosList{})
1355 all.register(KindHTTPChaos, &ChaosKind{
1356 Chaos: &HTTPChaos{},
1357 ChaosList: &HTTPChaosList{},
1358 })
1359
1360 SchemeBuilder.Register(&IOChaos{}, &IOChaosList{})
1361 all.register(KindIOChaos, &ChaosKind{
1362 Chaos: &IOChaos{},
1363 ChaosList: &IOChaosList{},
1364 })
1365
1366 SchemeBuilder.Register(&JVMChaos{}, &JVMChaosList{})
1367 all.register(KindJVMChaos, &ChaosKind{
1368 Chaos: &JVMChaos{},
1369 ChaosList: &JVMChaosList{},
1370 })
1371
1372 SchemeBuilder.Register(&KernelChaos{}, &KernelChaosList{})
1373 all.register(KindKernelChaos, &ChaosKind{
1374 Chaos: &KernelChaos{},
1375 ChaosList: &KernelChaosList{},
1376 })
1377
1378 SchemeBuilder.Register(&NetworkChaos{}, &NetworkChaosList{})
1379 all.register(KindNetworkChaos, &ChaosKind{
1380 Chaos: &NetworkChaos{},
1381 ChaosList: &NetworkChaosList{},
1382 })
1383
1384 SchemeBuilder.Register(&PodChaos{}, &PodChaosList{})
1385 all.register(KindPodChaos, &ChaosKind{
1386 Chaos: &PodChaos{},
1387 ChaosList: &PodChaosList{},
1388 })
1389
1390 SchemeBuilder.Register(&StressChaos{}, &StressChaosList{})
1391 all.register(KindStressChaos, &ChaosKind{
1392 Chaos: &StressChaos{},
1393 ChaosList: &StressChaosList{},
1394 })
1395
1396 SchemeBuilder.Register(&TimeChaos{}, &TimeChaosList{})
1397 all.register(KindTimeChaos, &ChaosKind{
1398 Chaos: &TimeChaos{},
1399 ChaosList: &TimeChaosList{},
1400 })
1401
1402
1403 allScheduleItem.register(KindAWSChaos, &ChaosKind{
1404 Chaos: &AWSChaos{},
1405 ChaosList: &AWSChaosList{},
1406 })
1407
1408 allScheduleItem.register(KindDNSChaos, &ChaosKind{
1409 Chaos: &DNSChaos{},
1410 ChaosList: &DNSChaosList{},
1411 })
1412
1413 allScheduleItem.register(KindGCPChaos, &ChaosKind{
1414 Chaos: &GCPChaos{},
1415 ChaosList: &GCPChaosList{},
1416 })
1417
1418 allScheduleItem.register(KindHTTPChaos, &ChaosKind{
1419 Chaos: &HTTPChaos{},
1420 ChaosList: &HTTPChaosList{},
1421 })
1422
1423 allScheduleItem.register(KindIOChaos, &ChaosKind{
1424 Chaos: &IOChaos{},
1425 ChaosList: &IOChaosList{},
1426 })
1427
1428 allScheduleItem.register(KindJVMChaos, &ChaosKind{
1429 Chaos: &JVMChaos{},
1430 ChaosList: &JVMChaosList{},
1431 })
1432
1433 allScheduleItem.register(KindKernelChaos, &ChaosKind{
1434 Chaos: &KernelChaos{},
1435 ChaosList: &KernelChaosList{},
1436 })
1437
1438 allScheduleItem.register(KindNetworkChaos, &ChaosKind{
1439 Chaos: &NetworkChaos{},
1440 ChaosList: &NetworkChaosList{},
1441 })
1442
1443 allScheduleItem.register(KindPodChaos, &ChaosKind{
1444 Chaos: &PodChaos{},
1445 ChaosList: &PodChaosList{},
1446 })
1447
1448 allScheduleItem.register(KindStressChaos, &ChaosKind{
1449 Chaos: &StressChaos{},
1450 ChaosList: &StressChaosList{},
1451 })
1452
1453 allScheduleItem.register(KindTimeChaos, &ChaosKind{
1454 Chaos: &TimeChaos{},
1455 ChaosList: &TimeChaosList{},
1456 })
1457
1458 allScheduleItem.register(KindWorkflow, &ChaosKind{
1459 Chaos: &Workflow{},
1460 ChaosList: &WorkflowList{},
1461 })
1462
1463 }
1464