Skip to content

Commit

Permalink
chore: nil check when creating stressor (#2655)
Browse files Browse the repository at this point in the history
Signed-off-by: STRRL <str_ruiling@outlook.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
STRRL and ti-chi-bot authored Dec 23, 2021
1 parent a2d2762 commit a44d8af
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pkg/apiserver/experiment/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,25 @@ func (s *Service) createStressChaos(exp *core.ExperimentInfo, kubeCli client.Cli
var stressors *v1alpha1.Stressors

// Error checking
if exp.Target.StressChaos.Stressors.CPUStressor.Workers <= 0 && exp.Target.StressChaos.Stressors.MemoryStressor.Workers > 0 {
originStressors := exp.Target.StressChaos.Stressors
if originStressors == nil {
return utils.ErrInvalidRequest.New("stressor is required")
}
if originStressors.CPUStressor == nil && originStressors.MemoryStressor == nil {
return utils.ErrInvalidRequest.New("one of CPUStressor or MemoryStressor is required")
}
if originStressors.MemoryStressor != nil && originStressors.MemoryStressor.Workers > 0 &&
(originStressors.CPUStressor == nil || originStressors.CPUStressor.Workers <= 0) {
stressors = &v1alpha1.Stressors{
MemoryStressor: exp.Target.StressChaos.Stressors.MemoryStressor,
MemoryStressor: originStressors.MemoryStressor,
}
} else if exp.Target.StressChaos.Stressors.MemoryStressor.Workers <= 0 && exp.Target.StressChaos.Stressors.CPUStressor.Workers > 0 {
} else if originStressors.CPUStressor != nil && originStressors.CPUStressor.Workers > 0 &&
(originStressors.MemoryStressor == nil || originStressors.MemoryStressor.Workers <= 0) {
stressors = &v1alpha1.Stressors{
CPUStressor: exp.Target.StressChaos.Stressors.CPUStressor,
CPUStressor: originStressors.CPUStressor,
}
} else {
stressors = exp.Target.StressChaos.Stressors
stressors = originStressors
}

chaos := &v1alpha1.StressChaos{
Expand Down

0 comments on commit a44d8af

Please sign in to comment.