Skip to content

Commit

Permalink
feat(operator): add cpu,memory,pod-kill chaos
Browse files Browse the repository at this point in the history
  • Loading branch information
moomman committed Jun 7, 2023
1 parent b5fe12e commit 662879c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
25 changes: 23 additions & 2 deletions shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ type PodChaosAction string
var (
PodFailure PodChaosAction = "PodFailure"
ContainerKill PodChaosAction = "ContainerKill"
PodKill PodChaosAction = "PodKill"
CPUStress PodChaosAction = "CPUStress"
MemoryStress PodChaosAction = "MemoryStress"
)

// PodChaosSpec Fields that need to be configured for pod type chaos
Expand All @@ -147,8 +150,10 @@ type PodChaosParams struct {
PodFailure *PodFailureParams `json:"podFailure,omitempty"`
// +optional
ContainerKill *ContainerKillParams `json:"containerKill,omitempty"`
// +optional
// PodKill *PodKillParams `json:"containerKill,omitempty"`
//+optional
CPUStress *CPUStressParams `json:"cpuStress,omitempty"`
//+optional
MemoryStress *MemoryStressParams `json:"memoryStress,omitempty"`
}

type PodFailureParams struct {
Expand All @@ -161,6 +166,22 @@ type ContainerKillParams struct {
ContainerNames []string `json:"containerNames,omitempty"`
}

type CPUStressParams struct {
Duration string `json:"duration"`
//+optional
Cores int `json:"cores,omitempty"`
//+optional
Load int `json:"load,omitempty"`
}

type MemoryStressParams struct {
Duration string `json:"duration"`
//+optional
Workers int `json:"workers,omitempty"`
//+optional
Consumption string `json:"consumption,omitempty"`
}

// NetworkChaosSpec Fields that need to be configured for network type chaos
type NetworkChaosSpec struct {
Source PodSelector `json:",inline"`
Expand Down
4 changes: 0 additions & 4 deletions shardingsphere-operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/database-mesh/golang-sdk v0.0.0-20230605075457-a525bc484e78 h1:442d1dVUQFHuT+KcSW0XtsZpYYwwCBlyJGDujb44vfM=
github.com/database-mesh/golang-sdk v0.0.0-20230605075457-a525bc484e78/go.mod h1:yUEdo+aGdROl9oC7A1GeDB9/ubUtV2k73uLL+qC3PC4=
github.com/database-mesh/golang-sdk v0.0.0-20230605093335-916ac7abc788 h1:YEF8BDXHnEiek/EnDVbTCOrVDP7OT3v/R3a8mGM6+vc=
github.com/database-mesh/golang-sdk v0.0.0-20230605093335-916ac7abc788/go.mod h1:yUEdo+aGdROl9oC7A1GeDB9/ubUtV2k73uLL+qC3PC4=
github.com/database-mesh/golang-sdk v0.0.0-20230606100535-23037381e4fb h1:p3tpHo24HjA7rW/JMjD9/6klWAVMn4fIefHXKgggVAg=
github.com/database-mesh/golang-sdk v0.0.0-20230606100535-23037381e4fb/go.mod h1:yUEdo+aGdROl9oC7A1GeDB9/ubUtV2k73uLL+qC3PC4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func generateMsgFromExec(exec *pressure.Pressure) *v1alpha1.Msg {
Result: fmt.Sprintf("%d/%d", exec.Result.Success, exec.Result.Total),
Duration: exec.Result.Duration.String(),
}

if exec.Err != nil {
msg.FailureDetails = exec.Err.Error()
}
Expand Down
80 changes: 79 additions & 1 deletion shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const (

AnnoPodAction = "podchaos.chaos-mesh.org/action"
AnnoGracePeriod = "podchaos.chaos-mesh.org/gracePeriod"
AnnoStressTime = "stresschaos.chaos-mesh.org/time"
AnnoOOMScoreAdj = "stresschaos.chaos-mesh.org/oomScoreAdj"

AnnoNetworkAction = "networkchaos.chaos-mesh.org/action"
AnnoDevice = "networkchaos.chaos-mesh.org/device"
Expand Down Expand Up @@ -101,10 +103,16 @@ func judgeCondition(condition map[chaosmeshv1alpha1.ChaosConditionType]bool, pha
}

func NewPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (PodChaos, error) {

chao := ssChao.Spec.PodChaos

if chao.Action == v1alpha1.MemoryStress || chao.Action == v1alpha1.CPUStress {
return NewStressChaos(ssChao)
}

pcb := NewPodChaosBuilder()
pcb.SetName(ssChao.Name).SetNamespace(ssChao.Namespace).SetLabels(ssChao.Labels)

chao := ssChao.Spec.PodChaos
if act, ok := ssChao.Annotations[AnnoPodAction]; ok {
pcb.SetAction(act)
if gp, ok := ssChao.Annotations[AnnoGracePeriod]; chaosmeshv1alpha1.PodChaosAction(act) == chaosmeshv1alpha1.PodKillAction && ok {
Expand Down Expand Up @@ -149,6 +157,76 @@ func NewPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (PodChaos, error) {
return podChao, nil
}

func NewStressChaos(chaos *v1alpha1.ShardingSphereChaos) (PodChaos, error) {
sc := &chaosmeshv1alpha1.StressChaos{}
sc.Namespace = chaos.Namespace
sc.Name = chaos.Name
sc.Labels = chaos.Labels

chao := chaos.Spec.PodChaos

psb := NewPodSelectorBuilder()

psb.SetNamespaces(chao.Namespaces).
SetExpressionSelectors(chao.ExpressionSelectors).
SetNodes(chao.Nodes).
SetNodeSelector(chao.NodeSelectors).
SetAnnotationSelectors(chao.AnnotationSelectors).
SetLabelSelector(chao.LabelSelectors).
SetPods(chao.Pods)

psb.SetSelectMode(chaos.Annotations[AnnoTargetPodSelectMode]).
SetValue(chaos.Annotations[AnnoTargetPodSelectValue])

sc.Spec.ContainerSelector = chaosmeshv1alpha1.ContainerSelector{
PodSelector: *psb.Build(),
}

if chao.Action == v1alpha1.CPUStress {
setCPUStressParams(chaos, sc)
}

if chao.Action == v1alpha1.MemoryStress {
if err := setMemoryStressParams(chaos, sc); err != nil {
return nil, err
}
}

return sc, nil
}

func setCPUStressParams(sschaos *v1alpha1.ShardingSphereChaos, chaos *chaosmeshv1alpha1.StressChaos) {
cpu := chaosmeshv1alpha1.CPUStressor{
Stressor: chaosmeshv1alpha1.Stressor{
Workers: sschaos.Spec.PodChaos.Params.CPUStress.Cores,
},
Load: &sschaos.Spec.PodChaos.Params.CPUStress.Load,
}

chaos.Spec.Stressors.CPUStressor = &cpu
chaos.Spec.Duration = &sschaos.Spec.PodChaos.Params.CPUStress.Duration
}

func setMemoryStressParams(sschaos *v1alpha1.ShardingSphereChaos, chaos *chaosmeshv1alpha1.StressChaos) error {

oom, err := strconv.Atoi(sschaos.Annotations[AnnoOOMScoreAdj])
memory := chaosmeshv1alpha1.MemoryStressor{
Stressor: chaosmeshv1alpha1.Stressor{
Workers: sschaos.Spec.PodChaos.Params.MemoryStress.Workers,
},
Size: sschaos.Spec.PodChaos.Params.MemoryStress.Consumption,
OOMScoreAdj: oom,
Options: []string{
sschaos.Annotations[AnnoStressTime],
},
}

chaos.Spec.Stressors.MemoryStressor = &memory
chaos.Spec.Duration = &sschaos.Spec.PodChaos.Params.MemoryStress.Duration

return err
}

func getAnnotation(anno map[string]string, k string) string {
if v, ok := anno[k]; ok {
return v
Expand Down

0 comments on commit 662879c

Please sign in to comment.