@@ -110,7 +110,9 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
110
110
return stacktrace .Propagate (err , "could not parse targets" )
111
111
}
112
112
113
- var targets []targetDetails
113
+ var (
114
+ targets []targetDetails
115
+ )
114
116
115
117
for _ , t := range targetList .Target {
116
118
td := targetDetails {
@@ -131,7 +133,7 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
131
133
return stacktrace .Propagate (err , "could not get container pid" )
132
134
}
133
135
134
- td .CGroupManager , err = getCGroupManager (td )
136
+ td .CGroupManager , err , td . GroupPath = getCGroupManager (td )
135
137
if err != nil {
136
138
return stacktrace .Propagate (err , "could not get cgroup manager" )
137
139
}
@@ -499,50 +501,69 @@ func abortWatcher(targets []targetDetails, resultName, chaosNS string) {
499
501
}
500
502
501
503
// getCGroupManager will return the cgroup for the given pid of the process
502
- func getCGroupManager (t targetDetails ) (interface {}, error ) {
504
+ func getCGroupManager (t targetDetails ) (interface {}, error , string ) {
503
505
if cgroups .Mode () == cgroups .Unified {
504
- groupPath , err := cgroupsv2 .PidGroupPath (t .Pid )
506
+ groupPath := ""
507
+ output , err := exec .Command ("bash" , "-c" , fmt .Sprintf ("nsenter -t 1 -C -m -- cat /proc/%v/cgroup" , t .Pids [index ])).CombinedOutput ()
505
508
if err != nil {
506
- return nil , cerrors.Error {ErrorCode : cerrors .ErrorTypeHelper , Source : t .Source , Target : fmt .Sprintf ("{podName: %s, namespace: %s, container: %s}" , t .Name , t .Namespace , t .TargetContainer ), Reason : fmt .Sprintf ("fail to get pid group path: %s" , err .Error ())}
509
+ return nil , errors .Errorf ("Error in getting groupPath,%s" , string (output )), ""
510
+ }
511
+ parts := strings .SplitN (string (output ), ":" , 3 )
512
+ if len (parts ) < 3 {
513
+ return "" , fmt .Errorf ("invalid cgroup entry: %s" , string (output )), ""
514
+ }
515
+ if parts [0 ] == "0" && parts [1 ] == "" {
516
+ groupPath = parts [2 ]
507
517
}
508
518
509
- cgroup2 , err := cgroupsv2 .LoadManager ("/sys/fs/cgroup" , groupPath )
519
+ log .Infof ("group path: %s" , groupPath )
520
+
521
+ cgroup2 , err := cgroupsv2 .LoadManager ("/sys/fs/cgroup" , string (groupPath ))
510
522
if err != nil {
511
- return nil , cerrors. Error { ErrorCode : cerrors . ErrorTypeHelper , Source : t . Source , Target : fmt . Sprintf ( "{podName: %s, namespace: %s, container: %s} " , t . Name , t . Namespace , t . TargetContainer ), Reason : fmt . Sprintf ( "fail to load the cgroup: %s" , err . Error ())}
523
+ return nil , errors . Errorf ( " Error loading cgroup v2 manager, %v " , err ), ""
512
524
}
513
- return cgroup2 , nil
525
+ return cgroup2 , nil , groupPath
514
526
}
515
527
path := pidPath (t )
516
528
cgroup , err := findValidCgroup (path , t )
517
529
if err != nil {
518
- return nil , stacktrace .Propagate (err , "could not find valid cgroup" )
530
+ return nil , stacktrace .Propagate (err , "could not find valid cgroup" ), ""
519
531
}
520
532
cgroup1 , err := cgroups .Load (cgroups .V1 , cgroups .StaticPath (cgroup ))
521
533
if err != nil {
522
- return nil , cerrors.Error {ErrorCode : cerrors .ErrorTypeHelper , Source : t .Source , Target : fmt .Sprintf ("{podName: %s, namespace: %s, container: %s}" , t .Name , t .Namespace , t .TargetContainer ), Reason : fmt .Sprintf ("fail to load the cgroup: %s" , err .Error ())}
534
+ return nil , cerrors.Error {ErrorCode : cerrors .ErrorTypeHelper , Source : t .Source , Target : fmt .Sprintf ("{podName: %s, namespace: %s, container: %s}" , t .Name , t .Namespace , t .TargetContainer ), Reason : fmt .Sprintf ("fail to load the cgroup: %s" , err .Error ())}, ""
523
535
}
524
536
525
- return cgroup1 , nil
537
+ return cgroup1 , nil , ""
526
538
}
527
539
528
540
// addProcessToCgroup will add the process to cgroup
529
541
// By default it will add to v1 cgroup
530
- func addProcessToCgroup (pid int , control interface {}) error {
542
+ func addProcessToCgroup (pid int , control interface {}, groupPath string ) error {
531
543
if cgroups .Mode () == cgroups .Unified {
532
- var cgroup1 = control .(* cgroupsv2.Manager )
533
- return cgroup1 .AddProc (uint64 (pid ))
544
+ args := []string {"-t" , "1" , "-C" , "--" , "sudo" , "sh" , "-c" , fmt .Sprintf ("echo %d >> /sys/fs/cgroup%s/cgroup.procs" , pid , strings .ReplaceAll (groupPath , "\n " , "" ))}
545
+ output , err := exec .Command ("nsenter" , args ... ).CombinedOutput ()
546
+ if err != nil {
547
+ return cerrors.Error {
548
+ ErrorCode : cerrors .ErrorTypeChaosInject ,
549
+ Reason : fmt .Sprintf ("failed to add process to cgroup %s: %v" , string (output ), err ),
550
+ }
551
+ }
552
+ return nil
534
553
}
535
554
var cgroup1 = control .(cgroups.Cgroup )
536
555
return cgroup1 .Add (cgroups.Process {Pid : pid })
537
556
}
538
557
558
+
539
559
func injectChaos (t targetDetails , stressors , stressType string ) (* exec.Cmd , error ) {
540
560
stressCommand := fmt .Sprintf ("pause nsutil -t %v -p -- %v" , strconv .Itoa (t .Pid ), stressors )
541
561
// for io stress,we need to enter into mount ns of the target container
542
562
// enabling it by passing -m flag
543
563
if stressType == "pod-io-stress" {
544
564
stressCommand = fmt .Sprintf ("pause nsutil -t %v -p -m -- %v" , strconv .Itoa (t .Pid ), stressors )
545
565
}
566
+
546
567
log .Infof ("[Info]: starting process: %v" , stressCommand )
547
568
548
569
// launch the stress-ng process on the target container in paused mode
@@ -556,7 +577,7 @@ func injectChaos(t targetDetails, stressors, stressType string) (*exec.Cmd, erro
556
577
}
557
578
558
579
// add the stress process to the cgroup of target container
559
- if err = addProcessToCgroup (cmd .Process .Pid , t .CGroupManager ); err != nil {
580
+ if err = addProcessToCgroup (cmd .Process .Pid , t .CGroupManager , t . GroupPath ); err != nil {
560
581
if killErr := cmd .Process .Kill (); killErr != nil {
561
582
return nil , cerrors.Error {ErrorCode : cerrors .ErrorTypeChaosInject , Source : t .Source , Target : fmt .Sprintf ("{podName: %s, namespace: %s, container: %s}" , t .Name , t .Namespace , t .TargetContainer ), Reason : fmt .Sprintf ("fail to add the stress process to cgroup %s and kill stress process: %s" , err .Error (), killErr .Error ())}
562
583
}
@@ -584,4 +605,5 @@ type targetDetails struct {
584
605
CGroupManager interface {}
585
606
Cmd * exec.Cmd
586
607
Source string
608
+ GroupPath string
587
609
}
0 commit comments