This is an automated email from the ASF dual-hosted git repository.

miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new 662879c  feat(operator): add cpu,memory,pod-kill chaos
     new 5b60f7a  Merge pull request #399 from moomman/add-chaos
662879c is described below

commit 662879c2175f6077a3e4dbc62b99c58c2f82339b
Author: moomman <[email protected]>
AuthorDate: Wed Jun 7 17:08:49 2023 +0800

    feat(operator): add cpu,memory,pod-kill chaos
---
 .../api/v1alpha1/shardingsphere_chaos_types.go     | 25 ++++++-
 shardingsphere-operator/go.sum                     |  4 --
 .../controllers/shardingsphere_chaos_controller.go |  1 +
 .../pkg/kubernetes/chaosmesh/builder.go            | 80 +++++++++++++++++++++-
 4 files changed, 103 insertions(+), 7 deletions(-)

diff --git a/shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go 
b/shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go
index 4cfa97f..174e2ab 100644
--- a/shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go
+++ b/shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go
@@ -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
@@ -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 {
@@ -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"`
diff --git a/shardingsphere-operator/go.sum b/shardingsphere-operator/go.sum
index fec74ac..2fdad0f 100644
--- a/shardingsphere-operator/go.sum
+++ b/shardingsphere-operator/go.sum
@@ -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=
diff --git 
a/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go 
b/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
index 535ec48..3d72b64 100644
--- a/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
+++ b/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
@@ -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()
        }
diff --git a/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go 
b/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
index 5d895ae..87a3ff6 100644
--- a/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
+++ b/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
@@ -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"
@@ -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 {
@@ -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

Reply via email to