From 5ae06c1fc6d058ed13b67828e5cd846f4a348307 Mon Sep 17 00:00:00 2001 From: Nico J Date: Mon, 12 Dec 2022 00:23:45 -0500 Subject: [PATCH] litmsuctl create scenario command independent of install-chaos-experiments step (#114) * Fixing FetchWeightages. Defaulting to 10 Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J * new pseudocode for calculating FetchWeightages Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J * New pseudocode for handling weightage Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J * re-implementing FetchWeightages for properly handling weightages Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J * Update pkg/utils/workflow.go Co-authored-by: Saranya Jena Signed-off-by: iamnicoj Signed-off-by: Nico J * Fixing FetchWeightages. Defaulting to 10 Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J * signing all using gpg commits Signed-off-by: Nico J Signed-off-by: Nico J Signed-off-by: iamnicoj Co-authored-by: Saranya Jena --- pkg/utils/workflow.go | 84 ++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/pkg/utils/workflow.go b/pkg/utils/workflow.go index 0e45b938..92539749 100644 --- a/pkg/utils/workflow.go +++ b/pkg/utils/workflow.go @@ -23,6 +23,7 @@ import ( "net/url" "regexp" "strconv" + "strings" "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" chaosTypes "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" @@ -120,41 +121,68 @@ func sliceContains(s []string, e string) bool { // both artifacts and remote experiment specs. func FetchWeightages(chaosWorkFlowRequest *model.ChaosWorkFlowRequest, templates []v1alpha1.Template) error { - var chaosEngines []string - - // Fetch all present experiments and append them to the experiments array for _, t := range templates { - // Only the template named "install-chaos-experiments" contains ChaosExperiment(s) - if t.Name == "install-chaos-experiments" { - for _, a := range t.Inputs.Artifacts { - chaosEngines = append(chaosEngines, a.Name) - } - } - // Template that contains ChaosEngine manifest - if sliceContains(chaosEngines, t.Name) { - var weightageInput model.WeightagesInput - var err error + var err error - var chaosEngine chaosTypes.ChaosEngine - err = yaml.Unmarshal([]byte(t.Inputs.Artifacts[0].Raw.Data), &chaosEngine) - if err != nil { - return errors.New("Error parsing ChaosEngine: " + err.Error()) + if t.Inputs.Artifacts != nil && len(t.Inputs.Artifacts) > 0 { + if t.Inputs.Artifacts[0].Raw == nil { + continue } - weightageInput.ExperimentName = chaosEngine.ObjectMeta.GenerateName - w, ok := t.Metadata.Labels["weight"] - if !ok { - White.Println("Weightage for ChaosExperiment/" + weightageInput.ExperimentName + " not provided, defaulting to 10.") - w = "10" - } - weightageInput.Weightage, err = strconv.Atoi(w) - if err != nil { - return errors.New("Invalid weightage for ChaosExperiment/" + weightageInput.ExperimentName + ".") - } + var data = t.Inputs.Artifacts[0].Raw.Data + if len(data) > 0 { + // This replacement is required because chaos engine yaml have a syntax template. example:{{ workflow.parameters.adminModeNamespace }} + // And it is not able the unmarshal the yamlstring to chaos engine struct + data = strings.ReplaceAll(data, "{{", "") + data = strings.ReplaceAll(data, "}}", "") + + var chaosEngine chaosTypes.ChaosEngine + + err = yaml.Unmarshal([]byte(data), &chaosEngine) + + if err != nil { + return errors.New("failed to unmarshal chaosengine") + } + + if strings.ToLower(chaosEngine.Kind) == "chaosengine" { + var weightageInput model.WeightagesInput - chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) + weightageInput.ExperimentName = chaosEngine.ObjectMeta.GenerateName + + if len(weightageInput.ExperimentName) == 0 { + return errors.New("empty chaos experiment name") + } + + if len(chaosEngine.Spec.Experiments) == 0 { + return errors.New("no experiments specified in chaosengine - " + weightageInput.ExperimentName) + } + + w, ok := t.Metadata.Labels["weight"] + + if !ok { + White.Println("Weightage for ChaosExperiment/" + weightageInput.ExperimentName + " not provided, defaulting to 10.") + w = "10" + } + weightageInput.Weightage, err = strconv.Atoi(w) + + if err != nil { + return errors.New("Invalid weightage for ChaosExperiment/" + weightageInput.ExperimentName + ".") + } + + chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) + } + } } } + + // If no experiments are present in the workflow, adds a 0 to the Weightages array so it doesn't fail (same behaviour as the UI) + if len(chaosWorkFlowRequest.Weightages) == 0 { + White.Println("No experiments found in the chaos scenario, defaulting experiments weightage to 0.") + var weightageInput model.WeightagesInput + weightageInput.ExperimentName = "" + weightageInput.Weightage = 0 + chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) + } return nil }