From 3e943b469e2261ee98d860d0a03561c3014bc3b9 Mon Sep 17 00:00:00 2001 From: Nico J Date: Sun, 4 Dec 2022 19:39:06 -0500 Subject: [PATCH 1/7] Fixing FetchWeightages. Defaulting to 10 Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J --- pkg/utils/workflow.go | 49 ++++++++++++------------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/pkg/utils/workflow.go b/pkg/utils/workflow.go index 0e45b938..4d8330ee 100644 --- a/pkg/utils/workflow.go +++ b/pkg/utils/workflow.go @@ -25,9 +25,7 @@ import ( "strconv" "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" - chaosTypes "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model" - "sigs.k8s.io/yaml" ) // ParseWorkflowManifest reads the manifest that is passed as an argument and @@ -120,41 +118,20 @@ 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) - } - } + // There is an issue with the current version of this method. When trying to create an scenario it is always returning: + // :x: Chaos Scenario/podtato-head-1668208428 failed to be created: graphql schema error% + // It is not properly parsing the weightages for the experiments and not even assigning the default weightage. + // Also, adding the "install-chaos-experiments" step in the workflow should not required. + // I just added a default Weightage of 10 and now it is allowing to create the scenario: + White.Println("Weightage for ChaosExperiment defaulting to 10.") + var weightageInput model.WeightagesInput + var err error - // Template that contains ChaosEngine manifest - if sliceContains(chaosEngines, t.Name) { - var weightageInput model.WeightagesInput - 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()) - } - 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 + ".") - } - - chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) - } + weightageInput.Weightage, err = strconv.Atoi("10") + if err != nil { + return errors.New("Invalid weightage for ChaosExperiment/" + weightageInput.ExperimentName + ".") } + chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) + return nil } From f654f6052286d12db0795f8f077678bf93740dec Mon Sep 17 00:00:00 2001 From: Nico J Date: Tue, 6 Dec 2022 20:38:24 -0500 Subject: [PATCH 2/7] new pseudocode for calculating FetchWeightages Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J --- pkg/utils/workflow.go | 54 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/pkg/utils/workflow.go b/pkg/utils/workflow.go index 4d8330ee..19de0f15 100644 --- a/pkg/utils/workflow.go +++ b/pkg/utils/workflow.go @@ -25,7 +25,9 @@ import ( "strconv" "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" + chaosTypes "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model" + "sigs.k8s.io/yaml" ) // ParseWorkflowManifest reads the manifest that is passed as an argument and @@ -121,17 +123,53 @@ func FetchWeightages(chaosWorkFlowRequest *model.ChaosWorkFlowRequest, templates // There is an issue with the current version of this method. When trying to create an scenario it is always returning: // :x: Chaos Scenario/podtato-head-1668208428 failed to be created: graphql schema error% // It is not properly parsing the weightages for the experiments and not even assigning the default weightage. - // Also, adding the "install-chaos-experiments" step in the workflow should not required. + // Also, adding the "install-chaos-experiments" step in the workflow should not be required. // I just added a default Weightage of 10 and now it is allowing to create the scenario: - White.Println("Weightage for ChaosExperiment defaulting to 10.") - var weightageInput model.WeightagesInput - var err error + // White.Println("Weightage for ChaosExperiment defaulting to 10.") + // var weightageInput model.WeightagesInput + // var err error + + for _, t := range templates { + var err error + + var chaosEngine chaosTypes.ChaosEngine + if t.Inputs.Artifacts != nil && len(t.Inputs.Artifacts) > 0 { + White.Println(t.Inputs.Artifacts[0].Raw.Data) + + err = yaml.Unmarshal([]byte(t.Inputs.Artifacts[0].Raw.Data), &chaosEngine) + if err == nil && chaosEngine.Kind == "ChaosEngine" { + var weightageInput model.WeightagesInput + 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 + ".") + } + + chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) + } else { + White.Println("Template not of chaosengine type." + t.Name) + if err != nil { + White.Println(err.Error()) + + } + // return errors.New("Error parsing ChaosEngine: " + err.Error()) + } + } + } - weightageInput.Weightage, err = strconv.Atoi("10") - if err != nil { - return errors.New("Invalid weightage for ChaosExperiment/" + weightageInput.ExperimentName + ".") + if len(chaosWorkFlowRequest.Weightages) == 0 { + White.Println("No experiments found for the workflow, defaulting to weightage 0.") + var weightageInput model.WeightagesInput + weightageInput.ExperimentName = "" + weightageInput.Weightage = 0 + chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) } - chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) return nil } From 2ce03a5e6a6148d29aabb675db01f189622fd0f6 Mon Sep 17 00:00:00 2001 From: Nico J Date: Tue, 6 Dec 2022 20:44:44 -0500 Subject: [PATCH 3/7] New pseudocode for handling weightage Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J --- pkg/utils/workflow.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/pkg/utils/workflow.go b/pkg/utils/workflow.go index 19de0f15..efd1e87b 100644 --- a/pkg/utils/workflow.go +++ b/pkg/utils/workflow.go @@ -124,17 +124,12 @@ func FetchWeightages(chaosWorkFlowRequest *model.ChaosWorkFlowRequest, templates // :x: Chaos Scenario/podtato-head-1668208428 failed to be created: graphql schema error% // It is not properly parsing the weightages for the experiments and not even assigning the default weightage. // Also, adding the "install-chaos-experiments" step in the workflow should not be required. - // I just added a default Weightage of 10 and now it is allowing to create the scenario: - // White.Println("Weightage for ChaosExperiment defaulting to 10.") - // var weightageInput model.WeightagesInput - // var err error for _, t := range templates { var err error var chaosEngine chaosTypes.ChaosEngine if t.Inputs.Artifacts != nil && len(t.Inputs.Artifacts) > 0 { - White.Println(t.Inputs.Artifacts[0].Raw.Data) err = yaml.Unmarshal([]byte(t.Inputs.Artifacts[0].Raw.Data), &chaosEngine) if err == nil && chaosEngine.Kind == "ChaosEngine" { @@ -152,19 +147,12 @@ func FetchWeightages(chaosWorkFlowRequest *model.ChaosWorkFlowRequest, templates } chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) - } else { - White.Println("Template not of chaosengine type." + t.Name) - if err != nil { - White.Println(err.Error()) - - } - // return errors.New("Error parsing ChaosEngine: " + err.Error()) } } } if len(chaosWorkFlowRequest.Weightages) == 0 { - White.Println("No experiments found for the workflow, defaulting to weightage 0.") + White.Println("No experiments found in the scenario, defaulting to weightage 0.") var weightageInput model.WeightagesInput weightageInput.ExperimentName = "" weightageInput.Weightage = 0 From bf7568cf8c643585100fd0ab16157f22da145883 Mon Sep 17 00:00:00 2001 From: Nico J Date: Wed, 7 Dec 2022 17:07:36 -0500 Subject: [PATCH 4/7] re-implementing FetchWeightages for properly handling weightages Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J --- pkg/utils/workflow.go | 65 ++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/pkg/utils/workflow.go b/pkg/utils/workflow.go index efd1e87b..516561cb 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,44 +121,68 @@ func sliceContains(s []string, e string) bool { // both artifacts and remote experiment specs. func FetchWeightages(chaosWorkFlowRequest *model.ChaosWorkFlowRequest, templates []v1alpha1.Template) error { - // There is an issue with the current version of this method. When trying to create an scenario it is always returning: - // :x: Chaos Scenario/podtato-head-1668208428 failed to be created: graphql schema error% - // It is not properly parsing the weightages for the experiments and not even assigning the default weightage. - // Also, adding the "install-chaos-experiments" step in the workflow should not be required. - for _, t := range templates { + var err error - var chaosEngine chaosTypes.ChaosEngine if t.Inputs.Artifacts != nil && len(t.Inputs.Artifacts) > 0 { + if t.Inputs.Artifacts[0].Raw == nil { + continue + } - err = yaml.Unmarshal([]byte(t.Inputs.Artifacts[0].Raw.Data), &chaosEngine) - if err == nil && chaosEngine.Kind == "ChaosEngine" { - var weightageInput model.WeightagesInput - weightageInput.ExperimentName = chaosEngine.ObjectMeta.GenerateName - w, ok := t.Metadata.Labels["weight"] + 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 !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 + ".") + return errors.New("failed to unmarshal chaosengine") } - chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) + if strings.ToLower(chaosEngine.Kind) == "chaosengine" { + var weightageInput model.WeightagesInput + + 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 scenario, defaulting to weightage 0.") + White.Println("No experiments found in the workflow, defaulting experiments weightage to 0.") var weightageInput model.WeightagesInput weightageInput.ExperimentName = "" weightageInput.Weightage = 0 chaosWorkFlowRequest.Weightages = append(chaosWorkFlowRequest.Weightages, &weightageInput) } - return nil } From 387a87471d817f52e2471323fa69f6f1d8975728 Mon Sep 17 00:00:00 2001 From: Nico J Date: Fri, 9 Dec 2022 09:00:53 -0500 Subject: [PATCH 5/7] Update pkg/utils/workflow.go Co-authored-by: Saranya Jena Signed-off-by: iamnicoj Signed-off-by: Nico J --- pkg/utils/workflow.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/utils/workflow.go b/pkg/utils/workflow.go index 516561cb..92539749 100644 --- a/pkg/utils/workflow.go +++ b/pkg/utils/workflow.go @@ -178,7 +178,7 @@ func FetchWeightages(chaosWorkFlowRequest *model.ChaosWorkFlowRequest, templates // 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 workflow, defaulting experiments weightage to 0.") + White.Println("No experiments found in the chaos scenario, defaulting experiments weightage to 0.") var weightageInput model.WeightagesInput weightageInput.ExperimentName = "" weightageInput.Weightage = 0 From 8f326ab6d4d8b8ec686c13fd3af9ef8d25a183b5 Mon Sep 17 00:00:00 2001 From: Nico J Date: Sun, 4 Dec 2022 19:39:06 -0500 Subject: [PATCH 6/7] Fixing FetchWeightages. Defaulting to 10 Signed-off-by: Nico J Signed-off-by: iamnicoj Signed-off-by: Nico J --- pkg/utils/workflow.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/utils/workflow.go b/pkg/utils/workflow.go index 92539749..b750f92a 100644 --- a/pkg/utils/workflow.go +++ b/pkg/utils/workflow.go @@ -26,9 +26,7 @@ import ( "strings" "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" - chaosTypes "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model" - "sigs.k8s.io/yaml" ) // ParseWorkflowManifest reads the manifest that is passed as an argument and From 070011f80da0f3c56c7168db9417c3c4aabc6b87 Mon Sep 17 00:00:00 2001 From: iamnicoj Date: Fri, 9 Dec 2022 17:23:02 -0500 Subject: [PATCH 7/7] signing all using gpg commits Signed-off-by: Nico J --- pkg/utils/workflow.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/utils/workflow.go b/pkg/utils/workflow.go index b750f92a..92539749 100644 --- a/pkg/utils/workflow.go +++ b/pkg/utils/workflow.go @@ -26,7 +26,9 @@ import ( "strings" "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" + chaosTypes "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model" + "sigs.k8s.io/yaml" ) // ParseWorkflowManifest reads the manifest that is passed as an argument and