diff --git a/chaos/backend/api/chaosvo.go b/chaos/backend/api/chaosvo.go index 65f4c1aa31e384379f00c34e341997bcbcc20b0b..3f4f81c9ed2722d65efb319a0d114ab1ab3e9388 100644 --- a/chaos/backend/api/chaosvo.go +++ b/chaos/backend/api/chaosvo.go @@ -29,7 +29,8 @@ type Chaos struct { Instance []string `json:"instance"` // 运行时参数,从ComplexAction截取chaos_type的部分,方便后续处理 - ChaosType string `json:"-"` + ChaosType string `json:"-"` + ChaosAction string `json:"-"` // 运行时参数,确定故障在模板中的序号,用于固定前端展示顺序 ChaosNumber int `json:"-"` } diff --git a/chaos/backend/internal/manager/chaos_template.go b/chaos/backend/internal/manager/chaos_template.go index 21e25becda367fa83dd6e30e3552b5215f790023..31317e9e07a9bc2432ad862e174aeea1eb9ee046 100644 --- a/chaos/backend/internal/manager/chaos_template.go +++ b/chaos/backend/internal/manager/chaos_template.go @@ -16,6 +16,7 @@ import ( ) const ( + NoOption string = "no-option" NoAction string = "no-action" Unstructured string = "unstructured" ) diff --git a/chaos/backend/internal/manager/manager.go b/chaos/backend/internal/manager/manager.go index 62960c999ac6c1fd8a33519030c76a60380640cd..b76f005e585de1c107d38a262e0ab195beb9be54 100644 --- a/chaos/backend/internal/manager/manager.go +++ b/chaos/backend/internal/manager/manager.go @@ -123,9 +123,17 @@ func (m BuildInChaosMap) RegisterUnstructuredChaos(client kubernetescli.ChaosCli } func (m BuildInChaosMap) GetBuildChaosInfo(vo *api.Chaos) *BuildInChaos { - chaosType, chaosAction := analysisChaosTypeAndChaosActionFromTemplate(vo.ComplexAction) - // 完善已知的参数 + chaosType, chaosAction, options := analysisChaosTypeAndChaosActionFromTemplate(vo.ComplexAction) + // 完善参数 vo.ChaosType = chaosType + vo.ChaosAction = chaosType + if options != NoOption { + vo.Params = append(vo.Params, api.ChaosParams{ + Name: "option", + Type: "string", + Value: options, + }) + } if chaosAction == Unstructured { return m[vo.ComplexAction] } @@ -138,10 +146,13 @@ func (m BuildInChaosMap) GetBuildChaosInfo(vo *api.Chaos) *BuildInChaos { return m[clientId] } -func analysisChaosTypeAndChaosActionFromTemplate(complexAction string) (chaosType string, action string) { +func analysisChaosTypeAndChaosActionFromTemplate(complexAction string) (chaosType string, action string, options string) { parts := strings.Split(complexAction, "#") if len(parts) < 2 { - return complexAction, NoAction + return complexAction, NoAction, NoOption + } else if len(parts) < 3 { + return parts[0], parts[1], NoAction + } else { + return parts[0], parts[1], strings.Join(parts[2:], "#") } - return parts[0], parts[1] } diff --git a/chaos/backend/tools/kubernetescli/httpchaos.go b/chaos/backend/tools/kubernetescli/httpchaos.go index 9c4895212308c865518cc21c7bb6db67fb51bb6a..94298d3b789e97e83c9db7f465f1b2d1e622ef48 100644 --- a/chaos/backend/tools/kubernetescli/httpchaos.go +++ b/chaos/backend/tools/kubernetescli/httpchaos.go @@ -144,16 +144,14 @@ func buildHTTPChaos(vo *api.Chaos, params v1alpha1.HTTPChaos) *v1alpha1.HTTPChao "complex-action": vo.ComplexAction, } + switch vo.ChaosAction { + case "replace": + resource.Spec.PodHttpChaosActions = defaultHTTPChaosParam.Spec.PodHttpChaosActions + resource.Spec.Target = defaultHTTPChaosParam.Spec.Target + } + for i := 0; i < len(vo.Params); i++ { switch vo.Params[i].Name { - case "action": - actual_action := vo.Params[i].Value - switch actual_action { - case "replace": - resource.Spec.PodHttpChaosActions = defaultHTTPChaosParam.Spec.PodHttpChaosActions - resource.Spec.Target = defaultHTTPChaosParam.Spec.Target - } - case "instance": resource.Spec.Selector.LabelSelectors = map[string]string{ "app": vo.Params[i].Value, diff --git a/chaos/backend/tools/kubernetescli/iochaos.go b/chaos/backend/tools/kubernetescli/iochaos.go index 43da4701db6ee17399550d4afcbe01c4bcea8f95..ddace6a52c870d9a35fda76812b30056fd5d9f51 100644 --- a/chaos/backend/tools/kubernetescli/iochaos.go +++ b/chaos/backend/tools/kubernetescli/iochaos.go @@ -112,11 +112,10 @@ func buildIOChaos(vo *api.Chaos, params v1alpha1.IOChaos) *v1alpha1.IOChaos { "describe": vo.Desc, "complex-action": vo.ComplexAction, } + resource.Spec.Action = v1alpha1.IOChaosType(vo.ChaosAction) for i := 0; i < len(vo.Params); i++ { switch vo.Params[i].Name { - case "action": - resource.Spec.Action = v1alpha1.IOChaosType(vo.Params[i].Value) case "instance": resource.Spec.Selector.LabelSelectors = map[string]string{ "app": vo.Params[i].Value, diff --git a/chaos/backend/tools/kubernetescli/jvmchaos.go b/chaos/backend/tools/kubernetescli/jvmchaos.go index ee8f9cf5dc3c10671aa1474eff3adfd1679104fb..2fe018bb1cc2324705ded28e198a29dbccc9380c 100644 --- a/chaos/backend/tools/kubernetescli/jvmchaos.go +++ b/chaos/backend/tools/kubernetescli/jvmchaos.go @@ -345,11 +345,11 @@ func buildJVMChaos(vo *api.Chaos, params v1alpha1.JVMChaos) *v1alpha1.JVMChaos { "describe": vo.Desc, "complex-action": vo.ComplexAction, } + + resource.Spec.Action = v1alpha1.JVMChaosAction(vo.ChaosAction) // result.Spec.Action = v1alpha1.JVMRuleDataAction("s") for i := 0; i < len(vo.Params); i++ { switch vo.Params[i].Name { - case "action": //这里给jvm故障的ruledata赋值 - resource.Spec.Action = v1alpha1.JVMChaosAction(vo.Params[i].Value) case "instance": resource.Spec.Selector.LabelSelectors = map[string]string{ "app": vo.Params[i].Value, diff --git a/chaos/backend/tools/kubernetescli/networkchaos.go b/chaos/backend/tools/kubernetescli/networkchaos.go index 06024143c52af720b62e94e9dc198d5d3af86415..00c8c1f46ff944a8039556651f47602e0ea1cff6 100644 --- a/chaos/backend/tools/kubernetescli/networkchaos.go +++ b/chaos/backend/tools/kubernetescli/networkchaos.go @@ -120,10 +120,10 @@ func buildNetworkChaos(vo *api.Chaos, params v1alpha1.NetworkChaos) *v1alpha1.Ne "complex-action": vo.ComplexAction, } + addTcParameter(resource, v1alpha1.NetworkChaosAction(vo.ChaosAction)) + for _, param := range vo.Params { switch param.Name { - case "action": - addTcParameter(resource, v1alpha1.NetworkChaosAction(param.Value)) case "instance": resource.Spec.Selector.LabelSelectors = map[string]string{ "app": param.Value, diff --git a/chaos/backend/tools/kubernetescli/stresschaos.go b/chaos/backend/tools/kubernetescli/stresschaos.go index 7bae833b9df09deab84a53fa09d72a67ef335eb2..7fd5b2e58d5a2c2e68678402a0f373359bcd8b87 100644 --- a/chaos/backend/tools/kubernetescli/stresschaos.go +++ b/chaos/backend/tools/kubernetescli/stresschaos.go @@ -123,15 +123,15 @@ func buildStressChaos(vo *api.Chaos, params v1alpha1.StressChaos) *v1alpha1.Stre "complex-action": vo.ComplexAction, } + switch vo.ChaosAction { + case "cpu": + resource.Spec.Stressors.MemoryStressor = nil + case "memory": + resource.Spec.Stressors.CPUStressor = nil + } + for i := 0; i < len(vo.Params); i++ { switch vo.Params[i].Name { - case "action": - switch vo.Params[i].Value { - case "cpu": - resource.Spec.Stressors.MemoryStressor = nil - case "memory": - resource.Spec.Stressors.CPUStressor = nil - } case "instance": resource.Spec.Selector.LabelSelectors = map[string]string{ "app": vo.Params[i].Value,